StartConfigComponent.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Base;
  5. using CommandLine;
  6. using MongoDB.Bson.Serialization;
  7. namespace Model
  8. {
  9. [ObjectEvent]
  10. public class OptionsComponentEvent : ObjectEvent<StartConfigComponent>, IAwake<string[]>
  11. {
  12. public void Awake(string[] args)
  13. {
  14. this.GetValue().Awake(args);
  15. }
  16. }
  17. public class StartConfigComponent: Component
  18. {
  19. private readonly List<StartConfig> allConfigs = new List<StartConfig>();
  20. private readonly Dictionary<int, StartConfig> configDict = new Dictionary<int, StartConfig>();
  21. public StartConfig MyConfig { get; private set; }
  22. public void Awake(string[] args)
  23. {
  24. //StartConfig sc = new StartConfig();
  25. ////sc.IP = "192.168.12.112";
  26. ////sc.Options.AppType = "Realm";
  27. ////sc.Options.Id = 1;
  28. //
  29. //InnerConfig inneConfig = sc.Config.AddComponent<InnerConfig>();
  30. ////inneConfig.Host = "127.0.0.1";
  31. ////inneConfig.Port = 10002;
  32. //
  33. //OuterConfig outerConfig = sc.Config.AddComponent<OuterConfig>();
  34. ////outerConfig.Host = "127.0.0.1";
  35. ////outerConfig.Port = 10003;
  36. //
  37. //string s3 = MongoHelper.ToJson(sc);
  38. //StartConfig s4 = MongoHelper.FromJson<StartConfig>(s3);
  39. //BsonClassMap.RegisterClassMap<OuterConfig>();
  40. //BsonClassMap.RegisterClassMap<InnerConfig>();
  41. string[] ss = File.ReadAllText("./Start.txt").Split('\n');
  42. foreach (string s in ss)
  43. {
  44. string s2 = s.Trim();
  45. if (s2 == "")
  46. {
  47. continue;
  48. }
  49. try
  50. {
  51. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  52. this.allConfigs.Add(startConfig);
  53. this.configDict.Add(startConfig.Options.Id, startConfig);
  54. }
  55. catch (Exception)
  56. {
  57. Log.Error($"config错误: {s2}");
  58. }
  59. }
  60. Options options = new Options();
  61. if (!Parser.Default.ParseArguments(args, options))
  62. {
  63. throw new Exception($"命令行格式错误!");
  64. }
  65. this.MyConfig = this.Get(options.Id);
  66. }
  67. public StartConfig Get(int id)
  68. {
  69. return this.configDict[id];
  70. }
  71. public StartConfig[] GetAll()
  72. {
  73. return this.allConfigs.ToArray();
  74. }
  75. }
  76. }