예제 #1
0
        /**<summary>Patches the Terraria executable.</summary>*/
        public static void Patch()
        {
            // Backup the file first
            if (!File.Exists(BackupPath))
            {
                File.Copy(ExePath, BackupPath, false);
            }

            // Do this first so we don't bork the executable if copying fails
            CopyRequiredFiles();
            CopyLocalizationFiles();

            // Load the assembly
            var resolver   = new EmbeddedAssemblyResolver();
            var parameters = new ReaderParameters {
                AssemblyResolver = resolver
            };

            AsmDefinition = AssemblyDefinition.ReadAssembly(ExePath, parameters);
            ModDefinition = AsmDefinition.MainModule;

            // Get links to Terraria types that will have their functions modified
            Main            = IL.GetTypeDefinition(ModDefinition, "Main");
            Dust            = IL.GetTypeDefinition(ModDefinition, "Dust");
            ItemText        = IL.GetTypeDefinition(ModDefinition, "ItemText");
            LanguageManager = IL.GetTypeDefinition(ModDefinition, "LanguageManager");

            // Get link and import CoinReplacer type
            CoinReplacer = ModDefinition.Import(typeof(CoinReplacer)).Resolve();

            // Check if we've already been patched
            if (IL.GetFieldDefinition(Main, AlreadyPatchedStaticField, false) != null)
            {
                throw new AlreadyPatchedException();
            }

            // Add a static field to let us know this exe has already been patched
            var objectType = IL.GetTypeReference(ModDefinition, "System.Object");

            IL.AddStaticField(Main, AlreadyPatchedStaticField, objectType);

            // Patch Terraria
            Patch_Main_DrawInventory();
            Patch_Main_GUIChatDrawInner();
            Patch_Main_MouseText_DrawItemTooltip();
            Patch_Main_ValueToCoins();

            Patch_Dust_UpdateDust();

            Patch_ItemText_NewText();
            Patch_ItemText_ValueToName();

            Patch_LanguageManager_LoadLanguage();

            // Save the modifications
            AsmDefinition.Write(ExePath);
            // Wait for the exe to be closed by AsmDefinition.Write()
            Thread.Sleep(400);
            IL.MakeLargeAddressAware(ExePath);
        }
        /**<summary>Patches the Terraria executable.</summary>*/
        public static void Patch()
        {
            // Backup the file first
            if (!File.Exists(BackupPath))
            {
                File.Copy(ExePath, BackupPath, false);
            }

            // Do this first so we don't bork the executable if copying fails
            CopyRequiredFiles();

            // Load the assembly
            var resolver   = new EmbeddedAssemblyResolver();
            var parameters = new ReaderParameters {
                AssemblyResolver = resolver
            };

            AsmDefinition = AssemblyDefinition.ReadAssembly(ExePath, parameters);
            ModDefinition = AsmDefinition.MainModule;

            // Get links to Terraria types that will have their functions modified
            Main = IL.GetTypeDefinition(ModDefinition, "Main");
            Item = IL.GetTypeDefinition(ModDefinition, "Item");

            // Get link and import ItemModifier type
            ItemModifier = ModDefinition.Import(typeof(ItemModifier)).Resolve();

            // Check if we've already been patched
            if (IL.GetFieldDefinition(Main, AlreadyPatchedStaticField, false) != null)
            {
                throw new AlreadyPatchedException();
            }

            // Add a static field to let us know this exe has already been patched
            var objectType = IL.GetTypeReference(ModDefinition, "System.Object");

            IL.AddStaticField(Main, AlreadyPatchedStaticField, objectType);

            // Patch Terraria
            Patch_Main_LoadPlayers();
            Patch_Item_SetDefaults();

            // Save the modifications
            AsmDefinition.Write(ExePath);
            // Wait for the exe to be closed by AsmDefinition.Write()
            Thread.Sleep(400);
            IL.MakeLargeAddressAware(ExePath);
        }