CrabUI
Loading...
Searching...
No Matches
ModStorage.cs
1using System;
2using System.Reflection;
3using System.Runtime.CompilerServices;
4using System.Collections.Generic;
5using System.Collections.Immutable;
6using System.Linq;
7
8using Barotrauma;
9using HarmonyLib;
10using Microsoft.Xna.Framework;
11
12
13namespace CrabUI
14{
15 /// <summary>
16 /// Global data repository
17 /// Treat it as Dictionary<string, object>
18 /// In fact data is stored in GUI.Canvas.GUIComponent.Userdata
19 /// </summary>
20 public class ModStorage
21 {
22 private static Dictionary<string, object> GetOrCreateRepo()
23 {
24 if (GUI.Canvas.GUIComponent is not GUIButton)
25 {
26 GUI.Canvas.GUIComponent = new GUIButton(new RectTransform(new Point(0, 0)));
27 }
28
29 if (GUI.Canvas.GUIComponent.UserData is not Dictionary<string, object>)
30 {
31 GUI.Canvas.GUIComponent.UserData = new Dictionary<string, object>();
32 }
33
34 return (Dictionary<string, object>)GUI.Canvas.GUIComponent.UserData;
35 }
36
37 public static object Get<TValue>(string key) => (TValue)Get(key);
38 public static object Get(string key)
39 {
40 Dictionary<string, object> repo = GetOrCreateRepo();
41 return repo.GetValueOrDefault(key);
42 }
43 public static void Set(string key, object value)
44 {
45 Dictionary<string, object> repo = GetOrCreateRepo();
46 repo[key] = value;
47 }
48 public static bool Has(string key)
49 {
50 Dictionary<string, object> repo = GetOrCreateRepo();
51 return repo.ContainsKey(key);
52 }
53 }
54}
Global data repository Treat it as Dictionary<string, object> In fact data is stored in GUI....
Definition ModStorage.cs:21