123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System.Collections.Generic;
- using System.Linq;
- using WingServerCommon.Config.Parameters;
- using WingServerCommon.Log;
- namespace WingServerCommon.ResourceManage
- {
- /// <summary>
- /// This class will load a glabal server resource
- /// </summary>
- public class ResourceManager
- {
- /// <summary>
- /// Notification Service Settings is from configuration file
- /// </summary>
- /// <value></value>
- private static Dictionary<string, SettingData> Settings { get; set; }
- private class SettingData
- {
- /// <summary>
- /// 重新加载json的路径
- /// </summary>
- /// <value></value>
- public string Path { get; set; } = "";
- /// <summary>
- /// 内容
- /// </summary>
- /// <value></value>
- public string Content { get; set; } = "";
- }
- static ResourceManager()
- {
- LoadSettings();
- }
- private static void LoadSettings()
- {
- Settings = new Dictionary<string, SettingData>();
- ForeachFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource","Languge"));
- }
- private static List<string> GetLagugeList()
- {
- var list = new List<string>();
- DirectoryInfo theFolder = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource", "Languge"));
- DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
- foreach (var item in dirInfo)
- {
- list.Add(item.Name);
- }
- return list;
- }
- /// <summary>
- /// 指定文件夹中的文件包括子文件夹的文件
- /// </summary>
- /// <param name="filePathByForeach">等待遍历的目录</param>
- /// <returns></returns>
- public static void ForeachFile(string filePathByForeach, string langugeKey = "")
- {
- DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach);
- DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
- FileInfo[] file = theFolder.GetFiles();//获取所在目录的文件
- foreach (FileInfo fileItem in file) //遍历文件
- {
- var json = File.ReadAllText(fileItem.FullName);
- //翻译文件支持在不同语言文件夹可以重名
- Settings.Add(langugeKey + fileItem.Name, new SettingData { Path = fileItem.FullName, Content = json });
- }
- //遍历文件夹
- foreach (DirectoryInfo NextFolder in dirInfo)
- {
- var tempName = string.Empty;
- var combinePath = Path.Combine("Resource","Languge");
- if (NextFolder.FullName.Contains(combinePath))
- {
- tempName = NextFolder.Name;
- }
- ForeachFile(NextFolder.FullName, tempName);
- }
- }
- /// <summary>
- /// 获取setting
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static string GetSetting(string key)
- {
- try
- {
- key = key.EndsWith(".json") ? key : (key + ".json");
- return Settings.FirstOrDefault(s => s.Key == key).Value.Content;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"GetSetting err{ex},key:{key}");
- return "";
- }
- }
- /// <summary>
- /// 重新加载setting
- /// </summary>
- public static void ReLoadSettings()
- {
- LoadSettings();
- }
- /// <summary>
- /// 重新加载单个setting
- /// </summary>
- public static void ReLoadSetting(string key)
- {
- var fileName = key.EndsWith(".json") ? key : (key + ".json");
- var settingData = Settings.FirstOrDefault(s => s.Key == fileName).Value;
- var json = File.ReadAllText(settingData.Path);
- Settings.Remove(fileName);
- Settings.Add(fileName, new SettingData { Path = settingData.Path, Content = json });
- }
- }
- }
|