Esempio n. 1
0
        public static void UpdateGuids(LocalizedStringReference reference)
        {
            SerializedObject obj;

            obj = new SerializedObject(reference);

            SerializedProperty identifiers;

            identifiers = obj.FindProperty("m_Identifiers");

            SerializedProperty guids;

            guids = obj.FindProperty("m_Guids");

            for (int index = 0; index < Mathf.Min(identifiers.arraySize, guids.arraySize); index++)
            {
                var guid = guids.GetArrayElementAtIndex(index);
                if (string.IsNullOrEmpty(guid.stringValue))
                {
                    var language = new Language(identifiers.GetArrayElementAtIndex(index).stringValue);
                    guid.stringValue = AssetDatabase.AssetPathToGUID(CreateTextAssetPath(reference, language));
                }
            }

            obj.ApplyModifiedProperties();
        }
Esempio n. 2
0
 public static string GetEditorPrefsKey(LocalizedStringReference reference)
 {
     return(string.Format(
                "SagoLocalizationEditor.LocalizedResourceReferenceEditor.ImportWindow.{0}",
                AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(reference))
                ));
 }
Esempio n. 3
0
        public static string GetTextAssetPath(LocalizedStringReference reference, Language language)
        {
            string guid = reference.GetInfo(language).Guid;
            string path = AssetDatabase.GUIDToAssetPath(guid);

            return(!string.IsNullOrEmpty(path) ? path : null);
        }
Esempio n. 4
0
        public static LocalizedStringReferenceImportWindow Open(LocalizedStringReference reference)
        {
            LocalizedStringReferenceImportWindow window;

            window             = ScriptableObject.CreateInstance <LocalizedStringReferenceImportWindow>();
            window.m_Reference = reference;
            window.ShowAuxWindow();
            return(window);
        }
Esempio n. 5
0
        public static string CreateTextAssetPath(LocalizedStringReference reference, Language language)
        {
            var path      = AssetDatabase.GetAssetPath(reference);
            var directory = Path.GetDirectoryName(path);
            var name      = Path.GetFileNameWithoutExtension(path);
            var ext       = string.Format(".{0}.json", language.Identifier);

            return(Path.Combine(directory, Path.ChangeExtension(name, ext)));
        }
Esempio n. 6
0
        public static TextAsset CreateTextAsset(LocalizedStringReference reference, Language language)
        {
            var path = CreateTextAssetPath(reference, language);

            File.WriteAllText(path, JsonUtility.ToJson(default(LocalizedStringDictionary.Json)).ReplaceSpacesWithTabs());
            AssetDatabase.Refresh();
            UpdateGuids(reference);
            return(GetTextAsset(reference, language));
        }
Esempio n. 7
0
        /// <summary>
        /// Gets the absolute size in bytes of the given record type.
        /// </summary>
        /// <param name="version">The version of the record.</param>
        /// <param name="recordType">The type to get the size of.</param>
        /// <returns>The absolute size in bytes of the record.</returns>
        public static int GetRecordSize(WarcraftVersion version, Type recordType)
        {
            var size = 0;

            foreach (var recordProperty in GetVersionRelevantProperties(version, recordType))
            {
                switch (recordProperty.PropertyType)
                {
                // Single-field types
                case Type foreignKeyType when foreignKeyType.IsGenericType && foreignKeyType.GetGenericTypeDefinition() == typeof(ForeignKey <>):
                case Type stringRefType when stringRefType == typeof(StringReference):
                case Type enumType when enumType.IsEnum:
                {
                    var underlyingType = DBCDeserializer.GetUnderlyingStoredPrimitiveType(recordProperty.PropertyType);

                    size += Marshal.SizeOf(underlyingType);
                    break;
                }

                // Multi-field types
                case Type genericListType when genericListType.IsGenericType && genericListType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList <>)):
                case Type arrayType when arrayType.IsArray:
                {
                    var elementSize        = Marshal.SizeOf(DBCDeserializer.GetUnderlyingStoredPrimitiveType(recordProperty.PropertyType));
                    var arrayInfoAttribute = GetVersionRelevantPropertyFieldArrayAttribute(version, recordProperty);

                    size += (int)(elementSize * arrayInfoAttribute.Count);

                    break;
                }

                // Special version-variant length handling
                case Type locStringRefType when locStringRefType == typeof(LocalizedStringReference):
                {
                    size += LocalizedStringReference.GetFieldCount(version) * sizeof(uint);
                    break;
                }

                case Type registeredType when CustomFieldTypeStorageSizes.ContainsKey(registeredType):
                {
                    size += CustomFieldTypeStorageSizes[registeredType];
                    break;
                }

                default:
                {
                    size += Marshal.SizeOf(recordProperty.PropertyType);
                    break;
                }
                }
            }

            return(size);
        }
