IW4M-Admin/Admin/Main.cs

111 lines
3.7 KiB
C#
Raw Normal View History

2015-05-09 12:46:25 -04:00
#define USINGMEMORY
using System;
2015-03-08 17:20:10 -04:00
using System.Collections.Generic;
using System.Text;
using System.Threading;
2015-08-17 16:38:42 -04:00
using System.Runtime.InteropServices;
using SharedLibrary;
2015-03-08 17:20:10 -04:00
namespace IW4MAdmin
{
class Program
{
2015-08-20 17:54:38 -04:00
static public double Version { get; private set; }
static private Manager serverManager;
static private IW4MAdmin_Web.WebFront frontEnd;
2015-03-08 17:20:10 -04:00
static void Main(string[] args)
{
2015-08-20 17:54:38 -04:00
Version = 0.95;
double latestVersion = 0;
2015-08-17 16:38:42 -04:00
handler = new ConsoleEventDelegate(OnProcessExit);
SetConsoleCtrlHandler(handler, true);
double.TryParse(checkUpdate(), out latestVersion);
Console.WriteLine("=====================================================");
Console.WriteLine(" IW4M ADMIN");
Console.WriteLine(" by RaidMax ");
if (latestVersion != 0)
Console.WriteLine(" Version " + Version + " (latest " + latestVersion + ")");
else
Console.WriteLine(" Version " + Version + " (unable to retrieve latest)");
Console.WriteLine("=====================================================");
serverManager = new IW4MAdmin.Manager();
Thread serverMGRThread = new Thread(serverManager.Init);
serverMGRThread.Name = "Server Manager thread";
serverMGRThread.Start();
while(!serverManager.isReady())
{
SharedLibrary.Utilities.Wait(1);
}
if (serverManager.getServers() != null)
Program.getManager().mainLog.Write("IW4M Now Initialized! Visit http://127.0.0.1:1624 for server overview.", Log.Level.Production);
2015-07-17 18:53:51 -04:00
if (serverManager.getServers().Count > 0)
{
frontEnd = new IW4MAdmin_Web.WebFront();
2015-07-17 18:53:51 -04:00
frontEnd.Init();
}
serverMGRThread.Join();
serverManager.mainLog.Write("Shutting down IW4MAdmin...", Log.Level.Debug);
}
2015-08-17 16:38:42 -04:00
static ConsoleEventDelegate handler;
static private bool OnProcessExit(int e)
{
try
{
foreach (Server S in IW4MAdmin.Program.getServers())
{
if (S == null)
continue;
2015-08-20 17:54:38 -04:00
S.Broadcast("^5IW4MAdmin ^7is going ^1offline^7");
S.isRunning = false;
if (Utilities.shutdownInterface(S.pID()))
2015-08-17 16:38:42 -04:00
Program.getManager().mainLog.Write("Successfully removed IW4MAdmin from server with PID " + S.pID(), Log.Level.Debug);
else
Program.getManager().mainLog.Write("Could not remove IW4MAdmin from server with PID " + S.pID(), Log.Level.Debug);
}
Program.getManager().shutDown();
frontEnd.webSchedule.Stop();
frontEnd.webSchedule.Dispose();
2015-08-20 17:54:38 -04:00
return false;
2015-08-17 16:38:42 -04:00
}
catch
{
return true;
}
}
private delegate bool ConsoleEventDelegate(int eventType);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
static private String checkUpdate()
{
Connection Ver = new Connection("http://raidmax.org/IW4M/Admin/version.php");
return Ver.Read();
}
static public Server[] getServers()
{
return serverManager.getServers().ToArray();
}
2015-08-17 16:38:42 -04:00
static public Manager getManager()
{
return serverManager;
}
2015-03-08 17:20:10 -04:00
}
}