Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit 61dd334

Browse files
committed
Add support for encoding charts back
1 parent 48ef5f7 commit 61dd334

File tree

6 files changed

+189
-22
lines changed

6 files changed

+189
-22
lines changed

AssetDownloader/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static void Main(string[] args)
3636
if (file.StartsWith("plain"))
3737
ext = "";
3838

39-
if (File.Exists($"assets/{file}{ext}"))
39+
if (File.Exists($"assets/{file}{ext}") || !file.Contains("MusicMaster"))
4040
continue;
4141

4242
Console.WriteLine($"Downloading {file}");

AssetTool/Program.cs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@ static bool DecryptMaster(FileInfo inputFile, byte[] decrypted)
1818
var typeName = inputFile.Name.Replace(".msgpack.enc", "");
1919
var targetType = MasterTypes.GetDeserializeType(typeName);
2020

21+
var options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
22+
2123
if (targetType == null)
2224
{
2325
Console.WriteLine($"Not supported master {typeName}.");
24-
return false;
26+
File.WriteAllText(
27+
inputFile.FullName.Replace(".msgpack.enc", ".json"),
28+
MessagePackSerializer.ConvertToJson(decrypted, options)
29+
);
30+
}
31+
else
32+
{
33+
var result = MessagePackSerializer.Deserialize(targetType, decrypted, options);
34+
File.WriteAllText(
35+
inputFile.FullName.Replace(".msgpack.enc", ".json"),
36+
DumpToJson(result)
37+
);
2538
}
26-
27-
var options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
28-
var result = MessagePackSerializer.Deserialize(targetType, decrypted, options);
29-
30-
File.WriteAllText(
31-
inputFile.FullName.Replace(".msgpack.enc", ".json"),
32-
DumpToJson(result)
33-
);
3439

3540
return true;
3641
}
@@ -64,7 +69,7 @@ static void EncryptMaster(FileInfo inputFile)
6469
}
6570

6671
var options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
67-
var result = JsonConvert.DeserializeObject(File.ReadAllText(inputFile.FullName), targetType);
72+
var result = JsonConvert.DeserializeObject(File.ReadAllText(inputFile.FullName), targetType, new SBConverterIntInt<ChartLessonMaster>());
6873
var output = MessagePackSerializer.Serialize(result, options);
6974
var encrypted = AssetDecryptor.Encrypt(output);
7075

