ResourceManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using WingServerCommon.Config.Parameters;
  4. using WingServerCommon.Log;
  5. namespace WingServerCommon.ResourceManage
  6. {
  7. /// <summary>
  8. /// This class will load a glabal server resource
  9. /// </summary>
  10. public class ResourceManager
  11. {
  12. /// <summary>
  13. /// Notification Service Settings is from configuration file
  14. /// </summary>
  15. /// <value></value>
  16. private static Dictionary<string, SettingData> Settings { get; set; }
  17. private class SettingData
  18. {
  19. /// <summary>
  20. /// 重新加载json的路径
  21. /// </summary>
  22. /// <value></value>
  23. public string Path { get; set; } = "";
  24. /// <summary>
  25. /// 内容
  26. /// </summary>
  27. /// <value></value>
  28. public string Content { get; set; } = "";
  29. }
  30. static ResourceManager()
  31. {
  32. LoadSettings();
  33. }
  34. private static void LoadSettings()
  35. {
  36. Settings = new Dictionary<string, SettingData>();
  37. ForeachFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource","Languge"));
  38. }
  39. private static List<string> GetLagugeList()
  40. {
  41. var list = new List<string>();
  42. DirectoryInfo theFolder = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource", "Languge"));
  43. DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
  44. foreach (var item in dirInfo)
  45. {
  46. list.Add(item.Name);
  47. }
  48. return list;
  49. }
  50. /// <summary>
  51. /// 指定文件夹中的文件包括子文件夹的文件
  52. /// </summary>
  53. /// <param name="filePathByForeach">等待遍历的目录</param>
  54. /// <returns></returns>
  55. public static void ForeachFile(string filePathByForeach, string langugeKey = "")
  56. {
  57. DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach);
  58. DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
  59. FileInfo[] file = theFolder.GetFiles();//获取所在目录的文件
  60. foreach (FileInfo fileItem in file) //遍历文件
  61. {
  62. var json = File.ReadAllText(fileItem.FullName);
  63. //翻译文件支持在不同语言文件夹可以重名
  64. Settings.Add(langugeKey + fileItem.Name, new SettingData { Path = fileItem.FullName, Content = json });
  65. }
  66. //遍历文件夹
  67. foreach (DirectoryInfo NextFolder in dirInfo)
  68. {
  69. var tempName = string.Empty;
  70. var combinePath = Path.Combine("Resource","Languge");
  71. if (NextFolder.FullName.Contains(combinePath))
  72. {
  73. tempName = NextFolder.Name;
  74. }
  75. ForeachFile(NextFolder.FullName, tempName);
  76. }
  77. }
  78. /// <summary>
  79. /// 获取setting
  80. /// </summary>
  81. /// <param name="key"></param>
  82. /// <returns></returns>
  83. public static string GetSetting(string key)
  84. {
  85. try
  86. {
  87. key = key.EndsWith(".json") ? key : (key + ".json");
  88. return Settings.FirstOrDefault(s => s.Key == key).Value.Content;
  89. }
  90. catch (Exception ex)
  91. {
  92. Logger.WriteLineError($"GetSetting err{ex},key:{key}");
  93. return "";
  94. }
  95. }
  96. /// <summary>
  97. /// 重新加载setting
  98. /// </summary>
  99. public static void ReLoadSettings()
  100. {
  101. LoadSettings();
  102. }
  103. /// <summary>
  104. /// 重新加载单个setting
  105. /// </summary>
  106. public static void ReLoadSetting(string key)
  107. {
  108. var fileName = key.EndsWith(".json") ? key : (key + ".json");
  109. var settingData = Settings.FirstOrDefault(s => s.Key == fileName).Value;
  110. var json = File.ReadAllText(settingData.Path);
  111. Settings.Remove(fileName);
  112. Settings.Add(fileName, new SettingData { Path = settingData.Path, Content = json });
  113. }
  114. }
  115. }