Engine.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using InABox.Logging;
  5. namespace PRSServer
  6. {
  7. public interface IEngine
  8. {
  9. string ServiceName { get; set; }
  10. string Version { get; set; }
  11. void Run();
  12. void Stop();
  13. void Configure(Server settings);
  14. }
  15. public abstract class Engine<TProperties> : IEngine where TProperties : ServerProperties
  16. {
  17. public TProperties Properties { get; private set; }
  18. public abstract void Run();
  19. public abstract void Stop();
  20. public string ServiceName { get; set; }
  21. public string Version { get; set; }
  22. protected string AppDataFolder { get; set; }
  23. public virtual void Configure(Server server)
  24. {
  25. Properties = server.Properties as TProperties;
  26. AppDataFolder = GetPath(server.Key);
  27. MainLogger.AddLogger(new LogFileLogger(AppDataFolder));
  28. MainLogger.AddLogger(new NamedPipeLogger(server.Key));
  29. }
  30. public static string GetPath(string key)
  31. {
  32. if (Assembly.GetEntryAssembly() != null)
  33. return Path.Combine(
  34. Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
  35. Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location),
  36. key
  37. );
  38. return Path.Combine(
  39. Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
  40. Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location),
  41. key
  42. );
  43. }
  44. }
  45. }