Пример #1
0
 //PUBLIC
 public static bool TryGetLocalizedEntriesFromJSONObject(JSONObject dictionary, out LocalizedEntries entries, bool safeMode = true)
 {
     entries = new LocalizedEntries();
     if (dictionary.Count > 0)
     {
         foreach (string stringID in dictionary.keys)
         {
             if (!entries.ContainsKey(stringID))
             {
                 Dictionary <string, string> valuesByLanguage = new Dictionary <string, string>();
                 foreach (string languageID in dictionary.GetField(stringID).keys)
                 {
                     if (!valuesByLanguage.ContainsKey(languageID))
                     {
                         valuesByLanguage[languageID] = dictionary.GetField(stringID).GetField(languageID).str;
                     }
                     else if (safeMode)
                     {
                         DebugUtils.LogError(null, "Found a duplicated language ID: " + DebugUtils.ToQuote(languageID) + " for string ID: " + DebugUtils.ToQuote(stringID) + "!");
                         return(false);
                     }
                 }
                 entries[stringID] = valuesByLanguage;
             }
             else if (safeMode)
             {
                 DebugUtils.LogError(null, "Found a duplicated string ID: " + DebugUtils.ToQuote(stringID) + "!");
                 return(false);
             }
         }
     }
     return(true);
 }
Пример #2
0
 public static bool WriteToFile(string path, string content, bool needLogs = false, bool createFile = true)
 {
     if (File.Exists(path) || createFile)
     {
         System.IO.File.WriteAllText(path, content);
         if (needLogs)
         {
             DebugUtils.LogSuccess(null, "FileUtils.WriteToFile: successfully wrote file " + DebugUtils.ToQuote(path) + ".");
         }
         return(true);
     }
     else if (createFile)
     {
         DebugUtils.LogError(null, "FileUtils.WriteToFile: File " + DebugUtils.ToQuote(path) + " does not exists.");
     }
     return(false);
 }
Пример #3
0
 public static bool DeleteFile(string path, bool needLogs = false)
 {
     if (File.Exists(path))
     {
         File.Delete(path);
         File.Delete(path + ".meta");
         if (needLogs)
         {
             DebugUtils.LogSuccess(null, "FileUtils.DeleteFile: successfully deleted file " + DebugUtils.ToQuote(path) + ".");
         }
         return(true);
     }
     else if (needLogs)
     {
         DebugUtils.LogWarning(null, "FileUtils.DeleteFile: File " + DebugUtils.ToQuote(path) + " does not exists.");
     }
     return(false);
 }
Пример #4
0
        public static bool DeleteFolder(string path, bool recursive = true, bool needLogs = false)
        {
            bool success = true;

            try
            {
                Directory.Delete(path, recursive);
                if (needLogs)
                {
                    DebugUtils.LogSuccess(null, "FileUtils.DeleteFolder: successfully deleted folder " + DebugUtils.ToQuote(path));
                }
            }
            catch
            {
                success = false;
                if (needLogs)
                {
                    DebugUtils.LogError(null, "FileUtils.DeleteFolder: Could not delete folder " + DebugUtils.ToQuote(path));
                }
            }
            return(success);
        }
Пример #5
0
        public static bool CreateFolder(string path, bool needLogs = false)
        {
            bool success = true;

            try
            {
                Directory.CreateDirectory(path);
                if (needLogs)
                {
                    DebugUtils.LogSuccess(null, "FileUtils.CreateFolder: successfully created folder " + DebugUtils.ToQuote(path));
                }
            }
            catch
            {
                success = false;
                if (needLogs)
                {
                    DebugUtils.LogError(null, "FileUtils.CreateFolder: Could not create folder " + DebugUtils.ToQuote(path));
                }
            }
            return(success);
        }
Пример #6
0
        public static bool CopyFile(string fromPath, string toPath, bool needLogs = false)
        {
            bool success = true;

            try
            {
                File.Copy(fromPath, toPath);
                if (needLogs)
                {
                    DebugUtils.LogSuccess(null, "FileUtils.CopyFile: Successfully copied file " + DebugUtils.ToQuote(fromPath) + " to " + DebugUtils.ToQuote(toPath));
                }
            }
            catch
            {
                success = false;
                if (needLogs)
                {
                    DebugUtils.LogError(null, "FileUtils.CopyFile: Could not copy file " + DebugUtils.ToQuote(fromPath) + " to " + DebugUtils.ToQuote(toPath));
                }
            }
            return(success);
        }
