Skip to content

Commit

Permalink
Merge pull request #234 from ServerMod/dank-3.3.0
Browse files Browse the repository at this point in the history
Finalize release 3.3.0
  • Loading branch information
Dankrushen authored Aug 30, 2020
2 parents d86e9e6 + 5c33ab6 commit 791f87b
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 75 deletions.
92 changes: 92 additions & 0 deletions MultiAdmin/Features/ConfigGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using MultiAdmin.Config;
using MultiAdmin.Config.ConfigHandler;
using MultiAdmin.Features.Attributes;
using MultiAdmin.Utility;

namespace MultiAdmin.Features
{
[Feature]
internal class ConfigGenerator : Feature, ICommand
{

public ConfigGenerator(Server server) : base(server)
{
}

public string GetCommand()
{
return "CONFIGGEN";
}

public string GetCommandDescription()
{
return "Generates a full default MultiAdmin config file";
}

public string GetUsage()
{
return "[FILE LOCATION]";
}

public void OnCall(string[] args)
{
if (args.IsEmpty())
{
Server.Write("You must specify the location of the file.");
return;
}

string path = Utils.GetFullPathSafe(string.Join(" ", args));

ConfigEntry[] registeredConfigs = MultiAdminConfig.GlobalConfig.GetRegisteredConfigs();

List<string> lines = new List<string>(registeredConfigs.Length);
foreach (ConfigEntry configEntry in registeredConfigs)
{
switch (configEntry)
{
case ConfigEntry<string[]> config:
{
lines.Add($"{config.Key}: {(config.Default == null ? "" : string.Join(", ", config.Default))}");
break;
}

default:
{
lines.Add($"{configEntry.Key}: {configEntry.ObjectDefault ?? ""}");
break;
}
}
}

File.WriteAllLines(path, lines);
Server.Write($"Default config written to \"{path}\"");
}

public bool PassToGame()
{
return false;
}

public override void OnConfigReload()
{
}

public override string GetFeatureDescription()
{
return "Generates a full default MultiAdmin config file";
}

public override string GetFeatureName()
{
return "Config Generator";
}

public override void Init()
{
}
}
}
6 changes: 2 additions & 4 deletions MultiAdmin/Features/GithubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public void OnCall(string[] args)

foreach (Feature feature in Server.features)
{
if (feature.Equals(this)) continue;

lines.Add($"- {feature.GetFeatureName()}: {feature.GetFeatureDescription()}");
}

Expand Down Expand Up @@ -152,12 +150,12 @@ public override void OnConfigReload()

public override string GetFeatureDescription()
{
return "NOT INCLUDED IN FILE";
return "Generates a GitHub README file outlining all the features/commands";
}

public override string GetFeatureName()
{
return "GITHUB GEN";
return "GitHub Generator";
}

public override void Init()
Expand Down
4 changes: 2 additions & 2 deletions MultiAdmin/Features/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void OnCall(string[] args)

Server.Write($"Launching new server with Server ID: \"{serverId}\"...");

Program.StartServer(new Server(serverId));
Program.StartServer(new Server(serverId, args: Program.Args));
}
}

Expand Down Expand Up @@ -86,7 +86,7 @@ public void OnServerFull()

Server.Write($"Launching new server with Server ID: \"{onFullServerId}\" due to this server being full...");

