SchemeDataService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using FrameWork.SDKManager;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using UnityEditor;
  7. using UnityEngine;
  8. public class SchemeDataService
  9. {
  10. public const string c_SDKCachePath = ".SDKCache";
  11. private static List<SchemeData> configList;
  12. private static List<string> configNameList;
  13. public static List<SchemeData> ConfigList
  14. {
  15. get
  16. {
  17. if (configList == null)
  18. {
  19. ReloadEditorSchemeData();
  20. }
  21. return configList;
  22. }
  23. }
  24. public static List<string> ConfigNameList
  25. {
  26. get
  27. {
  28. if(configNameList == null)
  29. {
  30. ReloadEditorSchemeData();
  31. }
  32. return configNameList;
  33. }
  34. }
  35. #region 存取配置
  36. /// <summary>
  37. ///加载编辑器设置
  38. /// </summary>
  39. public static void ReloadEditorSchemeData()
  40. {
  41. configList = new List<SchemeData>();
  42. configNameList = new List<string>();
  43. configNameList.Add("None");
  44. Dictionary<string, object> editConfig = ConfigEditorWindow.GetEditorConfigData(SDKEditorWindow.s_editorConfigName);
  45. if (editConfig != null)
  46. {
  47. List<object> list = (List<object>)editConfig[SDKEditorWindow.s_schemeKey];
  48. for (int i = 0; i < list.Count; i++)
  49. {
  50. SchemeData tmp = JsonUtility.FromJson<SchemeData>(list[i].ToString());
  51. configList.Add(tmp);
  52. configNameList.Add(tmp.SchemeName);
  53. }
  54. }
  55. }
  56. public static void UpdateSchemeData(SchemeData data)
  57. {
  58. bool isUpdate = false;
  59. for (int i = 0; i < ConfigList.Count; i++)
  60. {
  61. if(ConfigList[i].SchemeName == data.SchemeName)
  62. {
  63. isUpdate = true;
  64. ConfigList[i] = data;
  65. }
  66. }
  67. if(!isUpdate)
  68. {
  69. Debug.LogError("更新失败 没有找到对应的方案 " + data.SchemeName);
  70. }
  71. }
  72. public static void SaveEditorSchemeData()
  73. {
  74. Dictionary<string, object> editConfig = new Dictionary<string, object>();
  75. List<string> list = new List<string>();
  76. for (int i = 0; i < ConfigList.Count; i++)
  77. {
  78. list.Add(JsonUtility.ToJson(ConfigList[i]));
  79. }
  80. editConfig.Add(SDKEditorWindow.s_schemeKey, list);
  81. ConfigEditorWindow.SaveEditorConfigData(SDKEditorWindow.s_editorConfigName, editConfig);
  82. }
  83. /// <summary>
  84. /// 将传入的SchemeData保存到游戏可以读取的地方
  85. /// </summary>
  86. /// <param name="schemeData"></param>
  87. public static void SaveGameSchemeConfig(SchemeData schemeData)
  88. {
  89. Debug.Log("SaveGameSchemeConfig " + schemeData.LoginScheme.Count + " " + schemeData.SchemeName);
  90. if(schemeData != null)
  91. {
  92. Dictionary<string, SingleField> config = new Dictionary<string, SingleField>();
  93. config.Add(SDKManager.c_KeyName, new SingleField(JsonUtility.ToJson(schemeData)));
  94. ConfigEditorWindow.SaveData(SDKManager.c_ConfigName, config);
  95. ConfigManager.CleanCache();
  96. }
  97. else
  98. {
  99. File.Delete( ConfigEditorWindow.GetConfigPath(SDKManager.c_ConfigName));
  100. }
  101. }
  102. public static SchemeData CreateSchemeData(
  103. string schemeName,
  104. bool useNewSDKManager,
  105. List<LoginInterface> loginScheme,
  106. List<ADInterface> ADScheme,
  107. List<PayInterface> payScheme,
  108. List<LogInterface> logScheme,
  109. List<OtherSDKInterface> otherScheme)
  110. {
  111. SchemeData schemeData = new SchemeData();
  112. schemeData.SchemeName = schemeName;
  113. schemeData.UseNewSDKManager = useNewSDKManager;
  114. for (int i = 0; i < loginScheme.Count; i++)
  115. {
  116. schemeData.LoginScheme.Add(SerializeConfig(loginScheme[i]));
  117. }
  118. for (int i = 0; i < ADScheme.Count; i++)
  119. {
  120. schemeData.ADScheme.Add(SerializeConfig(ADScheme[i]));
  121. }
  122. for (int i = 0; i < payScheme.Count; i++)
  123. {
  124. schemeData.PayScheme.Add(SerializeConfig(payScheme[i]));
  125. }
  126. for (int i = 0; i < logScheme.Count; i++)
  127. {
  128. schemeData.LogScheme.Add(SerializeConfig(logScheme[i]));
  129. }
  130. for (int i = 0; i < otherScheme.Count; i++)
  131. {
  132. schemeData.OtherScheme.Add(SerializeConfig(otherScheme[i]));
  133. }
  134. return schemeData;
  135. }
  136. static SDKConfigData SerializeConfig(SDKInterfaceBase sdkInterface)
  137. {
  138. SDKConfigData result = new SDKConfigData();
  139. if (sdkInterface != null)
  140. {
  141. result.SDKName = sdkInterface.GetType().Name;
  142. result.SDKContent = JsonUtility.ToJson(sdkInterface);
  143. }
  144. else
  145. {
  146. result.SDKName = "Null";
  147. result.SDKContent = "";
  148. }
  149. return result;
  150. }
  151. #endregion
  152. #region 切换方案
  153. /// <summary>
  154. /// 切换方案
  155. /// 由于自动打包会调用这里,所以将切换宏定义的代码也放在此处,注意!
  156. /// </summary>
  157. /// <param name="SchemeName"></param>
  158. public static void ChangeScheme(string SchemeName)
  159. {
  160. SchemeData data = SDKManager.LoadGameSchemeConfig();
  161. string oldSchemeName = "None";
  162. Debug.Log("ChangeScheme " + SchemeName);
  163. if (!IsExitsSchemeName(SchemeName))
  164. {
  165. Debug.Log("->" + SchemeName + "<- 方案不存在! ");
  166. return;
  167. }
  168. if (data != null)
  169. {
  170. oldSchemeName = data.SchemeName;
  171. }
  172. //方案相同不切换
  173. if(SchemeName == oldSchemeName)
  174. {
  175. return;
  176. }
  177. //重新生成游戏内使用的配置
  178. SaveGameSchemeConfig(GetSchemeData(SchemeName));
  179. AssetDatabase.Refresh();
  180. }
  181. #region 功能函数
  182. public static bool IsExitsSchemeName(string name)
  183. {
  184. for (int i = 0; i < ConfigList.Count; i++)
  185. {
  186. if (ConfigList[i].SchemeName == name)
  187. {
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193. public static int GetSchemeIndex(string name)
  194. {
  195. for (int i = 0; i < ConfigList.Count; i++)
  196. {
  197. if (ConfigList[i].SchemeName == name)
  198. {
  199. return i;
  200. }
  201. }
  202. return -1;
  203. }
  204. public static SchemeData GetSchemeData(string name)
  205. {
  206. if (name == "None")
  207. return null;
  208. for (int i = 0; i < ConfigList.Count; i++)
  209. {
  210. if (ConfigList[i].SchemeName == name)
  211. {
  212. return ConfigList[i];
  213. }
  214. }
  215. throw new System.Exception("GetSchemeData Error not find ->" + name + "<-");
  216. }
  217. #endregion
  218. #region 文件操作
  219. /// <summary>
  220. /// 把当前Plugin目录下的所有文件存在 .SDKCache文件夹下,并加上schemeName前缀
  221. /// </summary>
  222. /// <param name="schemeName">方案名</param>
  223. public static void UnloadPluginFile(string schemeName)
  224. {
  225. string oldPath = Application.dataPath + "/Plugins";
  226. string newPath = Application.dataPath + "/" + c_SDKCachePath + "/" + schemeName + "Plugins";
  227. Debug.Log("SavePluginFile :oldPath: ->" + oldPath + "<- newPath: ->" + newPath + "<-");
  228. MoveFiles(oldPath, newPath);
  229. }
  230. /// <summary>
  231. /// 把当前schemeName目录下的所有文件存在 .SDKCache文件夹下
  232. /// </summary>
  233. /// <param name="schemeName">方案名</param>
  234. public static void UnloadSchemeFile(string schemeName)
  235. {
  236. string oldPath = Application.dataPath + "/" + schemeName;
  237. string newPath = Application.dataPath + "/" + c_SDKCachePath + "/" + schemeName;
  238. Debug.Log("SaveSchemeFile :oldPath: ->" + oldPath + "<- newPath: ->" + newPath + "<-");
  239. MoveFiles(oldPath, newPath);
  240. }
  241. /// <summary>
  242. /// 加载 .SDKCache 目录下的所有Plugin文件,放回Plugins目录
  243. /// 与SavePluginFile是相反操作
  244. /// </summary>
  245. /// <param name="schemeName">方案名</param>
  246. public static void LoadPluginFile(string schemeName)
  247. {
  248. string oldPath = Application.dataPath + "/" + c_SDKCachePath + "/" + schemeName + "Plugins";
  249. string newPath = Application.dataPath + "/Plugins";
  250. Debug.Log("LoadPluginFile :oldPath: ->" + oldPath + "<- newPath: ->" + newPath + "<-");
  251. MoveFiles(oldPath,newPath);
  252. }
  253. /// <summary>
  254. /// 加载 .SDKCache 目录下的方案文件,放回项目目录
  255. /// 与SaveSchemeFile是相反操作
  256. /// </summary>
  257. /// <param name="schemeName">方案名</param>
  258. public static void LoadSchemeFile(string schemeName)
  259. {
  260. string oldPath = Application.dataPath + "/" + c_SDKCachePath + "/" + schemeName;
  261. string newPath = Application.dataPath + "/" + schemeName;
  262. Debug.Log("LoadSchemeFile :oldPath: ->" + oldPath + "<- newPath: ->" + newPath + "<-");
  263. MoveFiles(oldPath, newPath);
  264. }
  265. /// <summary>
  266. /// 清空newPath,并把oldPath的文件全部复制到newPath中
  267. /// </summary>
  268. /// <param name="oldPath">旧路径</param>
  269. /// <param name="newPath">新路径</param>
  270. public static void MoveFiles(string oldPath,string newPath)
  271. {
  272. //删除目标文件夹下所有文件
  273. if (Directory.Exists(newPath))
  274. {
  275. FileTool.SafeDeleteDirectory(newPath);
  276. }
  277. else
  278. {
  279. FileTool.CreatPath(newPath);
  280. }
  281. if (Directory.Exists(oldPath))
  282. {
  283. //把当前文件加下的文件拷贝到旧文件夹下
  284. FileTool.SafeCopyDirectory(oldPath, newPath);
  285. FileTool.SafeDeleteDirectory(oldPath);
  286. }
  287. }
  288. #endregion
  289. #endregion
  290. #region 增删方案
  291. /// <summary>
  292. /// 新增方案
  293. /// </summary>
  294. /// <param name="name"></param>
  295. /// <returns></returns>
  296. public static SchemeData AddScheme(string name)
  297. {
  298. SchemeData data = new SchemeData();
  299. data.SchemeName = name;
  300. ConfigList.Add(data);
  301. ConfigNameList.Add(data.SchemeName);
  302. SaveEditorSchemeData();
  303. return data;
  304. }
  305. /// <summary>
  306. /// 删除方案
  307. /// </summary>
  308. /// <param name="data"></param>
  309. public static void DelectScheme(SchemeData data)
  310. {
  311. ConfigList.Remove(data);
  312. ConfigNameList.Remove(data.SchemeName);
  313. SaveEditorSchemeData();
  314. SaveGameSchemeConfig(null);
  315. string Path1 = Application.dataPath + "/" + c_SDKCachePath + "/" + data.SchemeName + "Plugins";
  316. string Path2 = Application.dataPath + "/" + c_SDKCachePath + "/" + data.SchemeName;
  317. string Path3 = Application.dataPath + "/Plugins";
  318. string Path4 = Application.dataPath + "/" + data.SchemeName;
  319. FileTool.SafeDeleteDirectory(Path1);
  320. FileTool.SafeDeleteDirectory(Path2);
  321. FileTool.SafeDeleteDirectory(Path3);
  322. FileTool.SafeDeleteDirectory(Path4);
  323. }
  324. #endregion
  325. }