diff --git a/MinecraftUSkinEditor/Classes/FileTypes/LOCFile.cs b/MinecraftUSkinEditor/Classes/FileTypes/LOCFile.cs index 6860182c..31a10e2e 100644 --- a/MinecraftUSkinEditor/Classes/FileTypes/LOCFile.cs +++ b/MinecraftUSkinEditor/Classes/FileTypes/LOCFile.cs @@ -92,6 +92,7 @@ namespace PckStudio.Classes.FileTypes private Dictionary> _lockeys = new Dictionary>(); private List _languages = new List(ValidLanguages.Length); + public Dictionary> LocKeys => _lockeys; public List Languages => _languages; @@ -103,15 +104,6 @@ namespace PckStudio.Classes.FileTypes foreach (var locKeyValue in locKeyValuePairs) AddLocKey(locKeyValue.Item1, locKeyValue.Item2); } - - public string GetLocEntry(string locKey, string language) - { - if (!LocKeys.ContainsKey(locKey)) - throw new KeyNotFoundException("Loc key not found"); - if (!Languages.Contains(language)) throw new KeyNotFoundException("Language Entry not found"); - return GetTranslation(locKey)[language]?? string.Empty; - } - private Dictionary GetTranslation(string locKey) { if (!LocKeys.ContainsKey(locKey)) @@ -119,44 +111,49 @@ namespace PckStudio.Classes.FileTypes return LocKeys[locKey]; } - public void SetLocEntry(string locKey, string language, string value) + public Dictionary GetLocEntries(string locKey) { if (!LocKeys.ContainsKey(locKey)) - LocKeys.Add(locKey, new Dictionary()); + throw new KeyNotFoundException("Loc key not found"); + return LocKeys[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 AddSingleLocEntry(string locKey, string language, string value) - { - if (string.IsNullOrEmpty(locKey) || - string.IsNullOrEmpty(language) || - string.IsNullOrEmpty(value) || - LocKeys.ContainsKey(locKey)) - return false; - SetLocEntry(locKey, language, value); - return true; - } - public bool AddLocKey(string locKey, string value) { if (LocKeys.ContainsKey(locKey)) return false; - Languages.ForEach( langauge => AddSingleLocEntry(locKey, langauge, value) ); + Languages.ForEach( langauge => SetLocEntry(locKey, langauge, value) ); return true; } - public void ChangeSingleEntry(string locKey, string language, string newValue) - => SetLocEntry(locKey, language, newValue); - public void ChangeEntry(string locKey, string newValue) - => Languages.ForEach(langauge => SetLocEntry(locKey, langauge, newValue)); - - public void RemoveLocKey(string locKey) + public bool RemoveLocKey(string locKey) { if (!LocKeys.ContainsKey(locKey)) - throw new KeyNotFoundException(nameof(locKey)); - LocKeys.Remove(locKey); + return false; + return LocKeys.Remove(locKey); } public void AddLanguage(string language) diff --git a/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileReader.cs b/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileReader.cs index ea5914e7..fa12766f 100644 --- a/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileReader.cs +++ b/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileReader.cs @@ -13,7 +13,7 @@ namespace PckStudio.Classes.IO.LOC public static LOCFile Read(Stream stream) { - return new LOCFileReader().ReadFile(stream); + return new LOCFileReader().ReadFromStream(stream); } private LOCFileReader() : base(false) @@ -21,7 +21,7 @@ namespace PckStudio.Classes.IO.LOC _file = new LOCFile(); } - private LOCFile ReadFile(Stream stream) + private LOCFile ReadFromStream(Stream stream) { int loc_type = ReadInt(stream); int language_count = ReadInt(stream); @@ -30,16 +30,16 @@ namespace PckStudio.Classes.IO.LOC for (int i = 0; i < language_count; i++) { string language = ReadString(stream); + ReadInt(stream); // unknown value _file.Languages.Add(language); - ReadInt(stream); // padding ??? } for (int i = 0; i < language_count; i++) { - ReadInt(stream); - stream.ReadByte(); + if (0 < ReadInt(stream)) + stream.ReadByte(); string language = ReadString(stream); if (!_file.Languages.Contains(language)) - throw new Exception("language not found"); + throw new KeyNotFoundException(nameof(language)); int count = ReadInt(stream); for (int j = 0; j < count; j++) { @@ -51,20 +51,20 @@ namespace PckStudio.Classes.IO.LOC return _file; } - internal List ReadKeys(Stream stream) + private List ReadKeys(Stream stream) { - stream.ReadByte(); // unknown + bool useUniqueIds = Convert.ToBoolean(stream.ReadByte()); int keyCount = ReadInt(stream); List keys = new List(keyCount); for (int i = 0; i < keyCount; i++) { - string key = ReadString(stream); + string key = useUniqueIds ? ReadInt(stream).ToString("X08") : ReadString(stream); keys.Add(key); } return keys; } - internal string ReadString(Stream stream) + private string ReadString(Stream stream) { int length = ReadShort(stream); return ReadString(stream, length, Encoding.UTF8); diff --git a/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileWriter.cs b/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileWriter.cs index e80ca4d5..0faf244b 100644 --- a/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileWriter.cs +++ b/MinecraftUSkinEditor/Classes/IO/LOC/LOCFileWriter.cs @@ -23,7 +23,7 @@ namespace PckStudio.Classes.IO.LOC private void WriteToStream(Stream stream, int type) { - if (_locfile == null) throw new ArgumentNullException("Loc File is null"); + if (_locfile == null) throw new ArgumentNullException(nameof(_locfile)); WriteInt(stream, type); WriteInt(stream, _locfile.Languages.Count); if (type == 2) WriteLocKeys(stream); @@ -34,7 +34,7 @@ namespace PckStudio.Classes.IO.LOC private void WriteLocKeys(Stream stream) { - stream.WriteByte(0); + stream.WriteByte(0); // dont use stringIds(ints) WriteInt(stream, _locfile.LocKeys.Count); foreach (var key in _locfile.LocKeys.Keys) WriteString(stream, key); @@ -42,19 +42,20 @@ namespace PckStudio.Classes.IO.LOC private void WriteLanguages(Stream stream) { - foreach (var language in _locfile.Languages) + _locfile.Languages.ForEach(language => { WriteString(stream, language); WriteInt(stream, 0); - } + }); } private void WriteLanguageEntries(Stream stream, int type) { - foreach (var language in _locfile.Languages) + _locfile.Languages.ForEach(language => { - WriteInt(stream, 0x1337); - stream.WriteByte(0); + 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) @@ -62,12 +63,12 @@ namespace PckStudio.Classes.IO.LOC if (type == 0) WriteString(stream, locKey); WriteString(stream, _locfile.LocKeys[locKey][language]); } - } + }); } - internal void WriteString(Stream stream, string s) + private void WriteString(Stream stream, string s) { - WriteShort(stream, (short)s.Length); + WriteShort(stream, Convert.ToInt16(Encoding.UTF8.GetByteCount(s))); WriteString(stream, s, Encoding.UTF8); } } diff --git a/MinecraftUSkinEditor/Forms/Editor/LOCEditor.cs b/MinecraftUSkinEditor/Forms/Editor/LOCEditor.cs index cd2b2800..842ac8a8 100644 --- a/MinecraftUSkinEditor/Forms/Editor/LOCEditor.cs +++ b/MinecraftUSkinEditor/Forms/Editor/LOCEditor.cs @@ -55,7 +55,7 @@ namespace PckStudio.Forms.Editor } tbl.Rows.Clear(); buttonReplaceAll.Enabled = true; - foreach (var l in currentLoc.LocKeys[node.Text]) + foreach (var l in currentLoc.GetLocEntries(node.Text)) tbl.Rows.Add(l.Key, l.Value); } @@ -67,7 +67,7 @@ namespace PckStudio.Forms.Editor { if (diag.ShowDialog() == DialogResult.OK) { - currentLoc.ChangeEntry(node.Text, diag.NewText); + currentLoc.SetLocEntry(node.Text, diag.NewText); wasModified = true; } } @@ -107,7 +107,7 @@ namespace PckStudio.Forms.Editor MessageBox.Show("something went wrong"); return; } - currentLoc.ChangeSingleEntry(treeViewLocKeys.SelectedNode.Text, tbl.Rows[e.RowIndex][0].ToString(), tbl.Rows[e.RowIndex][1].ToString()); + currentLoc.SetLocEntry(treeViewLocKeys.SelectedNode.Text, tbl.Rows[e.RowIndex][0].ToString(), tbl.Rows[e.RowIndex][1].ToString()); wasModified = true; } @@ -123,7 +123,8 @@ namespace PckStudio.Forms.Editor { tbl.Rows[i][1] = textBoxReplaceAll.Text; } - currentLoc.ChangeEntry(treeViewLocKeys.SelectedNode.Text, textBoxReplaceAll.Text); + + currentLoc.SetLocEntry(treeViewLocKeys.SelectedNode.Text, textBoxReplaceAll.Text); wasModified = true; }