Exemple #1
0
        static IEnumerable <CodeInstruction> Transpiler(IEnumerable <CodeInstruction> instructions, MethodBase original)
        {
            var codes         = instructions.ToCodeList();
            int iGetSegment1  = codes.Search(c => c.Calls(mGetSegment), count: 1);
            int iStSegmentID1 = codes.Search(c => c.IsStloc(), startIndex: iGetSegment1);
            var ldSegmentID1  = TranspilerUtils.BuildLdLocFromStLoc(codes[iStSegmentID1]);

            int iGetSegment2  = codes.Search(c => c.Calls(mGetSegment), count: 1);
            int iStSegmentID2 = codes.Search(c => c.IsStloc(), startIndex: iGetSegment2);
            var ldSegmentID2  = TranspilerUtils.BuildLdLocFromStLoc(codes[iStSegmentID2]);

            codes.InsertInstructions(iStSegmentID2 + 1, new[] {
                ldSegmentID1,
                ldSegmentID2,
                new CodeInstruction(OpCodes.Call, ShiftData.mPrefix),
            });

            // DC part of the code is before m_requireSegmentRenderers
            int i_requireSegmentRenderers = codes.Search(c => c.LoadsField(f_requireSegmentRenderers));

            codes.InsertInstructions(i_requireSegmentRenderers, new[] {
                new CodeInstruction(OpCodes.Call, ShiftData.mPostfix),
            });

            return(codes);
        }
