Document PreactionHandlers

This commit is contained in:
Cyberboss
2017-10-20 19:36:46 -04:00
parent 2568b786ff
commit 9c2d7cf9cd
2 changed files with 39 additions and 4 deletions
@@ -7,18 +7,44 @@ namespace TGServerService
// Some useful functions for triggering pre action events
sealed partial class ServerInstance
{
/// <summary>
/// The instance directory for Preaction handlers
/// </summary>
const string EventFolder = "EventHandlers/";
string GetPath(string eventName)
/// <summary>
/// Creates the <see cref="EventFolder"/>
/// </summary>
void InitEventHandlers()
{
Directory.CreateDirectory(EventFolder);
}
/// <summary>
/// Gets the path of an event given an <paramref name="eventName"/>
/// </summary>
/// <param name="eventName">The name of the event</param>
/// <returns>The path to the event handler</returns>
string GetEventPath(string eventName)
{
return string.Format("{0}{1}.bat", EventFolder, eventName);
}
/// <summary>
/// Check if an event handler for <paramref name="eventName"/> exists
/// </summary>
/// <param name="eventName">The name of the event</param>
/// <returns><see langword="true"/> if the event handler exists, <see langword="false"/> otherwise</returns>
bool EventHandlerExists(string eventName)
{
return File.Exists(GetPath(eventName));
return File.Exists(GetEventPath(eventName));
}
/// <summary>
/// Runs an event named <paramref name="eventName"/> if it exists
/// </summary>
/// <param name="eventName">The name of the event</param>
/// <returns><see langword="false"/> if the event handler exists and failed to run, <see langword="true"/> otherwise</returns>
bool HandleEvent(string eventName)
{
@@ -32,7 +58,7 @@ namespace TGServerService
{
StartInfo = new ProcessStartInfo
{
FileName = GetPath(eventName),
FileName = GetEventPath(eventName),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
@@ -45,7 +71,7 @@ namespace TGServerService
var stdout = process.StandardOutput.ReadToEnd();
var stderr = process.StandardError.ReadToEnd();
var success = process.ExitCode == 0;
var eventData = String.Format("Preaction Event: {0} @ {1} ran. Stdout:\n{2}\nStderr:\n{3}", eventName, GetPath(eventName), stdout, stderr);
var eventData = String.Format("Preaction Event: {0} @ {1} ran. Stdout:\n{2}\nStderr:\n{3}", eventName, GetEventPath(eventName), stdout, stderr);
if (success)
Service.WriteInfo(eventData, EventID.PreactionEvent);
@@ -55,11 +81,19 @@ namespace TGServerService
return success;
}
/// <summary>
/// Run the "precompile" event
/// </summary>
/// <returns><see langword="false"/> if the event handler exists and failed to run, <see langword="true"/> otherwise</returns>
public bool PrecompileHook()
{
return HandleEvent("precompile");
}
/// <summary>
/// Run the "postcompile" event
/// </summary>
/// <returns><see langword="false"/> if the event handler exists and failed to run, <see langword="true"/> otherwise</returns>
public bool PostcompileHook()
{
return HandleEvent("postcompile");
@@ -21,6 +21,7 @@ namespace TGServerService
public ServerInstance()
{
FindTheDroidsWereLookingFor();
InitEventHandlers();
InitChat();
InitRepo();
InitByond();