diff --git a/GZ.cs b/GZ.cs index c6fb0b6..04ce967 100644 --- a/GZ.cs +++ b/GZ.cs @@ -32,7 +32,6 @@ namespace TaikoSoundEditor public static byte[] DecompressBytes(string gzPath) { Logger.Info("GZ Decompressing bytes"); - using FileStream originalFileStream = File.OpenRead(gzPath); using GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress); using MemoryStream ms = new MemoryStream(); @@ -43,17 +42,16 @@ namespace TaikoSoundEditor public static byte[] CompressToBytes(string content) { Logger.Info("GZ Compressing bytes"); - - using var stream = new MemoryStream(); - using var writer = new StreamWriter(stream); - writer.Write(content); - using var ostream = new MemoryStream(); - using (var compressionStream = new GZipStream(ostream, CompressionMode.Compress)) + var uncompressed = Encoding.UTF8.GetBytes(content); + using (MemoryStream outStream = new MemoryStream()) { - stream.CopyTo(compressionStream); + using (GZipOutputStream gzoStream = new GZipOutputStream(outStream)) + { + gzoStream.SetLevel(5); + gzoStream.Write(uncompressed, 0, uncompressed.Length); + } + return outStream.ToArray(); } - - return ostream.ToArray(); } public static string CompressToFile(string fileName, string content) @@ -72,42 +70,7 @@ namespace TaikoSoundEditor File.WriteAllBytes(fileName, outStream.ToArray()); } - return ""; - - var tmp = "~ztmp"; - if (!Directory.Exists(tmp)) - Directory.CreateDirectory(tmp); - - var fn = Path.GetFileNameWithoutExtension(fileName); - fn = Path.Combine(tmp, fn); - File.WriteAllText(fn, content); - - fileName = Path.GetFullPath(fileName); - - - var p = new Process(); - p.StartInfo.FileName = Path.GetFullPath(@"Tools\7z\7za.exe"); - p.StartInfo.ArgumentList.Add("a"); - p.StartInfo.ArgumentList.Add("-tgzip"); - p.StartInfo.ArgumentList.Add(fileName); - p.StartInfo.ArgumentList.Add(Path.GetFullPath(fn)); - p.StartInfo.UseShellExecute = false; - p.StartInfo.RedirectStandardError = true; - p.StartInfo.RedirectStandardOutput = true; - - foreach (var a in p.StartInfo.ArgumentList) - Debug.WriteLine(a); - - p.Start(); - p.WaitForExit(); - int exitCode = p.ExitCode; - string stderr = p.StandardError.ReadToEnd(); - string stdout = p.StandardError.ReadToEnd(); - - if (exitCode != 0) - throw new Exception($"Process 7za failed with exit code {exitCode}:\n" + stderr); - - return stdout; + return ""; } } } diff --git a/MainForm.Exports.cs b/MainForm.Exports.cs index 1712906..a960784 100644 --- a/MainForm.Exports.cs +++ b/MainForm.Exports.cs @@ -20,41 +20,13 @@ namespace TaikoSoundEditor var mo = new MusicOrders(); mo.Items.AddRange(MusicOrderViewer.SongCards.Select(_ => _.MusicOrder)); - /*var mbyg = MusicOrders.Items.GroupBy(_ => _.GenreNo).Select(_ => (_.Key, List: _.ToList())).ToDictionary(_ => _.Key, _ => _.List); - - foreach (var m in AddedMusic.Select(_ => _.MusicOrder)) - { - if (!mbyg.ContainsKey(m.GenreNo)) - mbyg[m.GenreNo] = new List(); - mbyg[m.GenreNo] = mbyg[m.GenreNo].Prepend(m).ToList(); - } - - foreach (var key in mbyg.Keys.OrderBy(_ => _)) - { - mo.Items.AddRange(mbyg[key]); - }*/ - var wl = new WordList(); - wl.Items.AddRange(WordList.Items); - //wl.Items.AddRange(AddedMusic.Select(_ => new List() { _.Word, _.WordSub, _.WordDetail }).SelectMany(_ => _)); + wl.Items.AddRange(WordList.Items); - var jmi = JsonFix(Json.Serialize(mi, !DatatableSpaces.Checked)); - var jma = JsonFix(Json.Serialize(ma)); - var jmo = JsonFix(Json.Serialize(mo)); - var jwl = JsonFix(Json.Serialize(wl, !DatatableSpaces.Checked)); - - jma = jma.Replace("\"new\": true,", "\"new\":true,"); - jma = jma.Replace("\"new\": false,", "\"new\":false,"); - - File.WriteAllText(Path.Combine(path, "musicinfo"), jmi); - File.WriteAllText(Path.Combine(path, "music_attribute"), jma); - File.WriteAllText(Path.Combine(path, "music_order"), jmo); - File.WriteAllText(Path.Combine(path, "wordlist"), jwl); - - GZ.CompressToFile(Path.Combine(path, "musicinfo.bin"), jmi); - GZ.CompressToFile(Path.Combine(path, "music_attribute.bin"), jma); - GZ.CompressToFile(Path.Combine(path, "music_order.bin"), jmo); - GZ.CompressToFile(Path.Combine(path, "wordlist.bin"), jwl); + Config.DatatableIO.Serialize(Path.Combine(path, "musicinfo.bin"), mi, indented: !DatatableSpaces.Checked); + Config.DatatableIO.Serialize(Path.Combine(path, "music_attribute.bin"), ma, fixBools: true); + Config.DatatableIO.Serialize(Path.Combine(path, "music_order.bin"), mo); + Config.DatatableIO.Serialize(Path.Combine(path, "wordlist.bin"), wl, indented: true); } private void ExportNusBanks(string path) @@ -180,24 +152,6 @@ namespace TaikoSoundEditor MessageBox.Show("Done"); if (ExportOpenOnFinished.Checked) Process.Start($"explorer.exe", path); - }); - - private string JsonFix(string json) - { - var specialChars = "!@#$%^&*()_+=`~[]{}<>\\/'"; - foreach (var c in specialChars) - { - json = json.Replace($"\\u00{((int)c):X2}", $"{c}"); - } - - - return json - .Replace("\\u0022", "\\\"") - .Replace("\r\n ", "\r\n\t\t") - .Replace("\r\n ", "\r\n\t\t") - .Replace("{\r\n \"items\": [", "{\"items\":[") - .Replace(" }", "\t}") - .Replace(" ]\r\n}", "\t]\r\n}"); - } + }); } } diff --git a/Utils/DatatableIO.cs b/Utils/DatatableIO.cs index ce19d6a..17c5ac2 100644 --- a/Utils/DatatableIO.cs +++ b/Utils/DatatableIO.cs @@ -1,12 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TaikoSoundEditor.Data; - -namespace TaikoSoundEditor.Utils +namespace TaikoSoundEditor.Utils { public class DatatableIO { @@ -15,20 +7,48 @@ namespace TaikoSoundEditor.Utils public T Deserialize(string path) { if (!IsEncrypted) - { - var str = GZ.DecompressString(path); - Debug.WriteLine("----------------------------------------------------------------------"); - Debug.WriteLine(str); + { return Json.Deserialize(GZ.DecompressString(path)); } else - { - var bytes = SSL.DecryptDatatable(File.ReadAllBytes(path)); - File.WriteAllBytes("res.bin", bytes); - + { return Json.Deserialize(GZ.DecompressBytes(SSL.DecryptDatatable(File.ReadAllBytes(path)))); } } + public void Serialize(string path, T item, bool indented = false, bool fixBools = false) + { + var str = JsonFix(Json.Serialize(item, indented)); + if(fixBools) + { + str = str + .Replace("\"new\": true,", "\"new\":true,") + .Replace("\"new\": false,", "\"new\":false,"); // is this still needed? + } + + if (IsEncrypted) + File.WriteAllBytes(path, SSL.EncryptDatatable(GZ.CompressToBytes(str))); + else + File.WriteAllBytes(path, GZ.CompressToBytes(str)); + } + + private static string JsonFix(string json) + { + var specialChars = "!@#$%^&*()_+=`~[]{}<>\\/'"; + foreach (var c in specialChars) + { + json = json.Replace($"\\u00{((int)c):X2}", $"{c}"); + } + + + return json + .Replace("\\u0022", "\\\"") + .Replace("\r\n ", "\r\n\t\t") + .Replace("\r\n ", "\r\n\t\t") + .Replace("{\r\n \"items\": [", "{\"items\":[") + .Replace(" }", "\t}") + .Replace(" ]\r\n}", "\t]\r\n}"); + } + } }