-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cs
273 lines (253 loc) · 9.91 KB
/
Util.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
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using SoulsFormats;
using System.Reflection;
using System.Diagnostics;
namespace DSPorterUtil
{
public static class Util
{
public static void WritePortedSoulsFile(ISoulsFile file, string dataPath, string filePath, DCX.Type dcxType = DCX.Type.None)
{
string writePath = GetOutputPath(dataPath, filePath, dcxType != DCX.Type.None);
file.Write(writePath, dcxType);
}
public static string GetVirtualPath(string dataPath, string filePath)
{
var fileName = filePath.Split(dataPath)[1];
var virtualPath = fileName.Split("\\")[..^1];
return Path.Join(virtualPath);
}
public static string GetOutputPath(string dataPath, string filePath, bool isDCX = true)
{
string outputDirectory = $"{Directory.GetCurrentDirectory()}\\output\\{GetVirtualPath(dataPath, filePath)}";
string fileName = Path.GetFileName(filePath);
string outputPath = $"{outputDirectory}\\{fileName}";
Directory.CreateDirectory(outputDirectory);
if (isDCX && !outputPath.EndsWith(".dcx"))
{
outputPath += ".dcx";
}
return outputPath;
}
public static string GetByteArrayString(byte[] field)
{
string bytestr = "";
for (var i = 0; i < field.Length; i++)
{
bytestr += field[i];
}
bytestr = bytestr[..^1];
return bytestr + "]";
}
public static bool HasModifiedScaling(Vector3 v1)
{
if (Vector3.Distance(v1, new Vector3(1.0f, 1.0f, 1.0f)) > 0.001f)
{
return true;
}
return false;
}
public static bool Vector3IsEqual(Vector3 v1, Vector3 v2)
{
if (!FloatIsEqual(v1.X, v2.X))
return false;
if (!FloatIsEqual(v1.Y, v2.Y))
return false;
if (!FloatIsEqual(v1.Z, v2.Z))
return false;
return true;
}
public static bool FloatIsEqual(float f1, float f2)
{
if (Math.Abs(f1 - f2) < 0.001f)
{
return true;
}
return false;
}
public static ConcurrentBag<PARAMDEF> LoadParamDefXmls(string gameType)
{
ConcurrentBag<PARAMDEF> paramdefs_ptde = new();
foreach (string path in Directory.GetFiles($"{Directory.GetCurrentDirectory()}\\Resources\\Paramdex\\{gameType}\\Defs", "*.xml", SearchOption.AllDirectories))
{
var paramdef = PARAMDEF.XmlDeserialize(path);
paramdefs_ptde.Add(paramdef);
}
return paramdefs_ptde;
}
/// <summary>
/// Apply param defs for list of BinderFiles
/// </summary>
public static void ApplyParamDefs(ConcurrentBag<PARAMDEF> paramdefs, List<BinderFile> fileList, ConcurrentDictionary<string, PARAM> paramList)
{
ConcurrentBag<string> warningList = new();
Parallel.ForEach(Partitioner.Create(fileList), file =>
{
PARAM? param = null;
try
{
if (file.Name.Contains("m99") || !file.Name.EndsWith(".param"))
{
// Not a param
return;
}
string name = Path.GetFileNameWithoutExtension(file.Name);
param = PARAM.Read(file.Bytes);
param = Util.ApplyDefWithWarnings(param, paramdefs);
if (param != null)
paramList.TryAdd(name, param);
}
catch (InvalidDataException)
{
}
});
}
public static void ApplyRowNames(ConcurrentDictionary<string, string[]> rowNames, ConcurrentDictionary<string, PARAM> paramList)
{
Parallel.ForEach(Partitioner.Create(rowNames), file =>
{
if (paramList.TryGetValue(file.Key.Replace(".txt", ""), out PARAM param))
{
foreach (string line in file.Value)
{
if (string.IsNullOrWhiteSpace(line))
continue;
string[] split = line.Split(' ');
int id = int.Parse(split[0]);
string name = string.Join(' ', split[1..]);
PARAM.Row? row = param[id];
if (row != null)
{
row.Name = name;
}
}
}
});
}
public static PARAM? ApplyDef(PARAM param, PARAMDEF paramdef)
{
try
{
param.ApplyParamdef(paramdef);
return param;
}
catch(InvalidDataException e)
{
return null;
}
}
public static PARAM? ApplyDefWithWarnings(PARAM param, ConcurrentBag<PARAMDEF> paramdefs)
{
bool matchType = false;
bool matchDefVersion = false;
int bestDefVersion = -420;
long bestRowsize = -69;
long bestDefRowSize = -999;
foreach (PARAMDEF paramdef in paramdefs)
{
if (param.ParamType == paramdef.ParamType)
{
matchType = true;
bestDefVersion = paramdef.DataVersion;
if (param.ParamdefDataVersion == paramdef.DataVersion)
{
matchDefVersion = true;
bestRowsize = param.DetectedSize;
bestDefRowSize = paramdef.GetRowSize();
if (param.DetectedSize == -1 || param.DetectedSize == bestDefRowSize)
{
return ApplyDef(param, paramdef);
}
}
}
}
// Def could not be applied.
if (!matchType && !matchDefVersion)
throw new InvalidDataException($"Could not apply ParamDef for {param.ParamType}. Valid ParamDef could not be found.");
else if (matchType && !matchDefVersion)
throw new InvalidDataException($"Could not apply ParamDef for {param.ParamType}. Cannot find ParamDef version {param.ParamdefDataVersion}.");
else if (matchType && matchDefVersion)
throw new InvalidDataException($"Could not apply ParamDef for {param.ParamType}. Row sizes do not match. Param: {bestRowsize}, Def: {bestDefRowSize}.");
else
throw new Exception("Unhandled Apply ParamDef error.");
}
/// <summary>
/// Search an object's properties and return whichever object has the targeted property.
/// </summary>
/// <returns>Object that has the property, otherwise null.</returns>
public static object? FindPropertyObject(PropertyInfo prop, object obj, int classIndex = -1)
{
foreach (var p in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (p.MetadataToken == prop.MetadataToken)
return obj;
if (p.PropertyType.IsNested)
{
var retObj = FindPropertyObject(prop, p.GetValue(obj));
if (retObj != null)
return retObj;
}
else if (p.PropertyType.IsArray)
{
var pType = p.PropertyType.GetElementType();
if (pType.IsNested)
{
Array array = (Array)p.GetValue(obj);
if (classIndex != -1)
{
var retObj = FindPropertyObject(prop, array.GetValue(classIndex));
if (retObj != null)
return retObj;
}
else
{
foreach (var arrayObj in array)
{
var retObj = FindPropertyObject(prop, arrayObj);
if (retObj != null)
return retObj;
}
}
}
}
}
return null;
}
public static object? GetPropertyValue(PropertyInfo prop, object obj)
{
return prop.GetValue(FindPropertyObject(prop, obj));
}
/// <summary>
/// Loads a text resource.
/// </summary>
public static List<string[]> LoadTextResource(string path, int elementNum)
{
string[] file = File.ReadAllLines(path);
List<string[]> output = new();
for (var i = 0; i < file.Length; i++)
{
var line = file[i];
if (line.Contains("//"))
{
var index = line.IndexOf("//");
line = line.Remove(index);
}
if (string.IsNullOrWhiteSpace(line))
continue;
var split = line.Split("||");
if (split.Length != elementNum)
{
throw new Exception($"Text resource load error: \"{path}\" (Line {i + 1} has invalid formatting)");
}
output.Add(split);
}
return output;
}
}
}