Engine.cs 1.6 KB

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