-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
210 lines (189 loc) · 9.01 KB
/
Program.cs
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
using System;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
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)));
string LibraryFolder = Path.Combine(Path.GetDirectoryName(resoniteLocation), "Resonite_Data", "Managed");
Dictionary<string, Assembly> libraries = new Dictionary<string, Assembly>();
try
{
libraries.Add("Newtonsoft.Json", Assembly.LoadFrom(Path.Combine(LibraryFolder, "Newtonsoft.Json.dll")));
libraries.Add("Elements.Core", Assembly.LoadFrom(Path.Combine(LibraryFolder, "Elements.Core.dll")));
}
catch
{
Console.WriteLine("Resonite.exe not detected. Restarting...");
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);
var dataTreeConverter = ElementsCore.GetType("Elements.Core.DataTreeConverter");
var dataTreeLoad = dataTreeConverter.GetMethod("Load", new Type[] { typeof(string), typeof(string) });
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;
}
}
}