public static void UpdateNPCsText(TextFile.NPCFile oldFile, TextFile.NPCFile newFile) { var sb = new StringBuilder(); // Update npcs var npcResult = UpdateDict(oldFile.NPCs, newFile.NPCs); // New npc foreach (var npc in npcResult.New) { sb.AppendFormat(NewNPCLogFormat, npc.Key, npc.Value.Name); } // Changed npc foreach (var npc in npcResult.Change) { TextFile.NPCTranslation old; oldFile.NPCs.TryGetValue(npc.Key, out old); if (old != null && old.Name != npc.Value.Name) { sb.AppendFormat(NPCChangeLogFormat, npc.Key, old.Name, npc.Value.Name); old.Name = npc.Value.Name; } } Logger.TextUpdateLog(sb.ToString()); }
public void TranslateNPCs(TextFile.NPCFile npcs) { foreach (var npc in npcs.NPCs.Values) { Logger.DebugLog(string.Format("Translating npc: {0}", npc.Name)); npc.NameTranslation = Query(npc.Name); } }
public static void ExportNPCTexts(Mod mod, string path) { if (mod != null) { 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 = ILHelper.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 = ILHelper.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); } } using (var fs = new FileStream(Path.Combine(path, "NPCs.json"), FileMode.Create)) { using (var sw = new StreamWriter(fs)) { sw.Write(JsonConvert.SerializeObject(npcFile, Formatting.Indented)); } } } }
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); }