diff --git a/PCK-Studio/Classes/FileTypes/LOCFile.cs b/PCK-Studio/Classes/FileTypes/LOCFile.cs deleted file mode 100644 index 561b0189..00000000 --- a/PCK-Studio/Classes/FileTypes/LOCFile.cs +++ /dev/null @@ -1,184 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PckStudio.Classes.FileTypes -{ - public class LOCFile - { - public class InvalidLanguageException : Exception - { - public string Language { get; } - public InvalidLanguageException(string message, string language) : base(message) - { - Language = language; - } - } - - public static readonly string[] ValidLanguages = new string[] - { - "cs-CS", - "cs-CZ", - - "da-CH", - "da-DA", - "da-DK", - - "de-AT", - "de-DE", - - "el-EL", - "el-GR", - - "en-AU", - "en-CA", - "en-EN", - "en-GB", - "en-GR", - "en-IE", - "en-NZ", - "en-US", - - "es-ES", - "es-MX", - - "fi-BE", - "fi-CH", - "fi-FI", - - "fr-FR", - "fr-CA", - - "it-IT", - - "ja-JP", - - "ko-KR", - - "la-LAS", - - "no-NO", - - "nb-NO", - - "nl-NL", - "nl-BE", - - "pl-PL", - - "pt-BR", - "pt-PT", - - "ru-RU", - - "sk-SK", - - "sv-SE", - - "tr-TR", - - "zh-CN", - "zh-HK", - "zh-SG", - "zh-TW", - "zh-CHT", - "zh-HanS", - "zh-HanT", - }; - - private Dictionary> _lockeys = new Dictionary>(); - private List _languages = new List(ValidLanguages.Length); - - public Dictionary> LocKeys => _lockeys; - public List Languages => _languages; - - public void InitializeDefault(string packName) - => Initialize("en-EN", ("IDS_DISPLAY_NAME", packName)); - public void Initialize(string language, params (string, string)[] locKeyValuePairs) - { - AddLanguage(language); - foreach (var locKeyValue in locKeyValuePairs) - AddLocKey(locKeyValue.Item1, locKeyValue.Item2); - } - - private Dictionary GetTranslation(string locKey) - { - if (!LocKeys.ContainsKey(locKey)) - LocKeys.Add(locKey, new Dictionary()); - return LocKeys[locKey]; - } - - public Dictionary GetLocEntries(string locKey) - { - if (!LocKeys.ContainsKey(locKey)) - throw new KeyNotFoundException("Loc key not found"); - return LocKeys[locKey]; - } - - public bool HasLocEntry(string locKey) - => LocKeys.ContainsKey(locKey); - - public string GetLocEntry(string locKey, string language) - { - if (!LocKeys.ContainsKey(locKey)) - throw new KeyNotFoundException(nameof(locKey)); - if (!Languages.Contains(language)) throw new KeyNotFoundException("Language Entry not found"); - return GetTranslation(locKey)[language]?? string.Empty; - } - - public void SetLocEntry(string locKey, string value) - { - foreach (var language in Languages) - { - GetTranslation(locKey)[language] = value; - } - } - - public void SetLocEntry(string locKey, string language, string value) - { - if (!Languages.Contains(language)) - throw new KeyNotFoundException(nameof(language)); - GetTranslation(locKey)[language] = value; - } - - public bool AddLocKey(string locKey, string value) - { - if (LocKeys.ContainsKey(locKey)) - return false; - Languages.ForEach( language => SetLocEntry(locKey, language, value) ); - return true; - } - - public bool RemoveLocKey(string locKey) - { - if (!LocKeys.ContainsKey(locKey)) - return false; - return LocKeys.Remove(locKey); - } - - public void AddLanguage(string language) - { - if (!ValidLanguages.Contains(language)) - throw new InvalidLanguageException("Invalid language", language); - if (Languages.Contains(language)) - throw new InvalidLanguageException("Language already exists", language); - Languages.Add(language); - foreach(var key in LocKeys.Keys) - SetLocEntry(key, language, ""); - } - - public void RemoveLanguage(string language) - { - if (!ValidLanguages.Contains(language)) - throw new InvalidLanguageException("Invalid language", language); - if (!Languages.Contains(language)) - throw new InvalidLanguageException("Language doesn't exist", language); - if (Languages.Remove(language)) - foreach (var translation in LocKeys.Values) - translation.Remove(language); - } - } -} diff --git a/PCK-Studio/Classes/FileTypes/PCKAudioFile.cs b/PCK-Studio/Classes/FileTypes/PCKAudioFile.cs index f0d8b4b4..f6b02138 100644 --- a/PCK-Studio/Classes/FileTypes/PCKAudioFile.cs +++ b/PCK-Studio/Classes/FileTypes/PCKAudioFile.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using OMI.Formats.Languages; namespace PckStudio.Classes.FileTypes { diff --git a/PCK-Studio/Classes/IO/LOC/LOCFileReader.cs b/PCK-Studio/Classes/IO/LOC/LOCFileReader.cs deleted file mode 100644 index abb0af0f..00000000 --- a/PCK-Studio/Classes/IO/LOC/LOCFileReader.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using PckStudio.Classes.FileTypes; - -namespace PckStudio.Classes.IO.LOC -{ - internal class LOCFileReader : StreamDataReader - { - internal LOCFile _file; - - public static LOCFile Read(Stream stream) - { - return new LOCFileReader().ReadFromStream(stream); - } - - private LOCFileReader() : base(false) - { - _file = new LOCFile(); - } - - protected override LOCFile ReadFromStream(Stream stream) - { - int loc_type = ReadInt(stream); - int language_count = ReadInt(stream); - bool lookUpKey = loc_type == 2; - List keys = lookUpKey ? ReadKeys(stream) : null; - for (int i = 0; i < language_count; i++) - { - string language = ReadString(stream); - ReadInt(stream); // unknown value - _file.Languages.Add(language); - } - for (int i = 0; i < language_count; i++) - { - if (0 < ReadInt(stream)) - stream.ReadByte(); - string language = ReadString(stream); - if (!_file.Languages.Contains(language)) - throw new KeyNotFoundException(nameof(language)); - int count = ReadInt(stream); - for (int j = 0; j < count; j++) - { - string key = lookUpKey ? keys[j] : ReadString(stream); - string value = ReadString(stream); - _file.SetLocEntry(key, language, value); - } - } - return _file; - } - - private List ReadKeys(Stream stream) - { - bool useUniqueIds = Convert.ToBoolean(stream.ReadByte()); - int keyCount = ReadInt(stream); - List keys = new List(keyCount); - for (int i = 0; i < keyCount; i++) - { - string key = useUniqueIds ? ReadInt(stream).ToString("X08") : ReadString(stream); - keys.Add(key); - } - return keys; - } - - private string ReadString(Stream stream) - { - int length = ReadShort(stream); - return ReadString(stream, length, Encoding.UTF8); - } - } -} \ No newline at end of file diff --git a/PCK-Studio/Classes/IO/LOC/LOCFileWriter.cs b/PCK-Studio/Classes/IO/LOC/LOCFileWriter.cs deleted file mode 100644 index d564ebac..00000000 --- a/PCK-Studio/Classes/IO/LOC/LOCFileWriter.cs +++ /dev/null @@ -1,89 +0,0 @@ -using PckStudio.Classes.FileTypes; -using System; -using System.IO; -using System.Text; - -namespace PckStudio.Classes.IO.LOC -{ - internal class LOCFileWriter : StreamDataWriter - { - private LOCFile _locfile; - private int _type; - public static void Write(Stream stream, LOCFile file, int type = 2) - { - new LOCFileWriter(file, type).WriteToStream(stream); - } - - private LOCFileWriter(LOCFile file, int type) : base(false) - { - _type = type; - _locfile = file; - } - - protected override void WriteToStream(Stream stream) - { - _ = _locfile ?? throw new ArgumentNullException(nameof(_locfile)); - WriteInt(stream, _type); - WriteInt(stream, _locfile.Languages.Count); - if (_type == 2) WriteLocKeys(stream); - WriteLanguages(stream, _type); - WriteLanguageEntries(stream, _type); - } - - - private void WriteLocKeys(Stream stream) - { - stream.WriteByte(0); // dont use stringIds(ints) - WriteInt(stream, _locfile.LocKeys.Count); - foreach (var key in _locfile.LocKeys.Keys) - WriteString(stream, key); - } - - private void WriteLanguages(Stream stream, int type) - { - foreach(var language in _locfile.Languages) - { - WriteString(stream, language); - - //Calculate the size of the language entry - - int size = 0; - size += sizeof(int); // null long - size += sizeof(byte); // null byte - size += (sizeof(short) + Encoding.UTF8.GetByteCount(language)); // language name string - size += sizeof(int); // key count - - foreach (var locKey in _locfile.LocKeys.Keys) - { - if (type == 0) size += (2 + Encoding.UTF8.GetByteCount(locKey)); // loc key string - size += (2 + Encoding.UTF8.GetByteCount(_locfile.LocKeys[locKey][language])); // loc key string - } - - WriteInt(stream, size); - }; - } - - private void WriteLanguageEntries(Stream stream, int type) - { - foreach (var language in _locfile.Languages) - { - WriteInt(stream, 0x6D696B75); // :P - stream.WriteByte(0); // <- only write when the previous written int was >0 - - WriteString(stream, language); - WriteInt(stream, _locfile.LocKeys.Keys.Count); - foreach(var locKey in _locfile.LocKeys.Keys) - { - if (type == 0) WriteString(stream, locKey); - WriteString(stream, _locfile.LocKeys[locKey][language]); - } - }; - } - - private void WriteString(Stream stream, string s) - { - WriteShort(stream, Convert.ToInt16(Encoding.UTF8.GetByteCount(s))); - WriteString(stream, s, Encoding.UTF8); - } - } -} diff --git a/PCK-Studio/Forms/Editor/AudioEditor.cs b/PCK-Studio/Forms/Editor/AudioEditor.cs index 97df4da8..b2aef3bc 100644 --- a/PCK-Studio/Forms/Editor/AudioEditor.cs +++ b/PCK-Studio/Forms/Editor/AudioEditor.cs @@ -11,6 +11,7 @@ using PckStudio.Classes; using PckStudio.Classes.FileTypes; using PckStudio.Classes.IO.PCK; using PckStudio.Forms.Additional_Popups.Audio; +using OMI.Formats.Languages; // Audio Editor by MattNL // additional work and optimization by Miku-666 diff --git a/PCK-Studio/Forms/Editor/LOCEditor.cs b/PCK-Studio/Forms/Editor/LOCEditor.cs index df5c8665..b52db4a6 100644 --- a/PCK-Studio/Forms/Editor/LOCEditor.cs +++ b/PCK-Studio/Forms/Editor/LOCEditor.cs @@ -7,8 +7,9 @@ using System.Windows.Forms; using MetroFramework.Forms; using PckStudio.Classes.Misc; using PckStudio.Classes.FileTypes; -using PckStudio.Classes.IO.LOC; using PckStudio.Forms.Additional_Popups.Loc; +using OMI.Formats.Languages; +using OMI.Workers.Language; namespace PckStudio.Forms.Editor { @@ -24,7 +25,8 @@ namespace PckStudio.Forms.Editor _file = file; using (var ms = new MemoryStream(file.Data)) { - currentLoc = LOCFileReader.Read(ms); + var reader = new LOCFileReader(); + currentLoc = reader.FromStream(ms); } tbl = new DataTable(); tbl.Columns.Add(new DataColumn("Language") { ReadOnly = true }); diff --git a/PCK-Studio/Forms/Skins-And-Textures/addnewskin.cs b/PCK-Studio/Forms/Skins-And-Textures/addnewskin.cs index 3c878671..afe5c441 100644 --- a/PCK-Studio/Forms/Skins-And-Textures/addnewskin.cs +++ b/PCK-Studio/Forms/Skins-And-Textures/addnewskin.cs @@ -7,6 +7,7 @@ using PckStudio.Classes.FileTypes; using System.Drawing.Imaging; using PckStudio.Classes.Utils; using PckStudio.Classes._3ds.Utils; +using OMI.Formats.Languages; namespace PckStudio { diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index bc972e83..c65263a4 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -10,9 +10,11 @@ using System.Drawing.Imaging; using OMI.Formats.GameRule; using OMI.Workers.GameRule; +using OMI.Formats.Languages; +using OMI.Workers.Language; + using PckStudio.Properties; using PckStudio.Classes.FileTypes; -using PckStudio.Classes.IO.LOC; using PckStudio.Classes.IO.PCK; using PckStudio.Classes.Utils; using PckStudio.Classes.Utils.ARC; @@ -1530,7 +1532,8 @@ namespace PckStudio { using (var stream = new MemoryStream(locdata.Data)) { - locFile = LOCFileReader.Read(stream); + var reader = new LOCFileReader(); + locFile = reader.FromStream(stream); } return true; } diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index 85ddb6fd..7dbf2e63 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -194,8 +194,6 @@ - - @@ -464,7 +462,6 @@ Component - Form