Skip to content

Commit 04c94cc

Browse files
committed
Merge branch 'main' into internal-merge & Fix paths cache
2 parents 0e71ea3 + 6c96a69 commit 04c94cc

6 files changed

Lines changed: 183 additions & 142 deletions

File tree

source/funkin/backend/assets/AssetsLibraryList.hx

Lines changed: 79 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -54,70 +54,85 @@ class AssetsLibraryList extends AssetLibrary {
5454
return lib;
5555
}
5656

57-
var existsSpecificCacheLibrary:Map<AssetSource, Map<Null<String>, Map<String, AssetLibrary>>> = [];
58-
var existsSpecificCacheTime:Map<AssetSource, Map<Null<String>, Map<String, Float>>> = [];
57+
var assetPathCacheLibrary:Map<AssetSource, Map<Null<String>, Map<String, AssetLibrary>>> = [];
58+
var assetPathCacheTime: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+
}
6364

64-
// Prevent massive lags on repetitive usage, primarily with getting note sprite sheets in mania charts (usually 2k+ notes)
65-
final time = haxe.Timer.stamp();
65+
public function getAssetPathLibrary(id:String, type:Null<String>, source:AssetSource = BOTH):Null<AssetLibrary> {
66+
var cacheLibraryPaths:Map<String, AssetLibrary> = null;
67+
if (Flags.PATHS_CACHE_LIFETIME != 0) {
68+
// Prevent massive lags on repetitive usage, primarily with getting note sprite sheets in mania charts (usually 2k+ notes)
69+
final time = haxe.Timer.stamp();
6670

67-
var cacheLibraryTypes = existsSpecificCacheLibrary.get(source), cacheTimeTypes = existsSpecificCacheTime.get(source);
68-
if (cacheLibraryTypes == null) {
69-
existsSpecificCacheLibrary.set(source, cacheLibraryTypes = []);
70-
existsSpecificCacheTime.set(source, cacheTimeTypes = []);
71-
}
71+
var cacheLibraryTypes = assetPathCacheLibrary.get(source), cacheTimeTypes = assetPathCacheTime.get(source);
72+
if (cacheLibraryTypes == null) {
73+
assetPathCacheLibrary.set(source, cacheLibraryTypes = []);
74+
assetPathCacheTime.set(source, cacheTimeTypes = []);
75+
}
7276

73-
var cacheLibraryPaths = cacheLibraryTypes.get(type), cacheTimePaths = cacheTimeTypes.get(type);
74-
if (cacheLibraryPaths == null) {
75-
cacheLibraryTypes.set(type, cacheLibraryPaths = []);
76-
cacheTimeTypes.set(type, cacheTimePaths = []);
77-
}
77+
cacheLibraryPaths = cacheLibraryTypes.get(type);
78+
var cacheTimePaths = cacheTimeTypes.get(type);
79+
if (cacheLibraryPaths == null) {
80+
cacheLibraryTypes.set(type, cacheLibraryPaths = []);
81+
cacheTimeTypes.set(type, cacheTimePaths = []);
82+
}
7883

79-
if (cacheTimePaths.exists(id)) {
80-
final cacheSafeTime = cacheTimePaths.get(id) + 6, library = cacheLibraryPaths.get(id);
81-
if (library != null) {
82-
if (time < cacheSafeTime) return true;
83-
else if (!shouldSkipLib(library, source) && library.exists(id, type)) {
84-
cacheTimePaths.set(id, time);
85-
return true;
86-
}
84+
if (cacheTimePaths.exists(id)) {
85+
final library = cacheLibraryPaths.get(id);
8786

88-
cacheLibraryPaths.remove(id);
89-
}
90-
else if (time < cacheSafeTime) {
91-
return false;
87+
if (Flags.PATHS_CACHE_LIFETIME != null) {
88+
final cacheSafeTime = cacheTimePaths.get(id) + Flags.PATHS_CACHE_LIFETIME;
89+
90+
if (library != null) {
91+
if (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+
) {
95+
cacheTimePaths.set(id, time);
96+
return library;
97+
}
98+
99+
cacheLibraryPaths.remove(id);
100+
}
101+
else if (time < cacheSafeTime) {
102+
return null;
103+
}
104+
}
105+
else
106+
return library;
92107
}
93-
}
94108

95-
cacheTimePaths.set(id, time);
109+
cacheTimePaths.set(id, time);
110+
}
96111

97112
for (library in libraries) {
98113
if (shouldSkipLib(library, source)) continue;
99-
if (library.exists(id, type)) {
100-
cacheLibraryPaths.set(id, library);
101-
return true;
114+
if (type == null ? library.exists(id, @:privateAccess library.types.get(id)) : library.exists(id, type)) {
115+
if (cacheLibraryPaths != null) cacheLibraryPaths.set(id, library);
116+
return library;
102117
}
103118
}
104119

105-
return false;
120+
return null;
106121
}
122+
123+
public function existsSpecific(id:String, type:String, source:AssetSource = BOTH)
124+
return (!id.startsWith("assets/") && existsSpecific('assets/$id', type, source)) || getAssetPathLibrary(id, type, source) != null;
125+
107126
public override inline function exists(id:String, type:String):Bool
108127
return existsSpecific(id, type, BOTH);
109128

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-
}
129+
public function getSpecificPath(id:String, source:AssetSource = BOTH):Null<String> {
130+
final library = getAssetPathLibrary(id, null, source);
131+
if (library != null) {
132+
final path = library.getPath(id);
133+
if (path != null) return path;
120134
}
135+
121136
return null;
122137
}
123138

@@ -163,36 +178,20 @@ class AssetsLibraryList extends AssetLibrary {
163178
}
164179

165180
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;
181+
if (!id.startsWith("assets/")) {
182+
final asset = getSpecificAsset('assets/$id', type, source);
183+
if (asset != null) return asset;
184+
}
175185

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;
186+
final library = getAssetPathLibrary(id, type, source);
187+
if (library != null) {
188+
final asset = library.getAsset(id, type);
189+
if (asset != null) return asset;
188190
}
191+
189192
return null;
190193
}
191194

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

@@ -254,20 +253,23 @@ class AssetsLibraryList extends AssetLibrary {
254253

255254
public function reset() {
256255
unloadLibraries();
257-
258-
for(source in [AssetSource.SOURCE, AssetSource.MODS, AssetSource.BOTH]) {
259-
existsSpecificCacheLibrary[source]?.clear();
260-
existsSpecificCacheTime[source]?.clear();
261-
}
262-
existsSpecificCacheLibrary.clear();
263-
existsSpecificCacheTime.clear();
256+
resetAssetPathCache();
264257

265258
libraries.resize(0);
266259

267260
// adds default libraries in again
268261
for (d in __defaultLibraries) addLibrary(d);
269262
}
270263

264+
public function resetAssetPathCache() {
265+
for (source in [AssetSource.SOURCE, AssetSource.MODS, AssetSource.BOTH]) {
266+
assetPathCacheLibrary[source]?.clear();
267+
assetPathCacheTime[source]?.clear();
268+
}
269+
assetPathCacheLibrary.clear();
270+
assetPathCacheTime.clear();
271+
}
272+
271273
public function addLibrary(lib:AssetLibrary, ?tag:AssetSource, ?addTransLib:Bool = true) {
272274
libraries.insert(0, lib);
273275
if (tag != null) lib.tag = tag;

0 commit comments

Comments
 (0)