forked from solidDoWant/Planetbase-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
387 lines (324 loc) · 12.9 KB
/
Utils.cs
File metadata and controls
387 lines (324 loc) · 12.9 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Planetbase;
using UnityEngine;
namespace PlanetbaseFramework
{
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
};
try
{
//Create the reader
using (var reader = XmlReader.Create(absolutePath, readerSettings, parserContext))
{
//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}\""
);
}
}
catch (Exception e)
{
Debug.Log(
$"Exception thrown while attempting to create a stream reader for \"{absolutePath}\". Exception thrown: "
);
Debug.Log(e);
return;
}
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>
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 : UnityEngine.Object
{
try
{
return list.Find(x => x.name == filename);
}
catch (Exception e)
{
Debug.Log($"Error loading file: \"{filename}\" with type: \"typeof(T)\"");
Debug.Log("Stacktrace: ");
Debug.Log(e.ToString());
return null;
}
}
public static T FindObjectByFilepath<T>(this List<T> list, string filepath) where T : UnityEngine.Object
{
return FindObjectByFilename(list, Path.GetFileName(filepath));
}
public static bool IsValidTag(this string toCheck)
{
try
{
GameObject.FindGameObjectsWithTag(toCheck);
return true;
} catch(UnityException)
{
return false;
}
}
public static bool Compare(this Type t1, Type t2) => t1.FullName != null && t1.FullName.Equals(t2.FullName);
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];
}
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;
}
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) return;
Debug.Log("Inner exception: ".PadLeft(4 * tabCount));
LogException(e.InnerException, tabCount + 1);
}
public static void KeyValuePairToDictionary<TK, TV>(this Dictionary<TK, TV> dictionary, KeyValuePair<TK, TV> kvp)
{
dictionary.Add(kvp.Key, kvp.Value);
}
public static void AddCollision(this GameObject gameObject)
{
if (gameObject.GetComponent<MeshFilter>() != null)
{
gameObject.AddComponent<MeshCollider>().sharedMesh = gameObject.GetComponent<MeshFilter>().sharedMesh;
}
}
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;
}
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;
}
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;
}
public static void LogObjectProperties(object @object)
{
Debug.Log(GetObjectPropertyValues(@object));
}
public static void LogObjectProperties(GameObject gameObject)
{
Debug.Log(GetObjectPropertyValues(gameObject));
}
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)
{
foreach (Transform subTransform in t)
{
subTransform.RecursivelyAddColliders();
}
}
}
}
public static FrameworkMod GetFrameworkMod() =>
ModLoader.ModList.Find(mod => mod.ModName.Equals("Planetbase Framework")) as FrameworkMod;
public static void CopyTo(this Stream input, Stream output)
{
byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}