示例#1
0
        private void PatchTextFile(PatchData data, string sourcePath, string destPath, string patchFilePath)
        {
            LocalisationTable locTable = new LocalisationTable();


            var files = data.FindFiles(patchFilePath);


            if (files.Count() == 0)
            {
                File.Copy(sourcePath, destPath, true);
                return;
            }

            locTable.Load(sourcePath);

            foreach (string filePath in files)
            {
                Log("PatchFile: " + filePath);
                LocalisationTable otherTable = new LocalisationTable();
                otherTable.Load(filePath);
                locTable.Merge(otherTable);
            }

            Log("Save text file: " + destPath);
            locTable.Save(destPath);
        }
示例#2
0
 public void Merge(LocalisationTable table)
 {
     foreach (var kv in table.lookup)
     {
         AddRow(table.columns, kv.Value);
     }
 }
示例#3
0
        private void OnEnable()
        {
            locales            = serializedObject.FindProperty("locales");
            localisationKeys   = serializedObject.FindProperty("keys");
            localisationValues = serializedObject.FindProperty("values");
            crcEncodingVersion = serializedObject.FindProperty("crcEncodingVersion");
            targetTable        = (LocalisationTable)target;

            initialised = false;
        }
示例#4
0
        public static void ExportEmptyValues(LocalisationTable locTable, LocalisationKeySchema currentSchema)
        {
            if (locTable == null || currentSchema == null)
            {
                return;
            }

            var path = GuiUtils.SaveCsvFileDialog($"{locTable.name}-0.csv");

            if (!string.IsNullOrEmpty(path))
            {
                Exporter.ExportTableToCsv(path, locTable, currentSchema, true);
            }
        }
示例#5
0
        /*
         * FORMAT:
         * Category,Key,Content
         * Category,Key,"Complex, Content"
         */
        private void LoadFromFile(
            LocalisationTable locTable,
            LocalisationKeySchema currentSchema,
            bool emptyValuesOnly = false,
            bool saveAssets      = true)
        {
            if (locTable == null)
            {
                throw new ArgumentNullException(nameof(locTable));
            }
            if (currentSchema == null)
            {
                throw new ArgumentNullException(nameof(currentSchema));
            }

            var path = GuiUtils.OpenCsvFileDialog();

            if (!string.IsNullOrEmpty(path))
            {
                Importer.ImportFromCsv(path, targetTable, currentSchema, emptyValuesOnly, saveAssets);
            }
        }
示例#6
0
        public static int FindEmptyValues(LocalisationTable locTable, LocalisationKeySchema currentSchema,
                                          bool fixMissing = false)
        {
            var emptyValues = 0;

            foreach (var category in currentSchema.categories)
            {
                if (category == null || category.keys == null)
                {
                    continue;
                }

                foreach (var key in category.keys)
                {
                    var locKeyCRC = CrcUtils.GetCrc(category.name, key);
                    var locValue  = string.Empty;

                    try
                    {
                        locValue = locTable.GetString(locKeyCRC);
                    }
                    catch
                    {
                        if (fixMissing)
                        {
                            locTable.SetData(locKeyCRC, string.Empty);
                        }
                    }

                    if (string.IsNullOrEmpty(locValue))
                    {
                        emptyValues++;
                    }
                }
            }

            return(emptyValues);
        }