Esempio n. 8
0
        public static EditorPrefsValue GetEditorPrefsValue(LocalizedStringReference reference)
        {
            EditorPrefsValue value = JsonUtility.FromJson <EditorPrefsValue>(
                EditorPrefs.GetString(
                    GetEditorPrefsKey(reference),
                    JsonUtility.ToJson(default(EditorPrefsValue))
                    )
                );

            if (value.identifiers == null)
            {
                value.identifiers = reference.Identifiers;
            }
            return(value);
        }
Esempio n. 9
0
        /*
         *      BinaryReader Extensions for DBC-related types
         */

        /// <summary>
        /// Reads a <see cref="LocalizedStringReference"/> from the data stream.
        /// </summary>
        /// <param name="binaryReader"></param>
        /// <returns></returns>
        public static LocalizedStringReference ReadLocalizedStringReference(this BinaryReader binaryReader, WarcraftVersion version)
        {
            LocalizedStringReference locRef = new LocalizedStringReference();

            if (version < WarcraftVersion.Cataclysm)
            {
                locRef.English        = binaryReader.ReadStringReference();
                locRef.Korean         = binaryReader.ReadStringReference();
                locRef.French         = binaryReader.ReadStringReference();
                locRef.German         = binaryReader.ReadStringReference();
                locRef.Chinese        = binaryReader.ReadStringReference();
                locRef.Taiwan         = binaryReader.ReadStringReference();
                locRef.Spanish        = binaryReader.ReadStringReference();
                locRef.SpanishMexican = binaryReader.ReadStringReference();

                if (version >= WarcraftVersion.BurningCrusade)
                {
                    locRef.Russian = binaryReader.ReadStringReference();
                }

                if (version >= WarcraftVersion.Wrath)
                {
                    locRef.Unknown1  = binaryReader.ReadStringReference();
                    locRef.Portugese = binaryReader.ReadStringReference();
                    locRef.Italian   = binaryReader.ReadStringReference();
                    locRef.Unknown2  = binaryReader.ReadStringReference();
                    locRef.Unknown3  = binaryReader.ReadStringReference();
                    locRef.Unknown4  = binaryReader.ReadStringReference();
                    locRef.Unknown5  = binaryReader.ReadStringReference();
                }

                locRef.Flags = binaryReader.ReadUInt32();
            }
            else
            {
                locRef.ClientLocale = binaryReader.ReadStringReference();
            }

            return(locRef);
        }
Esempio n. 10
0
        /// <summary>
        /// Gets the number of properties marked with <see cref="RecordFieldAttribute"/> (or subclasses of it) in the
        /// given type.
        /// </summary>
        /// <param name="version">The version of the record.</param>
        /// <param name="recordType">The type with properties.</param>
        /// <returns>The number of properties in the type.</returns>
        public static int GetPropertyCount(WarcraftVersion version, Type recordType)
        {
            var count      = 0;
            var properties = GetVersionRelevantProperties(version, recordType);

            foreach (var recordProperty in properties)
            {
                switch (recordProperty.PropertyType)
                {
                case { } _ when IsPropertyFieldArray(recordProperty):
                {
                    var arrayInfoAttribute = GetVersionRelevantPropertyFieldArrayAttribute(version, recordProperty);
                    count += (int)arrayInfoAttribute.Count;

                    break;
                }

                case { } locStringRefType when locStringRefType == typeof(LocalizedStringReference):
                {
                    count += LocalizedStringReference.GetFieldCount(version);
                    break;
                }

                case { } registeredType when CustomFieldTypeFieldCounts.ContainsKey(registeredType):
                {
                    count += CustomFieldTypeFieldCounts[registeredType];
                    break;
                }

                default:
                {
                    ++count;
                    break;
                }
                }
            }

            return(count);
        }
