-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exporter.cs
223 lines (186 loc) · 6.83 KB
/
Exporter.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
211
212
213
214
215
216
217
218
219
220
221
222
223
using BtlB1;
using BtlShare;
using Google.Protobuf;
using Newtonsoft.Json;
using ResB1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
public class Exporter
{
public static Assembly s_protobufDB = Assembly.Load("GSE.ProtobufDB");
public static Assembly s_runtime = Assembly.Load("Protobuf.Runtime");
public static Dictionary<string, (Type, Assembly)> s_speFileTypeMapping = new Dictionary<string, (Type, Assembly)>()
{
{"FUStBeAttackedInfoOldDesc.data", (typeof(FUStBeAttackedInfoDesc), s_protobufDB)},
{"FUStBeAttackedInfoOldDesc1.data", (typeof(FUStBeAttackedInfoDesc), s_protobufDB)},
{"EndingCredits_EndA.data", (typeof(EndingCreditsData), s_runtime) },
{"EndingCredits_EndA_Other.data", (typeof(EndingCreditsData), s_runtime) },
{"EndingCredits_EndB.data", (typeof(EndingCreditsData), s_runtime) },
{"EndingCredits_EndB_Other.data", (typeof(EndingCreditsData), s_runtime) },
{"FUStUnitCommOverrideDesc.data", (typeof(FUStUnitCommDesc), s_protobufDB) },
};
public static bool InputJson2Data(string filePath, string outputPath)
{
var json = File.ReadAllText(filePath);
if (json == null) return false;
string fileName = Path.GetFileName(filePath);
var protobufDBTypes = s_protobufDB.GetTypes();
var runtimeTypes = s_runtime.GetTypes();
var tuple = GetTypeAssemblyTupleByFileName(fileName, protobufDBTypes, runtimeTypes);
var realType = tuple.Item1;
Type tbType = null;
try
{
tbType = tuple.Item2.GetTypes().First(a => a.Name == "TB" + realType.Name);
}
catch (Exception ex)
{
}
if (tbType != null)
{
realType = tbType;
}
IMessage data = JsonConvert.DeserializeObject(json, realType) as IMessage;
if (data == null) return false;
try
{
if (!Directory.Exists(outputPath))
{
var outDir = outputPath.Substring(0, outputPath.LastIndexOf("\\"));
Directory.CreateDirectory(outDir);
}
// 使用 FileStream 将序列化的数据写入文件
using (FileStream output = File.Create(outputPath))
{
data.WriteTo(output);
}
Console.WriteLine($"数据已成功写入到: {outputPath}");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"写入文件时发生错误: {ex.Message}");
return false;
}
}
static (Type, Assembly) GetTypeAssemblyTupleByFileName(string fileName, Type[] protobufDBTypes, Type[] runtimeTypes)
{
bool find = false;
foreach (var type in protobufDBTypes)
{
if (fileName.Contains(type.Name) && type.IsClass)
{
return (type, s_protobufDB);
}
}
foreach (var type in runtimeTypes)
{
if (fileName.Contains(type.Name))
{
return (type, s_runtime);
}
}
if (s_speFileTypeMapping.TryGetValue(fileName, out var typeAssemblyTuple))
{
return (typeAssemblyTuple.Item1, typeAssemblyTuple.Item2);
}
return (null, null);
}
public static bool ExportAll2Json(string outputDir)
{
List<string> fileNameList = new List<string>();
List<string> filePathList = new List<string>();
List<Type> types = new List<Type>();
Director("ProtoData", fileNameList, filePathList);
if (fileNameList.Count > 0)
{
int index = 0;
var protobufDBTypes = s_protobufDB.GetTypes();
var runtimeTypes = s_runtime.GetTypes();
foreach (string fileName in fileNameList)
{
var tuple = GetTypeAssemblyTupleByFileName(fileName, protobufDBTypes, runtimeTypes);
if (tuple.Item1 != null && tuple.Item2 != null)
{
ExportToJson(tuple.Item2, tuple.Item1, filePathList[index], outputDir);
}
index++;
}
}
return true;
}
static void ExportToJson(Assembly assmebly, Type type, string filePath, string outputDir)
{
if(assmebly == null || type == null || string.IsNullOrEmpty(filePath))
{ return; }
var bytes = File.ReadAllBytes(filePath);
var realType = type;
Type tbType = null;
try
{
tbType = assmebly.GetTypes().First(a => a.Name == "TB" + realType.Name);
}
catch (Exception ex)
{
}
if (tbType != null)
{
realType = tbType;
}
var parser = realType.GetProperty("Parser", BindingFlags.Static | BindingFlags.Public);
if (parser != null)
{
try
{
MessageParser parserValue = parser.GetMethod.Invoke(null, null) as MessageParser;
var message = parserValue.ParseFrom(bytes);
if (message != null)
{
string outPath = outputDir + "\\" + filePath + ".json";
if (!Directory.Exists(outPath))
{
var outDir = outPath.Substring(0, outPath.LastIndexOf("\\"));
Directory.CreateDirectory(outDir);
}
if (File.Exists(outPath))
{
File.Delete(outPath);
}
var outputJson = JsonConvert.SerializeObject(message, Formatting.Indented);
Console.WriteLine("Success : " + outPath);
File.WriteAllText(outPath, outputJson);
}
}
catch
{
Console.WriteLine("Data Failed File : " + filePath);
}
}
}
public static void Director(string dir, List<string> list, List<string> filePathList)
{
DirectoryInfo d = new DirectoryInfo(dir);
FileInfo[] files = d.GetFiles();//文件
DirectoryInfo[] directs = d.GetDirectories();//文件夹
foreach (FileInfo f in files)
{
if (f.Extension == ".data")
{
list.Add(f.Name);//添加文件名到列表中
var indexOf = f.FullName.IndexOf("ProtoData");
filePathList.Add(f.FullName.Substring(indexOf, f.FullName.Length - indexOf));
}
else
File.Delete(f.FullName);
}
//获取子文件夹内的文件列表,递归遍历
foreach (DirectoryInfo dd in directs)
{
Director(dd.FullName, list, filePathList);
}
}
}