@@ -80,7 +85,7 @@ static void ProcessFileSystemEntry(FileSystemInfo fileSystemInfo)
8085
}
8186
else if (fileSystemInfo is FileInfo fileInfo)
8287
{
83-
if(fileInfo.Extension == ".enc")
88+
if (fileInfo.Extension == ".enc")
8489
{
8590
Console.WriteLine($"Decrypting {fileInfo.Name}...");
8691
var decrypted = AssetDecryptor.Decrypt(fileInfo.OpenRead());
@@ -100,24 +105,26 @@ static void ProcessFileSystemEntry(FileSystemInfo fileSystemInfo)
100105
object result = null;
101106

102107
// Check if this is chart common data
103-
if(fileInfo.Name.EndsWith("0.enc"))
104-
result= DeserializeMsgPack<ChartCommonData>(decrypted);
108+
if (fileInfo.Name.EndsWith("0.enc"))
109+
result = DeserializeMsgPack<ChartCommonData>(decrypted);
105110
else
106111
result = DeserializeMsgPack<ChartData>(decrypted);
107-
112+
113+
var options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
114+
108115
File.WriteAllText(
109116
fileInfo.FullName.Replace(".enc", ".json"),
110117
DumpToJson(result)
111118
);
112119

113120
success = true;
114121
}
115-
catch(Exception ex)
122+
catch (Exception ex)
116123
{
117124
Console.WriteLine($"Failed to dump chart: {ex.Message}");
118125
}
119126
}
120-
else if(fileInfo.Name.EndsWith("ResourceList.msgpack.enc"))
127+
else if (fileInfo.Name.EndsWith("ResourceList.msgpack.enc"))
121128
{
122129
Console.WriteLine($"Dumping ResourceList...");
123130

@@ -141,7 +148,7 @@ static void ProcessFileSystemEntry(FileSystemInfo fileSystemInfo)
141148
}
142149
else if (fileInfo.Name.EndsWith("ResourceList.msgpack"))
143150
{
144-
151+
145152
try
146153
{
147154
var result = DeserializeMsgPack<Dictionary<string, int>>(File.ReadAllBytes(fileInfo.FullName));
@@ -181,11 +188,37 @@ static void ProcessFileSystemEntry(FileSystemInfo fileSystemInfo)
181188
}
182189
}
183190
// Encrypt master back
184-
else if(fileInfo.Name.EndsWith("Master.json"))
191+
else if (fileInfo.Name.EndsWith("Master.json"))
185192
{
186193
Console.WriteLine($"Encrypting {fileInfo.Name}...");
187194
EncryptMaster(fileInfo);
188195
}
196+
else if(fileInfo.Name.StartsWith("chart") && fileInfo.Extension == ".json")
197+
{
198+
Console.WriteLine("Encrypting chart...");
199+
200+
try
201+
{
202+
var json = File.ReadAllText(fileInfo.FullName);
203+
204+
object result = null;
205+
206+
// Check if this is chart common data
207+
if (fileInfo.Name.EndsWith("0.json"))
208+
result = JsonConvert.DeserializeObject<ChartCommonData>(json);
209+
else
210+
result = JsonConvert.DeserializeObject<ChartData>(json);
211+
212+
File.WriteAllBytes(
213+
fileInfo.FullName.Replace(".json", ".enc"),
214+
AssetDecryptor.Encrypt(SerializeMsgPack(result))
215+
);
216+
}
217+
catch (Exception ex)
218+
{
219+
Console.WriteLine($"Failed to dump chart: {ex.Message}");
220+
}
221+
}
189222
else
190223
{
191224
Console.WriteLine($"Encrypting {fileInfo.Name}...");

AssetTool/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"AssetTool": {
44
"commandName": "Project",
5-
"commandLineArgs": "iOSResourceList.msgpack.enc"
5+
"commandLineArgs": "chart_00000014.json"
66
}
77
}
88
}

