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. 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. string[] ss = File.ReadAllText("./Start.txt").Split('\n');
  25. foreach (string s in ss)
  26. {
  27. string s2 = s.Trim();
  28. if (s2 == "")
  29. {
  30. continue;
  31. }
  32. try
  33. {
  34. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  35. this.allConfigs.Add(startConfig);
  36. this.configDict.Add(startConfig.Options.Id, startConfig);
  37. }
  38. catch (Exception)
  39. {
  40. Log.Error($"config错误: {s2}");
  41. }
  42. }
  43. Options options = new Options();
  44. if (!Parser.Default.ParseArguments(args, options))
  45. {
  46. throw new Exception($"命令行格式错误!");
  47. }
  48. this.MyConfig = this.Get(options.Id);
  49. }
  50. public StartConfig Get(int id)
  51. {
  52. return this.configDict[id];
  53. }
  54. public StartConfig[] GetAll()
  55. {
  56. return this.allConfigs.ToArray();
  57. }
  58. }
  59. }