1
0
mirror of synced 2024-11-24 04:20:10 +01:00

refactored export method

This commit is contained in:
NotImplementedLife 2023-09-30 19:13:04 +03:00
parent d34e35a81e
commit c8fbf86afe
3 changed files with 52 additions and 115 deletions

53
GZ.cs
View File

@ -32,7 +32,6 @@ namespace TaikoSoundEditor
public static byte[] DecompressBytes(string gzPath) public static byte[] DecompressBytes(string gzPath)
{ {
Logger.Info("GZ Decompressing bytes"); Logger.Info("GZ Decompressing bytes");
using FileStream originalFileStream = File.OpenRead(gzPath); using FileStream originalFileStream = File.OpenRead(gzPath);
using GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress); using GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress);
using MemoryStream ms = new MemoryStream(); using MemoryStream ms = new MemoryStream();
@ -43,17 +42,16 @@ namespace TaikoSoundEditor
public static byte[] CompressToBytes(string content) public static byte[] CompressToBytes(string content)
{ {
Logger.Info("GZ Compressing bytes"); Logger.Info("GZ Compressing bytes");
var uncompressed = Encoding.UTF8.GetBytes(content);
using var stream = new MemoryStream(); using (MemoryStream outStream = new MemoryStream())
using var writer = new StreamWriter(stream);
writer.Write(content);
using var ostream = new MemoryStream();
using (var compressionStream = new GZipStream(ostream, CompressionMode.Compress))
{ {
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) public static string CompressToFile(string fileName, string content)
@ -73,41 +71,6 @@ namespace TaikoSoundEditor
} }
return ""; 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;
} }
} }
} }

View File

@ -20,41 +20,13 @@ namespace TaikoSoundEditor
var mo = new MusicOrders(); var mo = new MusicOrders();
mo.Items.AddRange(MusicOrderViewer.SongCards.Select(_ => _.MusicOrder)); 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<MusicOrder>();
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(); var wl = new WordList();
wl.Items.AddRange(WordList.Items); wl.Items.AddRange(WordList.Items);
//wl.Items.AddRange(AddedMusic.Select(_ => new List<Word>() { _.Word, _.WordSub, _.WordDetail }).SelectMany(_ => _));
var jmi = JsonFix(Json.Serialize(mi, !DatatableSpaces.Checked)); Config.DatatableIO.Serialize(Path.Combine(path, "musicinfo.bin"), mi, indented: !DatatableSpaces.Checked);
var jma = JsonFix(Json.Serialize(ma)); Config.DatatableIO.Serialize(Path.Combine(path, "music_attribute.bin"), ma, fixBools: true);
var jmo = JsonFix(Json.Serialize(mo)); Config.DatatableIO.Serialize(Path.Combine(path, "music_order.bin"), mo);
var jwl = JsonFix(Json.Serialize(wl, !DatatableSpaces.Checked)); Config.DatatableIO.Serialize(Path.Combine(path, "wordlist.bin"), wl, indented: true);
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);
} }
private void ExportNusBanks(string path) private void ExportNusBanks(string path)
@ -181,23 +153,5 @@ namespace TaikoSoundEditor
if (ExportOpenOnFinished.Checked) if (ExportOpenOnFinished.Checked)
Process.Start($"explorer.exe", path); 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}");
}
} }
} }

View File

@ -1,12 +1,4 @@
using System; namespace TaikoSoundEditor.Utils
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaikoSoundEditor.Data;
namespace TaikoSoundEditor.Utils
{ {
public class DatatableIO public class DatatableIO
{ {
@ -16,19 +8,47 @@ namespace TaikoSoundEditor.Utils
{ {
if (!IsEncrypted) if (!IsEncrypted)
{ {
var str = GZ.DecompressString(path);
Debug.WriteLine("----------------------------------------------------------------------");
Debug.WriteLine(str);
return Json.Deserialize<T>(GZ.DecompressString(path)); return Json.Deserialize<T>(GZ.DecompressString(path));
} }
else else
{ {
var bytes = SSL.DecryptDatatable(File.ReadAllBytes(path));
File.WriteAllBytes("res.bin", bytes);
return Json.Deserialize<T>(GZ.DecompressBytes(SSL.DecryptDatatable(File.ReadAllBytes(path)))); return Json.Deserialize<T>(GZ.DecompressBytes(SSL.DecryptDatatable(File.ReadAllBytes(path))));
} }
} }
public void Serialize<T>(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}");
}
} }
} }