Skip to content

Instantly share code, notes, and snippets.

@tommargar
Last active January 17, 2018 15:01
Show Gist options
  • Select an option

  • Save tommargar/f82b3e0494e93518baec84a4e35b1135 to your computer and use it in GitHub Desktop.

Select an option

Save tommargar/f82b3e0494e93518baec84a4e35b1135 to your computer and use it in GitHub Desktop.
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions,
ILGenerator ilGen)
{
List<CodeInstruction> instructionList = instructions.ToList();
for (int i = 0; i < instructionList.Count; ++i)
{
CodeInstruction instruction = instructionList[i];
MethodInfo drawMeshInfo = AccessTools.Method(typeof(Graphics), nameof(Graphics.DrawMesh),
new[]
{
typeof(Mesh), typeof(Vector3), typeof(Quaternion),
typeof(Material), typeof(int)
});
if (instruction.opcode == OpCodes.Call && instruction.operand == drawMeshInfo)
{
// create slots for our new variables
LocalBuilder myDrawLoc = ilGen.DeclareLocal(typeof(Vector3));
LocalBuilder myAimAngle = ilGen.DeclareLocal(typeof(float));
// copy parameters into variables
yield return new CodeInstruction(OpCodes.Ldarg_3);
yield return new CodeInstruction(OpCodes.Stloc, myAimAngle);
yield return new CodeInstruction(OpCodes.Ldarg_2);
yield return new CodeInstruction(OpCodes.Stloc, myDrawLoc);
// DoCalculations(eq, ref myDrawLoc, ref myAimAngle, pawn);
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldloca, myDrawLoc);
yield return new CodeInstruction(OpCodes.Ldloca, myAimAngle);
yield return new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(PawnRenderer), "pawn"));
yield return new CodeInstruction(OpCodes.Call,
AccessTools.Method(typeof(HarmonyPatchesFS),
nameof(DoCalculations)));
// Graphics.DrawMesh(mesh, drawLoc, Quaternion.AngleAxis(num, Vector3.up), matSingle, 0);
yield return new CodeInstruction(OpCodes.Ldloc_0);
yield return new CodeInstruction(OpCodes.Ldarg, myDrawLoc);
yield return new CodeInstruction(OpCodes.Ldloc_1);
// call valuetype[UnityEngine]UnityEngine.Vector3[UnityEngine]UnityEngine.Vector3::get_up()
yield return new CodeInstruction(OpCodes.Call,
AccessTools.Field(typeof(Vector3), nameof(Vector3.up)));
// call valuetype[UnityEngine]UnityEngine.Quaternion[UnityEngine]UnityEngine.Quaternion::AngleAxis(float32, valuetype[UnityEngine]UnityEngine.Vector3)
yield return new CodeInstruction(OpCodes.Call,
AccessTools.Field(typeof(Quaternion),
nameof(Quaternion.AngleAxis)));
yield return new CodeInstruction(OpCodes.Ldloc_2);
yield return new CodeInstruction(OpCodes.Ldc_I4_0);
// call void [UnityEngine]UnityEngine.Graphics::DrawMesh(class [UnityEngine] UnityEngine.Mesh, valuetype[UnityEngine] UnityEngine.Vector3, valuetype[UnityEngine] UnityEngine.Quaternion, class
yield return new CodeInstruction(OpCodes.Call,
AccessTools.Field(typeof(Graphics), nameof(Graphics.DrawMesh)));
}
yield return instruction;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment