public KeyTranslationData(string key, TranslationDataBase data)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data");
     }
     base.Path  = data.Path;
     this.Key   = key;
     base.Value = data.Value;
 }
Exemplo n.º 2
0
 private static void RemoveAllTranslation(OrderedDictionary od, string fromfile)
 {
     for (int i = od.Count - 1; i >= 0; i--)
     {
         TranslationDataBase item = od[i] as TranslationDataBase;
         if (item != null && item.Path == fromfile)
         {
             od.RemoveAt(i);
         }
     }
 }
Exemplo n.º 3
0
        private static void MakePredictions(string text, OrderedDictionary entries, Dictionary <string, KeyTranslationData> predictions)
        {
            string key = null;
            TranslationDataBase value = null;

            foreach (DictionaryEntry entry in entries)
            {
                key = entry.Key as string;
                if (key != null)
                {
                    value = entry.Value as TranslationDataBase;
                    if (value == null)
                    {
                        return;
                    }
                    else if (key.Length <= text.Length || !key.StartsWith(text))
                    {
                        if (key != text)
                        {
                            continue;
                        }
                        predictions.Clear();
                        predictions.Add(key, new KeyTranslationData(key, value));
                        return;
                    }
                    else
                    {
                        if (predictions.ContainsKey(key))
                        {
                            continue;
                        }
                        predictions.Add(key, new KeyTranslationData(key, value));
                    }
                }
                else
                {
                    return;
                }
            }
        }
Exemplo n.º 4
0
        public static bool Translate(ref string text)
        {
            OrderedDictionary           orderedDictionaries;
            List <RegexTranslationData> regexTranslationDatas;
            bool flag;
            OrderedDictionary orderedDictionaries1;
            object            translationLock = TextTranslator.TranslationLock;

            Monitor.Enter(translationLock);
            try
            {
                try
                {
                    string value = null;
                    if (TextTranslator.translationsLv.TryGetValue(Application.loadedLevel, out orderedDictionaries))
                    {
                        TranslationDataBase item = orderedDictionaries[text] as TranslationDataBase;
                        if (item == null)
                        {
                            goto Label2;
                        }
                        value = item.Value;
                        goto Label1;
                    }
Label2:
                    if (IniSettings.UseRegEx)
                    {
                        TextTranslator.UpdateRegexCacheSize();
                        if (TextTranslator.translationsLvR.TryGetValue(Application.loadedLevel, out regexTranslationDatas))
                        {
                            foreach (RegexTranslationData regexTranslationDatum in regexTranslationDatas)
                            {
                                Match match = Regex.Match(text, regexTranslationDatum.Key);
                                if (!match.Success || !(match.Groups[0].Value == text))
                                {
                                    continue;
                                }
                                value = match.Result(regexTranslationDatum.Value);
                                goto Label1;
                            }
                        }
                    }
                    TranslationDataBase translationDataBase = TextTranslator.translations[text] as TranslationDataBase;
                    if (translationDataBase != null)
                    {
                        value = translationDataBase.Value;
                    }
                    else if (IniSettings.UseRegEx)
                    {
                        TextTranslator.UpdateRegexCacheSize();
                        foreach (RegexTranslationData regexTranslationDatum1 in TextTranslator.translationsR)
                        {
                            Match match1 = Regex.Match(text, regexTranslationDatum1.Key);
                            if (!match1.Success || !(match1.Groups[0].Value == text))
                            {
                                continue;
                            }
                            value = match1.Result(regexTranslationDatum1.Value);
                            goto Label1;
                        }
                    }
Label1:
                    if (value != null)
                    {
                        if (!string.IsNullOrEmpty(value))
                        {
                            text = value;
                            flag = true;
                            return(flag);
                        }
                    }
                    else if (IniSettings.FindText)
                    {
                        if (!IniSettings.DumpTextByLevel)
                        {
                            string mainFilePath = TextTranslator.MainFilePath;
                            TextTranslator.translations.Add(text, new TranslationDataBase(mainFilePath));
                            TextTranslator.DumpText(mainFilePath, text);
                        }
                        else
                        {
                            if (!TextTranslator.translationsLv.TryGetValue(Application.loadedLevel, out orderedDictionaries1))
                            {
                                orderedDictionaries1 = new OrderedDictionary();
                                TextTranslator.translationsLv.Add(Application.loadedLevel, orderedDictionaries1);
                            }
                            string lvFilePath = TextTranslator.LvFilePath;
                            orderedDictionaries1.Add(text, new TranslationDataBase(lvFilePath));
                            TextTranslator.DumpText(lvFilePath, text);
                        }
                    }
                }
                catch (Exception exception)
                {
                    IniSettings.Error(string.Concat("TextTranslator::Translate:\n", exception.ToString()));
                }
                flag = false;
            }
            finally
            {
                Monitor.Exit(translationLock);
            }
            return(flag);
        }
