using System; using System.IO; using System.Data; using System.Linq; using System.Threading; using System.ServiceProcess; using System.Collections.Generic; using FilterAPI; using FilterZone.LocalHandlers; using FilterZone.LocalNetworking; using FilterZone.ZoneHandlers; using FilterZone.ZoneNetworking; using System.Diagnostics; using System.Runtime.InteropServices; namespace FilterZone { public partial class Service : ServiceBase { private Boolean AECheckerIsDone = true; private Boolean ISGiverIsDone = true; private Boolean DSenderIsDone = true; private static Thread AEThread; private static Thread ISThread; private static Thread DSThread; [StructLayout(LayoutKind.Sequential)] public struct SERVICE_STATUS { public int serviceType; public int currentState; public int controlsAccepted; public int win32ExitCode; public int serviceSpecificExitCode; public int checkPoint; public int waitHint; } public enum State { SERVICE_STOPPED = 0x00000001, SERVICE_START_PENDING = 0x00000002, SERVICE_STOP_PENDING = 0x00000003, SERVICE_RUNNING = 0x00000004, SERVICE_CONTINUE_PENDING = 0x00000005, SERVICE_PAUSE_PENDING = 0x00000006, SERVICE_PAUSED = 0x00000007, } [DllImport("ADVAPI32.DLL", EntryPoint = "SetServiceStatus")] public static extern bool SetServiceStatus(IntPtr hServiceStatus, ref SERVICE_STATUS lpServiceStatus); private SERVICE_STATUS serviceStatus; public Service() { } internal void Init(bool dbg) { if (dbg) { Program.ZoneID = "00"; } else { Program.ZoneID = AppDomain.CurrentDomain.FriendlyName.Replace("FilterZone", "").Replace(".exe", ""); } AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Program.Conf = new Config(); Program.SQL = new Database(Program.Conf); Program.ItemCreator = new Item(Program.SQL); Program.LocalHandlers = new LocalHandlerLoader(); Program.LocalClient = new LocalClient(); Program.LoadSHNs(); ZoneListener ZListen = new ZoneListener(); Program.ZoneHandlers = new ZoneHandlerLoader(); if (Convert.ToBoolean(Program.Conf.GetConfigValue(String.Format("Zone{0}StngAECTE", Program.ZoneID)))) { AEThread = new Thread(delegate () { while (true) { if (AECheckerIsDone) { RunAEChecker(); } Thread.Sleep(Convert.ToInt32(Program.Conf.GetConfigValue(String.Format("Zone{0}StngAECTI", Program.ZoneID))) * 1000); } }); AEThread.Start(); } if (Convert.ToBoolean(Program.Conf.GetConfigValue(String.Format("Zone{0}StngISGTE", Program.ZoneID)))) { ISThread = new Thread(delegate () { while (true) { if (ISGiverIsDone) { RunISGiver(); } Thread.Sleep(Convert.ToInt32(Program.Conf.GetConfigValue(String.Format("Zone{0}StngISGTI", Program.ZoneID))) * 1000); } }); ISThread.Start(); } if (Convert.ToBoolean(Program.Conf.GetConfigValue(String.Format("Zone{0}StngDSTE", Program.ZoneID)))) { DSThread = new Thread(delegate () { while (true) { if (DSenderIsDone) { RunDSender(); } Thread.Sleep(Convert.ToInt32(Program.Conf.GetConfigValue(String.Format("Zone{0}StngDSTI", Program.ZoneID))) * 1000); } }); DSThread.Start(); } } protected override void OnStart(String[] Args) { IntPtr handle = this.ServiceHandle; serviceStatus.currentState = (int)State.SERVICE_START_PENDING; SetServiceStatus(handle, ref serviceStatus); Init(false); serviceStatus.currentState = (int)State.SERVICE_RUNNING; SetServiceStatus(handle, ref serviceStatus); } private void RunAEChecker() { AECheckerIsDone = false; try { foreach (ZoneClient ZClient in Program.ZoneClients.Values.Where(IsReady => IsReady.IsReady == true).Where(IsMounted => IsMounted.IsMounted == true)) { if (ZClient.sID != String.Empty) { Int64 GuildMoney = Program.SQL.GetnMoneyFromnNo(Program.SQL.GetnNoFromnCharNo(ZClient.nCharNo)); if (0 > GuildMoney) { ZClient.Dispose(); } } } } catch (Exception Error) { Program.SendConsoleText(ConsoleColor.Red, "Zone error - Academy earn checker\n{0}", Error.ToString()); } AECheckerIsDone = true; } private void RunISGiver() { ISGiverIsDone = false; try { foreach (ZoneClient ZClient in Program.ZoneClients.Values.Where(IsReady => IsReady.IsReady == true).Where(IsVT => IsVT.IsVending == true || IsVT.InTrade == true)) { Command.SetAbstate(ZClient, "StaImmortal", 7500); } } catch (Exception Error) { Program.SendConsoleText(ConsoleColor.Red, "Zone error - Idle state giver\n{0}", Error.ToString()); } ISGiverIsDone = true; } private void RunDSender() { DSenderIsDone = false; try { foreach (ZoneClient ZClient in Program.ZoneClients.Values.Where(IsReady => IsReady.IsReady == true)) { Command.Distribute(ZClient); Thread.Sleep(100); } } catch (Exception Error) { Program.SendConsoleText(ConsoleColor.Red, "Zone error - &Distribute sender\n{0}", Error.ToString()); } DSenderIsDone = true; } private void CurrentDomain_UnhandledException(Object Sender, UnhandledExceptionEventArgs Args) { File.WriteAllLines(String.Format("{0}{1}Crash.txt", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "")), new String[] { ((Exception)Args.ExceptionObject).ToString() }); Program.SendConsoleText(ConsoleColor.Red, ((Exception)Args.ExceptionObject).ToString()); } protected override void OnStop() { IntPtr handle = this.ServiceHandle; serviceStatus.currentState = (int)State.SERVICE_STOP_PENDING; SetServiceStatus(handle, ref serviceStatus); var SQLState = Program.SQL.Connection.State; if (SQLState.HasFlag(ConnectionState.Open)) { Program.SQL.Connection.Close(); } Program.LocalClient.Dispose(); foreach (ZoneClient zc in Program.ZoneClients.Values) { zc.Dispose(); } serviceStatus.currentState = (int)State.SERVICE_STOPPED; SetServiceStatus(handle, ref serviceStatus); //ServiceBase serviceBase = Program.ServicesToRun[0]; //ServiceController service = ServiceController.GetServices().Where(x=> x.ServiceName == this.ServiceName).FirstOrDefault(); Process proc = Process.GetCurrentProcess(); proc.Kill(); } } }