StartConfigComponent.cs 1.3 KB

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