102 lines
No EOL
3.4 KiB
C#
102 lines
No EOL
3.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using ForgeCore.Assemble;
|
|
using ForgeCoreAPI;
|
|
using LibAC.Arguments;
|
|
using LibAC.NBT;
|
|
using LibAC.NBT.API;
|
|
|
|
namespace ForgeCore;
|
|
|
|
public class ForgeCore
|
|
{
|
|
|
|
public static int Main(string[] args)
|
|
{
|
|
|
|
Arguments arguments = ArgumentParser.Parse(args);
|
|
if (arguments.HasArg("help") || arguments.Count == 0)
|
|
{
|
|
// Print the help message for the default CLI Args
|
|
|
|
ArgumentBuilder builder = new ArgumentBuilder();
|
|
builder.withVersionArgument().withHelpArgument();
|
|
builder.withStringArgument("plugindir", "Plugins", true);
|
|
builder.withBooleanArgument("daemon", true);
|
|
|
|
|
|
Arguments defaults = builder.Build();
|
|
Console.WriteLine(ArgumentHelpers.GenerateHelpMessage(defaults.GetAllArguments().ToList(), "ForgeCore"));
|
|
return 0;
|
|
}
|
|
|
|
if (arguments.HasArg("version"))
|
|
{
|
|
Console.WriteLine($"ForgeCore Version {ASMInfo.BotVer}");
|
|
return 0;
|
|
}
|
|
|
|
if (arguments.HasArg("plugindir"))
|
|
PluginSystem.PluginDirectory = arguments.GetArgument("plugindir").GetValue() as string;
|
|
|
|
if (!Directory.Exists("Plugins"))
|
|
Directory.CreateDirectory("Plugins");
|
|
|
|
PluginSystem.InitializeSystem(PluginSystem.PluginDirectory);
|
|
|
|
SharedSessionData sessionData = SharedSessionData.GetInstance();
|
|
// Start the server tick loop
|
|
while (!sessionData.ShouldShutdown)
|
|
{
|
|
long tasksExecuted = 0;
|
|
if (sessionData.TotalTasksPerTick == 0 && sessionData.TotalTicks > 0)
|
|
{
|
|
sessionData.ShouldShutdown = true;
|
|
Console.WriteLine("FATAL ERROR\n\nNo plugins are loaded. This server would be doing nothing in a infinite loop with no way to exit. Aborting startup procedure. \n\n\n>> Recommendation: Add one of the standard ForgeCore Plugins which include methods of executing a shutdown");
|
|
}
|
|
|
|
foreach (var plugin in PluginSystem.Plugins)
|
|
{
|
|
if (plugin.enabled)
|
|
{
|
|
plugin.plugin.tick();
|
|
tasksExecuted++;
|
|
}
|
|
}
|
|
|
|
sessionData.TasksLastTick = tasksExecuted;
|
|
sessionData.TotalTicks++;
|
|
|
|
if(sessionData.TotalTasksPerTick == 0)
|
|
sessionData.TotalTasksPerTick = tasksExecuted;
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(5));
|
|
}
|
|
|
|
Console.WriteLine("Preparing to shut down... Please wait...");
|
|
Console.WriteLine("Gathering plugin settings...");
|
|
|
|
CompoundTag saveData = new CompoundTag();
|
|
foreach (var pluginContainer in PluginSystem.Plugins)
|
|
{
|
|
pluginContainer.plugin.TearDown();
|
|
|
|
CompoundTag entry = new CompoundTag();
|
|
NbtUtils.WriteBoolean(entry, "enabled", pluginContainer.enabled);
|
|
entry.Add("data", pluginContainer.plugin.SaveConfig());
|
|
saveData.Add(pluginContainer.pluginName, entry);
|
|
}
|
|
|
|
Console.WriteLine("Saving plugin data...");
|
|
NbtIo.Write("PluginStorage.dat", saveData);
|
|
|
|
Console.WriteLine("> Plugin Storage saved.");
|
|
Console.WriteLine("> Exiting server...");
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(5));
|
|
|
|
return 0;
|
|
}
|
|
} |