StartConfigComponent.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Options Options = new Options();
  21. public StartConfig MyConfig { get; private set; }
  22. public void Awake(string[] args)
  23. {
  24. if (!Parser.Default.ParseArguments(args, this.Options))
  25. {
  26. throw new Exception($"命令行格式错误!");
  27. }
  28. string[] ss = File.ReadAllText(this.Options.Config).Split('\n');
  29. foreach (string s in ss)
  30. {
  31. string s2 = s.Trim();
  32. if (s2 == "")
  33. {
  34. continue;
  35. }
  36. try
  37. {
  38. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  39. this.allConfigs.Add(startConfig);
  40. this.configDict.Add(startConfig.AppId, startConfig);
  41. }
  42. catch (Exception)
  43. {
  44. Log.Error($"config错误: {s2}");
  45. }
  46. }
  47. this.MyConfig = this.Get(this.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. }