Beispiel #1
0
        public static TextFile.ItemFile GetItemTexts(Mod mod)
        {
            var items = typeof(Mod).GetField("items", BindingFlags.Instance | BindingFlags.NonPublic).
                        GetValue(mod) as Dictionary <string, ModItem>;
            var itemFile = new TextFile.ItemFile();

            foreach (var itemPair in items)
            {
                // Get basic info (name and tooltip)
                var itemTranslation = new TextFile.ItemTranslation(itemPair.Value);
                itemFile.Items.Add(itemPair.Key, itemTranslation);

                // Get setbonus
                var updateArmorSetMethod = itemPair.Value.GetType()
                                           .GetMethod("UpdateArmorSet", BindingFlags.Instance | BindingFlags.Public);
                var instructions = ILTool.GetInstructions(updateArmorSetMethod);
                var target       = instructions.Find(i => i.opcode == OpCodes.Stfld && i.operand.ToString().Contains("setBonus"));
                if (target != null)
                {
                    var setBonus = ILTool.GetStrBeforeInstruction(instructions, target);
                    if (!string.IsNullOrWhiteSpace(setBonus))
                    {
                        itemFile.SetBonus.Add(itemPair.Key, new TextFile.SetBonusTranslation(setBonus));
                    }
                }
            }

            return(itemFile);
        }
Beispiel #2
0
        public static TextFile.NPCFile GetNPCTexts(Mod mod)
        {
            var npcs = typeof(Mod).GetField("npcs", BindingFlags.Instance | BindingFlags.NonPublic)
                       .GetValue(mod) as Dictionary <string, ModNPC>;
            var npcFile = new TextFile.NPCFile();

            foreach (var npcPair in npcs)
            {
                // Get name
                var npcTranslation = new TextFile.NPCTranslation(npcPair.Value);
                npcFile.NPCs.Add(npcPair.Key, npcTranslation);

                // Get chat
                var getChatMethod = npcPair.Value.GetType().GetMethod("GetChat", BindingFlags.Instance | BindingFlags.Public);
                var instructions  = ILTool.GetInstructions(getChatMethod);
                var chatlines     = new List <ILInstruction>();
                for (int i = 0; i < instructions.Count; i++)
                {
                    if (instructions[i].opcode == OpCodes.Ldstr)
                    {
                        if (i + 1 < instructions.Count &&
                            instructions[i + 1].operand != null &&
                            !instructions[i + 1].operand.ToString().Contains("GetTextValue"))
                        {
                            chatlines.Add(instructions[i]);
                        }
                    }
                }

                if (chatlines != null && chatlines.Count > 0)
                {
                    var chatLineTranslations = new List <TextFile.ChatLineTranslation>();
                    foreach (var line in chatlines)
                    {
                        chatLineTranslations.Add(new TextFile.ChatLineTranslation(line.operand.ToString()));
                    }

                    if (chatLineTranslations.Count > 0)
                    {
                        npcFile.ChatLines.Add(npcPair.Key, chatLineTranslations);
                    }
                }

                // Get button
                var setChatButtonsMethod = npcPair.Value.GetType()
                                           .GetMethod("SetChatButtons", BindingFlags.Instance | BindingFlags.Public);
                instructions = ILTool.GetInstructions(setChatButtonsMethod);
                var buttons = new List <ILInstruction>();
                for (int i = 0; i < instructions.Count; i++)
                {
                    if (instructions[i].opcode == OpCodes.Ldstr)
                    {
                        if (i + 1 < instructions.Count &&
                            instructions[i + 1].operand != null &&
                            !instructions[i + 1].operand.ToString().Contains("GetTextValue"))
                        {
                            buttons.Add(instructions[i]);
                        }
                    }
                }

                if (buttons != null && buttons.Count > 0)
                {
                    var chatButtonsTranslations = new List <TextFile.ChatButtonTranslation>();
                    foreach (var line in buttons)
                    {
                        chatButtonsTranslations.Add(new TextFile.ChatButtonTranslation(line.operand.ToString()));
                    }

                    npcFile.ChatButtons.Add(npcPair.Key, chatButtonsTranslations);
                }
            }

            return(npcFile);
        }