示例#7
0
        /// <summary>
        /// Import from csv file, in the format (Category,Key,Content)
        /// </summary>
        /// <param name="path">The path to the csv file to import from</param>
        /// <param name="locTable">The localisation table to import to</param>
        /// <param name="currentSchema">The current schema to import against</param>
        /// <param name="emptyValuesOnly"> If set to true, the import will only import values that are currently empty,
        /// and will not override existing values, defaults to false.
        /// </param>
        /// <param name="saveAssets">If set to true the LocalisationTable object will be saved to disk.
        /// Otherwise it will import and the changes will be in memory (and therefore require saving). defaults to true
        /// </param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void ImportFromCsv(
            string path,
            LocalisationTable locTable,
            LocalisationKeySchema currentSchema,
            bool emptyValuesOnly = false,
            bool saveAssets      = true)
        {
            if (locTable == null)
            {
                throw new ArgumentNullException(nameof(locTable));
            }
            if (currentSchema == null)
            {
                throw new ArgumentNullException(nameof(currentSchema));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            try
            {
                const char separator = ',';

                var newData = new Dictionary <int, string>();

                Func <string, string> cleanInput = input => input.Replace("\t", string.Empty);

                var lines = CsvParser.Parse(File.ReadAllText(path));
                foreach (var line in lines)
                {
                    if (line.Count < 3)
                    {
                        continue;
                    }

                    var category = cleanInput(line[0]);
                    var key      = cleanInput(line[1]);
                    var value    = cleanInput(line[2]);

                    var lookup = currentSchema.FindKey(category, key);
                    if (!lookup.IsValid)
                    {
                        continue;
                    }

                    var keyCRC = CrcUtils.GetCrc(category, key);
                    if (newData.ContainsKey(keyCRC))
                    {
                        continue;
                    }

                    newData.Add(keyCRC, value);
                }

                locTable.SetData(newData, emptyValuesOnly);
                EditorUtility.SetDirty(locTable);
                AssetDatabase.SaveAssets();

                Debug.Log($"Data populated from file: {path} {(emptyValuesOnly ? "(empty only)" : string.Empty)}");
            }
            catch (Exception e)
            {
                Debug.LogError($"Could not load from CSV format: {e.Message}");
            }
        }
示例#8
0
        /// <summary>
        /// Exports table contents to a csv file in the format (Category,Key,Content)
        /// </summary>
        /// <param name="path">Path where the csv file will be written</param>
        /// <param name="locTable">The localisation table to export</param>
        /// <param name="currentSchema">The current schema</param>
        /// <param name="emptyValuesOnly">If set to true only the missing values will be exported</param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void ExportTableToCsv(
            string path,
            LocalisationTable locTable,
            LocalisationKeySchema currentSchema,
            bool emptyValuesOnly = false)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (locTable == null)
            {
                throw new ArgumentNullException(nameof(locTable));
            }
            if (currentSchema == null)
            {
                throw new ArgumentNullException(nameof(currentSchema));
            }

            Func <string, string, string, string> cleanOutput = (csvKey1, csvKey2, csvValue) =>
                                                                $"{csvKey1},{csvKey2},\"{csvValue}\"";

            if (locTable.CRCEncodingVersion != CrcUtils.KEY_CRC_ENCODING_VERSION)
            {
                Debug.LogError(
                    $"Table encoding version ({locTable.CRCEncodingVersion}) does not match current version (1) - please update the table before trying to export anything.");
                return;
            }

            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                var streamWriter = new StreamWriter(File.OpenWrite(path));

                foreach (var category in currentSchema.categories)
                {
                    foreach (var key in category.keys)
                    {
                        var locKeyCRC = CrcUtils.GetCrc(category.name, key);
                        var locValue  = string.Empty;

                        try
                        {
                            locValue = locTable.GetString(locKeyCRC);
                        }
                        catch
                        {
                            // ignored
                        }

                        if (string.IsNullOrEmpty(locValue) || !emptyValuesOnly)
                        {
                            streamWriter.WriteLine(cleanOutput(category.name, key, locValue));
                        }
                    }
                }

                streamWriter.Close();
                Debug.Log(
                    $"Contents of table '{locTable.name}' written to: {path} {(emptyValuesOnly ? "(empty only)" : string.Empty)}");
            }
            catch (Exception e)
            {
                Debug.LogError($"Could not save to CSV: {e.Message}");
            }
        }
示例#9
0
 public TableDrawer(LocalisationTable table)
 {
     tablePath = AssetDatabase.GetAssetPath(table);
     tableName = Path.GetFileName(tablePath);
 }