onFullServerInstance = Program.StartServer(new Server(onFullServerId));
onFullServerInstance = Program.StartServer(new Server(onFullServerId, args: Program.Args));
}
}
}
16 changes: 16 additions & 0 deletions MultiAdmin/ModFeatures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace MultiAdmin
{
[Flags]
public enum ModFeatures
{
None = 0,

// Replaces detecting game output with MultiAdmin events for game events
CustomEvents = 1 << 0,

// Supporting all current features
All = ~(~0 << 1)
}
}
92 changes: 54 additions & 38 deletions MultiAdmin/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace MultiAdmin
{
public static class Program
{
public const string MaVersion = "3.3.0.1";
public const string MaVersion = "3.3.0.3";
public const string RecommendedMonoVersion = "5.18";

private static readonly List<Server> InstantiatedServers = new List<Server>();
Expand All @@ -28,6 +28,7 @@ public static class Program
: null;

private static uint? portArg;
public static readonly string[] Args = Environment.GetCommandLineArgs();

private static IExitSignal exitSignalListener;

Expand All @@ -37,7 +38,7 @@ public static class Program
#region Server Properties

public static Server[] Servers => ServerDirectories
.Select(serverDir => new Server(Path.GetFileName(serverDir), serverDir, portArg)).ToArray();
.Select(serverDir => new Server(Path.GetFileName(serverDir), serverDir, portArg, Args)).ToArray();

public static string[] ServerDirectories
{
Expand Down Expand Up @@ -206,28 +207,32 @@ public static void Main()
exitSignalListener.Exit += OnExit;
}

Headless = GetFlagFromArgs("headless", "h");
// Remove executable path
if (Args.Length > 0)
Args[0] = null;

Headless = GetFlagFromArgs(Args, "headless", "h");

if (!Headless)
CheckMonoVersion();

string serverIdArg = GetParamFromArgs("server-id", "id");
string configArg = GetParamFromArgs("config", "c");
portArg = uint.TryParse(GetParamFromArgs("port", "p"), out uint port) ? (uint?)port : null;
string serverIdArg = GetParamFromArgs(Args, "server-id", "id");
string configArg = GetParamFromArgs(Args, "config", "c");
portArg = uint.TryParse(GetParamFromArgs(Args, "port", "p"), out uint port) ? (uint?)port : null;

Server server = null;

if (!string.IsNullOrEmpty(serverIdArg) || !string.IsNullOrEmpty(configArg))
{
server = new Server(serverIdArg, configArg, portArg);
server = new Server(serverIdArg, configArg, portArg, Args);

InstantiatedServers.Add(server);
}
else
{
if (Servers.IsEmpty())
{
server = new Server(port: portArg);
server = new Server(port: portArg, args: Args);

InstantiatedServers.Add(server);
}
Expand All @@ -240,7 +245,7 @@ public static void Main()
Write("No servers are set to automatically start, please enter a Server ID to start:");
InputHandler.InputPrefix?.Write();

server = new Server(Console.ReadLine(), port: portArg);
server = new Server(Console.ReadLine(), port: portArg, args: Args);

InstantiatedServers.Add(server);
}
Expand Down Expand Up @@ -284,15 +289,13 @@ public static void Main()
}
}

public static string GetParamFromArgs(string[] keys = null, string[] aliases = null)
public static string GetParamFromArgs(string[] args, string[] keys = null, string[] aliases = null)
{
bool hasKeys = !keys.IsNullOrEmpty();
bool hasAliases = !aliases.IsNullOrEmpty();

if (!hasKeys && !hasAliases) return null;

string[] args = Environment.GetCommandLineArgs();

for (int i = 0; i < args.Length - 1; i++)
{
string lowArg = args[i]?.ToLower();
Expand All @@ -301,44 +304,61 @@ public static string GetParamFromArgs(string[] keys = null, string[] aliases = n

if (hasKeys)
{
if (keys.Any(key => !string.IsNullOrEmpty(key) && lowArg == $"--{key.ToLower()}"))
if (keys.Any(key => lowArg == $"--{key?.ToLower()}"))
{
return args[i + 1];
string value = args[i + 1];

args[i] = null;
args[i + 1] = null;

return value;
}
}

if (hasAliases)
{
if (aliases.Any(alias => !string.IsNullOrEmpty(alias) && lowArg == $"-{alias.ToLower()}"))
if (aliases.Any(alias => lowArg == $"-{alias?.ToLower()}"))
{
return args[i + 1];
string value = args[i + 1];

args[i] = null;
args[i + 1] = null;

return value;
}
}
}

return null;
}

public static bool ArgsContainsParam(string[] keys = null, string[] aliases = null)
public static bool ArgsContainsParam(string[] args, string[] keys = null, string[] aliases = null)
{
foreach (string arg in Environment.GetCommandLineArgs())
bool hasKeys = !keys.IsNullOrEmpty();
bool hasAliases = !aliases.IsNullOrEmpty();

if (!hasKeys && !hasAliases) return false;

for (int i = 0; i < args.Length; i++)
{
string lowArg = arg?.ToLower();
string lowArg = args[i]?.ToLower();

if (string.IsNullOrEmpty(lowArg)) continue;

if (!keys.IsNullOrEmpty())
if (hasKeys)
{
if (keys.Any(key => !string.IsNullOrEmpty(key) && lowArg == $"--{key.ToLower()}"))
if (keys.Any(key => lowArg == $"--{key?.ToLower()}"))
{
args[i] = null;
return true;
}
}

if (!aliases.IsNullOrEmpty())
if (hasAliases)
{
if (aliases.Any(alias => !string.IsNullOrEmpty(alias) && lowArg == $"-{alias.ToLower()}"))
if (aliases.Any(alias => lowArg == $"-{alias?.ToLower()}"))
{
args[i] = null;
return true;
}
}
Expand All @@ -347,28 +367,28 @@ public static bool ArgsContainsParam(string[] keys = null, string[] aliases = nu
return false;
}

public static bool GetFlagFromArgs(string[] keys = null, string[] aliases = null)
public static bool GetFlagFromArgs(string[] args, string[] keys = null, string[] aliases = null)
{
if (keys.IsNullOrEmpty() && aliases.IsNullOrEmpty()) return false;

return bool.TryParse(GetParamFromArgs(keys, aliases), out bool result)
return bool.TryParse(GetParamFromArgs(args, keys, aliases), out bool result)
? result
: ArgsContainsParam(keys, aliases);
: ArgsContainsParam(args, keys, aliases);
}

public static string GetParamFromArgs(string key = null, string alias = null)
public static string GetParamFromArgs(string[] args, string key = null, string alias = null)
{
return GetParamFromArgs(new string[] {key}, new string[] {alias});
return GetParamFromArgs(args, new string[] {key}, new string[] {alias});
}

public static bool ArgsContainsParam(string key = null, string alias = null)
public static bool ArgsContainsParam(string[] args, string key = null, string alias = null)
{
return ArgsContainsParam(new string[] {key}, new string[] {alias});
return ArgsContainsParam(args, new string[] {key}, new string[] {alias});
}

public static bool GetFlagFromArgs(string key = null, string alias = null)
public static bool GetFlagFromArgs(string[] args, string key = null, string alias = null)
{
return GetFlagFromArgs(new string[] {key}, new string[] {alias});
return GetFlagFromArgs(args, new string[] {key}, new string[] {alias});
}

public static Process StartServer(Server server)
Expand All @@ -380,7 +400,7 @@ public static Process StartServer(Server server)
Write("Error while starting new server: Could not find the executable location!", ConsoleColor.Red);
}

List<string> args = new List<string>();
List<string> args = new List<string>(server.args);

if (!string.IsNullOrEmpty(server.serverId))
args.Add($"-id \"{server.serverId}\"");
Expand All @@ -391,11 +411,7 @@ public static Process StartServer(Server server)
if (Headless)
args.Add("-h");

args.RemoveAll(string.IsNullOrEmpty);

string stringArgs = string.Join(" ", args);

ProcessStartInfo startInfo = new ProcessStartInfo(assemblyLocation, stringArgs);
ProcessStartInfo startInfo = new ProcessStartInfo(assemblyLocation, args.JoinArgs());

Write($"Launching \"{startInfo.FileName}\" with arguments \"{startInfo.Arguments}\"...");

Expand Down
Loading

0 comments on commit 791f87b

Please sign in to comment.