public PrepareRecord(ModRecord mod, string jsonPath, jtProcGeneric jtProc, List <Localize.Strings.Culture> cultures, Dictionary <string, string> locCache, object content, LocalizationFile locFile, string basePath) { this.mod = mod; this.jsonPath = jsonPath; this.jtProc = jtProc; this.cultures = new HashSet <Localize.Strings.Culture>(); foreach (Localize.Strings.Culture culture in cultures) { this.cultures.Add(culture); } ; this.locCache = locCache; this.content = content; this.updated = false; this.locFile = locFile; this.basePath = basePath; }
//private void PrepareMods_Click(object sender, EventArgs e) { // saveFileDialog.OverwritePrompt = false; // HashSet<string> statistics = new HashSet<string>(); // GoogleTranslate t = new GoogleTranslate(); // int debugCounter = 0; // int debugCounterMax = 5; // if (saveFileDialog.ShowDialog() == DialogResult.OK) { // string current_file = string.Empty; // try { // bool fileExists = File.Exists(saveFileDialog.FileName); // string baseFile = saveFileDialog.FileName; // LocalizationFile locFile = new LocalizationFile(saveFileDialog.FileName); // List<ModRecord> mods = modsList.CheckedItems.OfType<ModRecord>().ToList<ModRecord>(); // Dictionary<string, string> jsonUpdatedContent = new Dictionary<string, string>(); // Dictionary<string, ConversationFile> convUpdatedContent = new Dictionary<string, ConversationFile>(); // foreach (ModRecord mod in mods) { // if (debugCounter > debugCounterMax) { break; } // string modName = Normilize(mod.Name); // List<string> jsonsPath = new List<string>(); // GetAllJsons(mod.Path, ref jsonsPath, 0); // foreach (string jsonPath in jsonsPath) { // current_file = jsonPath; // bool updated = false; // //MessageBox.Show(jsonPath); // string filename = Normilize(Path.GetFileNameWithoutExtension(jsonPath)); // object content = null; // if (Path.GetFileName(jsonPath).ToUpper() == "LOCALIZATION.JSON") { continue; } // if (Path.GetExtension(jsonPath).ToUpper() == ".JSON") { // string jsonCont = File.ReadAllText(jsonPath); // content = JObject.Parse(jsonCont); // } else if (Path.GetExtension(jsonPath).ToUpper() == ".BYTES") { // content = new ConversationFile(jsonPath); // } // foreach (var jtproc in partsList.CheckedItems) { // if (debugCounter > debugCounterMax) { break; } // jtProcGeneric jtProc = jtproc as jtProcGeneric; // if (jtProc == null) { continue; } // Dictionary<string, string> replaced = new Dictionary<string, string>(); // jtProc.proc(modName, filename, ref content, replaced, false); // foreach (var replacements in replaced) { // if (debugCounter > debugCounterMax) { break; } // if (string.IsNullOrEmpty(replacements.Value) == false) { // CustomTranslation.TranslateRecord nTr = new CustomTranslation.TranslateRecord(); // nTr.FileName = jsonPath.Substring(GameBaseSelector.SelectedPath.Length); // nTr.Name = replacements.Key; // nTr.Original = replacements.Value; // //nTr.Localization.Add(Localize.Strings.Culture.CULTURE_EN_US, replacements.Value); // foreach (Localize.Strings.Culture locLang in langsList.CheckedItems.OfType<Localize.Strings.Culture>().ToList<Localize.Strings.Culture>()) { // string val = replacements.Value; // string nval = Normilize(val); // if (locLang == Localize.Strings.Culture.CULTURE_RU_RU) { // if (val.Length > 30) { // if (translationCache.ContainsKey(nval) == false) { // statistics.Add(val); // MessageBox.Show(val); // val = GoogleTranslate.Translate(val); // translationCache.Add(nval, val); // Log.Debug.Write(0, "'" + replacements.Value + "' - '" + val + "'\n", true); // MessageBox.Show(val); // ++debugCounter; // } else { // val = translationCache[nval]; // } // } // } // nTr.Localization.Add(locLang, val); // } // locFile.Merge(nTr); // updated = true; // } else { // if (locFile.map.ContainsKey(replacements.Key)) { // string original = locFile.map[replacements.Key].Original; // if (string.IsNullOrEmpty(original)) { // original = locFile.map[replacements.Key].Localization[Localize.Strings.Culture.CULTURE_EN_US]; // } // if (string.IsNullOrEmpty(original) == false) { // CustomTranslation.TranslateRecord nTr = new CustomTranslation.TranslateRecord(); // nTr.FileName = jsonPath.Substring(GameBaseSelector.SelectedPath.Length); ; // nTr.Name = replacements.Key; // nTr.Original = original; // foreach (Localize.Strings.Culture locLang in langsList.CheckedItems.OfType<Localize.Strings.Culture>().ToList<Localize.Strings.Culture>()) { // if (locFile.map[replacements.Key].Localization.ContainsKey(locLang) == false) { // string val = original; // string nval = Normilize(val); // statistics.Add(val); // if (locLang == Localize.Strings.Culture.CULTURE_RU_RU) { // if (val.Length > 30) { // if (translationCache.ContainsKey(nval) == false) { // statistics.Add(val); // MessageBox.Show(val); // val = GoogleTranslate.Translate(val); // translationCache.Add(nval, val); // Log.Debug.Write(0, "'" + replacements.Value + "' - '" + val + "'\n", true); // MessageBox.Show(val); // ++debugCounter; // } else { // val = translationCache[nval]; // } // } // } // nTr.Localization.Add(locLang, val); // } // } // locFile.Merge(nTr); // } // } // } // } // } // if (updated) { // if (Path.GetExtension(jsonPath).ToUpper() == ".JSON") { // JObject json = content as JObject; // if (json != null) { jsonUpdatedContent.Add(jsonPath, json.ToString(Formatting.Indented)); }; // } else if (Path.GetExtension(jsonPath).ToUpper() == ".BYTES") { // ConversationFile cfile = content as ConversationFile; // if (cfile != null) { convUpdatedContent.Add(jsonPath, cfile); }; // } // }; // } // current_file = string.Empty; // } // if (fileExists == false) { // locFile.Save(); // } else { // saveFileDialog.InitialDirectory = Path.GetDirectoryName(baseFile); // if (saveFileDialog.ShowDialog() == DialogResult.OK) { // if (saveFileDialog.FileName == baseFile) { // //MessageBox.Show("Same file"); // locFile.Save(); // } else { // //MessageBox.Show("Different file"); // locFile.filename = saveFileDialog.FileName; // locFile.removeOtherTranslations(langsList.CheckedItems.OfType<Localize.Strings.Culture>().ToList<Localize.Strings.Culture>()); // locFile.Save(true); // } // } // } // foreach (var uJsons in jsonUpdatedContent) { // File.WriteAllText(uJsons.Key, uJsons.Value); // } // foreach (var uConv in convUpdatedContent) { // uConv.Value.Save(); // } // int overralCharsLength = 0; // foreach (string str in statistics) { overralCharsLength += str.Length; } // MessageBox.Show("Done. Strings: " + statistics.Count + ". Characters:" + overralCharsLength); // } catch (Exception ex) { // MessageBox.Show("in file:" + current_file + "\n" + ex.ToString()); // } // string trCache = JsonConvert.SerializeObject(translationCache, Formatting.Indented); // File.WriteAllText(translationCache_filename, trCache); // } //} private void PrepareMods_Click(object sender, EventArgs e) { saveFileDialog.OverwritePrompt = false; HashSet <string> statistics = new HashSet <string>(); GoogleTranslate t = new GoogleTranslate(); int debugCounter = 0; int debugCounterMax = 5; bool deleteOtherTranslations = false; if (saveFileDialog.ShowDialog() == DialogResult.OK) { string current_file = string.Empty; try { bool fileExists = File.Exists(saveFileDialog.FileName); LocalizationFile locFile = new LocalizationFile(saveFileDialog.FileName); if (File.Exists(locFile.filename)) { saveFileDialog.InitialDirectory = Path.GetDirectoryName(locFile.filename); if (saveFileDialog.ShowDialog() == DialogResult.OK) { if (saveFileDialog.FileName != locFile.filename) { locFile.MergeFile(saveFileDialog.FileName); locFile.filename = saveFileDialog.FileName; deleteOtherTranslations = true; } } } //locFile.DebugLogDump(); //Application.Exit(); List <ModRecord> mods = modsList.CheckedItems.OfType <ModRecord>().ToList <ModRecord>(); Dictionary <string, string> jsonUpdatedContent = new Dictionary <string, string>(); Dictionary <string, ConversationFile> convUpdatedContent = new Dictionary <string, ConversationFile>(); List <PrepareRecord> prepareRecords = new List <PrepareRecord>(); foreach (ModRecord mod in mods) { if (debugCounter > debugCounterMax) { break; } List <string> jsonsPath = new List <string>(); GetAllJsons(mod.Path, ref jsonsPath, 0); foreach (string jsonPath in jsonsPath) { current_file = jsonPath; object content = null; if (Path.GetFileName(jsonPath).ToUpper() == "LOCALIZATION.JSON") { continue; } if (Path.GetExtension(jsonPath).ToUpper() == ".JSON") { string jsonCont = File.ReadAllText(jsonPath); content = JObject.Parse(jsonCont); } else if (Path.GetExtension(jsonPath).ToUpper() == ".BYTES") { content = new ConversationFile(jsonPath); } foreach (var jtproc in partsList.CheckedItems) { if (debugCounter > debugCounterMax) { break; } jtProcGeneric jtProc = jtproc as jtProcGeneric; if (jtProc == null) { continue; } prepareRecords.Add(new PrepareRecord(mod, jsonPath, jtProc, langsList.CheckedItems.OfType <Localize.Strings.Culture>().ToList <Localize.Strings.Culture>(), translationCache, content, locFile, GameBaseSelector.SelectedPath )); } } current_file = string.Empty; } ProcessForm processForm = new ProcessForm(); processForm.DeleteOtherTranslations = deleteOtherTranslations; processForm.prepareRecords = prepareRecords; processForm.progressBar.Maximum = prepareRecords.Count * 2; processForm.backgroundWorker.RunWorkerAsync(); processForm.ShowDialog(); if (deleteOtherTranslations) { locFile.removeOtherTranslations(langsList.CheckedItems.OfType <Localize.Strings.Culture>().ToList <Localize.Strings.Culture>()); } locFile.Save(deleteOtherTranslations); foreach (PrepareRecord pr in prepareRecords) { if (pr.updated) { if (Path.GetExtension(pr.jsonPath).ToUpper() == ".JSON") { JObject json = pr.content as JObject; if (json != null) { if (jsonUpdatedContent.ContainsKey(pr.jsonPath) == false) { jsonUpdatedContent.Add(pr.jsonPath, json.ToString(Formatting.Indented)); } } ; } else if (Path.GetExtension(pr.jsonPath).ToUpper() == ".BYTES") { ConversationFile cfile = pr.content as ConversationFile; if (cfile != null) { if (convUpdatedContent.ContainsKey(pr.jsonPath) == false) { convUpdatedContent.Add(pr.jsonPath, cfile); } ; } ; } } ; } foreach (var uJsons in jsonUpdatedContent) { File.WriteAllText(uJsons.Key, uJsons.Value); } foreach (var uConv in convUpdatedContent) { uConv.Value.Save(); } MessageBox.Show("Compleete"); } catch (Exception ex) { MessageBox.Show("in file:" + current_file + "\n" + ex.ToString()); } string trCache = JsonConvert.SerializeObject(translationCache, Formatting.Indented); File.WriteAllText(translationCache_filename, trCache); } }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { try { List <ModRecord> mods = ModRecord.GatherMods(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "..")); Dictionary <string, string> jsonUpdatedContent = new Dictionary <string, string>(); Dictionary <string, ConversationFile> convUpdatedContent = new Dictionary <string, ConversationFile>(); int modcounter = 0; foreach (ModRecord mod in mods) { backgroundWorker.ReportProgress((int)Math.Round((float)modcounter * 100.0f / (float)mods.Count)); ++modcounter; string modName = ModRecord.Normilize(mod.Name); //MessageBox.Show(modName); List <string> jsonsPath = new List <string>(); ModRecord.GetAllJsons(mod.Path, ref jsonsPath, 0); foreach (string jsonPath in jsonsPath) { bool updated = false; //MessageBox.Show(jsonPath); string filename = ModRecord.Normilize(Path.GetFileNameWithoutExtension(jsonPath)); object content = null; if (Path.GetFileName(jsonPath).ToUpper() == "LOCALIZATION.JSON") { continue; } if (Path.GetExtension(jsonPath).ToUpper() == ".JSON") { string jsonCont = File.ReadAllText(jsonPath); content = JObject.Parse(jsonCont); } else if (Path.GetExtension(jsonPath).ToUpper() == ".BYTES") { content = new ConversationFile(jsonPath); } foreach (var jtproc in partsList) { jtProcGeneric jtProc = jtproc as jtProcGeneric; if (jtProc == null) { continue; } Dictionary <string, string> replaced = new Dictionary <string, string>(); jtProc.proc(modName, filename, ref content, replaced, true); foreach (var replacements in replaced) { if (string.IsNullOrEmpty(replacements.Value) == false) { updated = true; } } } if (updated) { if (Path.GetExtension(jsonPath).ToUpper() == ".JSON") { JObject json = content as JObject; if (json != null) { jsonUpdatedContent.Add(jsonPath, json.ToString(Formatting.Indented)); } ; } else if (Path.GetExtension(jsonPath).ToUpper() == ".BYTES") { ConversationFile cfile = content as ConversationFile; if (cfile != null) { convUpdatedContent.Add(jsonPath, cfile); } ; } } ; } } foreach (var uJsons in jsonUpdatedContent) { File.WriteAllText(uJsons.Key, uJsons.Value); } foreach (var uConv in convUpdatedContent) { uConv.Value.Save(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }