public void LoadService(Type service) // Load the service using the type { if (!service.IsClass || !typeof(PointBlankService).IsAssignableFrom(service)) // If it isn't a service then return { return; } if (service == typeof(PointBlankService)) // Prevents the actual service API from being loaded { return; } PointBlankService ser = (PointBlankService)Activator.CreateInstance(service); if (ServicesData.CheckKey(ser.Name)) { ser.AutoStart = ((string)ServicesData.Document[ser.Name]["AutoStart"]).ToLower() == "true"; ser.Replace = ((string)ServicesData.Document[ser.Name]["Replace"]).ToLower() == "true"; } else { JObject jObj = new JObject { { "AutoStart", (ser.AutoStart ? "true" : "false") }, { "Replace", (ser.Replace ? "true" : "false") } }; ServicesData.Document.Add(ser.Name, jObj); UniServicesData.Save(); } _tempServices.Add(ser); }
} // The service class #endregion public ServiceWrapper(PointBlankService ServiceClass) { // Setup variables this.ServiceClass = ServiceClass; // Setup data Enviroment.services.Add(ServiceClass.GetType().Name + "." + ServiceClass.Name, this); // Add the service // Run functions if (ServiceClass.AutoStart) { Start(); } }
} // The services data #endregion #region Private Functions private void RunService(PointBlankService service) // Runs the service { try { if (service.Replace) { foreach (ServiceWrapper wrapper in Enviroment.services.Where(a => a.Value.ServiceClass.FullName == service.FullName && a.Value.ServiceClass.Replacable).Select(a => a.Value)) { wrapper.Stop(); // Stop all services with the same name } } else if (Enviroment.services.Count(a => a.Key == service.FullName) > 0) { return; // Make sure that the services with the same name aren't ran } new ServiceWrapper(service); // Create the service wrapper } catch (Exception ex) { PointBlankLogging.LogError("Error starting service: " + service.Name, ex); } }