Exemplo n.º 5
0
        private static void LoadTranslations(string file, bool retranslate = false)
        {
            string str             = file;
            object translationLock = TextTranslator.TranslationLock;

            Monitor.Enter(translationLock);
            try
            {
                try
                {
                    if (Path.GetExtension(str).ToLower() == ".txt")
                    {
                        if (!Path.GetFileName(str).StartsWith("."))
                        {
                            if (str.StartsWith(Environment.CurrentDirectory))
                            {
                                str = str.Remove(0, Environment.CurrentDirectory.Length);
                                if (!str.StartsWith("\\"))
                                {
                                    str = string.Concat("\\", str);
                                }
                                str = string.Concat(".", str);
                            }
                            int  fileLevel = TextTranslator.GetFileLevel(str);
                            bool flag      = fileLevel > -1;
                            OrderedDictionary           orderedDictionaries   = null;
                            List <RegexTranslationData> regexTranslationDatas = null;
                            if (!flag)
                            {
                                TextTranslator.RemoveAllTranslation(TextTranslator.translations, str);
                                TextTranslator.translationsR.RemoveAll((RegexTranslationData x) => x.Path == str);
                            }
                            else
                            {
                                TextTranslator.translationsLv.TryGetValue(fileLevel, out orderedDictionaries);
                                TextTranslator.translationsLvR.TryGetValue(fileLevel, out regexTranslationDatas);
                                if (orderedDictionaries != null)
                                {
                                    TextTranslator.RemoveAllTranslation(orderedDictionaries, str);
                                }
                                if (regexTranslationDatas != null)
                                {
                                    regexTranslationDatas.RemoveAll((RegexTranslationData x) => x.Path == str);
                                }
                            }
                            using (StreamReader streamReader = new StreamReader(str, Encoding.UTF8))
                            {
                                RegexOptions regexOption = RegexOptions.None;
                                regexOption = regexOption | RegexOptions.Singleline;
                                regexOption = regexOption | RegexOptions.IgnoreCase;
                                while (!streamReader.EndOfStream)
                                {
                                    string str1 = streamReader.ReadLine();
                                    if (str1.Length == 0 || str1.StartsWith("//"))
                                    {
                                        continue;
                                    }
                                    string[] strArrays = str1.Split(new char[] { '=' }, StringSplitOptions.None);
                                    if ((int)strArrays.Length != 2)
                                    {
                                        if (!IniSettings.FindText)
                                        {
                                            continue;
                                        }
                                        try
                                        {
                                            if (!Directory.Exists(IniSettings.LogFileDir))
                                            {
                                                Directory.CreateDirectory(IniSettings.LogFileDir);
                                            }
                                            if ((int)strArrays.Length <= 2)
                                            {
                                                IniSettings.Log(string.Concat(new string[] { "Error_On__File: ", str, Environment.NewLine, "Unknown__Value: ", str1, Environment.NewLine, Environment.NewLine, Environment.NewLine }));
                                            }
                                            else
                                            {
                                                string[] newLine = new string[] { "Error_On__File: ", str, Environment.NewLine, "SeparatorError: ", str1, Environment.NewLine, "  -> Replace the \"=\" after the first \"=\" by \"", null, null, null, null };
                                                char     chr     = '=';
                                                newLine[7]  = TextTranslator.HexEncode(chr.ToString(), true);
                                                newLine[8]  = "\"";
                                                newLine[9]  = Environment.NewLine;
                                                newLine[10] = Environment.NewLine;
                                                IniSettings.Log(string.Concat(newLine));
                                            }
                                        }
                                        catch
                                        {
                                        }
                                    }
                                    else
                                    {
                                        TranslationDataBase item = null;
                                        bool   flag1             = false;
                                        string value             = TextTranslator.Decode(strArrays[0].Trim());
                                        if (string.IsNullOrEmpty(value))
                                        {
                                            continue;
                                        }
                                        string str2 = TextTranslator.Decode(strArrays[1].Trim());
                                        if (value[0].ToString().ToUpper() == "R")
                                        {
                                            Match match = Regex.Match(value, "^R\\s*\\((.+)\\)$", regexOption);
                                            if (match.Success)
                                            {
                                                if (!IniSettings.UseRegEx)
                                                {
                                                    continue;
                                                }
                                                flag1 = true;
                                                value = match.Groups[1].Value;
                                            }
                                        }
                                        if (flag1)
                                        {
                                            if (!flag)
                                            {
                                                item = TextTranslator.translationsR.Find((RegexTranslationData t) => t.Key == value);
                                            }
                                            else if (regexTranslationDatas != null)
                                            {
                                                item = regexTranslationDatas.Find((RegexTranslationData t) => t.Key == value);
                                            }
                                        }
                                        else if (!flag)
                                        {
                                            item = TextTranslator.translations[value] as TranslationDataBase;
                                        }
                                        else if (orderedDictionaries != null)
                                        {
                                            item = orderedDictionaries[value] as TranslationDataBase;
                                        }
                                        if (item != null)
                                        {
                                            if (flag1)
                                            {
                                                if (!flag)
                                                {
                                                    TextTranslator.translationsR.Remove((RegexTranslationData)item);
                                                }
                                                else
                                                {
                                                    regexTranslationDatas.Remove((RegexTranslationData)item);
                                                }
                                            }
                                            else if (!flag)
                                            {
                                                TextTranslator.translations.Remove(value);
                                            }
                                            else
                                            {
                                                orderedDictionaries.Remove(value);
                                            }
                                            if (IniSettings.FindText)
                                            {
                                                try
                                                {
                                                    if (!Directory.Exists(IniSettings.LogFileDir))
                                                    {
                                                        Directory.CreateDirectory(IniSettings.LogFileDir);
                                                    }
                                                    string[] value1 = new string[] { "Replacing__Key: ", value, "=", item.Value, Environment.NewLine, "FromLoadedFile: ", item.Path, Environment.NewLine, "By__Next___Key: ", value, "=", str2, Environment.NewLine, "From_Next_File: ", str, Environment.NewLine, "These__Keys_Do: ", null, null, null };
                                                    value1[17] = (item.Value != str2 ? "Not Match" : "Match");
                                                    value1[18] = Environment.NewLine;
                                                    value1[19] = Environment.NewLine;
                                                    IniSettings.Log(string.Concat(value1));
                                                }
                                                catch
                                                {
                                                }
                                            }
                                        }
                                        if (!flag1)
                                        {
                                            item = new TranslationDataBase(str, str2);
                                            if (!flag)
                                            {
                                                TextTranslator.translations.Add(value, item);
                                            }
                                            else
                                            {
                                                if (orderedDictionaries == null)
                                                {
                                                    orderedDictionaries = new OrderedDictionary();
                                                    TextTranslator.translationsLv.Add(fileLevel, orderedDictionaries);
                                                }
                                                orderedDictionaries.Add(value, item);
                                            }
                                        }
                                        else
                                        {
                                            try
                                            {
                                                try
                                                {
                                                    item = new RegexTranslationData(str, value, str2);
                                                    TextTranslator.UpdateRegexCacheSize();
                                                    Regex.Replace("A", value, str2);
                                                }
                                                catch (Exception exception1)
                                                {
                                                    Exception exception = exception1;
                                                    item = null;
                                                    if (IniSettings.FindText)
                                                    {
                                                        try
                                                        {
                                                            IniSettings.Log(string.Concat(new string[] { "Error_On__File: ", str, Environment.NewLine, "Regex____Error: ", str1, Environment.NewLine, "  ", exception.Message, Environment.NewLine, Environment.NewLine, Environment.NewLine }));
                                                        }
                                                        catch
                                                        {
                                                        }
                                                    }
                                                }
                                            }
                                            finally
                                            {
                                                if (item != null)
                                                {
                                                    if (!flag)
                                                    {
                                                        TextTranslator.translationsR.Add((RegexTranslationData)item);
                                                    }
                                                    else
                                                    {
                                                        if (regexTranslationDatas == null)
                                                        {
                                                            regexTranslationDatas = new List <RegexTranslationData>();
                                                            TextTranslator.translationsLvR.Add(fileLevel, regexTranslationDatas);
                                                        }
                                                        regexTranslationDatas.Add((RegexTranslationData)item);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (retranslate)
                            {
                                MaskableGraphicText.Retranslate();
                            }
                            if (IniSettings.DebugMode || IniSettings.FindText)
                            {
                                IniSettings.Log(string.Concat("Loaded: ", str));
                            }
                        }
                    }
                }
                catch (Exception exception2)
                {
                    IniSettings.Error(string.Concat("LoadTranslations:\n", exception2.ToString()));
                }
            }
            finally
            {
                Monitor.Exit(translationLock);
            }
        }