StartConfigComponent.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace Model
  5. {
  6. [ObjectEvent]
  7. public class StartConfigComponentEvent : ObjectEvent<StartConfigComponent>, IAwake<string, int>
  8. {
  9. public void Awake(string a, int b)
  10. {
  11. this.Get().Awake(a, b);
  12. }
  13. }
  14. public class StartConfigComponent: Component
  15. {
  16. private readonly List<StartConfig> allConfigs = new List<StartConfig>();
  17. private readonly Dictionary<int, StartConfig> configDict = new Dictionary<int, StartConfig>();
  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 StartConfig MapConfig { get; private set; }
  23. public void Awake(string path, int appId)
  24. {
  25. string[] ss = File.ReadAllText(path).Split('\n');
  26. foreach (string s in ss)
  27. {
  28. string s2 = s.Trim();
  29. if (s2 == "")
  30. {
  31. continue;
  32. }
  33. try
  34. {
  35. StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
  36. this.allConfigs.Add(startConfig);
  37. this.configDict.Add(startConfig.AppId, startConfig);
  38. if (startConfig.AppType.Is(AppType.Realm))
  39. {
  40. this.RealmConfig = startConfig;
  41. }
  42. if (startConfig.AppType.Is(AppType.Location))
  43. {
  44. LocationConfig = startConfig;
  45. }
  46. if (startConfig.AppType.Is(AppType.Map))
  47. {
  48. MapConfig = startConfig;
  49. }
  50. }
  51. catch (Exception)
  52. {
  53. Log.Error($"config错误: {s2}");
  54. }
  55. }
  56. this.StartConfig = this.Get(appId);
  57. }
  58. public StartConfig Get(int id)
  59. {
  60. try
  61. {
  62. return this.configDict[id];
  63. }
  64. catch (Exception e)
  65. {
  66. throw new Exception($"not found startconfig: {id}", e);
  67. }
  68. }
  69. public StartConfig[] GetAll()
  70. {
  71. return this.allConfigs.ToArray();
  72. }
  73. }
  74. }