World.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. private List<IRunner> iRunners;
  20. private bool isStop;
  21. private World()
  22. {
  23. }
  24. public void Load()
  25. {
  26. this.assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll"));
  27. this.iRunners = new List<IRunner>();
  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. IRunner runner = component as IRunner;
  36. if (runner != null)
  37. {
  38. this.iRunners.Add(runner);
  39. }
  40. }
  41. }
  42. public void Start()
  43. {
  44. while (!isStop)
  45. {
  46. Thread.Sleep(1);
  47. foreach (IRunner runner in this.iRunners)
  48. {
  49. runner.Run();
  50. }
  51. }
  52. }
  53. public void Stop()
  54. {
  55. isStop = true;
  56. }
  57. }
  58. }