123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- using FrameWork.SDKManager;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- public class SchemeDataService
- {
- public const string c_SDKCachePath = ".SDKCache";
- private static List<SchemeData> configList;
- private static List<string> configNameList;
- public static List<SchemeData> ConfigList
- {
- get
- {
- if (configList == null)
- {
- ReloadEditorSchemeData();
- }
- return configList;
- }
- }
- public static List<string> ConfigNameList
- {
- get
- {
- if(configNameList == null)
- {
- ReloadEditorSchemeData();
- }
- return configNameList;
- }
- }
- #region 存取配置
- /// <summary>
- ///加载编辑器设置
- /// </summary>
- public static void ReloadEditorSchemeData()
- {
- configList = new List<SchemeData>();
- configNameList = new List<string>();
- configNameList.Add("None");
- Dictionary<string, object> editConfig = ConfigEditorWindow.GetEditorConfigData(SDKEditorWindow.s_editorConfigName);
- if (editConfig != null)
- {
- List<object> list = (List<object>)editConfig[SDKEditorWindow.s_schemeKey];
- for (int i = 0; i < list.Count; i++)
- {
- SchemeData tmp = JsonUtility.FromJson<SchemeData>(list[i].ToString());
- configList.Add(tmp);
- configNameList.Add(tmp.SchemeName);
- }
- }
- }
- public static void UpdateSchemeData(SchemeData data)
- {
- bool isUpdate = false;
- for (int i = 0; i < ConfigList.Count; i++)
- {
- if(ConfigList[i].SchemeName == data.SchemeName)
- {
- isUpdate = true;
- ConfigList[i] = data;
- }
- }
- if(!isUpdate)
- {
- Debug.LogError("更新失败 没有找到对应的方案 " + data.SchemeName);
- }
- }
- public static void SaveEditorSchemeData()
- {
- Dictionary<string, object> editConfig = new Dictionary<string, object>();
- List<string> list = new List<string>();
- for (int i = 0; i < ConfigList.Count; i++)
- {
- list.Add(JsonUtility.ToJson(ConfigList[i]));
- }
- editConfig.Add(SDKEditorWindow.s_schemeKey, list);
- ConfigEditorWindow.SaveEditorConfigData(SDKEditorWindow.s_editorConfigName, editConfig);
- }
- /// <summary>
- /// 将传入的SchemeData保存到游戏可以读取的地方
- /// </summary>
- /// <param name="schemeData"></param>
- public static void SaveGameSchemeConfig(SchemeData schemeData)
- {
- Debug.Log("SaveGameSchemeConfig " + schemeData.LoginScheme.Count + " " + schemeData.SchemeName);
- if(schemeData != null)
- {
- Dictionary<string, SingleField> config = new Dictionary<string, SingleField>();
- config.Add(SDKManager.c_KeyName, new SingleField(JsonUtility.ToJson(schemeData)));
- ConfigEditorWindow.SaveData(SDKManager.c_ConfigName, config);
- ConfigManager.CleanCache();
- }
- else
- {
- File.Delete( ConfigEditorWindow.GetConfigPath(SDKManager.c_ConfigName));
- }
- }
- public static SchemeData CreateSchemeData(
- string schemeName,
- bool useNewSDKManager,
- List<LoginInterface> loginScheme,
- List<ADInterface> ADScheme,
- List<PayInterface> payScheme,
- List<LogInterface> logScheme,
- List<OtherSDKInterface> otherScheme)
- {
- SchemeData schemeData = new SchemeData();
- schemeData.SchemeName = schemeName;
- schemeData.UseNewSDKManager = useNewSDKManager;
- for (int i = 0; i < loginScheme.Count; i++)
- {
- schemeData.LoginScheme.Add(SerializeConfig(loginScheme[i]));
- }
- for (int i = 0; i < ADScheme.Count; i++)
- {
- schemeData.ADScheme.Add(SerializeConfig(ADScheme[i]));
- }
- for (int i = 0; i < payScheme.Count; i++)
- {
- schemeData.PayScheme.Add(SerializeConfig(payScheme[i]));
- }
- for (int i = 0; i < logScheme.Count; i++)
- {
- schemeData.LogScheme.Add(SerializeConfig(logScheme[i]));
- }
- for (int i = 0; i < otherScheme.Count; i++)
- {
- schemeData.OtherScheme.Add(SerializeConfig(otherScheme[i]));
- }
- return schemeData;
- }
- static SDKConfigData SerializeConfig(SDKInterfaceBase sdkInterface)
- {
- SDKConfigData result = new SDKConfigData();
- if (sdkInterface != null)
- {
- result.SDKName = sdkInterface.GetType().Name;
- result.SDKContent = JsonUtility.ToJson(sdkInterface);
- }
- else
- {
- result.SDKName = "Null";
- result.SDKContent = "";
- }
- return result;
- }
- #endregion
- #region 切换方案
- /// <summary>
- /// 切换方案
- /// 由于自动打包会调用这里,所以将切换宏定义的代码也放在此处,注意!
- /// </summary>
- /// <param name="SchemeName"></param>
- public static void ChangeScheme(string SchemeName)
- {
- SchemeData data = SDKManager.LoadGameSchemeConfig();
- string oldSchemeName = "None";
- Debug.Log("ChangeScheme " + SchemeName);
- if (!IsExitsSchemeName(SchemeName))
- {
- Debug.Log("->" + SchemeName + "<- 方案不存在! ");
- return;
- }
- if (data != null)
- {
- oldSchemeName = data.SchemeName;
- }
- //方案相同不切换
- if(SchemeName == oldSchemeName)
- {
- return;
- }
- //重新生成游戏内使用的配置
- SaveGameSchemeConfig(GetSchemeData(SchemeName));
- AssetDatabase.Refresh();
- }
- #region 功能函数
- public static bool IsExitsSchemeName(string name)
- {
- for (int i = 0; i < ConfigList.Count; i++)
- {
- if (ConfigList[i].SchemeName == name)
- {
- return true;
- }
- }
- return false;
- }
- public static int GetSchemeIndex(string name)
- {
- for (int i = 0; i < ConfigList.Count; i++)
- {
- if (ConfigList[i].SchemeName == name)
- {
- return i;
- }
- }
- return -1;
- }
- public static SchemeData GetSchemeData(string name)
- {
- if (name == "None")
- return null;
- for (int i = 0; i < ConfigList.Count; i++)
- {
- if (ConfigList[i].SchemeName == name)
- {
- return ConfigList[i];
- }
- }
- throw new System.Exception("GetSchemeData Error not find ->" + name + "<-");
- }
- #endregion
- #region 文件操作
- /// <summary>
- /// 把当前Plugin目录下的所有文件存在 .SDKCache文件夹下,并加上schemeName前缀
- /// </summary>
- /// <param name="schemeName">方案名</param>
- public static void UnloadPluginFile(string schemeName)
- {
- string oldPath = Application.dataPath + "/Plugins";
- string newPath = Application.dataPath + "/" + c_SDKCachePath + "/" + schemeName + "Plugins";
- Debug.Log("SavePluginFile :oldPath: ->" + oldPath + "<- newPath: ->" + newPath + "<-");
- MoveFiles(oldPath, newPath);
- }
- /// <summary>
- /// 把当前schemeName目录下的所有文件存在 .SDKCache文件夹下
- /// </summary>
- /// <param name="schemeName">方案名</param>
- public static void UnloadSchemeFile(string schemeName)
- {
- string oldPath = Application.dataPath + "/" + schemeName;
- string newPath = Application.dataPath + "/" + c_SDKCachePath + "/" + schemeName;
- Debug.Log("SaveSchemeFile :oldPath: ->" + oldPath + "<- newPath: ->" + newPath + "<-");
- MoveFiles(oldPath, newPath);
- }
- /// <summary>
- /// 加载 .SDKCache 目录下的所有Plugin文件,放回Plugins目录
- /// 与SavePluginFile是相反操作
- /// </summary>
- /// <param name="schemeName">方案名</param>
- public static void LoadPluginFile(string schemeName)
- {
- string oldPath = Application.dataPath + "/" + c_SDKCachePath + "/" + schemeName + "Plugins";
- string newPath = Application.dataPath + "/Plugins";
- Debug.Log("LoadPluginFile :oldPath: ->" + oldPath + "<- newPath: ->" + newPath + "<-");
- MoveFiles(oldPath,newPath);
- }
- /// <summary>
- /// 加载 .SDKCache 目录下的方案文件,放回项目目录
- /// 与SaveSchemeFile是相反操作
- /// </summary>
- /// <param name="schemeName">方案名</param>
- public static void LoadSchemeFile(string schemeName)
- {
- string oldPath = Application.dataPath + "/" + c_SDKCachePath + "/" + schemeName;
- string newPath = Application.dataPath + "/" + schemeName;
- Debug.Log("LoadSchemeFile :oldPath: ->" + oldPath + "<- newPath: ->" + newPath + "<-");
- MoveFiles(oldPath, newPath);
- }
- /// <summary>
- /// 清空newPath,并把oldPath的文件全部复制到newPath中
- /// </summary>
- /// <param name="oldPath">旧路径</param>
- /// <param name="newPath">新路径</param>
- public static void MoveFiles(string oldPath,string newPath)
- {
- //删除目标文件夹下所有文件
- if (Directory.Exists(newPath))
- {
- FileTool.SafeDeleteDirectory(newPath);
- }
- else
- {
- FileTool.CreatPath(newPath);
- }
- if (Directory.Exists(oldPath))
- {
- //把当前文件加下的文件拷贝到旧文件夹下
- FileTool.SafeCopyDirectory(oldPath, newPath);
- FileTool.SafeDeleteDirectory(oldPath);
- }
- }
- #endregion
- #endregion
- #region 增删方案
- /// <summary>
- /// 新增方案
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static SchemeData AddScheme(string name)
- {
- SchemeData data = new SchemeData();
- data.SchemeName = name;
- ConfigList.Add(data);
- ConfigNameList.Add(data.SchemeName);
- SaveEditorSchemeData();
- return data;
- }
- /// <summary>
- /// 删除方案
- /// </summary>
- /// <param name="data"></param>
- public static void DelectScheme(SchemeData data)
- {
- ConfigList.Remove(data);
- ConfigNameList.Remove(data.SchemeName);
- SaveEditorSchemeData();
- SaveGameSchemeConfig(null);
- string Path1 = Application.dataPath + "/" + c_SDKCachePath + "/" + data.SchemeName + "Plugins";
- string Path2 = Application.dataPath + "/" + c_SDKCachePath + "/" + data.SchemeName;
- string Path3 = Application.dataPath + "/Plugins";
- string Path4 = Application.dataPath + "/" + data.SchemeName;
- FileTool.SafeDeleteDirectory(Path1);
- FileTool.SafeDeleteDirectory(Path2);
- FileTool.SafeDeleteDirectory(Path3);
- FileTool.SafeDeleteDirectory(Path4);
- }
- #endregion
- }
|