99
1010public static class AssetManager
1111{
12+ public static class Callbacks
13+ {
14+ public delegate void AssetManagerCallback ( ) ;
15+
16+ /// <summary>Called after Rust Asset Bundles are loaded into the editor. </summary>
17+ public static event AssetManagerCallback BundlesLoaded ;
18+
19+ /// <summary>Called after Rust Asset Bundles are unloaded from the editor. </summary>
20+ public static event AssetManagerCallback BundlesDisposed ;
21+
22+ public static void OnBundlesLoaded ( ) => BundlesLoaded ? . Invoke ( ) ;
23+ public static void OnBundlesDisposed ( ) => BundlesDisposed ? . Invoke ( ) ;
24+ }
25+
1226 public static string BundlePath { get ; private set ; }
1327
1428 public static GameManifest Manifest { get ; private set ; }
@@ -19,11 +33,11 @@ public static class AssetManager
1933
2034 public static Dictionary < uint , string > IDLookup { get ; private set ; } = new Dictionary < uint , string > ( ) ;
2135 public static Dictionary < string , uint > PathLookup { get ; private set ; } = new Dictionary < string , uint > ( ) ;
36+ public static Dictionary < string , AssetBundle > BundleLookup { get ; private set ; } = new Dictionary < string , AssetBundle > ( ) ;
2237
2338 public static Dictionary < string , AssetBundle > Bundles { get ; private set ; } = new Dictionary < string , AssetBundle > ( System . StringComparer . OrdinalIgnoreCase ) ;
24- public static Dictionary < string , AssetBundle > AssetPaths { get ; private set ; } = new Dictionary < string , AssetBundle > ( System . StringComparer . OrdinalIgnoreCase ) ;
25- public static Dictionary < string , Object > Cache { get ; private set ; } = new Dictionary < string , Object > ( ) ;
26- public static Dictionary < string , Texture2D > Previews { get ; private set ; } = new Dictionary < string , Texture2D > ( ) ;
39+ public static Dictionary < string , Object > AssetCache { get ; private set ; } = new Dictionary < string , Object > ( ) ;
40+ public static Dictionary < string , Texture2D > PreviewCache { get ; private set ; } = new Dictionary < string , Texture2D > ( ) ;
2741
2842 public static List < string > ManifestStrings { get => IsInitialised ? GetManifestStrings ( ) : new List < string > ( ) ; private set => ManifestStrings = value ; }
2943
@@ -47,7 +61,7 @@ public static void Dispose()
4761
4862 private static T GetAsset < T > ( string filePath ) where T : Object
4963 {
50- if ( ! AssetPaths . TryGetValue ( filePath , out AssetBundle bundle ) )
64+ if ( ! BundleLookup . TryGetValue ( filePath , out AssetBundle bundle ) )
5165 return null ;
5266
5367 return bundle . LoadAsset < T > ( filePath ) ;
@@ -57,29 +71,29 @@ public static T LoadAsset<T>(string filePath) where T : Object
5771 {
5872 var asset = default ( T ) ;
5973
60- if ( Cache . ContainsKey ( filePath ) )
61- asset = Cache [ filePath ] as T ;
74+ if ( AssetCache . ContainsKey ( filePath ) )
75+ asset = AssetCache [ filePath ] as T ;
6276 else
6377 {
6478 asset = GetAsset < T > ( filePath ) ;
6579 if ( asset != null )
66- Cache . Add ( filePath , asset ) ;
80+ AssetCache . Add ( filePath , asset ) ;
6781 }
6882 return asset ;
6983 }
7084
7185 public static GameObject LoadPrefab ( string filePath )
7286 {
73- if ( Cache . ContainsKey ( filePath ) )
74- return Cache [ filePath ] as GameObject ;
87+ if ( AssetCache . ContainsKey ( filePath ) )
88+ return AssetCache [ filePath ] as GameObject ;
7589
7690 else
7791 {
7892 GameObject val = GetAsset < GameObject > ( filePath ) ;
7993 if ( val != null )
8094 {
8195 PrefabManager . Setup ( val , filePath ) ;
82- Cache . Add ( filePath , val ) ;
96+ AssetCache . Add ( filePath , val ) ;
8397 return val ;
8498 }
8599 Debug . LogWarning ( "Prefab not loaded from bundle: " + filePath ) ;
@@ -89,7 +103,7 @@ public static GameObject LoadPrefab(string filePath)
89103
90104 public static Texture2D GetPreview ( string filePath )
91105 {
92- if ( Previews . TryGetValue ( filePath , out Texture2D preview ) )
106+ if ( PreviewCache . TryGetValue ( filePath , out Texture2D preview ) )
93107 return preview ;
94108 else
95109 {
@@ -99,7 +113,7 @@ public static Texture2D GetPreview(string filePath)
99113
100114 prefab . SetActive ( true ) ;
101115 var tex = AssetPreview . GetAssetPreview ( prefab ) ?? new Texture2D ( 60 , 60 ) ;
102- Previews . Add ( filePath , tex ) ;
116+ PreviewCache . Add ( filePath , tex ) ;
103117 prefab . SetActive ( false ) ;
104118 return tex ;
105119 }
@@ -121,7 +135,7 @@ private static List<string> GetManifestStrings()
121135 public static void AssetDump ( )
122136 {
123137 using ( StreamWriter streamWriter = new StreamWriter ( AssetDumpPath , false ) )
124- foreach ( var item in AssetPaths . Keys )
138+ foreach ( var item in BundleLookup . Keys )
125139 streamWriter . WriteLine ( item + " : " + ToID ( item ) ) ;
126140 }
127141
@@ -177,7 +191,7 @@ public static IEnumerator Initialise(string bundlesRoot)
177191 yield return EditorCoroutineUtility . StartCoroutineOwnerless ( SetMaterials ( materialID ) ) ;
178192
179193 IsInitialised = true ; IsInitialising = false ;
180- EventManager . OnBundlesLoaded ( ) ;
194+ Callbacks . OnBundlesLoaded ( ) ;
181195 PrefabManager . ReplaceWithLoaded ( PrefabManager . CurrentMapPrefabs , prefabID ) ;
182196 }
183197
@@ -209,9 +223,9 @@ public static IEnumerator Dispose()
209223 }
210224
211225 int bundleCount = Bundles . Count ;
212- AssetPaths . Clear ( ) ;
226+ BundleLookup . Clear ( ) ;
213227 Bundles . Clear ( ) ;
214- Cache . Clear ( ) ;
228+ AssetCache . Clear ( ) ;
215229
216230 Progress . Report ( bundleID , 0.99f , "Unloaded: " + bundleCount + " bundles." ) ;
217231 Progress . Finish ( bundleID , Progress . Status . Succeeded ) ;
@@ -282,7 +296,7 @@ public static IEnumerator SetBundleReferences((int parent, int bundle) ID)
282296 {
283297 foreach ( var filename in asset . GetAllAssetNames ( ) )
284298 {
285- AssetPaths . Add ( filename , asset ) ;
299+ BundleLookup . Add ( filename , asset ) ;
286300 if ( sw . Elapsed . TotalMilliseconds >= 0.5f )
287301 {
288302 yield return null ;
@@ -291,7 +305,7 @@ public static IEnumerator SetBundleReferences((int parent, int bundle) ID)
291305 }
292306 foreach ( var filename in asset . GetAllScenePaths ( ) )
293307 {
294- AssetPaths . Add ( filename , asset ) ;
308+ BundleLookup . Add ( filename , asset ) ;
295309 if ( sw . Elapsed . TotalMilliseconds >= 0.5f )
296310 {
297311 yield return null ;
0 commit comments