Esempio n. 11
0
 public static void SetEditorPrefsValue(LocalizedStringReference reference, EditorPrefsValue value)
 {
     EditorPrefs.SetString(GetEditorPrefsKey(reference), JsonUtility.ToJson(value));
 }
Esempio n. 12
0
        public static void ImportFromCSV(LocalizedStringReference reference, bool overwrite = false)
        {
            EditorPrefsValue prefs;

            prefs = GetEditorPrefsValue(reference);

            if (!prefs.HasValidIdentifiers)
            {
                throw new System.InvalidOperationException(string.Format("Invalid identifiers: {0}", string.Join(",", prefs.identifiers)));
            }

            if (!prefs.HasValidPath)
            {
                throw new System.InvalidOperationException(string.Format("Invalid path: {0}", prefs.path));
            }

            Dictionary <string, Dictionary <string, LocalizedString.Json> > lookup;

            lookup = new Dictionary <string, Dictionary <string, LocalizedString.Json> >();

            HashSet <string> identifiers;

            identifiers = new HashSet <string>();

            HashSet <string> keys;

            keys = new HashSet <string>();

            foreach (string identifier in reference.Identifiers)
            {
                lookup[identifier] = new Dictionary <string, LocalizedString.Json>();
                identifiers.Add(identifier);

                if (!overwrite)
                {
                    TextAsset asset;
                    asset = LocalizedStringReferenceEditor.GetTextAsset(reference, new Language(identifier));

                    foreach (var stringJson in LocalizedStringReferenceEditor.GetStringsFromTextAsset(asset))
                    {
                        lookup[identifier][stringJson.key] = stringJson;
                        keys.Add(stringJson.key);
                    }
                }
            }

            using (var reader = new CsvFileReader(prefs.path)) {
                List <string> header = new List <string>();
                reader.ReadRow(header);

                List <string> row = new List <string>();
                while (reader.ReadRow(row))
                {
                    string key;
                    key = row[0];

                    bool isPlural;
                    isPlural = System.Convert.ToBoolean(row[3]);

                    int valueIndex;
                    valueIndex = isPlural ? System.Convert.ToInt32(row[4]) : 0;

                    for (int index = 5; index < row.Count; index++)
                    {
                        string identifier;
                        identifier = header[index];

                        int valueCount;
                        valueCount = isPlural ? LocalizedString.GetPluralCount(new Language(identifier)) : 1;

                        if (valueIndex >= 0 && valueIndex < valueCount)
                        {
                            Dictionary <string, LocalizedString.Json> dictionary;
                            if (!lookup.TryGetValue(identifier, out dictionary))
                            {
                                lookup[identifier] = dictionary = new Dictionary <string, LocalizedString.Json>();
                            }

                            LocalizedString.Json localizedString;
                            if (!dictionary.TryGetValue(key, out localizedString))
                            {
                                localizedString           = new LocalizedString.Json();
                                localizedString.key       = key;
                                localizedString.comment   = row[1];
                                localizedString.plural    = isPlural;
                                localizedString.reference = row[2];
                                localizedString.values    = new string[valueCount];
                            }

                            string[] values;
                            values = new string[valueCount];

                            localizedString.values.CopyTo(values, 0);
                            values[valueIndex] = row[index];

                            localizedString.values = values;
                            dictionary[key]        = localizedString;
                        }
                    }
                }

                foreach (string identifier in prefs.identifiers)
                {
                    Dictionary <string, LocalizedString.Json> dictionary;
                    if (lookup.TryGetValue(identifier, out dictionary) && dictionary.Count != 0)
                    {
                        LocalizedString.Json[] localizedStrings;
                        localizedStrings = dictionary.Values.ToArray();

                        LocalizedStringDictionary.Json json;
                        json = new LocalizedStringDictionary.Json(localizedStrings);

                        Language language;
                        language = new Language(identifier);

                        string path;
                        path = LocalizedStringReferenceEditor.GetTextAssetPath(reference, language) ?? LocalizedStringReferenceEditor.CreateTextAssetPath(reference, language);

                        if (!string.IsNullOrEmpty(path))
                        {
                            File.WriteAllText(path, JsonUtility.ToJson(json, true).ReplaceSpacesWithTabs());
                        }
                    }
                }

                AssetDatabase.Refresh();
                LocalizedStringReferenceEditor.UpdateGuids(reference);
            }
        }
