Skip to content

Commit 3170346

Browse files
lunacynneMAJigsaw77
authored andcommitted
Create ModStore.hx
1 parent e6d7371 commit 3170346

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

source/funkin/modding/ModStore.hx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package funkin.modding;
2+
3+
import haxe.ds.StringMap;
4+
5+
/**
6+
* Temporary persistent data storage for mods to use.
7+
*/
8+
@:nullSafety
9+
class ModStore
10+
{
11+
/**
12+
* All registered stores for this session.
13+
*/
14+
public static final stores:StringMap<Dynamic> = new StringMap<Dynamic>();
15+
16+
/**
17+
* Attempts to register a new store with the given ID and return it.
18+
* If a store with the same ID already exists, that store will be returned instead (discards `data`).
19+
*
20+
* @id The unique ID for this store.
21+
* @data Optional initial data, uses an empty object by default.
22+
* @return The store data at the given ID.
23+
*/
24+
public static function register(id:String, ?data:Dynamic):Dynamic
25+
{
26+
if (stores.exists(id)) return stores.get(id);
27+
stores.set(id, data ??= {});
28+
return data;
29+
}
30+
31+
/**
32+
* Helper function to get a store by ID.
33+
*
34+
* @id The target ID of the store.
35+
* @return The store data, or `null` if the store did not exist.
36+
*/
37+
public static function get(id:String):Null<Dynamic>
38+
{
39+
return stores.get(id);
40+
}
41+
42+
/**
43+
* Helper function to remove a store by ID and return it.
44+
*
45+
* @id The target ID of the store.
46+
* @return The store data, or `null` if the store did not exist.
47+
*/
48+
public static function remove(id:String):Null<Dynamic>
49+
{
50+
var data:Null<Dynamic> = stores.get(id);
51+
stores.remove(id);
52+
return data;
53+
}
54+
}

0 commit comments

Comments
 (0)