From 68957d8802a9bd792eb37757020d26fd42df1835 Mon Sep 17 00:00:00 2001 From: Jamie Hankins Date: Sat, 9 Sep 2017 05:28:31 +0100 Subject: [PATCH] Adds a preaction framework so we can run a .bat file before or after compiling the code --- README.md | 3 ++ TGServerService/Compiler.cs | 15 +++++- TGServerService/PreactionHandler.cs | 65 ++++++++++++++++++++++++++ TGServerService/ServerService.cs | 2 + TGServerService/TGServerService.csproj | 1 + 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 TGServerService/PreactionHandler.cs diff --git a/README.md b/README.md index 0a9b038383..5d8314202a 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,9 @@ The process to update the server while DD is running is a functioning work in pr * `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. + * `BYOND/` * This contains the actual BYOND installation the server uses diff --git a/TGServerService/Compiler.cs b/TGServerService/Compiler.cs index 631a7e12a4..83a225f719 100644 --- a/TGServerService/Compiler.cs +++ b/TGServerService/Compiler.cs @@ -378,6 +378,13 @@ namespace TGServerService } } + if (!PrecompileHook()) + { + lastCompilerError = "The precompile hook failed"; + compilerCurrentStatus = TGCompilerStatus.Initialized; //still fairly valid + return; + } + using (var DM = new Process()) //will kill the process if the thread is terminated { DM.StartInfo.FileName = ByondDirectory + "/bin/dm.exe"; @@ -468,7 +475,13 @@ namespace TGServerService } } var staged = DaemonStatus() != TGDreamDaemonStatus.Offline; - var msg = String.Format("Compile complete!{0}", !staged ? "" : " Server will update next round."); + if (!PostcompileHook()) + { + lastCompilerError = "The postcompile hook failed"; + compilerCurrentStatus = TGCompilerStatus.Initialized; //still fairly valid + 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); lock (CompilerLock) diff --git a/TGServerService/PreactionHandler.cs b/TGServerService/PreactionHandler.cs new file mode 100644 index 0000000000..ac71129f24 --- /dev/null +++ b/TGServerService/PreactionHandler.cs @@ -0,0 +1,65 @@ +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("{}{}.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 silently. + 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 ran. Stdout:\n{}\nStderr:\n{}", 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 7cbc5fb224..4824a5ad38 100644 --- a/TGServerService/ServerService.cs +++ b/TGServerService/ServerService.cs @@ -75,6 +75,8 @@ namespace TGServerService ServerUpdateApplied = 6300, ChatBroadcastFail = 6400, IRCLogModes = 6500, + PreactionEvent = 6600, + PreactionFail = 6700 } static TGServerService ActiveService; //So everyone else can write to our eventlog diff --git a/TGServerService/TGServerService.csproj b/TGServerService/TGServerService.csproj index 5bd5b49e98..adebbc8836 100644 --- a/TGServerService/TGServerService.csproj +++ b/TGServerService/TGServerService.csproj @@ -93,6 +93,7 @@ + True