Client LuaCsForBarotrauma
LuaCsModStore.cs
1 using MoonSharp.Interpreter;
2 using MoonSharp.Interpreter.Interop;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Reflection;
7 
8 namespace Barotrauma
9 {
10  partial class LuaCsSetup
11  {
12  public class LuaCsModStore
13  {
14  public abstract class ModStore<T, TStore>
15  {
16  protected Dictionary<string, TStore> store;
17 
18  public TStore Set(string name, TStore value) => store[name] = value;
19  public TStore Get(string name) => store[name];
20 
21  public ModStore(Dictionary<string, TStore> store) => this.store = store;
22 
23  public abstract bool Equals(T value);
24  }
25  public class LuaModStore : ModStore<string, DynValue>
26  {
27  public string Name;
28 
29  public LuaModStore(Dictionary<string, DynValue> store) : base(store) { }
30  public override bool Equals(string value) => Name == value;
31  }
32  public class CsModStore : ModStore<ACsMod, object>
33  {
34  public ACsMod Mod;
35 
36  public CsModStore(Dictionary<string, object> store) : base(store) { }
37  public override bool Equals(ACsMod value) => Mod == value;
38  }
39 
40  private HashSet<LuaModStore> luaModInterface;
41  private HashSet<CsModStore> csModInterface;
42 
43  public LuaCsModStore()
44  {
45  luaModInterface = new HashSet<LuaModStore>();
46  csModInterface = new HashSet<CsModStore>();
47  }
48 
49  public void Initialize()
50  {
51  UserData.RegisterType<LuaModStore>();
52  UserData.RegisterType<CsModStore>();
53  var msType = UserData.RegisterType<LuaCsModStore>();
54  var msDesc = (StandardUserDataDescriptor)msType;
55 
56  typeof(StandardUserDataDescriptor).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).ToList().ForEach(m =>
57  {
58  if (
59  m.Name.Contains("Register")
60  )
61  {
62  msDesc.AddMember(m.Name, new MethodMemberDescriptor(m, InteropAccessMode.Default));
63  }
64  });
65  }
66  public void Clear()
67  {
68  luaModInterface.Clear();
69  csModInterface.Clear();
70  }
71 
72  protected LuaModStore Register(string modName)
73  {
74  if (luaModInterface.Any(i => i.Equals(modName)))
75  {
76  LuaCsLogger.HandleException(new ArgumentException($"'{modName}' entry already registered"), LuaCsMessageOrigin.LuaMod);
77  return null;
78  }
79 
80  var newHandle = new LuaModStore(new Dictionary<string, DynValue>());
81  if (luaModInterface.Add(newHandle)) return newHandle;
82  else return null;
83  }
84  [MoonSharpHidden]
86  {
87  if (csModInterface.Any(i => i.Equals(mod)))
88  {
89  LuaCsLogger.HandleException(new ArgumentException($"'{mod.GetType().FullName}' entry already registered"), LuaCsMessageOrigin.CSharpMod);
90  return null;
91  }
92 
93  var newHandle = new CsModStore(new Dictionary<string, object>());
94  if (csModInterface.Add(newHandle)) return newHandle;
95  else return null;
96  }
97 
98  public CsModStore GetCsStore(string modName) {
99  var result = csModInterface.Where(i => i.Mod.GetType().FullName == modName).FirstOrDefault();
100  if (result != null)
101  {
102  if (!result.Mod.IsDisposed) return result;
103  else
104  {
105  csModInterface.Remove(result);
106  return null;
107  }
108  }
109  else return null;
110  }
111  protected LuaModStore GetLuaStore(string modName) => luaModInterface.Where(i => i.Name == modName).FirstOrDefault();
112  }
113  }
114 }
static void HandleException(Exception ex, LuaCsMessageOrigin origin)
CsModStore(Dictionary< string, object > store)
LuaModStore(Dictionary< string, DynValue > store)
Dictionary< string, TStore > store
TStore Set(string name, TStore value)
ModStore(Dictionary< string, TStore > store)
CsModStore GetCsStore(string modName)
CsModStore Register(ACsMod mod)
LuaModStore Register(string modName)
LuaModStore GetLuaStore(string modName)