Exemple #2
0
        public static IEnumerable <CodeInstruction> Transpiler(ILGenerator il, IEnumerable <CodeInstruction> instructions)
        {
            var netSegmentInfoGetter = typeof(global::NetSegment).GetProperty("Info")?.GetGetMethod();

            if (netSegmentInfoGetter == null)
            {
                Debug.LogError("Necessary field not found. Cancelling transpiler!");
                return(instructions);
            }

            var originalCodes = new List <CodeInstruction>(instructions);
            var codes         = new List <CodeInstruction>(originalCodes);

            var index = 0;

            CodeInstruction infoLocalVarLdloc = null;

            for (; index < codes.Count; index++)
            {
                // IL_0003: call instance class NetInfo NetSegment::get_Info()
                if (codes[index].opcode == OpCodes.Call && codes[index].operand == netSegmentInfoGetter && TranspilerUtils.IsStLoc(codes[index + 1]))
                {
                    infoLocalVarLdloc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 1]);
                    index            += 2;
                    break;
                }
            }

            if (infoLocalVarLdloc == null)
            {
                Debug.LogError("NetSegmentCalculateGroupDataPatch: info local variable not found. Cancelling transpiler!");
                return(originalCodes);
            }

            var segmentIdLdInstruction = new CodeInstruction(OpCodes.Ldarg_1); // segmentID is first argument

            if (!NetSegmentRenderPatch.PatchLanesAndSegments(il, codes, infoLocalVarLdloc, segmentIdLdInstruction, ref index))
            {
                Debug.LogError("NetSegmentCalculateGroupDataPatch: Could not apply NetSegmentRenderPatch. Cancelling transpiler!");
                return(originalCodes);
            }

            return(codes);
        }
        public static IEnumerable <CodeInstruction> Transpiler(ILGenerator il, IEnumerable <CodeInstruction> instructions)
        {
            var originalInstructions = new List <CodeInstruction>(instructions);

            var netInfoCreatePavementField        = typeof(NetInfo).GetField("m_createPavement");
            var netInfoFlattenTerrainField        = typeof(NetInfo).GetField("m_flattenTerrain");
            var segmentSkinsField                 = typeof(NetworkSkinManager).GetField("SegmentSkins", BindingFlags.Static | BindingFlags.Public);
            var terrainSurfacePatcherApplyMethod  = typeof(TerrainSurfacePatcher).GetMethod("Apply");
            var terrainSurfacePatcherRevertMethod = typeof(TerrainSurfacePatcher).GetMethod("Revert");

            if (netInfoCreatePavementField == null || netInfoFlattenTerrainField == null || segmentSkinsField == null || terrainSurfacePatcherApplyMethod == null || terrainSurfacePatcherRevertMethod == null)
            {
                Debug.LogError("NetNodeTerrainUpdatedPatch: Necessary field and methods not found. Cancelling transpiler!");
                return(originalInstructions);
            }


            var codes = new List <CodeInstruction>(originalInstructions);

            var patcherStateLocalVar = il.DeclareLocal(typeof(TerrainSurfacePatcherState));

            patcherStateLocalVar.SetLocalSymInfo("patcherState");

            int index = 0;

            CodeInstruction num14LocalVarLdLoc    = null;
            CodeInstruction num13LocalVarLdLoc    = null;
            CodeInstruction info4LocalVarLdLoc    = null;
            CodeInstruction netInfo2LocalVarLdLoc = null;
            CodeInstruction segment7LocalVarLdLoc = null;
            CodeInstruction num6LocalVarLdLoc     = null;

            for (; index < codes.Count; index++)
            {
                // NetInfo netInfo2 = (num14 > num13 >> 1) ? netInfo : info4;
                //IL_0a41: ldloc.s 74 (num14)
                //IL_0a43: ldloc.s 71 (num13)
                //IL_0a45: ldc.i4.1
                //IL_0a46: shr
                //IL_0a47: bgt IL_0a53
                //IL_0a4c: ldloc.s 40 (info4)
                //IL_0a4e: br IL_0a55
                //IL_0a53: ldloc.s 48 (netInfo)
                //IL_0a55: stloc.s 75 (netInfo2)
                if (codes[index].opcode == OpCodes.Shr &&
                    TranspilerUtils.IsLdLoc(codes[index - 3]) &&
                    TranspilerUtils.IsLdLoc(codes[index - 2]) &&
                    codes[index - 1].opcode == OpCodes.Ldc_I4_1
                    // shr
                    && codes[index + 1].opcode == OpCodes.Bgt &&
                    TranspilerUtils.IsLdLoc(codes[index + 2]) &&
                    codes[index + 3].opcode == OpCodes.Br &&
                    TranspilerUtils.IsLdLoc(codes[index + 4]) &&
                    TranspilerUtils.IsStLoc(codes[index + 5])
                    )
                {
                    TranspilerUtils.LogDebug("Found NetInfo netInfo2 = (num14 > num13 >> 1) ? netInfo : info4;");
                    num14LocalVarLdLoc = TranspilerUtils.BuildLdLocFromLdLoc(codes[index - 3]);
                    num13LocalVarLdLoc = TranspilerUtils.BuildLdLocFromLdLoc(codes[index - 2]);
                    info4LocalVarLdLoc = TranspilerUtils.BuildLdLocFromLdLoc(codes[index + 2]);       // 40
                    var netInfoLocalVarLdLoc = TranspilerUtils.BuildLdLocFromLdLoc(codes[index + 4]); // 48
                    netInfo2LocalVarLdLoc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 5]);    // 75

                    var findIndex = 0;
                    segment7LocalVarLdLoc = FindSegment7LocalVar(codes, TranspilerUtils.BuildStLocFromLdLoc(info4LocalVarLdLoc), ref findIndex, index - 3); // 38
                    num6LocalVarLdLoc     = FindNum6LocalVar(codes, TranspilerUtils.BuildStLocFromLdLoc(netInfoLocalVarLdLoc), ref findIndex, index - 3);   // 51
                    break;
                }
            }



            if (num14LocalVarLdLoc == null || num13LocalVarLdLoc == null || info4LocalVarLdLoc == null || netInfo2LocalVarLdLoc == null || segment7LocalVarLdLoc == null || num6LocalVarLdLoc == null)
            {
                Debug.LogError("NetNodeTerrainUpdatedPatch: Some local variables not found! Cancelling transpiler!");
                Debug.LogError($"num14: {num14LocalVarLdLoc}, num13: {num13LocalVarLdLoc}, info4: {info4LocalVarLdLoc}, netInfo2: {netInfo2LocalVarLdLoc}, segment7: {segment7LocalVarLdLoc}, num6: {num6LocalVarLdLoc}");

                return(originalInstructions);
            }

            // segment7 is the segmentID of info4
            // num6 is the segmentID of netInfo

            // TerrainSurfacePatcherState patcherState = TerrainSurfacePatcher.Apply(netInfo, NetworkSkinManager.SegmentSkins[(int)((num14 > num13 >> 1) ? num6 : segment7)]);
            // bool flag8 = netInfo2.m_createPavement && (!netInfo2.m_lowerTerrain || (m_flags & Flags.OnGround) != Flags.None);
            var apply1Inserted = false;

            for (; index < codes.Count; index++)
            {
                // bool flag8 = netInfo2.m_createPavement && (!netInfo2.m_lowerTerrain || (m_flags & Flags.OnGround) != Flags.None);
                if (TranspilerUtils.IsSameInstruction(codes[index], netInfo2LocalVarLdLoc) &&
                    codes[index + 1].opcode == OpCodes.Ldfld && codes[index + 1].operand == netInfoCreatePavementField)
                {
                    TranspilerUtils.LogDebug("Found info4.m_createPavement");

                    var ldLocSegment7Label = il.DefineLabel();
                    var ldElemtRefLabel    = il.DefineLabel();

                    // TerrainSurfacePatcherState patcherState = TerrainSurfacePatcher.Apply(netInfo, NetworkSkinManager.SegmentSkins[(int)((num14 > num13 >> 1) ? num6 : segment7)]);
                    var apply1Instructions = new[]
                    {
                        new CodeInstruction(netInfo2LocalVarLdLoc),
                        new CodeInstruction(OpCodes.Ldsfld, segmentSkinsField),
                        new CodeInstruction(num14LocalVarLdLoc),
                        new CodeInstruction(num13LocalVarLdLoc),
                        new CodeInstruction(OpCodes.Ldc_I4_1),
                        new CodeInstruction(OpCodes.Shr),
                        new CodeInstruction(OpCodes.Bgt, ldLocSegment7Label),
                        new CodeInstruction(segment7LocalVarLdLoc),
                        new CodeInstruction(OpCodes.Br, ldElemtRefLabel),
                        new CodeInstruction(num6LocalVarLdLoc),  // ldLocSegment7Label
                        new CodeInstruction(OpCodes.Ldelem_Ref), // ldElemtRefLabel
                        new CodeInstruction(OpCodes.Call, terrainSurfacePatcherApplyMethod),
                        new CodeInstruction(OpCodes.Stloc, patcherStateLocalVar)
                    };
                    apply1Instructions[0].labels.AddRange(codes[index].labels);
                    codes[index].labels.Clear();

                    apply1Instructions[9].labels.Add(ldLocSegment7Label);
                    apply1Instructions[10].labels.Add(ldElemtRefLabel);

                    codes.InsertRange(index, apply1Instructions);
                    TranspilerUtils.LogDebug("Apply 1 inserted");

                    apply1Inserted = true;
                    index         += apply1Instructions.Length;
                    break;
                }
            }

            if (!apply1Inserted)
            {
                Debug.LogError("NetNodeTerrainUpdatedPatch: Apply Insertion 1 failed! Cancelling transpiler!");
                return(originalInstructions);
            }


            // NetworkSkins.TerrainSurfacePatcher.Revert(netInfo2, patcherState);
            // bool flag12 = netInfo2.m_flattenTerrain || (netInfo2.m_netAI.FlattenGroundNodes() && (m_flags & Flags.OnGround) != Flags.None);
            var revert1Inserted = false;

            for (; index < codes.Count; index++)
            {
                // bool flag12 = netInfo2.m_flattenTerrain || (netInfo2.m_netAI.FlattenGroundNodes() && (m_flags & Flags.OnGround) != Flags.None);
                if (TranspilerUtils.IsSameInstruction(codes[index], netInfo2LocalVarLdLoc) &&
                    codes[index + 1].opcode == OpCodes.Ldfld && codes[index + 1].operand == netInfoFlattenTerrainField)
                {
                    TranspilerUtils.LogDebug("Found info4.m_flattenTerrain");

                    var revert1Instructions = new[]
                    {
                        new CodeInstruction(netInfo2LocalVarLdLoc),
                        new CodeInstruction(OpCodes.Ldloc, patcherStateLocalVar),
                        new CodeInstruction(OpCodes.Call, terrainSurfacePatcherRevertMethod),
                    };

                    revert1Instructions[0].labels.AddRange(codes[index].labels);
                    codes[index].labels.Clear();

                    codes.InsertRange(index, revert1Instructions);
                    TranspilerUtils.LogDebug("Revert 1 inserted");

                    revert1Inserted = true;
                    index          += revert1Instructions.Length;
                    break;
                }
            }

            if (!revert1Inserted)
            {
                Debug.LogError("NetNodeTerrainUpdatedPatch: Revert Insertion 1 failed! Cancelling transpiler!");
                return(originalInstructions);
            }

            // patcherState = NetworkSkins.TerrainSurfacePatcher.Apply(info4, NetworkSkins.Skins.NetworkSkinManager.SegmentSkins[segment7]);
            // bool flag13 = info4.m_createPavement && (!info4.m_lowerTerrain || (m_flags & Flags.OnGround) != Flags.None);
            var apply2Inserted = false;

            for (; index < codes.Count; index++)
            {
                // bool flag13 = info4.m_createPavement && (!info4.m_lowerTerrain || (m_flags & Flags.OnGround) != Flags.None);
                if (TranspilerUtils.IsSameInstruction(codes[index], info4LocalVarLdLoc) &&
                    codes[index + 1].opcode == OpCodes.Ldfld && codes[index + 1].operand == netInfoCreatePavementField)
                {
                    TranspilerUtils.LogDebug("Found info4.m_createPavement");

                    // patcherState = NetworkSkins.TerrainSurfacePatcher.Apply(info4, NetworkSkins.Skins.NetworkSkinManager.SegmentSkins[segment7]);
                    var apply2Instructions = new[]
                    {
                        new CodeInstruction(info4LocalVarLdLoc),
                        new CodeInstruction(OpCodes.Ldsfld, segmentSkinsField),
                        new CodeInstruction(segment7LocalVarLdLoc),
                        new CodeInstruction(OpCodes.Ldelem_Ref),
                        new CodeInstruction(OpCodes.Call, terrainSurfacePatcherApplyMethod),
                        new CodeInstruction(OpCodes.Stloc, patcherStateLocalVar)
                    };

                    apply2Instructions[0].labels.AddRange(codes[index].labels);
                    codes[index].labels.Clear();

                    codes.InsertRange(index, apply2Instructions);
                    TranspilerUtils.LogDebug("Apply 2 inserted");

                    apply2Inserted = true;
                    index         += apply2Instructions.Length;
                    break;
                }
            }

            if (!apply2Inserted)
            {
                Debug.LogError("NetNodeTerrainUpdatedPatch: Apply Insertion 2 failed! Cancelling transpiler!");
                return(originalInstructions);
            }

            // NetworkSkins.TerrainSurfacePatcher.Revert(info4, patcherState);
            // bool flag17 = info4.m_flattenTerrain || (info4.m_netAI.FlattenGroundNodes() && (m_flags & Flags.OnGround) != Flags.None);
            var revert2Inserted = false;

            for (; index < codes.Count; index++)
            {
                // bool flag17 = info4.m_flattenTerrain || (info4.m_netAI.FlattenGroundNodes() && (m_flags & Flags.OnGround) != Flags.None);
                if (TranspilerUtils.IsSameInstruction(codes[index], info4LocalVarLdLoc) &&
                    codes[index + 1].opcode == OpCodes.Ldfld && codes[index + 1].operand == netInfoFlattenTerrainField)
                {
                    TranspilerUtils.LogDebug("Found info4.m_flattenTerrain");

                    // NetworkSkins.TerrainSurfacePatcher.Revert(info4, patcherState);
                    var revert2Instructions = new[]
                    {
                        new CodeInstruction(info4LocalVarLdLoc),
                        new CodeInstruction(OpCodes.Ldloc, patcherStateLocalVar),
                        new CodeInstruction(OpCodes.Call, terrainSurfacePatcherRevertMethod),
                    };

                    revert2Instructions[0].labels.AddRange(codes[index].labels);
                    codes[index].labels.Clear();

                    codes.InsertRange(index, revert2Instructions);
                    TranspilerUtils.LogDebug("Revert 2 inserted");

                    revert2Inserted = true;
                    index          += revert2Instructions.Length;
                    break;
                }
            }

            if (!revert2Inserted)
            {
                Debug.LogError("NetNodeTerrainUpdatedPatch: Revert Insertion 2 failed! Cancelling transpiler!");
                return(originalInstructions);
            }

            return(codes);
        }
        public static IEnumerable <CodeInstruction> Transpiler(ILGenerator il, IEnumerable <CodeInstruction> instructions)
        {
            var netNodeFlagsField = typeof(global::NetNode).GetField("m_flags");

            var netNodeGetSegmentMethod = typeof(global::NetNode).GetMethod("GetSegment");

            var netInfoNodesField = typeof(NetInfo).GetField("m_nodes");

            var netInfoNodeDirectConnectField = typeof(NetInfo.Node).GetField("m_directConnect");

            var netNodeRenderPatchShouldRenderJunctionNodeMethod =
                typeof(NetNodeRenderPatch).GetMethod("ShouldRenderJunctionNode");

            var netNodeRenderPatchShouldRenderBendNodeLodMethod =
                typeof(NetNodeRenderPatch).GetMethod("ShouldRenderBendNodeLod");

            if (netNodeFlagsField == null || netNodeGetSegmentMethod == null || netInfoNodesField == null || netInfoNodeDirectConnectField == null || netNodeRenderPatchShouldRenderJunctionNodeMethod == null || netNodeRenderPatchShouldRenderBendNodeLodMethod == null)
            {
                Debug.LogError("Necessary methods and field not found. Cancelling transpiler!");
                return(instructions);
            }

            var index = 0;

            var originalCodes = new List <CodeInstruction>(instructions);
            var codes         = new List <CodeInstruction>(originalCodes);

            var junctionFlagCheckFound = false;

            for (; index < codes.Count; index++)
            {
                // if ((m_flags & Flags.Junction) != 0)
                // IL_004b: ldfld valuetype NetNode/Flags NetNode::m_flags
                // IL_0079: ldc.i4 128
                if (codes[index].opcode == OpCodes.Ldfld && codes[index].operand == netNodeFlagsField &&
                    codes[index + 1].opcode == OpCodes.Ldc_I4 && (int)codes[index + 1].operand == (int)global::NetNode.Flags.Junction)
                {
                    junctionFlagCheckFound = true;
                    break;
                }
            }

            if (!junctionFlagCheckFound)
            {
                Debug.LogError("NetNodeGroupDataPatch: junctionFlagCheck not found. Cancelling transpiler!");
                return(originalCodes);
            }

            CodeInstruction segmentLocalVarLdloc  = null;
            CodeInstruction segment2LocalVarLdloc = null;
            CodeInstruction nodeLocalVarLdLoc     = null;

            // ushort segment = GetSegment(i);
            for (; index < codes.Count; index++)
            {
                // IL_0073: call instance uint16 NetNode::GetSegment(int32)
                if (codes[index].opcode == OpCodes.Call && codes[index].operand == netNodeGetSegmentMethod && TranspilerUtils.IsStLoc(codes[index + 1]))
                {
                    // IL_0078: stloc.s 5
                    segmentLocalVarLdloc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 1]);
                    index += 2;
                    break;
                }
            }

            // ushort segment2 = GetSegment(j);
            for (; index < codes.Count; index++)
            {
                // IL_0107: call instance uint16 NetNode::GetSegment(int32)
                if (codes[index].opcode == OpCodes.Call && codes[index].operand == netNodeGetSegmentMethod && TranspilerUtils.IsStLoc(codes[index + 1]))
                {
                    // IL_010c: stloc.s 11
                    segment2LocalVarLdloc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 1]);
                    index += 2;
                    break;
                }
            }

            // NetInfo.Node node = info2.m_nodes[k];
            for (; index < codes.Count; index++)
            {
                // IL_0359: ldfld class NetInfo/Node[] NetInfo::m_nodes
                // IL_035e: ldloc.s 21
                // IL_0360: ldelem.ref
                // IL_0361: stloc.s 22
                if (codes[index].opcode == OpCodes.Ldfld && codes[index].operand == netInfoNodesField &&
                    TranspilerUtils.IsLdLoc(codes[index + 1]) &&
                    codes[index + 2].opcode == OpCodes.Ldelem_Ref &&
                    TranspilerUtils.IsStLoc(codes[index + 3]))
                {
                    // IL_0361: stloc.s 22
                    nodeLocalVarLdLoc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 3]);
                    index            += 4;
                    break;
                }
            }

            if (segmentLocalVarLdloc == null || segment2LocalVarLdloc == null || nodeLocalVarLdLoc == null)
            {
                Debug.LogError("NetNodeGroupDataPatch: Necessary field for junction not found. Cancelling transpiler!");
                Debug.LogError($"{segmentLocalVarLdloc}, {segment2LocalVarLdloc}, {nodeLocalVarLdLoc}");
                return(originalCodes);
            }

            var junctionRenderCheckInserted = false;

            for (; index < codes.Count; index++)
            {
                // IL_017c: ldloc.s 22
                // IL_0181: ldfld bool NetInfo/Node::m_directConnect
                // IL_0182: brfalse IL_04c3
                if (TranspilerUtils.IsSameInstruction(codes[index], nodeLocalVarLdLoc, true) &&
                    codes[index + 1].opcode == OpCodes.Ldfld && codes[index + 1].operand == netInfoNodeDirectConnectField &&
                    codes[index + 2].opcode == OpCodes.Brfalse)
                {
                    var labelIfFalse      = codes[index + 2].operand;
                    var insertionPosition = index + 3;

                    // && ShouldRenderJunctionNode(node, segment, segment2)
                    // IL_01FD: ldloc.s 22
                    // IL_01FF: ldloc.s 11
                    // IL_0200: ldloc.s 22
                    // IL_0201: call bool[NetworkSkins] NetworkSkins.Patches.NetNodeRenderPatch::ShouldRenderJunctionNode(class NetInfo/Node, uint16, uint16)
                    // IL_0206: brfalse.s IL_04c3
                    var renderCheckInstructions = new[]
                    {
                        new CodeInstruction(nodeLocalVarLdLoc),
                        new CodeInstruction(segmentLocalVarLdloc),
                        new CodeInstruction(segment2LocalVarLdloc),
                        new CodeInstruction(OpCodes.Call, netNodeRenderPatchShouldRenderJunctionNodeMethod),
                        new CodeInstruction(OpCodes.Brfalse, labelIfFalse),
                    };

                    renderCheckInstructions[0].labels.AddRange(codes[insertionPosition].labels);
                    codes[insertionPosition].labels.Clear();

                    codes.InsertRange(insertionPosition, renderCheckInstructions);
                    TranspilerUtils.LogDebug("Junction render check inserted");

                    index = insertionPosition + renderCheckInstructions.Length;
                    junctionRenderCheckInserted = true;
                    break;
                }
            }

            if (!junctionRenderCheckInserted)
            {
                Debug.LogError("NetNodeGroupDataPatch: Junction render check not inserted. Cancelling transpiler!");
                return(originalCodes);
            }

            var bendFlagCheckFound = false;

            for (; index < codes.Count; index++)
            {
                // else if ((m_flags & Flags.Bend) != 0)
                // IL_0e28: ldarg.s 'flags'
                // IL_0e2a: ldc.i4.s 64
                if (codes[index].opcode == OpCodes.Ldfld && codes[index].operand == netNodeFlagsField &&
                    codes[index + 1].opcode == OpCodes.Ldc_I4_S && (sbyte)codes[index + 1].operand == (sbyte)global::NetNode.Flags.Bend)
                {
                    bendFlagCheckFound = true;
                    break;
                }
            }

            if (!bendFlagCheckFound)
            {
                Debug.LogError("NetNodeGroupDataPatch: bendFlagCheck not found. Cancelling transpiler!");
                return(originalCodes);
            }

            CodeInstruction node5LocalVarLdLoc = null;

            for (; index < codes.Count; index++)
            {
                // IL_0359: ldfld class NetInfo/Node[] NetInfo::m_nodes
                // IL_05ed: ldloc.s 44
                // IL_05ef: ldelem.ref
                // IL_05f0: stloc.s 45
                if (codes[index].opcode == OpCodes.Ldfld && codes[index].operand == netInfoNodesField &&
                    TranspilerUtils.IsLdLoc(codes[index + 1]) &&
                    codes[index + 2].opcode == OpCodes.Ldelem_Ref &&
                    TranspilerUtils.IsStLoc(codes[index + 3]))
                {
                    // IL_0361: stloc.s 22
                    node5LocalVarLdLoc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 3]);
                    index += 4;
                    break;
                }
            }

            if (node5LocalVarLdLoc == null)
            {
                Debug.LogError("NetNodeGroupDataPatch: node5 local var for bend not found. Cancelling transpiler!");
                return(originalCodes);
            }

            var bendRenderCheckInserted = false;

            for (; index < codes.Count; index++)
            {
                // IL_0990: ldloc.s 40
                // IL_0992: ldfld bool NetInfo/Node::m_directConnect
                // IL_0997: brfalse IL_0b19
                if (codes[index].opcode == OpCodes.Ldc_I4 && (int)codes[index].operand == (int)NetInfo.ConnectGroup.AllGroups &&
                    codes[index + 1].opcode == OpCodes.And && codes[index + 2].opcode == OpCodes.Brfalse)
                {
                    var labelIfFalse      = codes[index + 2].operand;
                    var insertionPosition = index + 3;

                    // && NetNodeRenderPatch.ShouldRenderBendNodeLod(nodeID, node5)
                    var renderCheckInstructions = new[]
                    {
                        new CodeInstruction(OpCodes.Ldarg_1), // nodeID
                        new CodeInstruction(node5LocalVarLdLoc),
                        new CodeInstruction(OpCodes.Call, netNodeRenderPatchShouldRenderBendNodeLodMethod),
                        new CodeInstruction(OpCodes.Brfalse, labelIfFalse),
                    };

                    renderCheckInstructions[0].labels.AddRange(codes[insertionPosition].labels);
                    codes[insertionPosition].labels.Clear();

                    codes.InsertRange(insertionPosition, renderCheckInstructions);
                    TranspilerUtils.LogDebug("Bend render check inserted");

                    index = insertionPosition + renderCheckInstructions.Length;
                    bendRenderCheckInserted = true;
                    break;
                }
            }

            if (!bendRenderCheckInserted)
            {
                Debug.LogError("NetNodeGroupDataPatch: Bend render check not inserted. Cancelling transpiler!");
                return(originalCodes);
            }

            return(codes);
        }
