using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DumpLib;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace ReCodeItLib.Dumper;
public static class DumpyInstructionsHelper
{
///
/// Sets up local variables and returns a List of instructions to add.
///
/// AssemblyDefinition
/// MethodDefinition
/// List
public static List GetBackRequestInstructions(AssemblyDefinition assembly, MethodDefinition method)
{
return new List
{
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Ldloc_S, method.Body.Variables[6]),
Instruction.Create(OpCodes.Call, assembly.MainModule.ImportReference(typeof(DumpLib.DumpyTool).GetMethod("LogRequestResponse", new[] { typeof(object), typeof(object) })))
};
}
///
/// Returns a List of instructions to be added to the method.
/// This is an Async method so there is two parts, this part and a RunValidation method.
///
/// AssemblyDefinition
/// MethodDefinition
/// List
public static List GetRunValidationInstructionsMoveNext(AssemblyDefinition assembly, MethodDefinition method)
{
// Add our own local variables
// var1 index0 class1159Type
var sptClassType = assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType);
var sptClass = new VariableDefinition(sptClassType);
method.Body.Variables.Add(sptClass);
// var2 index1 ExceptionType
var sptExceptionType = method.Module.ImportReference(typeof(Exception));
var sptException = new VariableDefinition(sptExceptionType);
method.Body.Variables.Add(sptException);
return new List
{
// most of this is to keep the Async happy
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[2]),
Instruction.Create(OpCodes.Stloc_0),
// this.Succeed = true;
Instruction.Create(OpCodes.Ldloc_0),
Instruction.Create(OpCodes.Ldc_I4_1),
Instruction.Create(OpCodes.Call, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).Methods.First(x => x.Name == "set_Succeed")),
Instruction.Create(OpCodes.Stloc_1),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-2),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldflda, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
Instruction.Create(OpCodes.Ldloc_1),
Instruction.Create(OpCodes.Call,
method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "SetException"))),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-2),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldflda, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
Instruction.Create(OpCodes.Call, method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "SetResult"))),
Instruction.Create(OpCodes.Ret),
};
}
///
/// Returns a List of instructions to be added to the method.
/// This is an Async method so there is two parts, this part and a RunValidation method.
///
/// AssemblyDefinition
/// MethodDefinition
/// List
public static List GetEnsureConsistencyInstructions(AssemblyDefinition oldFileChecker, MethodDefinition method)
{
// init local vars
// var1 index0 TimeSpan type
var sptTimeSpanType = method.Module.ImportReference(typeof(TimeSpan));
var sptClass = new VariableDefinition(sptTimeSpanType);
method.Body.Variables.Add(sptClass);
// Create genericInstance of a method
var type = oldFileChecker.MainModule.GetTypes().First(DumpyTypeHelper.GetEnsureConsistencyType).NestedTypes[0].Interfaces[0].InterfaceType;
var typeMethod = method.Module.ImportReference(typeof(Task).GetMethod("FromResult"));
var instanceType = new GenericInstanceMethod(typeMethod);
instanceType.GenericArguments.Add(type);
return new List
{
// return Task.FromResult(ConsistencyController.CheckResult.Succeed(default(TimeSpan)));
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Initobj, method.Module.ImportReference(typeof(TimeSpan))),
Instruction.Create(OpCodes.Ldloc_0),
Instruction.Create(OpCodes.Call, oldFileChecker.MainModule.GetTypes().First(DumpyTypeHelper.GetEnsureConsistencyType).NestedTypes[0].Methods.First(x => x.Name == "Succeed")),
Instruction.Create(OpCodes.Call, instanceType),
Instruction.Create(OpCodes.Ret)
};
}
///
/// Returns a List of instructions to be added to the method.
/// This is an Async method so there is two parts, this part and a MoveNext method.
///
/// AssemblyDefinition
/// MethodDefinition
/// List
public static List GetRunValidationInstructions(AssemblyDefinition assembly, MethodDefinition method)
{
// Create genericInstance of a method
var type = assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0];
var typeMethod = method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "Start"));
var instanceMethod = new GenericInstanceMethod(typeMethod);
instanceMethod.GenericArguments.Add(type);
return new List
{
// d__.<>t__builder = AsyncTaskMethodBuilder.Create();
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Call, method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "Create"))),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
// d__.<>4__this = this;
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[2]),
// d__.<>1__state = -1;
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Ldc_I4_M1),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]),
// d__.<>t__builder.Startd__0>(ref d__);
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Ldflda, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Call, instanceMethod),
// return d__.<>t__builder.Task;
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Ldflda, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
Instruction.Create(OpCodes.Call, method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "get_Task"))),
Instruction.Create(OpCodes.Ret),
};
}
public static List GetDumpyTaskInstructions(AssemblyDefinition oldAssembly, MethodDefinition method)
{
return new List
{
Instruction.Create(OpCodes.Call, oldAssembly.MainModule.ImportReference(typeof(DumpyTool).GetMethod("StartDumpyTask"))),
Instruction.Create(OpCodes.Pop)
};
}
}