diff --git a/README.md b/README.md index d1086d793b..abc3e3943f 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,9 @@ The service supports updates while running a DreamDaemon instance. Simply instal * `RepoKey/` * This contains ssh key information for automatic changelog pushing. +* `EventHandlers/` + * This contains batch files that run after certain events, currently only precompile and postcompile events are implemented. If you'd like an event handler you should create a file in this folder with the following name: `{event_name}.bat` for example: if I want an event handler for precompile, I would name it `precompile.bat`. + * `BYOND/` * This contains the actual BYOND installation the server uses diff --git a/TGServerService/Compiler.cs b/TGServerService/Compiler.cs index 80b67a7f2c..5768508be1 100644 --- a/TGServerService/Compiler.cs +++ b/TGServerService/Compiler.cs @@ -383,6 +383,14 @@ namespace TGServerService } } + if (!PrecompileHook()) + { + lastCompilerError = "The precompile hook failed"; + compilerCurrentStatus = TGCompilerStatus.Initialized; //still fairly valid + TGServerService.WriteWarning("Precompile hook failed!", TGServerService.EventID.DMCompileError); + return; + } + using (var DM = new Process()) //will kill the process if the thread is terminated { DM.StartInfo.FileName = ByondDirectory + "/bin/dm.exe"; @@ -473,6 +481,13 @@ namespace TGServerService } } var staged = DaemonStatus() != TGDreamDaemonStatus.Offline; + if (!PostcompileHook()) + { + lastCompilerError = "The postcompile hook failed"; + compilerCurrentStatus = TGCompilerStatus.Initialized; //still fairly valid + TGServerService.WriteWarning("Postcompile hook failed!", TGServerService.EventID.DMCompileError); + return; + } var msg = String.Format("Compile complete!{0}", !staged ? "" : " Server will update next round."); SendMessage("DM: " + msg, ChatMessageType.DeveloperInfo); TGServerService.WriteInfo(msg, TGServerService.EventID.DMCompileSuccess); diff --git a/TGServerService/PreactionHandler.cs b/TGServerService/PreactionHandler.cs new file mode 100644 index 0000000000..0cf3432ca3 --- /dev/null +++ b/TGServerService/PreactionHandler.cs @@ -0,0 +1,66 @@ +using System; +using System.Diagnostics; +using System.IO; + +namespace TGServerService +{ + // Some useful functions for triggering pre action events + partial class TGStationServer + { + const string EventFolder = "EventHandlers/"; + + string GetPath(string eventName) + { + return string.Format("{0}{1}.bat", EventFolder, eventName); + } + + bool EventHandlerExists(string eventName) + { + return File.Exists(GetPath(eventName)); + } + + bool HandleEvent(string eventName) + { + if (!EventHandlerExists(eventName)) + { + // We don't need a handler, so let's just fail semi-silently. + TGServerService.WriteWarning(String.Format("Preaction Event: {0} not running due to missing event handler: {1}", eventName, GetPath(eventName)), TGServerService.EventID.PreactionFail); + return true; + } + + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = GetPath(eventName), + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + } + }; + process.Start(); + process.WaitForExit(); + + var stdout = process.StandardOutput.ReadToEnd(); + var stderr = process.StandardError.ReadToEnd(); + + TGServerService.WriteInfo( + String.Format("Preaction Event: {0} @ {1} ran. Stdout:\n{2}\nStderr:\n{3}", eventName, GetPath(eventName), stdout, stderr), + process.ExitCode == 0 ? TGServerService.EventID.PreactionEvent : TGServerService.EventID.PreactionFail + ); + + return process.ExitCode == 0 ? true : false; + } + + public bool PrecompileHook() + { + return HandleEvent("precompile"); + } + + public bool PostcompileHook() + { + return HandleEvent("postcompile"); + } + } +} diff --git a/TGServerService/ServerService.cs b/TGServerService/ServerService.cs index 8b1f952207..f02ce445d0 100644 --- a/TGServerService/ServerService.cs +++ b/TGServerService/ServerService.cs @@ -78,6 +78,8 @@ namespace TGServerService IRCLogModes = 6500, SubmoduleReclone = 6600, Authentication = 6700, + PreactionEvent = 6800, + PreactionFail = 6900 } static TGServerService ActiveService; //So everyone else can write to our eventlog diff --git a/TGServerService/TGServerService.csproj b/TGServerService/TGServerService.csproj index d38aaa0e30..2cf2d93f5c 100644 --- a/TGServerService/TGServerService.csproj +++ b/TGServerService/TGServerService.csproj @@ -95,6 +95,7 @@ + True