Example #1
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}");
            }
        }
Example #2
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}");
            }
        }