public void ButtonDeleteDictionary()
 {
     if (EditorUtility.DisplayDialog("Warning", "Are you sure you want to delete the following dictionary?\n" + m_FilePath, "Confirm (No turning back!)", "Cancel"))
     {
         if (BackupDictionary())
         {
             FileUtils.DeleteFile(m_FilePath, true);
             m_GeneratedDictionary = null;
         }
         else
         {
             DebugUtils.LogError(this, "Could not backup dictionary, aborting deletion!");
         }
     }
 }
Exemple #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);
 }
Exemple #3
0
 //MONOBEHAVIOUR
 protected override void Awake()
 {
     base.Awake();
     if (m_DictionaryData != null)
     {
         TextAsset dictionary = m_DictionaryData.GetGeneratedDictionary();
         if (dictionary != null)
         {
             m_RuntimeDictionary.LoadFromText(dictionary.text);
         }
         else
         {
             DebugUtils.LogError(this, "Dictionary datas have not been generated!");
         }
     }
     else
     {
         DebugUtils.LogError(this, "No dictionary data!");
     }
 }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
        }
        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!");
                    }
                }