Пример #7
0
        //PUBLIC
        public static bool TryReadFileFromPath(string path, out string content, bool createFile = false)
        {
            bool success = true;

            content = "";

            try
            {
                content = System.IO.File.ReadAllText(path);
            }
            catch (Exception exception) when(
                exception is FileNotFoundException ||
                exception is DirectoryNotFoundException
                )
            {
                if (createFile)
                {
                    System.IO.File.WriteAllText(path, "");
                }
                else
                {
                    DebugUtils.LogError(null, "FileUtils.TryReadFileFromPath: File " + DebugUtils.ToQuote(path) + " does not exists!");
                    success = false;
                }
            }

            return(success);
        }
        private void GenerateDictionary(bool safeMode = true)
        {
            if (m_SupportedLanguages.Length == 0)
            {
                DebugUtils.LogError(this, "No supported languages, aborting generation!");
                return;
            }

            if (HasDuplicatedLanguages(out string duplicatedLanguage))
            {
                DebugUtils.LogError(this, "Found a duplicated language ID: " + DebugUtils.ToQuote(duplicatedLanguage) + " in supported languages, aborting generation!");
                return;
            }

            if (HasDuplicatedStrings(out string duplicatedString))
            {
                DebugUtils.LogError(this, "Found a duplicated string ID: " + DebugUtils.ToQuote(duplicatedString) + " in registered strings, aborting generation!");
                return;
            }

            if (JSONUtils.TryLoadFromPath(m_FilePath, out JSONObject dictionary, true))
            {
                //Start by parsing existing content from file
                if (LocalizationUtils.TryGetLocalizedEntriesFromJSONObject(dictionary, out LocalizedEntries dictionaryEntries, safeMode))
                {
                    //Adding new languages for existing strings
                    List <string> stringIDsToRemove = new List <string>();
                    foreach (string stringID in dictionaryEntries.Keys)
                    {
                        //Remove unsupported strings if needed
                        if (!IsStringSupported(stringID))
                        {
                            if (safeMode)
                            {
                                DebugUtils.LogWarning(this, "Found a non supported string ID: " + DebugUtils.ToQuote(stringID) + ". Please consider removing this entry.");
                            }
                            else
                            {
                                stringIDsToRemove.Add(stringID);
                                break;
                            }
                        }

                        List <string> missingLanguages = new List <string>();
                        foreach (LocalizedLanguage language in m_SupportedLanguages)
                        {
                            missingLanguages.Add(language.GetID());
                        }

                        List <string> languagesToRemove = new List <string>();
                        foreach (string languageID in dictionaryEntries[stringID].Keys)
                        {
                            if (IsLanguageSupported(languageID))
                            {
                                missingLanguages.Remove(languageID);
                            }
                            else
                            {
                                if (safeMode)
                                {
                                    DebugUtils.LogWarning(this, "Found a non supported language ID: " + DebugUtils.ToQuote(languageID) + " for string " + DebugUtils.ToQuote(stringID) + ". Please consider removing this entry.");
                                }
                                else
                                {
                                    languagesToRemove.Add(languageID);
                                }
                            }
                        }

                        //Remove unsupported languages if needed
                        foreach (string languageID in languagesToRemove)
                        {
                            dictionaryEntries[stringID].Remove(languageID);
                            DebugUtils.Log(this, "Removed a non supported language entry: " + DebugUtils.ToQuote(languageID) + " for string " + DebugUtils.ToQuote(stringID) + ".");
                        }

                        //Adding missing language entries
                        foreach (string languageID in missingLanguages)
                        {
                            if (!dictionaryEntries[stringID].ContainsKey(languageID))
                            {
                                dictionaryEntries[stringID][languageID] = "";
                            }
                        }
                    }

                    //Remove unsupported string Ids if needed
                    foreach (string stringID in stringIDsToRemove)
                    {
                        dictionaryEntries.Remove(stringID);
                        DebugUtils.Log(this, "Removed a non supported entry: " + DebugUtils.ToQuote(stringID) + ".");
                    }

                    //Adding missing entries
                    foreach (LocalizedStringData stringData in m_Strings)
                    {
                        if (!dictionaryEntries.ContainsKey(stringData.GetID()))
                        {
                            if (stringData.GetID() != "")
                            {
                                Dictionary <string, string> valuesByLanguage = new Dictionary <string, string>();
                                foreach (LocalizedLanguage language in m_SupportedLanguages)
                                {
                                    valuesByLanguage[language.GetID()] = "";
                                }
                                dictionaryEntries[stringData.GetID()] = valuesByLanguage;
                            }
                            else
                            {
                                DebugUtils.LogError(this, "Localized string \"" + stringData.name + "\" has no defined ID, aborting generation!");
                                return;
                            }
                        }
                    }

                    //Write dictionary back to file
                    if (BackupDictionary())
                    {
                        JSONUtils.WriteToPath(m_FilePath, LocalizationUtils.GetJSONObjectFromLocalizedEntries(dictionaryEntries), true);
                        ObjectUtils.TryCast(AssetDatabase.LoadAssetAtPath(m_FilePath, typeof(TextAsset)), out m_GeneratedDictionary);
                    }
                    else
                    {
                        DebugUtils.LogError(this, "Could not backup dictionary, aborting generation!");
                    }
                }