AssetTool/TupleConvertor.cs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using MessagePack;
5+
using Newtonsoft.Json;
6+
7+
namespace D4DJ_Tools
8+
{
9+
public class SBConverterIntInt<T> : JsonConverter<Dictionary<(int, int), T>>
10+
{
11+
/// <summary>
12+
/// Override ReadJson to read the dictionary key and value
13+
/// </summary>
14+
/// <param name="reader"></param>
15+
/// <param name="objectType"></param>
16+
/// <param name="existingValue"></param>
17+
/// <param name="serializer"></param>
18+
/// <returns></returns>
19+
public override Dictionary<(int, int), T> ReadJson(JsonReader reader, Type objectType, [AllowNull] Dictionary<(int, int), T> existingValue, bool hasExistingValue, JsonSerializer serializer)
20+
{
21+
var _dict = new Dictionary<(int, int), T>();
22+
23+
//loop through the JSON string reader
24+
while (reader.Read())
25+
{
26+
// check whether it is a property
27+
if (reader.TokenType == JsonToken.PropertyName)
28+
{
29+
string readerValue = reader.Value.ToString();
30+
if (reader.Read())
31+
{
32+
// check if the property is tuple (Dictionary key)
33+
if (readerValue.Contains('(') && readerValue.Contains(')'))
34+
{
35+
string[] result = ConvertTuple(readerValue);
36+
37+
if (result == null)
38+
continue;
39+
40+
// Custom Deserialize the Dictionary key (Tuple)
41+
var _tuple = (int.Parse(result[0].Trim()), int.Parse(result[1].Trim()));
42+
43+
// Custom Deserialize the Dictionary value
44+
var _value = serializer.Deserialize<T>(reader);
45+
46+
_dict.Add(_tuple, _value);
47+
}
48+
else
49+
{
50+
// Deserialize the remaining data from the reader
51+
serializer.Deserialize(reader);
52+
break;
53+
}
54+
}
55+
}
56+
}
57+
return _dict;
58+
}
59+
60+
/// <summary>
61+
/// To convert Tuple
62+
/// </summary>
63+
/// <param name="_string"></param>
64+
/// <returns></returns>
65+
public string[] ConvertTuple(string _string)
66+
{
67+
string tempStr = null;
68+
69+
// remove the first character which is a brace '('
70+
if (_string.Contains('('))
71+
tempStr = _string.Remove(0, 1);
72+
73+
// remove the last character which is a brace ')'
74+
if (_string.Contains(')'))
75+
tempStr = tempStr.Remove(tempStr.Length - 1, 1);
76+
77+
// seperate the Item1 and Item2
78+
if (_string.Contains(','))
79+
return tempStr.Split(',');
80+
81+
return null;
82+
}
83+
84+
/// <summary>
85+
/// WriteJson needs to be implemented since it is an abstract function.
86+
/// </summary>
87+
/// <param name="writer"></param>
88+
/// <param name="value"></param>
89+
/// <param name="serializer"></param>
90+
public override void WriteJson(JsonWriter writer, Dictionary<(int, int), T> value, JsonSerializer serializer)
91+
{
92+
serializer.Serialize(writer, value);
93+
}
94+
}
95+
96+
public class IntIntTuple
97+
{
98+
public int Item1;
99+
public int Item2;
100+
101+
public static implicit operator IntIntTuple(string value)
102+
{
103+
var values = value.Trim('(', ')').Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
104+
return new IntIntTuple { Item1 = int.Parse(values[0]), Item2 = int.Parse(values[1]) };
105+
}
106+
107+
public static implicit operator string(IntIntTuple value)
108+
{
109+
return $"({value.Item1}, {value.Item2})";
110+
}
111+
112+
public override string ToString()
113+
{
114+
return this;
115+
}
116+
}
117+
118+
public class IntIntTupleDic<T> : Dictionary<(int,int), T>
119+
{
120+
121+
}
122+
}

D4DJ-Tools.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D4DJ.Types", "D4DJ.Types\D4
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MusicRenamer", "MusicRenamer\MusicRenamer.csproj", "{9EBF88C9-1671-4B07-92AF-8166DF8643BA}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssetDownloader", "AssetDownloader\AssetDownloader.csproj", "{628D6092-2307-49F0-BEF1-58198B9B65E4}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssetDownloader", "AssetDownloader\AssetDownloader.csproj", "{628D6092-2307-49F0-BEF1-58198B9B65E4}"
1313
EndProject
1414
Global
1515
GlobalSection(SolutionConfigurationPlatforms) = preSolution

D4DJ.Types/ChartData.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ public class NoteData
3636
public float EffectParameter { get; set; }
3737
}
3838

39+
[MessagePackObject]
40+
public class SoflanData
41+
{
42+
[Key(0)]
43+
public float Time { get; set; }
44+
[Key(1)]
45+
public float TimeScale { get; set; }
46+
[Key(2)]
47+
public int LeftRight { get; set; }
48+
}
49+
3950
[MessagePackObject]
4051
public class ChartData
4152
{
4253
[Key(0)]
4354
public string MusicName { get; set; }
4455
[Key(1)]
45-
public (float, float, int)[] SoflanDataList { get; set; }
56+
//public (float, float, int)[] SoflanDataList { get; set; }
57+
public SoflanData[] SoflanDataList { get; set; }
4658
[Key(2)]
4759
public float[] BarLineList { get; set; }
4860
[Key(3)]

0 commit comments

Comments
 (0)