4using System.Collections.Generic;
6using System.Reflection;
7using System.Diagnostics;
8using System.Runtime.CompilerServices;
12using Microsoft.Xna.Framework;
13using Microsoft.Xna.Framework.Input;
14using Microsoft.Xna.Framework.Graphics;
16using MoonSharp.Interpreter;
20 public class CUIInternalAttribute : System.Attribute { }
22 public class CUILuaRegistrar
24 public static string CUITypesFile => Path.Combine(CUI.LuaFolder,
"CUITypes.lua");
26 public static bool IsRealCUIType(Type T)
28 if (T.DeclaringType !=
null)
return false;
29 if (T.Name ==
"<>c")
return false;
30 if (T.IsGenericType)
return false;
31 if (T.IsInterface)
return false;
32 if (T.IsSubclassOf(typeof(Attribute)))
return false;
33 if (Attribute.IsDefined(T, typeof(CUIInternalAttribute)))
return false;
34 if (typeof(CUILuaRegistrar).Namespace != T.Namespace)
return false;
43 public void Register()
45 if (CUI.LuaFolder ==
null)
return;
46 if (!Directory.Exists(CUI.LuaFolder) || !CUI.UseLua)
return;
48 Assembly thisAssembly = Assembly.GetAssembly(typeof(CUILuaRegistrar));
50 foreach (Type T
in thisAssembly.GetTypes().Where(IsRealCUIType))
52 LuaUserData.RegisterType(T.FullName);
57 GameMain.LuaCs.RegisterAction<CUIInput>();
58 GameMain.LuaCs.RegisterAction<float,
float>();
59 GameMain.LuaCs.RegisterAction<TextInputEventArgs>();
60 GameMain.LuaCs.RegisterAction<
string>();
61 GameMain.LuaCs.RegisterAction<CUIComponent>();
62 GameMain.LuaCs.RegisterAction<
bool>();
63 GameMain.LuaCs.RegisterAction<CUIComponent,
int>();
66 LuaUserData.RegisterType(typeof(CUI).FullName);
67 GameMain.LuaCs.Lua.Globals[nameof(CUI)] = UserData.CreateStatic(typeof(CUI));
69 ConstructLuaStaticsFile();
75 public void Deregister()
79 GameMain.LuaCs.Lua.Globals[nameof(CUI)] =
null;
87 public void ConstructLuaStaticsFile()
89 if (!Directory.Exists(CUI.LuaFolder))
return;
91 Assembly thisAssembly = Assembly.GetAssembly(typeof(CUILuaRegistrar));
93 string content =
"-- This file is autogenerated\n";
95 foreach (Type T
in thisAssembly.GetTypes().Where(IsRealCUIType))
97 content += $
"{T.Name} = LuaUserData.CreateStatic('{T.FullName}', true)\n";
100 using (StreamWriter writer =
new StreamWriter(CUITypesFile,
false))
102 writer.Write(content);