Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from Jesus-QC/dev
Browse files Browse the repository at this point in the history
bump
  • Loading branch information
Jesus-QC authored Nov 17, 2021
2 parents 17a78a7 + 78ba8db commit 3a3f011
Show file tree
Hide file tree
Showing 27 changed files with 809 additions and 103 deletions.
16 changes: 16 additions & 0 deletions SecretAdmin/API/Events/EventArgs/ReceivedActionEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SecretAdmin.Features.Server.Enums;

namespace SecretAdmin.API.Events.EventArgs
{
public class ReceivedActionEventArgs : System.EventArgs
{
public ReceivedActionEventArgs(byte actionCode)
{
OutputCode = (OutputCodes)actionCode;
IsEnabled = true;
}

public OutputCodes OutputCode { get; set; }
public bool IsEnabled { get; set; }
}
}
16 changes: 16 additions & 0 deletions SecretAdmin/API/Events/EventArgs/ReceivedMessageEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace SecretAdmin.API.Events.EventArgs
{
public class ReceivedMessageEventArgs : System.EventArgs
{
public ReceivedMessageEventArgs(string message, byte code)
{
Message = message;
Code = code;
IsAllowed = !string.IsNullOrWhiteSpace(message);
}

public string Message { get; set; }
public byte Code { get; set; }
public bool IsAllowed { get; set; }
}
}
19 changes: 19 additions & 0 deletions SecretAdmin/API/Events/Handlers/Server.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SecretAdmin.API.Events.EventArgs;

namespace SecretAdmin.API.Events.Handlers
{
public static class Server
{
public static event Utils.CustomEventHandler<ReceivedMessageEventArgs> ReceivedMessage;
public static event Utils.CustomEventHandler<ReceivedActionEventArgs> ReceivedAction;
public static event Utils.CustomEventHandler Restarted;
public static event Utils.CustomEventHandler RestartedRound;
public static event Utils.CustomEventHandler RestartingRound;

public static void OnReceivedMessage(ReceivedMessageEventArgs ev) => ReceivedMessage?.Invoke(ev);
public static void OnReceivedAction(ReceivedActionEventArgs ev) => ReceivedAction?.Invoke(ev);
public static void OnRestarted() => Restarted?.Invoke();
public static void OnRestartedRound() => RestartedRound?.Invoke();
public static void OnRestartingRound() => RestartingRound?.Invoke();
}
}
8 changes: 8 additions & 0 deletions SecretAdmin/API/Events/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SecretAdmin.API.Events
{
public static class Utils
{
public delegate void CustomEventHandler<T>(T ev) where T : System.EventArgs;
public delegate void CustomEventHandler();
}
}
15 changes: 15 additions & 0 deletions SecretAdmin/API/Features/IModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace SecretAdmin.API.Features
{
public interface IModule
{
string Name { get; set; }
string Author { get; set; }
Version Version { get; set; }

void OnEnabled();
void OnDisabled();
void OnRegisteringCommands();
}
}
37 changes: 37 additions & 0 deletions SecretAdmin/API/Features/Module.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Reflection;
using SecretAdmin.Features.Console;

namespace SecretAdmin.API.Features
{
public abstract class Module : IModule
{
protected Module()
{
Assembly = Assembly.GetCallingAssembly();
Name ??= Assembly.GetName().Name;
Author ??= "Unknown";
Version ??= Assembly.GetName().Version;
}

private Assembly Assembly { get; }
public virtual string Name { get; set; }
public virtual string Author { get; set; }
public virtual Version Version { get; set; }

public virtual void OnEnabled()
{
Log.Raw($"The module {Name} [{Version}] by {Author} was enabled.", ConsoleColor.DarkMagenta);
}

public virtual void OnDisabled()
{
Log.Raw($"The module {Name} [{Version}] by {Author} was disabled.", ConsoleColor.DarkMagenta);
}

public virtual void OnRegisteringCommands()
{
Program.CommandHandler.RegisterCommands(Assembly);
}
}
}
73 changes: 73 additions & 0 deletions SecretAdmin/API/ModuleManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using SecretAdmin.API.Features;
using SecretAdmin.Features.Console;
using SecretAdmin.Features.Program;
using Module = SecretAdmin.API.Features.Module;

namespace SecretAdmin.API
{
public static class ModuleManager
{
public static List<IModule> Modules = new ();

public static void LoadAll()
{
Log.WriteLine();
Log.Raw("Loading modules dependencies!", ConsoleColor.DarkCyan);

var startTime = DateTime.Now;

foreach (var file in Directory.GetFiles(Paths.ModulesDependenciesFolder, "*.dll"))
{
try
{
var assembly = Assembly.UnsafeLoadFrom(file);
Log.Raw($"Dependency {assembly.GetName().Name} ({assembly.GetName().Version}) has been loaded!");
}
catch (Exception e)
{
Log.Raw($"Couldn't load the dependency in the path {file}\n{e}", ConsoleColor.Red);
throw;
}
}

Log.Raw($"Dependencies loaded in {(DateTime.Now - startTime).TotalMilliseconds}ms", ConsoleColor.Cyan);
Log.Raw("Loading modules!", ConsoleColor.DarkCyan);

startTime = DateTime.Now;

foreach (var file in Directory.GetFiles(Paths.ModulesFolder, "*.dll"))
{
var assembly = Assembly.Load(File.ReadAllBytes(file));

try
{
foreach (var type in assembly.GetTypes())
{
if(type.IsAbstract || type.IsInterface || type.BaseType != typeof(Module))
continue;

var constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor == null)
continue;

var module = constructor.Invoke(null) as IModule;
module?.OnEnabled();
module?.OnRegisteringCommands();

Modules.Add(module);
}
}
catch (Exception e)
{
Log.Raw(e, ConsoleColor.Red);
}
}

Log.Raw($"Modules loaded in {(DateTime.Now - startTime).TotalMilliseconds}ms", ConsoleColor.Cyan);
}
}
}
32 changes: 23 additions & 9 deletions SecretAdmin/Features/Console/Log.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using SecretAdmin.API.Events.EventArgs;
using SConsole = System.Console;
using static SecretAdmin.Program;
using SEvents = SecretAdmin.API.Events.Handlers.Server;

namespace SecretAdmin.Features.Console
{
public class Log
public static class Log
{
private static readonly Regex FrameworksRegex = new (@"\[(DEBUG|INFO|WARN|ERROR)\] (\[.*?\]) (.*)", RegexOptions.Compiled | RegexOptions.Singleline);

// Program Alerts

public static void Intro()
{
SConsole.Clear();
WriteLine(@" .--. .-. .--. .-. _
: .--' .' `. : .; : : : :_;
`. `. .--. .--. .--. .--.`. .' : : .-' :,-.,-.,-..-.,-.,-.
Expand All @@ -22,7 +25,12 @@ public static void Intro()
Write($"Secret Admin - Version v{SecretAdmin.Program.Version}");
WriteLine(" by Jesus-QC", ConsoleColor.Blue);
WriteLine("Released under MIT License Copyright © Jesus-QC 2021", ConsoleColor.Red);

if (!ConfigManager.SecretAdminConfig.ManualStart)
return;

WriteLine("Press any key to continue.", ConsoleColor.Green);
SConsole.ReadKey();
}

public static void Input(string message, string title = "SERVER")
Expand All @@ -32,7 +40,7 @@ public static void Input(string message, string title = "SERVER")
Raw(message, ConsoleColor.Magenta, false);
}

public static void Alert(string message, bool showTimeStamp = true)
public static void Alert(object message, bool showTimeStamp = true)
{
if (showTimeStamp)
Write($"[{DateTime.Now:T}] ", ConsoleColor.DarkRed);
Expand All @@ -44,7 +52,7 @@ public static void Alert(string message, bool showTimeStamp = true)

// Alerts

public static void Raw(string message, ConsoleColor color = ConsoleColor.White, bool showTimeStamp = true) => WriteLine(showTimeStamp ? $"[{DateTime.Now:T}] {message}" : message, color);
public static void Raw(object message, ConsoleColor color = ConsoleColor.White, bool showTimeStamp = true) => WriteLine(showTimeStamp ? $"[{DateTime.Now:T}] {message}" : message, color);

private static void Info(string title, string message)
{
Expand Down Expand Up @@ -78,21 +86,29 @@ private static void Warn(string title, string message)
WriteLine(message, ConsoleColor.Yellow);
}

private static void WriteLine(string message, ConsoleColor color = ConsoleColor.White)
public static void WriteLine(object message = null, ConsoleColor color = ConsoleColor.White)
{
SConsole.ForegroundColor = color;
SConsole.WriteLine(message);
ProgramLogger?.AppendLog(message, true);
}

private static void Write(string message, ConsoleColor color = ConsoleColor.White)
public static void Write(object message = null, ConsoleColor color = ConsoleColor.White)
{
SConsole.ForegroundColor = color;
SConsole.Write(message);
ProgramLogger?.AppendLog(message);
}

public static void HandleMessage(string message, byte code)
{
if(message == null)
var ev = new ReceivedMessageEventArgs(message, code);
SEvents.OnReceivedMessage(ev);

message = ev.Message;
code = ev.Code;

if(!ev.IsAllowed|| string.IsNullOrWhiteSpace(message))
return;

var match = FrameworksRegex.Match(message);
Expand All @@ -117,8 +133,6 @@ public static void HandleMessage(string message, byte code)
break;
}
}


}
else
{
Expand Down
24 changes: 24 additions & 0 deletions SecretAdmin/Features/Console/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.IO;

namespace SecretAdmin.Features.Console
{
public class Logger
{
private readonly string _path;

public Logger(string path)
{
_path = path;
}

public void AppendLog(object message, bool newLine = false)
{
using var stream = File.AppendText(_path);

if (newLine)
stream.WriteLine(message);
else
stream.Write(message);
}
}
}
36 changes: 32 additions & 4 deletions SecretAdmin/Features/Program/ArgumentsManager.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
namespace SecretAdmin.Features.Program
{
public class ArgumentsManager
public static class ArgumentsManager
{
// TODO: this
/*
* Arguments:
* --reconfigure -r
* --config <path> -c
* --logs <path> -l
* --game-logs <path> -gl
* --config <filename> -c
* --no-logs -nl
*/

public static Args GetArgs(string[] args)
{
var ret = new Args();

for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "--reconfigure" or "-r":
ret.Reconfigure = true;
break;
case "--config" or "-c" when args.Length > i + 1:
ret.Config = args[i + 1];
break;
case "--no-logs" or "-nl":
ret.Logs = false;
break;
}
}

return ret;
}

public class Args
{
public bool Reconfigure = false;
public string Config = "default.yml";
public bool Logs = true;
}
}
}
10 changes: 10 additions & 0 deletions SecretAdmin/Features/Program/AutoUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SecretAdmin.Features.Program
{
public static class AutoUpdater
{
public static void CheckForUpdates()
{
// todo: this
}
}
}
Loading

0 comments on commit 3a3f011

Please sign in to comment.