protected override IUserPlugin _LoadPlugin(IUserPluginDescriptor descriptor, FSGameLoop game) { try { var plugin = new LuaPlugin(descriptor); plugin.Game = game; plugin.LogSystem = LogSystem; plugin.Load(); return(plugin); } catch (System.Exception e) { LogSystem?.PrintWarning(e.ToString()); } return(null); }
private void OnFileChanged(object source, FileSystemEventArgs ev) { //Info("file changed: " + ev.FullPath); string plFolder = ev.FullPath.Substring(ev.FullPath.IndexOf("lua") + 4); LuaPlugin plugin = PluginList.Find(pl => plFolder.Contains(pl.folder)); //Info("plugin changed: " + plugin.folder); if (!plugin.reloadCooldown) { log.Info("qlay", "Reloading lua plugin " + plugin.id); plugin.reloadCooldown = true; Modules.Timer.singleton.Create("pluginreload_" + plugin.id, 1000, 1, () => { ReloadPlugin(plugin); }); } }
private void ReloadPlugin(LuaPlugin plugin) { try { plugin.reloadCooldown = false; LoadPluginFile(plugin.absolutePath, plugin.context); if (plugin.context["Init"] != null) { lua.Call(plugin.context["Init"]); } log.Info("qlay", "Reloaded"); } catch (InterpreterException ex) { log.Error("qlay", "Lua error " + ex.DecoratedMessage); } catch (Exception e) { log.Error("qlay", "Error " + e.Message); log.Error("qlay", e.StackTrace); } }
public override void OnEnable() { //Register user data type; UserData.RegisterType <UnityEngine.GameObject>(); UserData.RegisterType <Smod2.API.UserGroup>(); UserData.RegisterType <Smod2.API.Item>(); UserData.RegisterType <TeamRole>(); UserData.RegisterType <Connection>(); UserData.RegisterType <Server>(); UserData.RegisterType <Map>(); UserData.RegisterType <Door>(); UserData.RegisterType <Elevator>(); UserData.RegisterType <Round>(); UserData.RegisterType <RoundStats>(); UserData.RegisterType <Vector>(); UserData.RegisterType <PocketDimensionExit>(); UserData.RegisterType <TeslaGate>(); UserData.RegisterType <Scp914.Recipe>(); UserData.RegisterType <Modules.Timer>(); UserData.RegisterType <Modules.Timer.TimerInstance>(); UserData.RegisterType <Modules.File>(); UserData.RegisterType <Functions.Player>(); UserData.RegisterType <Functions.Warhead>(); UserData.RegisterType <Functions.SCP914>(); UserData.RegisterType <Modules.MySQL>(); UserData.RegisterType <Modules.MySQLConnection>(); UserData.RegisterType <Modules.MySQLQuery>(); UserData.RegisterType <Modules.LuaSocket.LuaWrapper>(); UserData.RegisterType <Modules.LuaSocket.qLuaPacket>(); UserData.RegisterType <Modules.LuaSocket.qConnection>(); UserData.RegisterType <Role>(); UserData.RegisterType <Team>(); UserData.RegisterType <UserRank>(); UserData.RegisterType <ItemType>(); UserData.RegisterType <AmmoType>(); UserData.RegisterType <DamageType>(); UserData.RegisterType <ElevatorStatus>(); UserData.RegisterType <ElevatorType>(); UserData.RegisterType <IntercomStatus>(); UserData.RegisterType <KnobSetting>(); UserData.RegisterType <PocketDimensionExitType>(); UserData.RegisterType <RadioStatus>(); UserData.RegisterType <ROUND_END_STATUS>(); //Register classes; Hooks hooks = new Hooks(this); Functions.SCP914 scp914 = new Functions.SCP914(this); //Register events; #if !DEBUG this.AddEventHandler(typeof(IEventHandlerRoundStart), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerCheckRoundEnd), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerRoundEnd), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerConnect), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerDisconnect), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerWaitingForPlayers), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerRoundRestart), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerUpdate), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerFixedUpdate), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerCheckEscape), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPlayerJoin), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerSpawn), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerTeamRespawn), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerSpawnRagdoll), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerCheckEscape), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerSetRole), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPlayerHurt), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPlayerDie), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPlayerPickupItem), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPlayerPickupItemLate), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPlayerDropItem), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerMedkitUse), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerLure), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerRadioSwitch), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerShoot), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerThrowGrenade), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandler106CreatePortal), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandler106Teleport), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerContain106), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPocketDimensionEnter), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPocketDimensionExit), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerPocketDimensionDie), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerIntercom), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerWarheadStartCountdown), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerWarheadStopCountdown), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerWarheadDetonate), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerLCZDecontaminate), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerAdminQuery), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerAuthCheck), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerBan), hooks, Priority.Highest); this.AddEventHandler(typeof(IEventHandlerSCP914Activate), scp914, Priority.Highest); #endif try { string luaPath = Directory.GetCurrentDirectory() + "/lua"; #if DEBUG lua = new Script(CoreModules.Preset_Complete); #else lua = new Script(); #endif UserData.RegisterType <Player>(new DynamicIndex(lua, typeof(Player), typeof(Functions.PlayerExtension))); wrappedMeta = lua.DoString(@" return { __index = function(t, name) local obj = rawget(t, '_object'); if (obj) then return obj[name]; end local meta = rawget(t, '_base'); if (meta) then return meta[name]; end end } ").Table; //Register lua globals; //ne //lua.Globals["file"] = new Modules.File(dir + "/"); lua.Globals["timer"] = Modules.Timer.singleton; // lua.Globals["hook"] = hooks; lua.Globals["mysql"] = new Modules.MySQL(this); lua.Globals["socket"] = new Modules.LuaSocket.LuaWrapper(this); Dictionary <string, object> StaticResource = new Dictionary <string, object>(); StaticResource.Add("Role", UserData.CreateStatic <Role>()); StaticResource.Add("Item", UserData.CreateStatic <ItemType>()); StaticResource.Add("Ammo", UserData.CreateStatic <AmmoType>()); StaticResource.Add("DamageType", UserData.CreateStatic <DamageType>()); StaticResource.Add("ElevatorStatus", UserData.CreateStatic <ElevatorStatus>()); StaticResource.Add("ElevatorType", UserData.CreateStatic <ElevatorType>()); StaticResource.Add("IntercomStatus", UserData.CreateStatic <IntercomStatus>()); StaticResource.Add("KnobSetting", UserData.CreateStatic <KnobSetting>()); StaticResource.Add("PocketDimensionExitType", UserData.CreateStatic <PocketDimensionExitType>()); StaticResource.Add("RadioStatus", UserData.CreateStatic <RadioStatus>()); StaticResource.Add("RoundEndStatus", UserData.CreateStatic <ROUND_END_STATUS>()); StaticResource.Add("Team", UserData.CreateStatic <Smod2.API.Team>()); StaticResource.Add("UserRank", UserData.CreateStatic <UserRank>()); lua.Globals["static"] = StaticResource; lua.Globals["player"] = new Functions.Player(); lua.Globals["warhead"] = new Functions.Warhead(); lua.Globals["scp914"] = scp914; List <string> cvars = new List <string>(); Table con = new Table(lua); con["RegString"] = new Action <string, string>((key, def) => { if (cvars.Contains(key)) { return; } AddConfig(new Smod2.Config.ConfigSetting(key, (def != null) ? def : "default", Smod2.Config.SettingType.STRING, true, "qlay-variable")); cvars.Add(key); }); con["GetString"] = CallbackFunction.FromDelegate(lua, new Func <string, string>(GetConfigString)); con["GetNumber"] = CallbackFunction.FromDelegate(lua, new Func <string, float>(GetConfigFloat)); con["GetBool"] = CallbackFunction.FromDelegate(lua, new Func <string, bool>(GetConfigBool)); lua.Globals["ConVar"] = con; luaHookCall = lua.DoString(Modules.StaticLua.LuaHook, null, "Hook system"); lua.DoString(Modules.StaticLua.TableExtension, null, "Table extension"); lua.DoString(Modules.StaticLua.DBModel, null, "DataBase Model"); //file refresh #if !DEBUG watcher = new FileSystemWatcher(luaPath + "/", "*.lua"); //watcher.Filter = "*.*"; watcher.NotifyFilter = NotifyFilters.LastWrite; watcher.Changed += OnFileChanged; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; #endif //load plugins if (Directory.Exists(luaPath)) { foreach (string dir in Directory.GetDirectories(luaPath, "*", SearchOption.TopDirectoryOnly)) { var g = lua.Globals; Table localEnv = new Table(lua); foreach (var item in g.Keys) { localEnv[item] = g[item]; } localEnv["file"] = new Modules.File(dir + "/"); localEnv["include"] = new Action <string>((file) => { LoadPluginFile(dir, localEnv, file); }); localEnv["require"] = null; //Info("env created "); LoadPluginFile(dir, localEnv); //Info("file loaded "); Table pluginInfo = localEnv.Get("pluginInfo").ToObject <Table>(); string id = pluginInfo.Get("id").String; string name = pluginInfo.Get("name").String; string author = pluginInfo.Get("author").String; string version = pluginInfo.Get("version").String; //lua.Options.DebugPrint = s => { Info( "[" + id + "] " + s); }; lua.Options.DebugPrint = s => { Info("[LUA] " + s); }; log.Info("qlay->LUA", name + "(ver:" + version + ") by " + author + " has loaded"); localEnv["MsgN"] = new Action <string>((message) => { log.Info(id, message); luaHookCall.Function.Call("OnLog", "[" + id + "] " + message); }); if (localEnv["Init"] != null) { lua.Call(localEnv["Init"]); } //Info("init called "); LuaPlugin plugin = new LuaPlugin(dir); plugin.id = id; plugin.context = localEnv; PluginList.Add(plugin); //Info("plugin added"); } } else { Directory.CreateDirectory(luaPath); } } catch (InterpreterException ex) { log.Error("qlay", "Lua error: " + ex.DecoratedMessage); } }