Skip to content

Commit 56ecc52

Browse files
committed
Improve the assets path cache algorithm?
1 parent 3210dd6 commit 56ecc52

3 files changed

Lines changed: 54 additions & 55 deletions

File tree

source/funkin/backend/assets/AssetsLibraryList.hx

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,16 @@ class AssetsLibraryList extends AssetLibrary {
5757
var existsSpecificCacheLibrary:Map<AssetSource, Map<Null<String>, Map<String, AssetLibrary>>> = [];
5858
var existsSpecificCacheTime:Map<AssetSource, Map<Null<String>, Map<String, Float>>> = [];
5959

60-
public function existsSpecific(id:String, type:String, source:AssetSource = BOTH) {
61-
if (!id.startsWith("assets/") && existsSpecific('assets/$id', type, source))
62-
return true;
60+
private function shouldSkipLib(lib:AssetLibrary, source:AssetSource) {
61+
if (source == BOTH || lib.tag == BOTH) return false;
62+
return source != lib.tag;
63+
}
64+
65+
public function getAssetPathLibrary(id:String, type:String, source:AssetSource = BOTH, checkAssetsPrefix:Bool = true):Null<AssetLibrary> {
66+
if (checkAssetsPrefix && !id.startsWith("assets/")) {
67+
final library = getAssetPathLibrary('assets/$id', type, source, false);
68+
if (library != null) return library;
69+
}
6370

6471
// Prevent massive lags on repetitive usage, primarily with getting note sprite sheets in mania charts (usually 2k+ notes)
6572
final time = haxe.Timer.stamp();
@@ -77,47 +84,51 @@ class AssetsLibraryList extends AssetLibrary {
7784
}
7885

7986
if (cacheTimePaths.exists(id)) {
80-
final cacheSafeTime = cacheTimePaths.get(id) + 6, library = cacheLibraryPaths.get(id);
87+
final library = cacheLibraryPaths.get(id);
88+
final cacheSafeTime = (Flags.PATHS_CACHE_LIFETIME == null ? null : cacheTimePaths.get(id) + Flags.PATHS_CACHE_LIFETIME);
89+
8190
if (library != null) {
82-
if (time < cacheSafeTime) return true;
83-
else if (!shouldSkipLib(library, source) && library.exists(id, type)) {
91+
if (cacheSafeTime == null || time < cacheSafeTime) return library;
92+
else if (!shouldSkipLib(library, source)
93+
&& (type == null ? library.exists(id, @:privateAccess library.types.get(id)) : library.exists(id, type))
94+
) {
8495
cacheTimePaths.set(id, time);
85-
return true;
96+
return library;
8697
}
8798

8899
cacheLibraryPaths.remove(id);
89100
}
90-
else if (time < cacheSafeTime) {
91-
return false;
92-
}
101+
/*else if (time < cacheSafeTime) {
102+
return null;
103+
}*/
93104
}
94105

95106
cacheTimePaths.set(id, time);
96107

97108
for (library in libraries) {
98109
if (shouldSkipLib(library, source)) continue;
99-
if (library.exists(id, type)) {
110+
if (type == null ? library.exists(id, @:privateAccess library.types.get(id)) : library.exists(id, type)) {
100111
cacheLibraryPaths.set(id, library);
101-
return true;
112+
return library;
102113
}
103114
}
104115

105-
return false;
116+
return null;
106117
}
118+
119+
public function existsSpecific(id:String, type:String, source:AssetSource = BOTH)
120+
return getAssetPathLibrary(id, type, source) != null;
121+
107122
public override inline function exists(id:String, type:String):Bool
108123
return existsSpecific(id, type, BOTH);
109124

110-
public function getSpecificPath(id:String, source:AssetSource = BOTH) {
111-
for(k=>e in libraries) {
112-
if (shouldSkipLib(e, source)) continue;
113-
114-
@:privateAccess
115-
if (e.exists(id, e.types.get(id))) {
116-
var path = e.getPath(id);
117-
if (path != null)
118-
return path;
119-
}
125+
public function getSpecificPath(id:String, source:AssetSource = BOTH):Null<String> {
126+
final library = getAssetPathLibrary(id, type, source);
127+
if (library != null) {
128+
final path = library.getPath(id);
129+
if (path != null) return path;
120130
}
131+
121132
return null;
122133
}
123134

@@ -163,36 +174,15 @@ class AssetsLibraryList extends AssetLibrary {
163174
}
164175

165176
public function getSpecificAsset(id:String, type:String, source:AssetSource = BOTH):Dynamic {
166-
try {
167-
if (!id.startsWith("assets/")) {
168-
var ass = getSpecificAsset('assets/$id', type, source);
169-
if (ass != null) {
170-
return ass;
171-
}
172-
}
173-
for(k=>l in libraries) {
174-
if (shouldSkipLib(l, source)) continue;
175-
176-
@:privateAccess
177-
if (l.exists(id, l.types.get(id))) {
178-
var asset = l.getAsset(id, type);
179-
if (asset != null) {
180-
return asset;
181-
}
182-
}
183-
}
184-
return null;
185-
} catch(e) {
186-
// TODO: trace the error
187-
throw e;
177+
final library = getAssetPathLibrary(id, type, source);
178+
if (library != null) {
179+
final asset = library.getAsset(id, type);
180+
if (asset != null) return asset;
188181
}
182+
189183
return null;
190184
}
191185

192-
private function shouldSkipLib(lib:AssetLibrary, source:AssetSource) {
193-
if (source == BOTH || lib.tag == BOTH) return false;
194-
return source != lib.tag;
195-
}
196186
public override inline function getAsset(id:String, type:String):Dynamic
197187
return getSpecificAsset(id, type, BOTH);
198188

@@ -254,18 +244,21 @@ class AssetsLibraryList extends AssetLibrary {
254244

255245
public function reset() {
256246
unloadLibraries();
247+
resetAssetPathCache();
248+
249+
libraries.resize(0);
257250

258-
for(source in [AssetSource.SOURCE, AssetSource.MODS, AssetSource.BOTH]) {
251+
// adds default libraries in again
252+
for (d in __defaultLibraries) addLibrary(d);
253+
}
254+
255+
public function resetAssetPathCache() {
256+
for (source in [AssetSource.SOURCE, AssetSource.MODS, AssetSource.BOTH]) {
259257
existsSpecificCacheLibrary[source]?.clear();
260258
existsSpecificCacheTime[source]?.clear();
261259
}
262260
existsSpecificCacheLibrary.clear();
263261
existsSpecificCacheTime.clear();
264-
265-
libraries.resize(0);
266-
267-
// adds default libraries in again
268-
for (d in __defaultLibraries) addLibrary(d);
269262
}
270263

271264
public function addLibrary(lib:AssetLibrary, ?tag:AssetSource, ?addTransLib:Bool = true) {

source/funkin/backend/scripting/GlobalScript.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class GlobalScript {
7171
FlxG.signals.preStateSwitch.add(function() {
7272
call("preStateSwitch");
7373

74+
if (Flags.PATHS_CACHE_RESET_ON_SWITCH_STATE) Paths.assetsTree.resetAssetPathCache();
75+
7476
var stateName = Type.getClassName(Type.getClass(@:privateAccess FlxG.game._requestedState));
7577
stateName = stateName.substring(stateName.lastIndexOf(".") + 1);
7678
if (Flags.MOD_REDIRECT_STATES.exists(stateName)) {

source/funkin/backend/system/Flags.hx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class Flags {
6161
public static var REPO_OWNER:String = "CodenameCrew";
6262
public static var REPO_URL:String = 'https://github.com/$REPO_OWNER/$REPO_NAME';
6363

64+
@:lazy public static var PATHS_CACHE_LIFETIME:Null<Int> = null;
65+
public static var PATHS_CACHE_RESET_ON_SWITCH_STATE:Bool = true;
66+
//public static var PATHS_CHECK_ASSETS_PREFIX:Bool = true; // Doesn't work fully
67+
6468
/**
6569
* Preferred sound extension for the game's audio files.
6670
* Currently is set to `mp3` for web targets, and `ogg` for other targets.

0 commit comments

Comments
 (0)