-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModLoader.cs
More file actions
404 lines (320 loc) · 12.9 KB
/
ModLoader.cs
File metadata and controls
404 lines (320 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using ICSharpCode.SharpZipLib.Zip;
public class ModLoader : MonoBehaviour {
static readonly Dictionary<string, ZipFile> _loadedZips = new Dictionary<string, ZipFile>();
static readonly Dictionary<string, object> _assetCache = new Dictionary<string, object>();
static readonly Dictionary<string, byte[]> _binaryCache = new Dictionary<string, byte[]>();
public class SpriteConfig {
public float pixelsPerUnit = UIUtility.pixelsPerUnit;
public FilterMode filterMode = FilterMode.Point;
public TextureWrapMode wrapMode = TextureWrapMode.Clamp;
public Vector2 pivot = new Vector2(0.5f, 0.5f);
}
public static ZipFile LoadMod(string modName) {
string key = ModData.GetDefaultModPath(modName);
List<string> modPaths = ModData.GetModPaths(modName);
ZipFile zip = null;
foreach(string modPath in modPaths) {
zip = LoadZip(modPath, key);
if(zip != null) break;
}
return zip;
}
public static ZipFile LoadZip(string zipPath, string key = "") {
if(key == "") key = zipPath;
ZipFile zip;
if(_loadedZips.TryGetValue(key, out zip))
return zip;
if(!File.Exists(zipPath)) {
Debug.LogError("[ModLoader] Mod file not found: " + zipPath);
return null;
}
FileStream fs = File.OpenRead(zipPath);
zip = new ZipFile(fs);
_loadedZips[key] = zip;
//LogEntriesInZipFile(zip);
Debug.Log("[ModLoader] Mod file loaded: " + zipPath);
return zip;
}
public static byte[] LoadBytes(string zipPath, string entryName) {
string key = zipPath + "::" + entryName;
byte[] cached;
if(_binaryCache.TryGetValue(key, out cached))
return cached;
ZipFile zip = LoadMod(zipPath);
if(zip == null) return null;
ZipEntry entry = zip.GetEntry(entryName);
if(entry == null) {
Debug.LogWarning("[ModLoader] Entry not found: " + entryName + "(" + zipPath + ")");
return null;
}
Stream stream = zip.GetInputStream(entry);
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[4096];
int bytesRead;
while((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, bytesRead);
byte[] data = ms.ToArray();
ms.Close();
stream.Close();
_binaryCache[key] = data;
return data;
}
public static string LoadText(string zipPath, string entryName) {
byte[] data = LoadBytes(zipPath, entryName);
if(data == null) return null;
return System.Text.Encoding.UTF8.GetString(data);
}
public static Texture2D LoadTexture(string zipPath, string entryName) {
string key = zipPath + "::" + entryName;
object cachedObj;
if(_assetCache.TryGetValue(key, out cachedObj))
return cachedObj as Texture2D;
byte[] data = LoadBytes(zipPath, entryName);
if(data == null) return null;
Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
if(!tex.LoadImage(data)) {
Debug.LogError("[ModLoader] Texture load failed: " + entryName + "(" + zipPath + ")");
return null;
}
tex.name = entryName;
_assetCache[key] = tex;
return tex;
}
public static Sprite LoadSprite(string zipPath, string entryName, SpriteConfig config = null) {
string key = zipPath + "::" + entryName;
object cachedObj;
if(_assetCache.TryGetValue(key, out cachedObj))
return cachedObj as Sprite;
if(config == null) config = new SpriteConfig();
byte[] data = LoadBytes(zipPath, entryName);
if (data == null) return null;
Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
if (!tex.LoadImage(data)) {
Debug.LogError("[ModLoader] Sprite load failed: " + entryName + "(" + zipPath + ")");
return null;
}
tex.name = entryName;
tex.filterMode = config.filterMode;
tex.wrapMode = config.wrapMode;
Rect spriteRect = new Rect(0, 0, tex.width, tex.height);
Sprite sprite = Sprite.Create(tex, spriteRect, config.pivot, config.pixelsPerUnit);
_assetCache[key] = sprite;
return sprite;
}
public static T LoadData<T>(string zipPath, string entryName) {
string key = zipPath + "::" + entryName;
object cachedObj;
if(_assetCache.TryGetValue(key, out cachedObj))
return (T)cachedObj;
string json = LoadText(zipPath, entryName);
if(string.IsNullOrEmpty(json)) {
Debug.LogError("[ModLoader] Failed to load JSON data: " + entryName);
return default(T);
}
T obj = JsonUtil.Parse<T>(json);
_assetCache[key] = obj;
return obj;
}
public static void UnloadZip(string key) {
ZipFile zip;
if (_loadedZips.TryGetValue(key, out zip)) {
zip.Close();
_loadedZips.Remove(key);
Debug.Log("[ModLoader] Mod file unloaded: " + key);
}
RemoveCacheByPrefix(key + "::");
}
public static void UnloadAll() {
foreach(KeyValuePair<string, ZipFile> pair in _loadedZips)
pair.Value.Close();
_loadedZips.Clear();
foreach(KeyValuePair<string, object> pair in _assetCache) {
if(pair.Value is UnityEngine.Object) Destroy((UnityEngine.Object)pair.Value);
}
_assetCache.Clear();
_binaryCache.Clear();
Debug.Log("[ModLoader] All mod files unloaded");
}
static void RemoveCacheByPrefix(string prefix) {
List<string> keys = new List<string>();
foreach(KeyValuePair<string, object> pair in _assetCache) {
if (pair.Key.StartsWith(prefix))
keys.Add(pair.Key);
}
foreach(string k in keys) {
if(_assetCache[k] is UnityEngine.Object) Destroy((UnityEngine.Object)_assetCache[k]);
_assetCache.Remove(k);
}
keys.Clear();
foreach(KeyValuePair<string, byte[]> pair in _binaryCache) {
if(pair.Key.StartsWith(prefix))
keys.Add(pair.Key);
}
foreach(string k in keys)
_binaryCache.Remove(k);
}
public static IEnumerator LoadBytesAsync(string zipPath, string entryName, Action<byte[]> onComplete) {
string key = zipPath + "::" + entryName;
if(_binaryCache.ContainsKey(key)) {
onComplete(_binaryCache[key]);
yield break;
}
ZipFile zip = LoadZip(zipPath);
if(zip == null) {
onComplete(null);
yield break;
}
ZipEntry entry = zip.GetEntry(entryName);
if(entry == null) {
Debug.LogWarning("[ModLoader] Entry not found: " + entryName);
onComplete(null);
yield break;
}
Stream stream = zip.GetInputStream(entry);
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[4096];
int bytesRead = 0;
int frameGuard = 0;
while((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) {
ms.Write(buffer, 0, bytesRead);
frameGuard++;
if(frameGuard >= 8) {
frameGuard = 0;
yield return null;
}
}
byte[] data = ms.ToArray();
ms.Close();
stream.Close();
_binaryCache[key] = data;
onComplete(data);
}
public static IEnumerator LoadTextAsync(string zipPath, string entryName, Action<string> onComplete){
string key = zipPath + "::" + entryName;
if(_assetCache.ContainsKey(key)) {
onComplete(_assetCache[key] as string);
yield break;
}
byte[] data = null;
yield return LoadBytesAsync(zipPath, entryName, d => data = d);
if(data == null) {
onComplete(null);
yield break;
}
string text = System.Text.Encoding.UTF8.GetString(data);
if(string.IsNullOrEmpty(text)) {
Debug.LogWarning("[ModLoader] Text data is empty: " + entryName + "(" + zipPath + ")");
onComplete(text);
yield break;
}
_assetCache[key] = text;
onComplete(text);
}
public static IEnumerator LoadTextureAsync(string zipPath, string entryName, Action<Texture2D> onComplete) {
string key = zipPath + "::" + entryName;
if(_assetCache.ContainsKey(key)) {
onComplete((Texture2D)_assetCache[key]);
yield break;
}
byte[] data = null;
yield return LoadBytesAsync(zipPath, entryName, delegate(byte[] d) { data = d; });
if(data == null) {
onComplete(null);
yield break;
}
yield return null;
Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
tex.LoadImage(data);
tex.name = entryName;
_assetCache[key] = tex;
onComplete(tex);
}
public static IEnumerator LoadSpriteAsync(string zipPath, string entryName, Action<Sprite> onComplete, SpriteConfig config = null) {
string key = zipPath + "::" + entryName;
if(_assetCache.ContainsKey(key)) {
onComplete((Sprite)_assetCache[key]);
yield break;
}
if(config == null) config = new SpriteConfig();
Texture2D tex = null;
yield return LoadTextureAsync(zipPath, entryName, delegate(Texture2D t) { tex = t; });
if(tex == null) {
onComplete(null);
yield break;
}
tex.filterMode = config.filterMode;
tex.wrapMode = config.wrapMode;
Rect rect = new Rect(0, 0, tex.width, tex.height);
Sprite sprite = Sprite.Create(tex, rect, config.pivot, config.pixelsPerUnit);
_assetCache[key] = sprite;
onComplete(sprite);
}
public static IEnumerator LoadDataAsync<T>(string zipPath, string entryName, Action<T> onComplete) {
string key = zipPath + "::" + entryName;
if(_assetCache.ContainsKey(key)) {
onComplete((T)_assetCache[key]);
yield break;
}
byte[] data = null;
yield return LoadBytesAsync(zipPath, entryName, d => data = d);
if(data == null) {
onComplete(default(T));
yield break;
}
string json = System.Text.Encoding.UTF8.GetString(data);
if(string.IsNullOrEmpty(json)) {
Debug.LogWarning("[ModLoader] JSON data is empty: " + entryName + "(" + zipPath + ")");
onComplete(default(T));
yield break;
}
T obj = JsonUtil.Parse<T>(json);
_assetCache[key] = obj;
onComplete(obj);
}
public static IEnumerator LoadAllDataAsync<T>(string zipPath, string folderEntryName, Action<List<T>> onComplete) {
List<T> resultList = new List<T>();
ZipFile zip = LoadZip(zipPath);
if(zip == null) {
onComplete(resultList);
yield break;
}
List<ZipEntry> entries = new List<ZipEntry>();
foreach(ZipEntry entry in zip) {
if(!entry.IsFile) continue;
if(entry.Name.StartsWith(folderEntryName)) entries.Add(entry);
}
foreach(ZipEntry entry in entries) {
byte[] data = null;
yield return LoadBytesAsync(zipPath, entry.Name, d => data = d);
if(data == null) {
Debug.LogWarning("[ModLoader] Failed to load file: " + entry.Name);
continue;
}
string json = System.Text.Encoding.UTF8.GetString(data);
if(string.IsNullOrEmpty(json)) {
Debug.LogWarning("[ModLoader] JSON data is empty: " + entry.Name);
continue;
}
T obj = JsonUtil.Parse<T>(json);
string key = zipPath + "::" + entry.Name;
_assetCache[key] = obj;
resultList.Add(obj);
yield return null;
}
onComplete(resultList);
}
public static void LogLoadingFailed(Identifier id, string type) {
Debug.Log("[ModLoader] Cannot load asset " + id.ToString() + "(" + type + ")");
}
public static void LogEntriesInZipFile(ZipFile zip) {
Debug.Log("[ModLoader] Listing all entries...");
foreach(ZipEntry zipEntry in zip)
Debug.Log(zipEntry.Name);
Debug.Log("----------");
}
}