private void LoadLocalizations(string branch = null)
        {
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += (x, y) =>
            {
                if (!LocalizationBranches.Any())
                {
                    var ghclient = new GitHubClient(new ProductHeaderValue(@"ME3TweaksModManager"));
                    try
                    {
                        var branches    = ghclient.Repository.Branch.GetAll("ME3Tweaks", "ME3TweaksModManager").Result;
                        var locbranches = branches.Where(x => x.Name.Contains("master") || x.Name.Contains("-localization"));
                        System.Windows.Application.Current.Dispatcher.Invoke(delegate { LocalizationBranches.ReplaceAll(locbranches.Select(x => x.Name)); });
                    }
                    catch (Exception e)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(delegate { MessageBox.Show("Error getting list of localization branches: " + e.Message); });
                        return;
                    }
                }

                if (LocalizationBranches.Any())
                {
                    if (branch == null)
                    {
                        branch         = LocalizationBranches.First();
                        SelectedBranch = branch;
                        oldBranch      = branch;
                    }
                }
                else
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(delegate { MessageBox.Show("Could not find any branches on ME3TweaksModManager repo containing name 'localization'"); });
                    return;
                }

                var       dictionaries = new Dictionary <string, string>();
                string    endpoint     = $"https://raw.githubusercontent.com/ME3Tweaks/ME3TweaksModManager/{branch}/MassEffectModManagerCore/modmanager/localizations/"; //make dynamic, maybe with octokit.
                WebClient client       = new WebClient();
                foreach (var lang in LocalizedString.Languages)
                {
                    var url  = endpoint + lang + ".xaml";
                    var dict = client.DownloadStringAwareOfEncoding(url);
                    dictionaries[lang] = dict;
                }

                //Parse INT.
                int currentLine          = 3; //Skip header.
                LocalizationCategory cat = null;
                int numBlankLines        = 0;
                List <LocalizationCategory> categories = new List <LocalizationCategory>();
                var intLines = Regex.Split(dictionaries["int"], "\r\n|\r|\n");
                for (int i = 3; i < intLines.Length - 2; i++)
                {
                    var line = intLines[i].Trim();
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        numBlankLines++;
                        continue;
                    }

                    if (line.StartsWith("<!--") && line.EndsWith("-->"))
                    {
                        //Comment - parse
                        line = line.Substring(4);
                        line = line.Substring(0, line.Length - 3);
                        line = line.Trim();
                        if (numBlankLines > 0 || cat == null)
                        {
                            //New category?
                            if (cat != null)
                            {
                                categories.Add(cat);
                            }

                            cat = new LocalizationCategory()
                            {
                                CategoryName = line
                            };
                        }

                        //notes for previous item?
                        var prevItem = cat.LocalizedStringsForSection.LastOrDefault();
                        if (prevItem != null)
                        {
                            prevItem.notes = line;
                        }
                        //Debug.WriteLine(line);

                        //New Category
                        //line = line.
                        continue;
                    }

                    numBlankLines = 0;
                    var             lineInfo = extractInfo(line);
                    LocalizedString ls       = new LocalizedString()
                    {
                        key = lineInfo.key,
                        preservewhitespace = lineInfo.preserveWhitespace,
                        INT = lineInfo.text
                    };
                    if (lineInfo.key == null)
                    {
                        Debugger.Break();
                    }
                    if (ls.INT == null)
                    {
                        Debugger.Break();
                    }
                    cat.LocalizedStringsForSection.Add(ls);
                }

                if (cat != null)
                {
                    categories.Add(cat);
                }

                parseLocalizations(categories, dictionaries);
                y.Result = categories;

                //TIPS SERVICE
                string tipsEndpoint = "https://me3tweaks.com/modmanager/services/tipsservice";
                string contents;
                using var wc = new System.Net.WebClient();
                var tipsJson = wc.DownloadString(tipsEndpoint);
                var jsonObj  = JsonConvert.DeserializeObject <Dictionary <string, List <string> > >(tipsJson);
                var langs    = LocalizedString.Languages.Where(x => x != "int");
                var locTips  = new List <LocalizedString>();
                for (int i = 0; i < jsonObj["int"].Count; i++)
                {
                    LocalizedString ls = new LocalizedString()
                    {
                        INT = jsonObj["int"][i]
                    };
                    foreach (var lang in langs)
                    {
                        if (jsonObj[lang].Count <= i)
                        {
                            continue;                           //skip
                        }
                        switch (lang)
                        {
                        case "rus":
                            ls.RUS = jsonObj["rus"][i];
                            break;

                        case "deu":
                            ls.DEU = jsonObj["deu"][i];
                            break;

                        case "pol":
                            ls.POL = jsonObj["pol"][i];
                            break;

                        case "fra":
                            ls.FRA = jsonObj["fra"][i];
                            break;

                        case "esn":
                            ls.ESN = jsonObj["esn"][i];
                            break;
                        }
                    }
                    locTips.Add(ls);
                }
                System.Windows.Application.Current.Dispatcher.Invoke(delegate
                {
                    LocalizedTips.ReplaceAll(locTips);
                });

                //DYNAMIC HELP
                endpoint = $"https://raw.githubusercontent.com/ME3Tweaks/ME3TweaksModManager/{branch}/MassEffectModManagerCore/staticfiles/dynamichelp/latesthelp-localized.xml";
                var       dynamicHelpXml = wc.DownloadString(endpoint);
                XDocument doc            = XDocument.Parse(dynamicHelpXml);
                var       intxml         = doc.XPathSelectElement("/localizations/helpmenu[@lang='int']");
                dynamicHelpLocalizations["int"] = intxml.ToString();

                Debug.WriteLine(doc.ToString());
                foreach (var lang in langs)
                {
                    var langxml = doc.XPathSelectElement($"/localizations/helpmenu[@lang='{lang}']");
                    if (langxml != null)
                    {
                        dynamicHelpLocalizations[lang] = langxml.ToString();
                    }
                }

                /*
                 * var langs = LocalizedString.Languages.Where(x => x != "int");
                 * var locTips = new List<LocalizedString>();
                 * for (int i = 0; i < jsonObj["int"].Count; i++)
                 * {
                 *  LocalizedString ls = new LocalizedString()
                 *  {
                 *      INT = jsonObj["int"][i]
                 *  };
                 *  foreach (var lang in langs)
                 *  {
                 *      if (jsonObj[lang].Count <= i) continue; //skip
                 *      switch (lang)
                 *      {
                 *          case "rus":
                 *              ls.RUS = jsonObj["rus"][i];
                 *              break;
                 *          case "deu":
                 *              ls.DEU = jsonObj["deu"][i];
                 *              break;
                 *          case "pol":
                 *              ls.POL = jsonObj["pol"][i];
                 *              break;
                 *          case "fra":
                 *              ls.FRA = jsonObj["fra"][i];
                 *              break;
                 *          case "esn":
                 *              ls.ESN = jsonObj["esn"][i];
                 *              break;
                 *      }
                 *  }
                 *  locTips.Add(ls);
                 * }
                 */
                System.Windows.Application.Current.Dispatcher.Invoke(delegate
                {
                    intViewer.Text = intxml.ToString();
                    LocalizedTips.ReplaceAll(locTips);
                });
            };
            bw.RunWorkerCompleted += (a, b) =>
            {
                if (b.Error == null && b.Result is List <LocalizationCategory> categories)
                {
                    LocalizationCategories.ReplaceAll(categories);
                }
            };
            bw.RunWorkerAsync();
        }
Пример #2
0
        private void LoadLocalizations(string branch = null)
        {
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += (x, y) =>
            {
                if (!LocalizationBranches.Any())
                {
                    PleaseWaitString = "Fetching remote localization branches";
                    var ghclient = new GitHubClient(new ProductHeaderValue(@"ME3TweaksModManager"));
                    try
                    {
                        var branches    = ghclient.Repository.Branch.GetAll("ME3Tweaks", "ME3TweaksModManager").Result;
                        var locbranches = branches.Where(x => /*x.Name.Contains("master") ||*/ x.Name.Contains("-localization"));
                        System.Windows.Application.Current.Dispatcher.Invoke(delegate { LocalizationBranches.ReplaceAll(locbranches.Select(x => x.Name).OrderByDescending(x => x)); });
                    }
                    catch (Exception e)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(delegate { MessageBox.Show("Error getting list of localization branches: " + e.Message); });
                        return;
                    }
                }

                string oldBuildBranch = null;
                if (LocalizationBranches.Any())
                {
                    if (branch == null)
                    {
                        branch         = LocalizationBranches.First();
                        SelectedBranch = branch;
                        oldBranch      = branch;
                        if (LocalizationBranches.Count() > 1)
                        {
                            oldBuildBranch = LocalizationBranches[1];
                        }
                    }
                }
                else
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(delegate { MessageBox.Show("Could not find any branches on ME3TweaksModManager repo containing name 'localization'"); });
                    return;
                }

                var       dictionaries = new Dictionary <string, string>();
                string    endpoint     = $"https://raw.githubusercontent.com/ME3Tweaks/ME3TweaksModManager/{branch}/MassEffectModManagerCore/modmanager/localizations/"; //make dynamic, maybe with octokit.
                WebClient client       = new WebClient();
                foreach (var lang in LocalizedString.Languages)
                {
                    PleaseWaitString = $"Fetching {branch} {lang}";

                    var url  = endpoint + lang + ".xaml";
                    var dict = client.DownloadStringAwareOfEncoding(url);
                    dictionaries[lang] = dict;
                }

                if (oldBuildBranch != null)
                {
                    PleaseWaitString = $"Fetching {oldBuildBranch} int";

                    endpoint = $"https://raw.githubusercontent.com/ME3Tweaks/ME3TweaksModManager/{oldBuildBranch}/MassEffectModManagerCore/modmanager/localizations/"; //make dynamic, maybe with octokit.
                    var url  = endpoint + "int.xaml";
                    var dict = client.DownloadStringAwareOfEncoding(url);
                    dictionaries["int-prev"] = dict;
                }

                PleaseWaitString = $"Parsing main strings";

                Dictionary <string, string> oldStuff = new Dictionary <string, string>();
                if (dictionaries.TryGetValue("int-prev", out var oldStrXml))
                {
                    XDocument  oldBuildDoc = XDocument.Parse(oldStrXml);
                    XNamespace system      = "clr-namespace:System;assembly=System.Runtime";
                    XNamespace xk          = "http://schemas.microsoft.com/winfx/2006/xaml";
                    var        lstrings    = oldBuildDoc.Root.Descendants(system + "String").ToList();
                    foreach (var lstring in lstrings)
                    {
                        oldStuff[lstring.Attribute(xk + "Key").Value] = lstring.Value;
                    }
                }


                //Parse INT.
                int currentLine          = 3; //Skip header.
                LocalizationCategory cat = null;
                int numBlankLines        = 0;
                List <LocalizationCategory> categories = new List <LocalizationCategory>();
                var intLines = Regex.Split(dictionaries["int"], "\r\n|\r|\n");
                for (int i = 3; i < intLines.Length - 2; i++)
                {
                    var line = intLines[i].Trim();
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        numBlankLines++;
                        continue;
                    }

                    if (line.StartsWith("<!--") && line.EndsWith("-->"))
                    {
                        //Comment - parse
                        line = line.Substring(4);
                        line = line.Substring(0, line.Length - 3);
                        line = line.Trim();
                        if (numBlankLines > 0 || cat == null)
                        {
                            //New category?
                            if (cat != null)
                            {
                                categories.Add(cat);
                            }

                            cat = new LocalizationCategory()
                            {
                                CategoryName = line
                            };
                        }

                        //notes for previous item?
                        var prevItem = cat.LocalizedStringsForSection.LastOrDefault();
                        if (prevItem != null)
                        {
                            prevItem.notes = line;
                        }
                        //Debug.WriteLine(line);

                        //New Category
                        //line = line.
                        continue;
                    }

                    numBlankLines = 0;
                    var             lineInfo = extractInfo(line);
                    LocalizedString ls       = new LocalizedString()
                    {
                        key = lineInfo.key,
                        preservewhitespace = lineInfo.preserveWhitespace,
                        INT = lineInfo.text
                    };

                    if (oldStuff.TryGetValue(lineInfo.key, out var oldString))
                    {
                        var       oldValue = new XText(oldString).ToString();
                        var       newValue = new XText(lineInfo.text).ToString();
                        XDocument newV     = XDocument.Parse("<text>" + lineInfo.text + "</text>");
                        if (oldString != newV.Root.Value)
                        {
                            if (ls.key == "string_modEndorsed")
                            {
                                Debugger.Break();
                            }
                            Debug.WriteLine("Changed: " + ls.key);
                            Debug.WriteLine("  OLD: " + oldString);
                            Debug.WriteLine("  NEW: " + lineInfo.text);
                            ls.ChangedFromPrevious = true;
                        }
                    }
                    else if (oldStuff.Any())
                    {
                        Debug.WriteLine("New: " + ls.key);
                        ls.ChangedFromPrevious = true;
                    }

                    if (lineInfo.key == null)
                    {
                        Debugger.Break();
                    }
                    if (ls.INT == null)
                    {
                        Debugger.Break();
                    }
                    cat.LocalizedStringsForSection.Add(ls);
                }

                if (cat != null)
                {
                    categories.Add(cat);
                }

                parseLocalizations(categories, dictionaries);
                y.Result = categories;

                //TIPS SERVICE
                PleaseWaitString = $"Fetching Tips Service";

                string tipsEndpoint = "https://me3tweaks.com/modmanager/services/tipsservice";
                string contents;
                var    wc       = new System.Net.WebClient();
                var    tipsJson = wc.DownloadString(tipsEndpoint);
                var    jsonObj  = JsonConvert.DeserializeObject <Dictionary <string, List <string> > >(tipsJson);
                var    langs    = LocalizedString.Languages.Where(x => x != "int");
                var    locTips  = new List <LocalizedString>();
                for (int i = 0; i < jsonObj["int"].Count; i++)
                {
                    LocalizedString ls = new LocalizedString()
                    {
                        INT = jsonObj["int"][i]
                    };
                    foreach (var lang in langs)
                    {
                        if (jsonObj[lang].Count <= i)
                        {
                            continue;                           //skip
                        }
                        switch (lang)
                        {
                        case "rus":
                            ls.RUS = jsonObj["rus"][i];
                            break;

                        case "deu":
                            ls.DEU = jsonObj["deu"][i];
                            break;

                        case "pol":
                            ls.POL = jsonObj["pol"][i];
                            break;

                        case "fra":
                            ls.FRA = jsonObj["fra"][i];
                            break;

                        case "esn":
                            ls.ESN = jsonObj["esn"][i];
                            break;

                        case "bra":
                            ls.BRA = jsonObj["bra"][i];
                            break;
                        }
                    }
                    locTips.Add(ls);
                }
                System.Windows.Application.Current.Dispatcher.Invoke(delegate
                {
                    LocalizedTips.ReplaceAll(locTips);
                });

                //DYNAMIC HELP
                PleaseWaitString = $"Fetching Dynamic Help";

                endpoint = $"https://raw.githubusercontent.com/ME3Tweaks/ME3TweaksModManager/{branch}/MassEffectModManagerCore/staticfiles/dynamichelp/latesthelp-localized.xml";
                var       dynamicHelpXml = wc.DownloadString(endpoint);
                XDocument doc            = XDocument.Parse(dynamicHelpXml);
                var       intxml         = doc.XPathSelectElement("/localizations/helpmenu[@lang='int']");
                dynamicHelpLocalizations["int"] = intxml.ToString();

                //Debug.WriteLine(doc.ToString());
                foreach (var lang in langs)
                {
                    var langxml = doc.XPathSelectElement($"/localizations/helpmenu[@lang='{lang}']");
                    if (langxml != null)
                    {
                        dynamicHelpLocalizations[lang] = langxml.ToString();
                    }
                }

                // TUTORIAL SERVICE
                PleaseWaitString = $"Fetching Tutorial Service";

                string tutorialEndpoint = "https://me3tweaks.com/modmanager/services/tutorialservice";
                wc.Dispose();
                wc = new System.Net.WebClient();
                var tutorialJson = wc.DownloadString(tutorialEndpoint);
                var TSjsonObj    = JsonConvert.DeserializeObject <List <Dictionary <string, string> > >(tutorialJson);
                langs = LocalizedString.Languages.Where(x => x != "int");
                var locTutorial = new List <LocalizedString>();
                for (int i = 0; i < TSjsonObj.Count; i++)
                {
                    LocalizedString ls = new LocalizedString()
                    {
                        INT = TSjsonObj[i]["lang_int"]
                    };
                    foreach (var lang in langs)
                    {
                        switch (lang)
                        {
                        case "rus":
                            ls.RUS = TSjsonObj[i]["lang_rus"];
                            break;

                        case "deu":
                            ls.DEU = TSjsonObj[i]["lang_deu"];
                            break;

                        case "pol":
                            ls.POL = TSjsonObj[i]["lang_pol"];
                            break;

                        case "fra":
                            ls.FRA = TSjsonObj[i]["lang_fra"];
                            break;

                        case "esn":
                            ls.ESN = TSjsonObj[i]["lang_esn"];
                            break;

                        case "bra":
                            ls.BRA = TSjsonObj[i]["lang_bra"];
                            break;
                        }
                    }
                    locTutorial.Add(ls);
                }

                PleaseWaitString = "";

                System.Windows.Application.Current.Dispatcher.Invoke(delegate
                {
                    LocalizedTutorialService.ReplaceAll(locTutorial);
                    intViewer.Text = intxml.ToString();
                });
            };
            bw.RunWorkerCompleted += (a, b) =>
            {
                if (b.Error == null && b.Result is List <LocalizationCategory> categories)
                {
                    LoadingVisibility = Visibility.Collapsed;
                    LocalizationCategories.ReplaceAll(categories);
                }
            };
            bw.RunWorkerAsync();
        }