-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAssetPackLoaderSystem.cs
830 lines (740 loc) · 35.1 KB
/
AssetPackLoaderSystem.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Colossal.IO.AssetDatabase;
using Colossal.PSI.Common;
using Colossal.PSI.Environment;
using Game;
using Colossal.Serialization.Entities;
using Colossal.UI;
using Game.Modding;
using Game.Prefabs;
using Game.PSI;
using Game.SceneFlow;
using Game.UI.Localization;
using Game.UI.Menu;
using UnityEngine;
using Hash128 = Colossal.Hash128;
using StreamReader = System.IO.StreamReader;
namespace AssetPacksManager
{
public partial class AssetPackLoaderSystem : GameSystemBase
{
private static PrefabSystem _prefabSystem;
private static NotificationUISystem _notificationUISystem;
// Each mod has a dict entry that contains the missing cid prefabs
private static readonly string[] SupportedThumbnailExtensions = { ".png", ".svg", ".jpg" };
private static readonly string ThumbnailDir = EnvPath.kUserDataPath + "/ModsData/AssetPacksManager/thumbnails";
private static ApmLogger Logger;
public static AssetPackLoaderSystem Instance;
private static MonoComponent _monoComponent;
private readonly GameObject _monoObject = new();
public static bool AssetsLoaded = false;
private static DateTime _assetLoadStartTime;
private static string LoadedAssetPacksText { get; set; } = "";
private static NotificationUISystem.NotificationInfo _adaptiveAssetsNotification;
private static readonly List<AssetPack> AssetPacks = new();
protected override void OnCreate()
{
base.OnCreate();
Instance = this;
Enabled = false;
_prefabSystem = World.GetOrCreateSystemManaged<PrefabSystem>();
_notificationUISystem = World.GetOrCreateSystemManaged<NotificationUISystem>();
_monoComponent = _monoObject.AddComponent<MonoComponent>();
Logger = ApmLogger.Instance;
// VERY old CustomAssets folder migration, shouldn't be needed anymore, but just in case
CheckForMigration();
_notificationUISystem.AddOrUpdateNotification(
$"APM-NoLoad",
title: "Asset Packs Manager not loaded",
text: "Click here to load Asset Packs Manager.",
progressState: ProgressState.Indeterminate,
progress: 0,
thumbnail: "coui://apm/game_crash_warning.svg",
onClicked: Initialize
);
if (!Directory.Exists(ThumbnailDir))
Directory.CreateDirectory(ThumbnailDir);
UIManager.defaultUISystem.AddHostLocation("customassets", ThumbnailDir, false);
if (Setting.Instance.ShowWarningForLocalAssets)
{
int localAssets = FindLocalAssets($"{EnvPath.kUserDataPath}");
if (localAssets > 0)
{
NotificationSystem.Pop("APM-local", 30f, "Local Assets Found",
$"Found {localAssets} local assets in the user folder. These are loaded automatically.");
}
}
if (!Setting.Instance.DisableSettingsWarning)
{
if (Setting.Instance.AdaptiveAssetLoading)
{
_adaptiveAssetsNotification = _notificationUISystem.AddOrUpdateNotification(
$"{nameof(AssetPacksManager)}.{nameof(AssetPackLoaderSystem)}.{nameof(DisableAdaptiveLoading)}",
title: "Seeing gray boxes or missing assets? Click here",
progressState: ProgressState.Warning,
thumbnail: "coui://apm/notify_icon.png",
text: "You can disable this hint in the settings",
progress: 0, onClicked: DisableAdaptiveLoading);
}
else
{
_adaptiveAssetsNotification = _notificationUISystem.AddOrUpdateNotification(
$"{nameof(AssetPacksManager)}.{nameof(AssetPackLoaderSystem)}.{nameof(EnableAdaptiveLoading)}",
title: "Seeing duplicate assets? Click here",
progressState: ProgressState.Warning,
thumbnail: "coui://apm/notify_icon.png",
text: "You can disable this hint in the settings",
progress: 0, onClicked: EnableAdaptiveLoading);
}
}
GameManager.instance.RegisterUpdater(Initialize);
}
/// <summary>
/// First time initialization of the AssetPackLoaderSystem, loads asset packs if enabled
/// </summary>
private void Initialize()
{
_notificationUISystem.RemoveNotification("APM-NoLoad");
Logger.Info("Initializing Asset Pack Loader System");
if (Setting.Instance.EnableAssetPackLoadingOnStartup)
{
Logger.Info("Loading Asset Packs on Startup");
LoadAssetPacks();
}
else
{
_ = TelemetryTransmitter.SubmitAsync(-1, -1, -1, Setting.Instance.AdaptiveAssetLoading);
Logger.Info("Asset Pack Loading on Startup is disabled");
}
}
/// <summary>
/// If player still has the old CustomAssets folder, move it to the desktop and notify the player.
/// This is to prevent the game from loading these assets by itself.
/// </summary>
private void CheckForMigration()
{
try
{
string customAssetsDir = $"{EnvPath.kUserDataPath}/CustomAssets";
if (Directory.Exists(customAssetsDir))
{
var migrationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"CustomAssets_backup");
Directory.Move(customAssetsDir, migrationPath);
NotificationSystem.Push("APM-legacy", "Custom Assets folder migrated, restart game",
"The Custom Assets is no longer being used and has been moved to Desktop. Please restart the game");
}
}
catch (Exception x)
{
var migrationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"CustomAssets_backup");
NotificationSystem.Push("APM-legacy", "Error migrating Custom Assets folder",
$"Please delete {migrationPath} manually.");
Logger.Error("Error moving Custom Assets folder: " + x.Message);
}
}
public static LocalizedString GetLoadedAssetPacksText()
{
return LocalizedString.IdWithFallback("APM-LoadedAssetPacks", LoadedAssetPacksText);
}
public void LoadAssetPacks()
{
if (AssetsLoaded)
{
Logger.Info("Assets are already loaded, skipping...");
return;
}
_monoComponent.StartCoroutine(CollectAssets());
AssetsLoaded = true;
}
public static void DeleteModsWithMissingCid()
{
int successfulDeletions = 0;
foreach (var pack in AssetPacks)
{
if (pack.MissingCids.Count == 0)
continue;
foreach (string key in pack.MissingCids)
{
try
{
string path = $"{EnvPath.kUserDataPath}/.cache/Mods/mods_subscribed/{key}";
if (Directory.Exists(path))
{
Directory.Delete(path, true);
Logger.Info($"Deleted mod cache for {key}");
successfulDeletions++;
}
}
catch (Exception x)
{
Logger.Error($"Error deleting mod cache for mod {{key}}: {x.Message}");
}
}
}
NotificationSystem.Push("APM-delete", "Deleted Mods",
$"Deleted {successfulDeletions} mods with missing CID. Click to close game.",
onClicked: CloseGame);
}
private static int FindLocalAssets(string currentDir)
{
int localAssets = 0;
var currentDirectory = new DirectoryInfo(currentDir);
if (currentDirectory.Name.StartsWith("."))
return 0;
if (!Directory.Exists(currentDir))
{
return 0;
}
foreach (var dir in Directory.GetDirectories(currentDir))
{
localAssets += FindLocalAssets(dir);
}
foreach (var file in Directory.GetFiles(currentDir))
{
if (file.EndsWith(".Prefab"))
{
localAssets++;
}
}
return localAssets;
}
public static void OpenLogFile()
{
Logger.OpenLogFile();
}
private static IEnumerator CollectAssets()
{
if (!Setting.Instance.EnableLocalAssetPacks && !Setting.Instance.EnableSubscribedAssetPacks)
{
NotificationSystem.Pop("APM-status", 30f, "Asset Packs Disabled",
"Both local and subscribed asset packs are disabled. No assets will be loaded.");
yield break;
}
_assetLoadStartTime = DateTime.Now;
var notificationInfo = _notificationUISystem.AddOrUpdateNotification(
$"{nameof(AssetPacksManager)}.{nameof(AssetPackLoaderSystem)}.{nameof(CollectAssets)}",
title: "Step 1/3: Collecting Asset Packs",
progressState: ProgressState.Indeterminate,
thumbnail: "coui://apm/notify_icon.png",
progress: 0);
int currentIndex = 0;
int packsFound = 0;
var assetFinderStartTime = DateTime.Now;
foreach (var modInfo in GameManager.instance.modManager)
{
var assemblyName = modInfo.name.Split(',')[0];
Logger.Debug($"Checking mod {assemblyName}");
var modDir = Path.GetDirectoryName(modInfo.asset.path);
var modId = $"{modInfo.asset.subPath.Split('/')[1].Split('_')[0]}";
var mod = new DirectoryInfo(modDir);
if (modDir == null)
continue;
if (assemblyName == "CustomAssetPack")
{
Logger.Warn($"Mod {modInfo.asset.name} is using default name");
NotificationSystem.Push(Guid.NewGuid().ToString(), title: $"Mod {mod.Name} is using default name",
text:
$"Please contact the developer of this mod to change the assembly name to something unique");
}
if (modInfo.asset.isEnabled)
{
var assetDir = new DirectoryInfo(Path.Combine(modDir, "assets"));
if (assetDir.Exists)
{
notificationInfo = _notificationUISystem.AddOrUpdateNotification(
$"{nameof(AssetPacksManager)}.{nameof(AssetPackLoaderSystem)}.{nameof(CollectAssets)}",
title: "Step 1/3: Collecting Asset Packs",
text: $"Collecting: {modInfo.asset.name}",
progressState: ProgressState.Progressing,
thumbnail: "coui://apm/notify_icon.png",
progress: (int)(currentIndex / (float)GameManager.instance.modManager.Count() * 100));
AssetPack currentPack = new AssetPack()
{
Path = modDir,
AssetPath = assetDir.FullName,
Name = assemblyName,
};
if (int.TryParse(modId, out int id))
{
currentPack.ID = id;
}
var localModsPath = Path.Combine(EnvPath.kUserDataPath, "Mods").Replace("/", "\\");
if (modDir.Contains(localModsPath))
{
currentPack.Type = AssetPackType.Local;
if (!Setting.Instance.EnableLocalAssetPacks)
{
Logger.Debug($"Skipping local mod {assemblyName} (" + modInfo.name + ")");
continue;
}
}
if (!Setting.Instance.EnableSubscribedAssetPacks)
continue;
Logger.Debug($"Collecting assets from {currentPack.Name} (" + currentPack.ID + ")");
currentPack.AddAssetFiles(GetPrefabsFromDirectoryRecursively(currentPack.AssetPath, currentPack));
AssetPacks.Add(currentPack);
Logger.Debug($"Found {currentPack.AssetFiles.Count} assets from mod {currentPack.Name}");
packsFound++;
}
//var modTimeEnd = DateTime.Now - modTime;
//Logger.Info("Mod Time: " + modTimeEnd.TotalMilliseconds + "ms");
}
else
{
Logger.Debug($"Skipping disabled mod {modInfo.name} (" + modInfo.name + ")");
}
currentIndex++;
yield return null;
}
Setting.LoadedAssetPacksTextVersion++;
SkyveInterface.CheckPlaysetStatus(AssetPacks);
foreach(AssetPack pack in AssetPacks)
{
if (pack.Stability == PackageStability.Broken)
{
ShowAssetPackWarning(pack);
}
}
WriteLoadedPacks();
_notificationUISystem.RemoveNotification(
identifier: notificationInfo.id,
delay: 1f,
text: $"Asset Pack collection complete. Found {packsFound} Asset Packs",
progressState: ProgressState.Complete,
progress: 100
);
var assetFinderEndTime = DateTime.Now - assetFinderStartTime;
Logger.Info("Asset Collection Time: " + assetFinderEndTime.TotalMilliseconds + "ms");
Logger.Debug("All mod prefabs have been collected. Adding to database now.");
_monoComponent.StartCoroutine(PrepareAssets());
foreach (var pack in AssetPacks)
{
if (pack.MissingCids.Count == 0)
continue;
NotificationSystem.Pop($"MissingCID_{pack.ID}", 300f, title: $"Missing CID for {pack.MissingCids.Count} prefabs",
text: $"{pack.Name} has {pack.MissingCids.Count} missing CIDs. Click to delete cache",
onClicked: DeleteModsWithMissingCid);
}
}
private static string ConvertCamelCaseToSpaces(string input)
{
input = input.Replace("_", "");
return Regex.Replace(input, "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])", " $1");
}
private static void ShowAssetPackWarning(AssetPack pack)
{
Logger.Warn($"Broken asset pack detected: {pack.Name} ({pack.ID}). No support will be provided unless this pack is removed from the playset.");
_notificationUISystem.AddOrUpdateNotification(
$"Broken_{pack.ID}",
title: "Broken Asset Pack",
text: $"{pack.Name} is {pack.Stability}, no support will be provided",
progressState: ProgressState.Warning,
onClicked:() => _notificationUISystem.RemoveNotification($"Broken_{pack.ID}"));
}
private static void WriteLoadedPacks()
{
List<AssetPack> sortedPacks = new(AssetPacks);
sortedPacks.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal));
foreach (var pack in sortedPacks)
{
string id = pack.ID == 0 ? "Local" : pack.ID.ToString();
LoadedAssetPacksText += $"[{pack.Stability}] {ConvertCamelCaseToSpaces(pack.Name)} ({id}) ({pack.AssetFiles.Count} Assets)\n";
}
Logger.Info($"Loaded Asset Packs: \n{LoadedAssetPacksText}");
}
private static int _loaded;
private static int _skipped;
private static int _autoLoaded;
private static int _notLoaded;
private static IEnumerator PrepareAssets()
{
var notificationInfo = _notificationUISystem.AddOrUpdateNotification(
$"{nameof(AssetPacksManager)}.{nameof(AssetPackLoaderSystem)}.{nameof(PrepareAssets)}",
title: "Step 2/3: Preparing Assets",
progressState: ProgressState.Indeterminate,
thumbnail: "coui://apm/notify_icon.png",
progress: 0);
int currentIndex = 0;
_loaded = 0;
_skipped = 0;
_autoLoaded = 0;
_notLoaded = 0;
var assetDatabaseStartTime = DateTime.Now;
foreach (var pack in AssetPacks)
{
notificationInfo = _notificationUISystem.AddOrUpdateNotification(
$"{nameof(AssetPacksManager)}.{nameof(AssetPackLoaderSystem)}.{nameof(PrepareAssets)}",
title: "Step 2/3: Preparing Assets",
text: $"Preparing Pack: {pack.Name}",
progressState: ProgressState.Progressing,
thumbnail: "coui://apm/notify_icon.png",
progress: (int)(currentIndex / (float)pack.AssetFiles.Count * 100));
var modStartTime = DateTime.Now;
foreach (var file in pack.AssetFiles)
{
try
{
Logger.Debug("Loading File: " + file.FullName);
var absolutePath = file.FullName;
// Replace backslashes with forward slashes
absolutePath = absolutePath.Replace('\\', '/');
// get relative path from absolute path
var relativePath = absolutePath.Replace(EnvPath.kUserDataPath + "/", "");
// Remove content after last / from relative path
relativePath = relativePath.Substring(0, relativePath.LastIndexOf('/'));
var fileName = Path.GetFileNameWithoutExtension(file.FullName);
var path = AssetDataPath.Create(relativePath, fileName);
var cidFilename = EnvPath.kUserDataPath + "\\" + relativePath + "\\" + fileName + ".Prefab.cid";
using StreamReader sr = new StreamReader(cidFilename);
var guid = sr.ReadToEnd();
sr.Close();
//The game automatically loads assets from the PDX Mods folder in the AssetDatabase.PDX_MODS (dynamic) database
if (Setting.Instance.AdaptiveAssetLoading)
{
if (AssetDatabase.global.TryGetAsset(Hash128.Parse(guid), out var asset))
{
_autoLoaded++;
if (asset.state != LoadState.NotLoaded)
{
// TODO: Find out why some assets are already loaded
Logger.Debug("Asset already loaded: " + asset.name);
}
else
{
Logger.Debug("Prefab asset already in database");
}
}
}
else
{
AssetDatabase.user.AddAsset<PrefabAsset>(path, Hash128.Parse(guid));
Logger.Debug("Prefab added to database successfully");
}
}
catch (Exception e)
{
Logger.Warn($"Asset {file} could not be loaded: {e.Message}");
}
}
var modEndTime = DateTime.Now - modStartTime;
Logger.Debug($"Mod Time for {pack.Name}: {modEndTime.TotalMilliseconds} ms (average {pack.AssetFiles.Count / modEndTime.TotalMilliseconds} ms per asset)");
currentIndex++;
yield return null;
}
//WriteAnalysisInfo();
var assetDatabaseEndTime = DateTime.Now - assetDatabaseStartTime;
Logger.Info("Asset Database Time: " + assetDatabaseEndTime.TotalMilliseconds + "ms");
_notificationUISystem.RemoveNotification(
identifier: notificationInfo.id,
delay: 1f,
text: $"Asset Preparing complete.",
progressState: ProgressState.Complete,
progress: 100
);
_monoComponent.StartCoroutine(LoadAssets());
}
private static IEnumerator LoadAssets()
{
var notificationInfo = _notificationUISystem.AddOrUpdateNotification(
$"{nameof(AssetPacksManager)}.{nameof(AssetPackLoaderSystem)}.{nameof(LoadAssets)}",
title: "Step 3/3: Loading Assets",
progressState: ProgressState.Indeterminate,
thumbnail: "coui://apm/notify_icon.png",
progress: 0);
int currentIndex = 0;
var prefabSystemStartTime = DateTime.Now;
var allPrefabs = AssetDatabase.user.GetAssets<PrefabAsset>();
var prefabAssets = allPrefabs as PrefabAsset[] ?? allPrefabs.ToArray();
Dictionary<string, int> times = new();
foreach (PrefabAsset prefabAsset in prefabAssets)
{
try
{
if (prefabAsset.path.Contains(EnvPath.kGameDataPath))
continue;
var prefabStartTime = DateTime.Now;
notificationInfo = _notificationUISystem.AddOrUpdateNotification(
$"{nameof(AssetPacksManager)}.{nameof(AssetPackLoaderSystem)}.{nameof(LoadAssets)}",
title: "Step 3/3: Loading Assets",
text: $"Loading: {prefabAsset.name}",
progressState: ProgressState.Progressing,
thumbnail: "coui://apm/notify_icon.png",
progress: (int)(currentIndex / (float)prefabAssets.Count() * 100));
Logger.Debug($"Asset Name: {prefabAsset.name}, Path: {prefabAsset.path}");
var prefabBaseTime = DateTime.Now;
PrefabBase prefabBase = prefabAsset.Load() as PrefabBase;
var prefabBaseEndTime = DateTime.Now - prefabBaseTime;
Logger.Debug("Loaded Prefab");
var prefabAddTime = DateTime.Now;
_prefabSystem.AddPrefab(prefabBase, null, null, null);
Logger.Debug($"Added {prefabAsset.name} to Prefab System");
var prefabAddEndTime = DateTime.Now - prefabAddTime;
//Logger.Debug($"Added {prefabAsset.name} to Prefab System");
_loaded++;
var prefabEndTime = DateTime.Now - prefabStartTime;
Logger.Debug("Prefab Time: " + prefabEndTime.TotalMilliseconds + "ms");
Logger.Debug("Prefab Add Time: " + prefabAddEndTime.TotalMilliseconds + "ms");
if (times.ContainsKey(prefabAsset.name))
{
times[prefabAsset.name] += (int)prefabEndTime.TotalMilliseconds;
}
else
{
times.Add(prefabAsset.name, (int)prefabEndTime.TotalMilliseconds);
}
}
catch (Exception e)
{
_notLoaded++;
Logger.Info(
$"Please see AssetPacksManager Log for details. Asset {prefabAsset.name} could not be added to Database: {e.Message}Path: {prefabAsset.path}\nUnique Name: {prefabAsset.uniqueName}\nCID: {prefabAsset.guid}\nSubPath: {prefabAsset.subPath}");
}
currentIndex++;
yield return null;
}
var prefabSystemEndTime = DateTime.Now - prefabSystemStartTime;
Logger.Info("Prefab System Time: " + prefabSystemEndTime.TotalMilliseconds + "ms");
Logger.Info($"Average: {prefabSystemEndTime.TotalMilliseconds / prefabAssets.Length}ms per asset");
string minAsset = "", maxAsset = "";
int min = Int32.MaxValue, max = -1;
foreach(var time in times)
{
if (time.Value < min)
{
min = time.Value;
minAsset = time.Key;
}
if (time.Value > max)
{
max = time.Value;
maxAsset = time.Key;
}
}
Logger.Info($"Min: {minAsset} - {min}ms");
Logger.Info($"Max: {maxAsset} - {max}ms");
using(StreamWriter sw = new StreamWriter(Path.Combine(EnvPath.kUserDataPath, "ModsData", nameof(AssetPacksManager), "AssetLoadTimes.txt")))
{
foreach (var time in times)
{
sw.WriteLine($"{time.Key}: {time.Value}ms");
}
}
int delay = 100000;
if (Setting.Instance.AutoHideNotifications)
delay = 30;
string text = $"{_loaded} assets loaded by APM.";
if (_skipped > 0)
{
text += $", {_skipped} skipped";
}
if (_notLoaded > 0)
{
text += $", {_notLoaded} failed to load";
}
if (_autoLoaded > 0)
{
text += $", {_autoLoaded} loaded automatically";
}
_notificationUISystem.RemoveNotification(
identifier: notificationInfo.id,
delay: delay,
text: text,
progressState: ProgressState.Complete,
progress: 100
);
var totalAssetTime = DateTime.Now - _assetLoadStartTime;
ApmLogger.Logger.Info("Asset Time: " + totalAssetTime);
_ = TelemetryTransmitter.SubmitAsync(_loaded, _autoLoaded, _notLoaded, Setting.Instance.AdaptiveAssetLoading);
}
/// <summary>
/// Checks if the asset has a CID file. If not, it will try to restore it from the backup
/// Creates a backup file if it doesn't exist.
///
/// This is only needed because PDX Mods deleted CID files when disabling a mod while ingame.
/// </summary>
/// <param name="file">Asset file to be checked</param>
/// <param name="pack">Current pack</param>
/// <returns></returns>
private static bool CheckAsset(FileInfo file, AssetPack pack)
{
//AnalyzeAsset(file, modName);
if (!File.Exists(file.FullName + ".cid"))
{
if (File.Exists(file.FullName + ".cid.backup"))
{
File.Copy(file.FullName + ".cid.backup", file.FullName + ".cid");
Logger.Info($"Restored CID for {file.FullName}");
return true;
}
Logger.Warn($"Asset has no CID: {file.FullName}. No CID Backup was found");
pack.MissingCids.Add(file.Name);
return false;
}
// Back up CID
if (!File.Exists(file.FullName + ".cid.backup"))
File.Copy(file.FullName + ".cid", file.FullName + ".cid.backup", true);
return true;
}
private static readonly Dictionary<string, int> ObsoleteIdentifiers = new();
private static void AnalyzeAsset(FileInfo file, string modName)
{
try
{
string prefabContent = File.ReadAllText(file.FullName);
if (prefabContent.Contains("ObsoleteIdentifiers"))
{
int startIndex = prefabContent.IndexOf("ObsoleteIdentifiers", StringComparison.Ordinal);
string obsoleteIdentifierWrapper = prefabContent.Substring(startIndex, 2000);
int nameStartIndex = obsoleteIdentifierWrapper.IndexOf("\"m_Name\": \"", StringComparison.Ordinal);
string obsoleteIdentifierCloseWrapper = obsoleteIdentifierWrapper.Substring(nameStartIndex);
int nameEndIndex = obsoleteIdentifierCloseWrapper.IndexOf("\",", StringComparison.Ordinal);
string obsoleteIdentifier = obsoleteIdentifierCloseWrapper.Substring(0, nameEndIndex);
if (obsoleteIdentifier.Contains("\"m_Name\": \""))
{
obsoleteIdentifier = obsoleteIdentifier.Replace("\"m_Name\": \"", "");
}
if (ObsoleteIdentifiers.ContainsKey(obsoleteIdentifier))
{
ObsoleteIdentifiers[obsoleteIdentifier]++;
}
else
{
ObsoleteIdentifiers.Add(obsoleteIdentifier, 1);
}
}
}
catch (Exception)
{
//Logger.Warn($"Error analyzing asset {file.Name}: {e.Message}");
}
}
private static void WriteAnalysisInfo()
{
string fileName = EnvPath.kUserDataPath + "/ModsData/AssetPacksManager/ObsoleteIdentifiers.txt";
using StreamWriter sw = new StreamWriter(fileName);
foreach (var identifier in ObsoleteIdentifiers)
{
sw.WriteLine($"{identifier.Key}: {identifier.Value}");
}
}
private static readonly List<string> AdditionalCidChecks = [".Geometry", ".Surface", ".Texture"];
private static List<FileInfo> GetPrefabsFromDirectoryRecursively(string directory, AssetPack currentPack)
{
List<FileInfo> files = new();
var dir = new DirectoryInfo(directory);
if (!dir.Exists)
Logger.Error($"Source directory not found: {dir.FullName}");
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (FileInfo file in dir.GetFiles())
{
if (file.Extension == ".Prefab")
{
if (CheckAsset(file, currentPack))
files.Add(file);
}
else if (AdditionalCidChecks.Contains(file.Extension))
{
CheckAsset(file, currentPack);
}
if (SupportedThumbnailExtensions.Contains(file.Extension))
{
CopyThumbnail(file, currentPack.Name);
}
}
foreach (DirectoryInfo subDir in dirs)
{
files.AddRange(GetPrefabsFromDirectoryRecursively(subDir.FullName, currentPack));
}
return files;
}
private static void CopyThumbnail(FileInfo file, string modName)
{
try
{
var fullPath = file.FullName.Replace('\\', '/');
// Get all after /assets/ to get the relative path
var relativePath = fullPath.Substring(fullPath.IndexOf("/assets/", StringComparison.Ordinal) + 8);
FileInfo target = new FileInfo(Path.Combine(ThumbnailDir, relativePath));
if (target.Directory != null && !target.Directory.Exists)
target.Directory.Create();
File.Copy(file.FullName, target.FullName, true);
}
catch (Exception e)
{
Logger.Error($"Error copying thumbnail for {file.Name}: {e.Message}. Details: {file.FullName} should have been copied to mod name {modName}");
}
}
private static void EnableAdaptiveLoading()
{
Setting.Instance.AdaptiveAssetLoading = true;
_notificationUISystem.RemoveNotification(
identifier: _adaptiveAssetsNotification.id,
delay: 5f,
text: $"Adaptive Loading has been enabled. Please restart the game.",
progressState: ProgressState.Warning
);
}
private static void DisableAdaptiveLoading()
{
Setting.Instance.AdaptiveAssetLoading = false;
_notificationUISystem.RemoveNotification(
identifier: _adaptiveAssetsNotification.id,
delay: 5f,
text: $"Adaptive Loading has been disabled. Please restart the game.",
progressState: ProgressState.Warning
);
}
public static void CloseGame()
{
Logger.Info("Closing Game...");
Application.Quit(0);
}
public static void DeleteCachedAssetPacks()
{
var foldersToDelete = new[]
{
Path.Combine(EnvPath.kUserDataPath, ".cache", "Mods", "mods_unmanaged"),
Path.Combine(EnvPath.kUserDataPath, ".cache", "Mods", "mods_workInProgress")
};
Logger.Info("Deleting Mods Cache");
foreach (var folder in foldersToDelete)
{
if (Directory.Exists(folder))
{
Directory.Delete(folder, true);
Logger.Info($"Deleted folder: {folder}");
}
}
var modsSubscribed = new DirectoryInfo(Path.Combine(EnvPath.kUserDataPath, ".cache", "Mods", "mods_subscribed"));
foreach (DirectoryInfo di in modsSubscribed.GetDirectories())
{
Logger.Debug("Looking for assets folder in " + di.FullName);
foreach (DirectoryInfo subfolder in di.GetDirectories())
{
if (subfolder.Name == "assets")
{
Logger.Debug($"Found assets folder in {di.FullName}");
Directory.Delete(di.FullName, true);
Logger.Info($"Deleted folder: {di.FullName}");
break;
/*if (FindLocalAssets(di.FullName) > 0)
{
Directory.Delete(di.FullName, true);
Logger.Info($"Deleted folder: {di.FullName}");
}*/
}
}
}
}
protected override void OnUpdate()
{
}
}
}