-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
304 lines (276 loc) · 13.7 KB
/
Program.cs
File metadata and controls
304 lines (276 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
namespace Resonite_DataTree_Converter
{
internal class Program
{
[STAThread]
static void Main(string[] args)
{
start_find:
string settingsLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Lexevolution", "Resonite DataTree Converter", "app.config");
if (!File.Exists(settingsLocation))
{
Directory.CreateDirectory(Path.GetDirectoryName(settingsLocation));
Console.WriteLine("Please find Resonite.exe");
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "Executable Files (*.exe)|*.exe",
Title = "Please select Resonite.exe",
CheckFileExists = true,
CheckPathExists = true,
Multiselect = false
};
DialogResult result = ofd.ShowDialog();
Console.WriteLine("");
if (result == DialogResult.Cancel)
{
Console.WriteLine("Cancelled, restarting...");
Console.WriteLine("");
goto start_find;
}
File.WriteAllText(settingsLocation, ofd.FileName);
}
StreamReader sr = new StreamReader(settingsLocation);
string resoniteLocation = sr.ReadToEnd();
sr.Close();
Console.WriteLine(string.Format("DIRECTORY: {0}", Path.GetDirectoryName(resoniteLocation)));
// Try the new location first (same directory as exe), then fall back to old location
string resoniteDir = Path.GetDirectoryName(resoniteLocation);
string LibraryFolder = resoniteDir;
string oldLibraryFolder = Path.Combine(resoniteDir, "Resonite_Data", "Managed");
Dictionary<string, Assembly> libraries = new Dictionary<string, Assembly>();
try
{
// Try new location first (post-August update)
if (File.Exists(Path.Combine(LibraryFolder, "Newtonsoft.Json.dll")) &&
File.Exists(Path.Combine(LibraryFolder, "Elements.Core.dll")))
{
Console.WriteLine("Found DLLs in new location (same directory as exe)");
libraries.Add("Newtonsoft.Json", Assembly.LoadFrom(Path.Combine(LibraryFolder, "Newtonsoft.Json.dll")));
libraries.Add("Elements.Core", Assembly.LoadFrom(Path.Combine(LibraryFolder, "Elements.Core.dll")));
}
// Fall back to old location
else if (File.Exists(Path.Combine(oldLibraryFolder, "Newtonsoft.Json.dll")) &&
File.Exists(Path.Combine(oldLibraryFolder, "Elements.Core.dll")))
{
Console.WriteLine("Found DLLs in old location (Resonite_Data/Managed)");
libraries.Add("Newtonsoft.Json", Assembly.LoadFrom(Path.Combine(oldLibraryFolder, "Newtonsoft.Json.dll")));
libraries.Add("Elements.Core", Assembly.LoadFrom(Path.Combine(oldLibraryFolder, "Elements.Core.dll")));
}
else
{
throw new FileNotFoundException("Required DLL files not found in either location");
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load Resonite libraries: {ex.Message}");
Console.WriteLine("Looking for:");
Console.WriteLine($" - {Path.Combine(LibraryFolder, "Newtonsoft.Json.dll")}");
Console.WriteLine($" - {Path.Combine(LibraryFolder, "Elements.Core.dll")}");
Console.WriteLine("Or in old location:");
Console.WriteLine($" - {Path.Combine(oldLibraryFolder, "Newtonsoft.Json.dll")}");
Console.WriteLine($" - {Path.Combine(oldLibraryFolder, "Elements.Core.dll")}");
Console.WriteLine("");
File.Delete(settingsLocation);
goto start_find;
}
start_select:
Console.WriteLine("Which way do you want to convert the file? (press 1 or 2)");
Console.WriteLine("1. DataTree to JSON");
Console.WriteLine("2. JSON to DataTree");
Console.WriteLine("q to quit");
ConsoleKeyInfo test = Console.ReadKey();
bool success;
switch (test.Key)
{
case ConsoleKey.D1:
success = ToJSON(libraries);
if (!success) { goto start_select; }
break;
case ConsoleKey.D2:
success = ToDataTree(libraries);
if (!success) { goto start_select; }
break;
case ConsoleKey.Q:
break;
default:
Console.WriteLine("Unsupported choice. Please press 1 or 2.");
Console.WriteLine("");
goto start_select;
}
}
static bool ToJSON(Dictionary<string, Assembly> libraries)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "Resonite DataTree File (*.frdt;*.brson;*.lz4bson;*.7zbson)|*.frdt;*.brson;*.lz4bson;*.7zbson",
Title = "Choose a DataTree file for conversion",
CheckFileExists = true,
CheckPathExists = true,
Multiselect = false
};
DialogResult result = ofd.ShowDialog();
if (result == DialogResult.Cancel)
{
Console.WriteLine("Cancelled opening file. Restarting.");
Console.WriteLine("");
return false;
}
libraries.TryGetValue("Elements.Core", out Assembly ElementsCore);
if (ElementsCore == null)
{
Console.WriteLine("ERROR: Elements.Core assembly not loaded properly");
return false;
}
// List all types in Elements.Core to help debug
Console.WriteLine("Searching for DataTreeConverter in Elements.Core...");
var dataTreeConverter = ElementsCore.GetType("Elements.Core.DataTreeConverter");
if (dataTreeConverter == null)
{
Console.WriteLine("ERROR: DataTreeConverter not found in Elements.Core");
Console.WriteLine("Attempting to list available types...");
try
{
Type[] types = null;
try
{
types = ElementsCore.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
// Some types may not load, but we can still get the ones that did
types = ex.Types.Where(t => t != null).ToArray();
Console.WriteLine($"Warning: Some types could not be loaded due to missing dependencies");
}
if (types != null && types.Length > 0)
{
Console.WriteLine("Available types containing 'DataTree' or 'Converter':");
foreach (var type in types)
{
if (type != null && type.FullName != null &&
(type.FullName.Contains("DataTree") || type.FullName.Contains("Converter")))
{
Console.WriteLine($" - {type.FullName}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Could not enumerate types: {ex.Message}");
Console.WriteLine("\nThis might be due to .NET version mismatch.");
Console.WriteLine("Resonite may be using .NET 9.0 while this converter uses .NET 8.0");
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
return false;
}
var dataTreeLoad = dataTreeConverter.GetMethod("Load", new Type[] { typeof(string), typeof(string) });
if (dataTreeLoad == null)
{
Console.WriteLine("ERROR: Load method not found in DataTreeConverter");
Console.WriteLine("Available methods:");
foreach (var method in dataTreeConverter.GetMethods(BindingFlags.Public | BindingFlags.Static))
{
Console.WriteLine($" - {method.Name}");
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
return false;
}
object convert = dataTreeLoad.Invoke(null, new object[] { ofd.FileName, null });
//DataTreeDictionary convert = DataTreeConverter.Load(ofd.FileName);
SaveFileDialog save = new SaveFileDialog()
{
Filter = "JSON File|*.json",
Title = "Save Output JSON to...",
OverwritePrompt = true
};
result = save.ShowDialog();
if (result == DialogResult.Cancel)
{
Console.WriteLine("Cancelled saving file. Restarting.");
Console.WriteLine("");
return false;
}
StreamWriter fileStream = File.CreateText(save.FileName);
libraries.TryGetValue("Newtonsoft.Json", out Assembly NewtonsoftJson);
ConstructorInfo jsonTextWriterConstructor = NewtonsoftJson.GetType("Newtonsoft.Json.JsonTextWriter").GetConstructor(new Type[] {typeof(TextWriter)});
object jsonTextWriter = jsonTextWriterConstructor.Invoke(new object[] { fileStream });
var writemethod = dataTreeConverter.GetMethod("Write", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
writemethod.Invoke(null, new object[] { convert, jsonTextWriter });
fileStream.Dispose();
fileStream.Close();
return true;
}
static bool ToDataTree(Dictionary<string, Assembly> libraries)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "JSON File (*.json)|*.json",
Title = "Choose a JSON file for conversion",
CheckFileExists = true,
CheckPathExists = true,
Multiselect = false
};
DialogResult result = ofd.ShowDialog();
if (result == DialogResult.Cancel)
{
Console.WriteLine("Cancelled opening file. Restarting.");
Console.WriteLine("");
return false;
}
libraries.TryGetValue("Elements.Core", out Assembly ElementsCore);
Type dataTreeConverter = ElementsCore.GetType("Elements.Core.DataTreeConverter");
TextReader testReader = new StreamReader(ofd.FileName);
libraries.TryGetValue("Newtonsoft.Json", out Assembly Newtonsoft);
ConstructorInfo JsonTextReaderConstructor = Newtonsoft.GetType("Newtonsoft.Json.JsonTextReader").GetConstructor(new Type[] {typeof(TextReader)});
object JsonTextReader = JsonTextReaderConstructor.Invoke(new object[] { testReader });
MethodInfo JsonConvertMethod = dataTreeConverter.GetMethod("Read", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
object DataTree = JsonConvertMethod.Invoke(null, new object[] { JsonTextReader });
SaveFileDialog save = new SaveFileDialog()
{
Filter = "Brotli Compressed BSON (*.brson)|*.brson|LZ4 Compressed BSON (*.lz4bson)|*.lz4bson|7-Zip compressed BSON (*.7zbson)|*.7zbson",
Title = "Save DataTree file to...",
OverwritePrompt = true,
AddExtension = true
};
result = save.ShowDialog();
if (result == DialogResult.Cancel)
{
Console.WriteLine("Cancelled saving file. Restarting.");
Console.WriteLine("");
return false;
}
MethodInfo DataTreeSaveMethod = dataTreeConverter.GetMethod("Save", BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
Type compressionType = ElementsCore.GetType("Elements.Core.DataTreeConverter+Compression");
Array compressionTypeValues = compressionType.GetEnumValues();
object selectedCompressionType;
switch (Path.GetExtension(save.FileName))
{
case ".brson":
selectedCompressionType = compressionTypeValues.GetValue(3);
break;
case ".7zbson":
selectedCompressionType = compressionTypeValues.GetValue(2);
break;
case ".lz4bson":
selectedCompressionType = compressionTypeValues.GetValue(1);
break;
default:
selectedCompressionType = compressionTypeValues.GetValue(0);
break;
}
DataTreeSaveMethod.Invoke(null, new object[] {DataTree, save.FileName, selectedCompressionType});
return true;
}
}
}