Skip to content

Commit

Permalink
updated exports
Browse files Browse the repository at this point in the history
  • Loading branch information
NotImplementedLife committed Oct 1, 2023
1 parent be0f4c1 commit 7de65ea
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 28 deletions.
16 changes: 15 additions & 1 deletion Collections/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json.Serialization;
using TaikoSoundEditor.Commons.IO;

Expand All @@ -27,11 +28,24 @@ public static Collection<T> FromJson<T>(string json, Type expectedItemType)
}

public static Type MakeGeneric(Type type) => typeof(Collection<>).MakeGenericType(type);
public static object Instantiate(Type type) => Activator.CreateInstance(MakeGeneric(type));
}

public class Collection<T>
{
[JsonPropertyName("items")]
public List<T> Items { get; set; } = new List<T>();
public List<T> Items { get; set; } = new List<T>();

public object Cast(Type type)
{
var col = Collections.Instantiate(type);
var itemsProp = col.GetType().GetProperty("Items");
var add = itemsProp.PropertyType.GetMethod("Add");
var items = itemsProp.GetValue(col);
foreach (var item in Items)
add.Invoke(items, new object[] { Convert.ChangeType(item, type) });

return col;
}
}
}
3 changes: 3 additions & 0 deletions Commons/Emit/DatatableEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ private void GenerateProperty(TypeBuilder tb, ILGenerator ilg, EntityPropertyInf

if (property.JsonPropertyName != null)
AddAttribute(pb, typeof(JsonPropertyNameAttribute), property.JsonPropertyName);
else
AddAttribute(pb, typeof(JsonIgnoreAttribute), new object[0]);

if (property.IsReadOnly)
AddAttribute(pb, typeof(ReadOnlyAttribute), true);
}
Expand Down
2 changes: 1 addition & 1 deletion Commons/Emit/EntityPropertyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class EntityPropertyInfo
public Type Type { get; }
public string JsonPropertyName { get; }
public bool IsReadOnly { get; }
public object DefaultValue { get; }
public object DefaultValue { get; }

public EntityPropertyInfo(string name, Type type, string jsonPropertyName = null, bool isReadOnly = false, object defaultValue = null)
{
Expand Down
11 changes: 11 additions & 0 deletions Commons/IO/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,16 @@ public static string Serialize<T>(T item, bool indented = true)
});
}

public static string DynamicSerialize(object item, bool indented = true)
{
Logger.Info($"Serializing dynamic {item.GetType()}:\n{item}");

return JsonSerializer.Serialize(item, item.GetType(), new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
WriteIndented = indented
});
}

}
}
16 changes: 16 additions & 0 deletions Commons/Utils/DatatableIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ public T Deserialize<T>(string path)
}
}

public void DynamicSerialize(string path, object item, bool indented = false, bool fixBools = false)
{
var str = JsonFix(Json.DynamicSerialize(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));
}

public void Serialize<T>(string path, T item, bool indented = false, bool fixBools = false)
{
var str = JsonFix(Json.Serialize(item, indented));
Expand Down
51 changes: 47 additions & 4 deletions MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 29 additions & 20 deletions MainForm.Exports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Windows.Forms;
using TaikoSoundEditor.Collections;
using TaikoSoundEditor.Commons;
using TaikoSoundEditor.Commons.IO;
using TaikoSoundEditor.Commons.Utils;
using TaikoSoundEditor.Data;

Expand All @@ -27,12 +29,12 @@ private void ExportDatatable(string path)
mo.Items.AddRange(MusicOrderViewer.SongCards.Select(_ => _.MusicOrder));

var wl = new WordList();
wl.Items.AddRange(WordList.Items);
wl.Items.AddRange(WordList.Items);

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);
Config.DatatableIO.DynamicSerialize(Path.Combine(path, "musicinfo.bin"), mi.Cast(DatatableTypes.MusicInfo) , indented: !DatatableSpaces.Checked);
Config.DatatableIO.DynamicSerialize(Path.Combine(path, "music_attribute.bin"), ma.Cast(DatatableTypes.MusicAttribute), fixBools: true);
Config.DatatableIO.DynamicSerialize(Path.Combine(path, "music_order.bin"), mo.Cast(DatatableTypes.MusicOrder));
Config.DatatableIO.DynamicSerialize(Path.Combine(path, "wordlist.bin"), wl.Cast(DatatableTypes.Word), indented: true);
}

private void ExportNusBanks(string path)
Expand All @@ -54,26 +56,33 @@ private void ExportSoundBinaries(string path)
if (!Directory.Exists(sdir))
Directory.CreateDirectory(sdir);

File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_e.bin"), ns.EBin);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_n.bin"), ns.NBin);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_h.bin"), ns.HBin);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_m.bin"), ns.MBin);
void Save(string suffix, byte[] bytes)
{
if (UseEncryptionBox.Checked)
bytes = SSL.EncryptFumen(bytes);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_{suffix}.bin"), bytes);
}

Save("e", ns.EBin);
Save("n", ns.NBin);
Save("h", ns.HBin);
Save("m", ns.MBin);

File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_e_1.bin"), ns.EBin1);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_n_1.bin"), ns.NBin1);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_h_1.bin"), ns.HBin1);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_m_1.bin"), ns.MBin1);
Save("e_1", ns.EBin1);
Save("n_1", ns.NBin1);
Save("h_1", ns.HBin1);
Save("m_1", ns.MBin1);

File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_e_2.bin"), ns.EBin2);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_n_2.bin"), ns.NBin2);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_h_2.bin"), ns.HBin2);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_m_2.bin"), ns.MBin2);
Save("e_2", ns.EBin2);
Save("n_2", ns.NBin2);
Save("h_2", ns.HBin2);
Save("m_2", ns.MBin2);

if (ns.MusicAttribute.CanPlayUra)
{
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_x.bin"), ns.XBin);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_x_1.bin"), ns.XBin1);
File.WriteAllBytes(Path.Combine(sdir, $"{ns.Id}_x_2.bin"), ns.XBin2);
Save("x", ns.XBin);
Save("x_1", ns.XBin1);
Save("x_2", ns.XBin2);
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using TaikoSoundEditor.Commons.Controls;
Expand Down Expand Up @@ -28,7 +29,8 @@ public MainForm()
DatatableKeyBox.Text = Config.IniFile.Read("DatatableKey");
FumenKeyBox.Text = Config.IniFile.Read("FumenKey");

LoadPreferences();
LoadPreferences();
DatatableDef.Path = Config.DatatableDefPath;
//SortByGenreToolStripMenuItem.RadioCheck = true;
}

Expand Down Expand Up @@ -426,7 +428,20 @@ private void LoadedMusicBox_DrawItem(object sender, DrawItemEventArgs e)
var selItem = LoadedMusicBox.Items[e.Index] as IMusicInfo;
TextRenderer.DrawText(e.Graphics, $"{selItem.UniqueId}. {selItem.Id}", Font, e.Bounds, e.ForeColor, e.BackColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
e.DrawFocusRectangle();
}
private void DatatableDef_PathChanged(object sender, EventArgs args)
{
try
{
var json = File.ReadAllText(DatatableDef.Path);
DatatableTypes.LoadFromJson(json);
Config.DatatableDefPath = DatatableDef.Path;
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}

}
}
Loading

0 comments on commit 7de65ea

Please sign in to comment.