StartConfigComponent.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace Model
  6. {
  7. [ObjectEvent]
  8. public class StartConfigComponentSystem : ObjectSystem<StartConfigComponent>, IAwake<string, int>
  9. {
  10. public void Awake(string a, int b)
  11. {
  12. this.Get().Awake(a, b);
  13. }
  14. }
  15. public class StartConfigComponent: Component
  16. {
  17. private Dictionary<int, StartConfig> configDict;
  18. public StartConfig StartConfig { get; private set; }
  19. public StartConfig DBConfig { get; private set; }
  20. public StartConfig RealmConfig { get; private set; }
  21. public StartConfig LocationConfig { get; private set; }
  22. public List<StartConfig> MapConfigs { get; private set; }
  23. public List<StartConfig> GateConfigs { get; private set; }
  24. public void Awake(string path, int appId)
  25. {
  26. this.configDict = new Dictionary<int, StartConfig>();
  27. this.MapConfigs = new List<StartConfig>();
  28. this.GateConfigs = new List<StartConfig>();
  29. string[] ss = File.ReadAllText(path).Split('\n');
  30. foreach (string s in ss)
  31. {
  32. string s2 = s.Trim();
  33. if (s2 == "")
  34. {
  35. continue;
  36. }
  37. try
  38. {
  39. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  40. this.configDict.Add(startConfig.AppId, startConfig);
  41. if (startConfig.AppType.Is(AppType.Realm))
  42. {
  43. this.RealmConfig = startConfig;
  44. }
  45. if (startConfig.AppType.Is(AppType.Location))
  46. {
  47. this.LocationConfig = startConfig;
  48. }
  49. if (startConfig.AppType.Is(AppType.DB))
  50. {
  51. this.DBConfig = startConfig;
  52. }
  53. if (startConfig.AppType.Is(AppType.Map))
  54. {
  55. this.MapConfigs.Add(startConfig);
  56. }
  57. if (startConfig.AppType.Is(AppType.Gate))
  58. {
  59. this.GateConfigs.Add(startConfig);
  60. }
  61. }
  62. catch (Exception e)
  63. {
  64. Log.Error($"config错误: {s2} {e}");
  65. }
  66. }
  67. this.StartConfig = this.Get(appId);
  68. }
  69. public StartConfig Get(int id)
  70. {
  71. try
  72. {
  73. return this.configDict[id];
  74. }
  75. catch (Exception e)
  76. {
  77. throw new Exception($"not found startconfig: {id}", e);
  78. }
  79. }
  80. public StartConfig[] GetAll()
  81. {
  82. return this.configDict.Values.ToArray();
  83. }
  84. public int Count
  85. {
  86. get
  87. {
  88. return this.configDict.Count;
  89. }
  90. }
  91. }
  92. }