Exemple #5
0
        public static IEnumerable <CodeInstruction> Transpiler(ILGenerator il, IEnumerable <CodeInstruction> instructions)
        {
            var netNodeRefreshEndDataMethod =
                typeof(global::NetNode).GetMethod("RefreshEndData", BindingFlags.NonPublic | BindingFlags.Instance);
            var netNodeGetSegmentMethod = typeof(global::NetNode).GetMethod("GetSegment");
            var netInfoNodesField       = typeof(NetInfo).GetField("m_nodes");

            var netNodeRenderPatchShouldRenderJunctionNodeMethod =
                typeof(NetNodeRenderPatch).GetMethod("ShouldRenderJunctionNode");

            var netNodeRenderPatchShouldRenderBendNodeMethod =
                typeof(NetNodeRenderPatch).GetMethod("ShouldRenderBendNode");

            if (netNodeRefreshEndDataMethod == null || netNodeGetSegmentMethod == null || netInfoNodesField == null || netNodeRenderPatchShouldRenderJunctionNodeMethod == null || netNodeRenderPatchShouldRenderBendNodeMethod == null)
            {
                Debug.LogError("NetNodeRenderInstancePatch: Necessary methods and field not found. Cancelling transpiler!");
                return(instructions);
            }

            var index = 0;

            var originalCodes = new List <CodeInstruction>(instructions);
            var codes         = new List <CodeInstruction>(originalCodes);

            var refreshEndDataCallFound = false;

            for (; index < codes.Count; index++)
            {
                // IL_0066: call instance void NetNode::RefreshEndData(uint16, class NetInfo, uint32, valuetype RenderManager/Instance&)
                if (codes[index].opcode == OpCodes.Call && codes[index].operand == netNodeRefreshEndDataMethod)
                {
                    refreshEndDataCallFound = true;
                    break;
                }
            }

            if (!refreshEndDataCallFound)
            {
                Debug.LogError("NetNodeRenderInstancePatch: RefreshEndData call not found. Cancelling transpiler!");
                return(originalCodes);
            }

            var junctionFlagCheckFound = false;

            for (; index < codes.Count; index++)
            {
                // if ((flags & Flags.Junction) != 0)
                // IL_0077: ldarg.s 'flags'
                // IL_0079: ldc.i4 128
                if (codes[index].opcode == OpCodes.Ldarg_S && (byte)codes[index].operand == FlagsArgIndex &&
                    codes[index + 1].opcode == OpCodes.Ldc_I4 && (int)codes[index + 1].operand == (int)global::NetNode.Flags.Junction)
                {
                    junctionFlagCheckFound = true;
                    break;
                }
            }

            if (!junctionFlagCheckFound)
            {
                Debug.LogError("NetNodeRenderInstancePatch: junctionFlagCheck not found. Cancelling transpiler!");
                return(originalCodes);
            }

            CodeInstruction segmentLocalVarLdloc  = null;
            CodeInstruction segment2LocalVarLdloc = null;
            CodeInstruction nodeLocalVarLdLoc     = null;

            for (; index < codes.Count; index++)
            {
                // IL_009c: call instance uint16 NetNode::GetSegment(int32)
                if (codes[index].opcode == OpCodes.Call && codes[index].operand == netNodeGetSegmentMethod && TranspilerUtils.IsStLoc(codes[index + 1]))
                {
                    // IL_00a1: stloc.0
                    segmentLocalVarLdloc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 1]);
                    index += 2;
                    break;
                }
            }

            for (; index < codes.Count; index++)
            {
                // IL_00ac: call instance uint16 NetNode::GetSegment(int32)
                if (codes[index].opcode == OpCodes.Call && codes[index].operand == netNodeGetSegmentMethod && TranspilerUtils.IsStLoc(codes[index + 1]))
                {
                    // IL_00b1: stloc.1
                    segment2LocalVarLdloc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 1]);
                    index += 2;
                    break;
                }
            }

            for (; index < codes.Count; index++)
            {
                // IL_013e: ldfld class NetInfo/Node[] NetInfo::m_nodes
                if (codes[index].opcode == OpCodes.Ldfld && codes[index].operand == netInfoNodesField && TranspilerUtils.IsStLoc(codes[index + 3]))
                {
                    // IL_0146: stloc.s 6
                    nodeLocalVarLdLoc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 3]);
                    index            += 4;
                    break;
                }
            }

            if (segmentLocalVarLdloc == null || segment2LocalVarLdloc == null || nodeLocalVarLdLoc == null)
            {
                Debug.LogError("NetNodeRenderInstancePatch: Necessary field for junction not found. Cancelling transpiler!");
                Debug.LogError($"{segmentLocalVarLdloc}, {segment2LocalVarLdloc}, {nodeLocalVarLdLoc}");
                return(originalCodes);
            }

            var junctionRenderCheckInserted = false;

            for (; index < codes.Count; index++)
            {
                // IL_017c: ldc.i4 987135
                // IL_0181: and
                // IL_0182: brfalse IL_0570
                if (codes[index].opcode == OpCodes.Ldc_I4 && (int)codes[index].operand == (int)NetInfo.ConnectGroup.AllGroups &&
                    codes[index + 1].opcode == OpCodes.And && codes[index + 2].opcode == OpCodes.Brfalse)
                {
                    var labelIfFalse      = codes[index + 2].operand;
                    var insertionPosition = index + 3;

                    // && NetNodeRenderPatch.NetNodeRenderPatch(node, segment, segment2)
                    // IL_01FD: ldloc.s V_6
                    // IL_01FF: ldloc.0
                    // IL_0200: ldloc.1
                    // IL_0201: call bool[NetworkSkins] NetworkSkins.Patches.NetNodeRenderPatch::ShouldRenderJunctionNode(class NetInfo/Node, uint16, uint16)
                    // IL_0206: brfalse.s IL_024A
                    var renderCheckInstructions = new[]
                    {
                        new CodeInstruction(nodeLocalVarLdLoc),
                        new CodeInstruction(segmentLocalVarLdloc),
                        new CodeInstruction(segment2LocalVarLdloc),
                        new CodeInstruction(OpCodes.Call, netNodeRenderPatchShouldRenderJunctionNodeMethod),
                        new CodeInstruction(OpCodes.Brfalse, labelIfFalse),
                    };

                    renderCheckInstructions[0].labels.AddRange(codes[insertionPosition].labels);
                    codes[insertionPosition].labels.Clear();

                    codes.InsertRange(insertionPosition, renderCheckInstructions);
                    TranspilerUtils.LogDebug("Junction render check inserted");

                    index = insertionPosition + renderCheckInstructions.Length;
                    junctionRenderCheckInserted = true;
                    break;
                }
            }

            if (!junctionRenderCheckInserted)
            {
                Debug.LogError("NetNodeRenderInstancePatch: Render check 1 not inserted. Cancelling transpiler!");
                return(originalCodes);
            }

            var bendFlagCheckFound = false;

            for (; index < codes.Count; index++)
            {
                // else if ((flags & Flags.Bend) != 0)
                // IL_0e28: ldarg.s 'flags'
                // IL_0e2a: ldc.i4.s 64
                if (codes[index].opcode == OpCodes.Ldarg_S && (byte)codes[index].operand == FlagsArgIndex &&
                    codes[index + 1].opcode == OpCodes.Ldc_I4_S && (sbyte)codes[index + 1].operand == (sbyte)global::NetNode.Flags.Bend)
                {
                    bendFlagCheckFound = true;
                    break;
                }
            }

            if (!bendFlagCheckFound)
            {
                Debug.LogError("NetNodeRenderInstancePatch: bendFlagCheck not found. Cancelling transpiler!");
                return(originalCodes);
            }

            CodeInstruction segment5LocalVarLdloc = null;
            CodeInstruction segment6LocalVarLdloc = null;
            CodeInstruction node4LocalVarLdLoc    = null;

            for (; index < codes.Count; index++)
            {
                // IL_11ae: call instance uint16 NetNode::GetSegment(int32)
                if (codes[index].opcode == OpCodes.Call && codes[index].operand == netNodeGetSegmentMethod && TranspilerUtils.IsStLoc(codes[index + 1]))
                {
                    // IL_00a1: stloc.s 32
                    segment5LocalVarLdloc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 1]);
                    index += 2;
                    break;
                }
            }

            for (; index < codes.Count; index++)
            {
                // IL_11bf: call instance uint16 NetNode::GetSegment(int32)
                if (codes[index].opcode == OpCodes.Call && codes[index].operand == netNodeGetSegmentMethod && TranspilerUtils.IsStLoc(codes[index + 1]))
                {
                    // IL_11c4: stloc.s 33
                    segment6LocalVarLdloc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 1]);
                    index += 2;
                    break;
                }
            }

            for (; index < codes.Count; index++)
            {
                // IL_120d: ldfld class NetInfo/Node[] NetInfo::m_nodes
                if (codes[index].opcode == OpCodes.Ldfld && codes[index].operand == netInfoNodesField && TranspilerUtils.IsStLoc(codes[index + 3]))
                {
                    // IL_1215: stloc.s 35
                    node4LocalVarLdLoc = TranspilerUtils.BuildLdLocFromStLoc(codes[index + 3]);
                    index += 4;
                    break;
                }
            }

            if (segment5LocalVarLdloc == null || segment6LocalVarLdloc == null || node4LocalVarLdLoc == null)
            {
                Debug.LogError("NetNodeRenderInstancePatch: Necessary field for bend not found. Cancelling transpiler!");
                Debug.LogError($"{segment5LocalVarLdloc} {segment6LocalVarLdloc} {node4LocalVarLdLoc}");
                return(originalCodes);
            }

            var bendRenderCheckInserted = false;

            for (; index < codes.Count; index++)
            {
                // IL_124b: ldc.i4 987135
                // IL_1250: and
                // IL_1251: brfalse IL_1637
                if (codes[index].opcode == OpCodes.Ldc_I4 && (int)codes[index].operand == (int)NetInfo.ConnectGroup.AllGroups &&
                    codes[index + 1].opcode == OpCodes.And && codes[index + 2].opcode == OpCodes.Brfalse)
                {
                    var labelIfFalse      = codes[index + 2].operand;
                    var insertionPosition = index + 3;

                    // ldloc.s 35
                    // ldloc.s 32
                    // ldloc.s 33
                    // call bool[NetworkSkins] NetworkSkins.Patches.NetNodeRenderPatch::ShouldRenderJunctionNode(class NetInfo/Node, uint16, uint16)
                    // brfalse.s IL_1637
                    var renderCheckInstructions = new[]
                    {
                        new CodeInstruction(node4LocalVarLdLoc),
                        new CodeInstruction(segment5LocalVarLdloc),
                        new CodeInstruction(segment6LocalVarLdloc),
                        new CodeInstruction(OpCodes.Call, netNodeRenderPatchShouldRenderBendNodeMethod),
                        new CodeInstruction(OpCodes.Brfalse, labelIfFalse),
                    };

                    renderCheckInstructions[0].labels.AddRange(codes[insertionPosition].labels);
                    codes[insertionPosition].labels.Clear();

                    codes.InsertRange(insertionPosition, renderCheckInstructions);
                    TranspilerUtils.LogDebug("Bend render check inserted");

                    index = insertionPosition + renderCheckInstructions.Length;
                    bendRenderCheckInserted = true;
                    break;
                }
            }

            if (!bendRenderCheckInserted)
            {
                Debug.LogError("NetNodeRenderInstancePatch: Bend render check not inserted. Cancelling transpiler!");
                return(originalCodes);
            }

            return(codes);
        }