World.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Threading;
  5. using Common.Base;
  6. namespace Model
  7. {
  8. public class World: Entity<World>
  9. {
  10. private static readonly World instance = new World();
  11. private Assembly assembly;
  12. public static World Instance
  13. {
  14. get
  15. {
  16. return instance;
  17. }
  18. }
  19. public Options Options { get; set; }
  20. private readonly List<IUpdate> iUpdates = new List<IUpdate>();
  21. private bool isStop;
  22. private World()
  23. {
  24. }
  25. public void Load()
  26. {
  27. this.assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll"));
  28. foreach (Component<World> component in this.GetComponents())
  29. {
  30. IAssemblyLoader assemblyLoader = component as IAssemblyLoader;
  31. if (assemblyLoader != null)
  32. {
  33. assemblyLoader.Load(this.assembly);
  34. }
  35. }
  36. }
  37. public void Start()
  38. {
  39. Load();
  40. foreach (Component<World> component in this.GetComponents())
  41. {
  42. IUpdate update = component as IUpdate;
  43. if (update != null)
  44. {
  45. this.iUpdates.Add(update);
  46. }
  47. IStart start = component as IStart;
  48. if (start != null)
  49. {
  50. start.Start();
  51. }
  52. }
  53. // loop
  54. while (!isStop)
  55. {
  56. Thread.Sleep(1);
  57. foreach (IUpdate update in this.iUpdates)
  58. {
  59. update.Update();
  60. }
  61. }
  62. }
  63. public void Stop()
  64. {
  65. isStop = true;
  66. }
  67. }
  68. }