Esempio n. 13
0
        public static void ExportToCSV(LocalizedStringReference reference)
        {
            EditorPrefsValue prefs;

            prefs = GetEditorPrefsValue(reference);

            if (!prefs.HasValidIdentifiers)
            {
                throw new System.InvalidOperationException(string.Format("Invalid identifiers: {0}", string.Join(",", prefs.identifiers)));
            }

            if (!prefs.HasValidPath)
            {
                throw new System.InvalidOperationException(string.Format("Invalid path: {0}", prefs.path));
            }

            Dictionary <string, Dictionary <string, LocalizedString.Json> > lookup;

            lookup = new Dictionary <string, Dictionary <string, LocalizedString.Json> >();

            HashSet <string> identifiers;

            identifiers = new HashSet <string>();

            HashSet <string> keys;

            keys = new HashSet <string>();

            foreach (string identifier in reference.Identifiers)
            {
                lookup[identifier] = new Dictionary <string, LocalizedString.Json>();
                identifiers.Add(identifier);

                TextAsset asset;
                asset = LocalizedStringReferenceEditor.GetTextAsset(reference, new Language(identifier));

                foreach (var stringJson in LocalizedStringReferenceEditor.GetStringsFromTextAsset(asset))
                {
                    lookup[identifier][stringJson.key] = stringJson;
                    keys.Add(stringJson.key);
                }
            }

            int maxPluralCount;

            maxPluralCount = 1;

            foreach (string identifier in prefs.identifiers)
            {
                maxPluralCount = Mathf.Max(maxPluralCount, LocalizedString.GetPluralCount(new Language(identifier)));
            }

            using (var writer = new CsvFileWriter(prefs.path)) {
                //
                // header
                //
                var header = new List <string>();
                header.Add("Key");
                header.Add("Comment");
                header.Add("Reference");
                header.Add("Plural");
                header.Add("Index");
                header.AddRange(prefs.identifiers);
                writer.WriteRow(header);

                //
                // data
                //
                foreach (var key in keys)
                {
                    LocalizedString.Json src;
                    src = lookup["en"][key];

                    int valueCount;
                    valueCount = src.plural ? maxPluralCount : 1;

                    for (int valueIndex = 0; valueIndex < valueCount; valueIndex++)
                    {
                        var row = new List <string>();
                        row.Add(key);
                        row.Add(src.comment);
                        row.Add(src.reference);
                        row.Add(src.plural.ToString());
                        row.Add(valueIndex.ToString());

                        foreach (var identifier in prefs.identifiers)
                        {
                            int languagePluralCount;
                            languagePluralCount = LocalizedString.GetPluralCount(new Language(identifier));

                            try {
                                if (valueIndex < languagePluralCount)
                                {
                                    row.Add(lookup[identifier][key].values[valueIndex]);
                                }
                                else
                                {
                                    row.Add("N/A");
                                }
                            } catch {
                                row.Add(string.Empty);
                            }
                        }

                        writer.WriteRow(row);
                    }
                }
            }

            AssetDatabase.Refresh();
        }
Esempio n. 14
0
 public static TextAsset GetTextAsset(LocalizedStringReference reference, Language language)
 {
     return(AssetDatabase.LoadAssetAtPath <TextAsset>(GetTextAssetPath(reference, language)));
 }