File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments