StartConfigComponent.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Base;
  5. using CommandLine;
  6. namespace Model
  7. {
  8. [ObjectEvent]
  9. public class OptionsComponentEvent : ObjectEvent<StartConfigComponent>, IAwake<string[]>
  10. {
  11. public void Awake(string[] args)
  12. {
  13. this.GetValue().Awake(args);
  14. }
  15. }
  16. public class StartConfigComponent: Component
  17. {
  18. private readonly List<StartConfig> allConfigs = new List<StartConfig>();
  19. private readonly Dictionary<int, StartConfig> configDict = new Dictionary<int, StartConfig>();
  20. public StartConfig MyConfig { get; private set; }
  21. public void Awake(string[] args)
  22. {
  23. string[] ss = File.ReadAllText("./Start.txt").Split('\n');
  24. foreach (string s in ss)
  25. {
  26. string s2 = s.Trim();
  27. if (s2 == "")
  28. {
  29. continue;
  30. }
  31. try
  32. {
  33. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  34. this.allConfigs.Add(startConfig);
  35. this.configDict.Add(startConfig.AppId, startConfig);
  36. }
  37. catch (Exception)
  38. {
  39. Log.Error($"config错误: {s2}");
  40. }
  41. }
  42. Options options = new Options();
  43. if (!Parser.Default.ParseArguments(args, options))
  44. {
  45. throw new Exception($"命令行格式错误!");
  46. }
  47. this.MyConfig = this.Get(options.AppId);
  48. }
  49. public StartConfig Get(int id)
  50. {
  51. return this.configDict[id];
  52. }
  53. public StartConfig[] GetAll()
  54. {
  55. return this.allConfigs.ToArray();
  56. }
  57. }
  58. }