-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUtils.cs
478 lines (402 loc) · 17.4 KB
/
Utils.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Planetbase;
using PlanetbaseFramework.Extensions;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
namespace PlanetbaseFramework
{
// TODO this class is a mess, clean it up
public static class Utils
{
/// <summary>
/// The default texture to use when one fails to load
/// </summary>
public static Texture2D ErrorTexture { get; internal set; }
/// <summary>
/// Load the a XML file containing strings (for localization/translations)
/// </summary>
/// <param name="absolutePath">The absolute path to the XML file</param>
public static void LoadStringsFromFile(string absolutePath)
{
//Setup the deserializer
XmlSerializer xmlDeserializer;
try
{
xmlDeserializer = new XmlSerializer(typeof(StringFile));
}
catch (Exception e)
{
Debug.Log($"Unable to create a deserializer for type \"{typeof(StringFile).Name}\"");
LogException(e);
return;
}
var nameTable = new NameTable();
var namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("", "");
var parserContext = new XmlParserContext(nameTable, namespaceManager, "", XmlSpace.Default);
var readerSettings = new XmlReaderSettings
{
NameTable = nameTable,
ValidationFlags = XmlSchemaValidationFlags.None
};
XmlReader reader = null;
try
{
//Create the reader
reader = XmlReader.Create(absolutePath, readerSettings, parserContext);
}
catch (Exception e)
{
Debug.Log(
$"Exception thrown while attempting to create a stream reader for \"{absolutePath}\". Exception thrown: "
);
Debug.Log(e);
if (reader == null)
return;
// Attempt cleanup
try
{
reader.Close();
}
catch (Exception)
{
// The reader may or may not be in a closeable state if an exception occurred while creating it.
// Attempt to close and assume if an exception is thrown then it was not in a closeable state.
}
return;
}
using (reader)
{
//Read and deserialize the file the file
StringFile deserializedStrings;
try
{
deserializedStrings = xmlDeserializer.Deserialize(reader) as StringFile;
}
catch (Exception e)
{
Debug.Log($"Unable to deserialize file \"{absolutePath}\"");
LogException(e);
return;
}
if (deserializedStrings?.Strings == null)
{
Debug.Log(
$"\"{absolutePath}\" is not recognized as a valid strings file. Please check your syntax."
);
return;
}
foreach (var loadedString in deserializedStrings.Strings)
{
//Add the strings to the list
StringList.mStrings.Add(loadedString.Key, loadedString.Value);
//Add loading hints
if (loadedString.Key.Contains("loading_hint"))
{
StringList.mLoadingHints.Add(loadedString.Value);
}
}
StringList.mLoadedFiles.Add(absolutePath);
Debug.Log(
$"Successfully loaded {deserializedStrings.Strings.Length} string(s) from \"{absolutePath}\""
);
}
StringList.loadFile(absolutePath, StringList.mStrings, false);
}
/// <summary>
/// Load a PNG into a Texture2D object
/// </summary>
/// <param name="absolutePath">The absolute path to the PNG file</param>
public static Texture2D LoadPngFromFile(string absolutePath)
{
Texture2D loadedTexture;
if (File.Exists(absolutePath))
{
var fileData = File.ReadAllBytes(absolutePath);
loadedTexture = new Texture2D(2, 2); //TODO fix this to be of arbitrary size
loadedTexture.LoadImage(fileData); //..this will auto-resize the texture dimensions.
loadedTexture.name = Path.GetFileName(absolutePath);
}
else
{
Debug.Log($"Error loading texture: \"{absolutePath}\"");
loadedTexture = ErrorTexture;
}
return loadedTexture;
}
/// <summary>
/// Set the normal map on a texture. This should be called on any normal maps either at the init stage or the constructor of the mod.
/// </summary>
/// <param name="texture">The texture to update.</param>
// ReSharper disable once UnusedMember.Global
public static void SetNormalMap(this Texture2D texture)
{
var pixels = texture.GetPixels();
for (var i = 0; i < pixels.Length; i++)
{
var temp = pixels[i];
temp.r = pixels[i].g;
temp.a = pixels[i].r;
pixels[i] = temp;
}
texture.SetPixels(pixels);
}
public static T FindObjectByFilename<T>(this List<T> list, string filename) where T : Object
{
try
{
return list.Find(x => x.name == filename);
}
catch (Exception e)
{
Debug.Log($"Error loading file: \"{filename}\" with type: \"typeof(T)\"");
LogException(e);
return null;
}
}
public static T FindObjectByFilepath<T>(this List<T> list, string filepath) where T : Object
{
return FindObjectByFilename(list, Path.GetFileName(filepath));
}
public static bool IsValidTag(this string toCheck)
{
try
{
GameObject.FindGameObjectsWithTag(toCheck);
return true;
} catch(UnityException)
{
return false;
}
}
// ReSharper disable once UnusedMember.Global
public static bool Compare(this Type t1, Type t2) => t1.FullName != null && t1.FullName.Equals(t2.FullName);
// ReSharper disable once UnusedMember.Global
public static string[] ListEmbeddedFiles()
{
var assembly = Assembly.GetCallingAssembly();
return assembly.GetManifestResourceNames();
}
public static string GetFileNameFromAssemblyResourceName(string embeddedResourceName)
{
var split = embeddedResourceName.Split('.');
return split[split.Length - 2] + '.' + split[split.Length - 1];
}
// ReSharper disable once UnusedMember.Global
public static string[] GetFileNamesFromAssemblyResourceNames(string[] embeddedResourceNames)
{
var fileNames = new string[embeddedResourceNames.Length];
for (var i = 0; i < embeddedResourceNames.Length; i++)
fileNames[i] = GetFileNameFromAssemblyResourceName(embeddedResourceNames[i]);
return fileNames;
}
// ReSharper disable once UnusedMember.Global
public static Stream LoadEmbeddedFile(string filePath)
{
try
{
var assembly = Assembly.GetCallingAssembly();
return assembly.GetManifestResourceStream(filePath);
} catch(Exception e)
{
Debug.Log($"Error loading resource \"{filePath}\" from stream");
LogException(e);
return null;
}
}
public static void LogException(Exception e, int tabCount = 0)
{
Debug.Log("Exception thrown:".PadLeft(4 * tabCount));
Debug.Log(e.ToString().PadLeft(4 * tabCount));
if (e.InnerException != null)
{
Debug.Log("Inner exception: ".PadLeft(4 * tabCount));
LogException(e.InnerException, tabCount + 1);
}
if (e.GetType() != typeof(ReflectionTypeLoadException))
return;
Debug.Log("Loader exceptions:");
foreach (var loaderException in ((ReflectionTypeLoadException) e).LoaderExceptions)
LogException(loaderException);
}
// ReSharper disable once UnusedMember.Global
public static void KeyValuePairToDictionary<TK, TV>(this Dictionary<TK, TV> dictionary, KeyValuePair<TK, TV> kvp)
{
dictionary.Add(kvp.Key, kvp.Value);
}
// ReSharper disable once UnusedMember.Global
[Obsolete("Use PlanetbaseFramework.Extensions.GameObjectExtensions.AddCollisionGeometry instead. Will be removed in a future version.")]
public static void AddCollision(this GameObject gameObject)
{
gameObject.AddCollisionGeometry();
}
// ReSharper disable once UnusedMember.Global
public static Texture2D FindTextureWithName(this List<Texture2D> textures, string name)
{
foreach (var texture in textures)
if (texture.name.Equals(name))
return texture;
Debug.Log($"Couldn't find texture with filename \"{name}\"");
return ErrorTexture;
}
// ReSharper disable once UnusedMember.Global
public static List<Type> GetTypeByName(string className)
{
var matchingTypes = new List<Type>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (var type in assembly.GetTypes())
if (className.IndexOf('.') != -1) //Presence of a '.' indicates that the classname includes the namespace
if (type.FullName.Equals(className))
matchingTypes.Add(type);
else
if (type.Name.Equals(className))
matchingTypes.Add(type);
return matchingTypes;
}
// ReSharper disable once UnusedMember.Global
public static int ToInt(this bool toConvert)
{
return toConvert ? 1 : 0;
}
//Credit goes to https://stackoverflow.com/a/3446112/4352225
public static string GetObjectPropertyValues(object @object)
{
var logString = "Object properties: ";
foreach(var property in @object.GetType().GetProperties())
logString += $"\r\n{property.Name}: " + (property.GetValue(@object, null) ?? "NULL");
return logString;
}
public static string GetObjectPropertyValues(GameObject gameObject)
{
var logString = "GameObject properties: ";
logString += GetObjectPropertyValues((object) gameObject);
logString += "\r\nComponents: ";
foreach (var component in gameObject.GetComponents(typeof(Component)))
{
logString += "\r\n" + component.GetType().Name + ": ";
logString += GetObjectPropertyValues(component);
}
foreach (Transform subTransform in gameObject.transform)
{
logString += "\r\nSub-object properties: ";
logString += GetObjectPropertyValues((object) subTransform.gameObject);
logString += "\r\nComponents: ";
foreach (var component in subTransform.gameObject.GetComponents(typeof(Component)))
{
logString += "\r\n" + component.GetType().Name + ": ";
logString += GetObjectPropertyValues(component);
}
}
return logString;
}
// ReSharper disable once UnusedMember.Global
public static void LogObjectProperties(object @object)
{
Debug.Log(GetObjectPropertyValues(@object));
}
// ReSharper disable once UnusedMember.Global
public static void LogObjectProperties(GameObject gameObject)
{
Debug.Log(GetObjectPropertyValues(gameObject));
}
// ReSharper disable once UnusedMember.Global
public static void RecursivelyAddColliders(this Transform t)
{
foreach (Transform transform in t)
{
if (transform.gameObject.GetComponent<MeshFilter>() != null)
transform.gameObject.AddComponent<MeshCollider>().sharedMesh =
transform.gameObject.GetComponent<MeshFilter>().sharedMesh;
if (t.childCount <= 0)
continue;
foreach (Transform subTransform in t)
subTransform.RecursivelyAddColliders();
}
}
// ReSharper disable once UnusedMember.Global
public static FrameworkMod GetFrameworkMod() =>
ModLoader.ModList.Find(mod => mod.ModName.Equals("Planetbase Framework")) as FrameworkMod;
public static void CopyTo(this Stream input, Stream output)
{
var buffer = new byte[16 * 1024]; // Fairly arbitrary size
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
output.Write(buffer, 0, bytesRead);
}
public static Assembly LoadAssembly(string filePath)
{
Assembly loadedAssembly = null;
try
{
loadedAssembly = Assembly.LoadFile(filePath);
// This call will usually throw an exception if the assembly load failed to some reason
loadedAssembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
LogException(e);
// If you're reading this then you're probably wondering why I would compile against .Net 3.5 instead of 2.0.5.0. I've found that
// some .Net 3.5 functions can be executed, but only if they don't depend on CLR features introduced after 2.0.5.0. Compiling against
// .Net 3.5 allows mod makers to import newer libraries (like all the ones I'm using here) despite running on .Net 2.0.5.0 CLR.
Debug.Log(
"************************ Note to mod makers: If you're seeing this exception, you probably are using a a post .Net 2.0.5.0 function.\r\n" +
"For convenience I've made it so you can use mods compiled after 2.0.5.0, however modern features are not available. ************************"
);
}
catch (Exception e)
{
LogException(e);
}
return loadedAssembly;
}
// ReSharper disable once UnusedMember.Global
public static Type GetCallingModType<T>(int skipFrameCount = 0) where T : ModBase
{
// Starting at 1 because we know the current frame won't be a ModBase class in any case
var stacktrace = new StackTrace(skipFrameCount + 1);
for (var i = 0; i < stacktrace.FrameCount; i++)
{
var frame = stacktrace.GetFrame(i);
var method = frame.GetMethod();
var stackFrameType = method.DeclaringType;
if (stackFrameType == null || !stackFrameType.IsSubclassOf(typeof(T)))
continue;
return stackFrameType;
}
return null;
}
public static bool ArePathsEqual(this string firstPath, string secondPath)
{
return string.Compare(
Path.GetFullPath(firstPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar),
Path.GetFullPath(secondPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar),
SystemInfo.operatingSystem.StartsWith("Windows")
? StringComparison.InvariantCultureIgnoreCase
: StringComparison.CurrentCulture
) == 0;
}
// The chosen type here (`Planetbase.Constants') is arbitrary and can be any type in the Planetbase
// assembly
public static bool IsBaseGameType(Type type) => type.Assembly.Equals(typeof(Planetbase.Constants).Assembly);
public static bool IsBaseGameType(object instance) => IsBaseGameType(instance.GetType());
public static bool IsBaseGameType<T>() => IsBaseGameType(typeof(T));
/// <summary>
/// Throws an exception if the type "T" is not in the "Planetbase" namespace
/// </summary>
public static void ThrowIfNotBaseGameType<T>()
{
var genericType = typeof(T);
if (!IsBaseGameType(genericType))
throw new Exception(
$"the provided type {genericType.FullName} is not a base game type");
}
}
}