/// <summary> /// 根据路径加载类对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <returns></returns> public T LoadData <T>(string path) where T : ExcelBase { if (string.IsNullOrEmpty(path)) { Debug.LogError("路径为空"); return(null); } if (m_AllExcelData.ContainsKey(path)) { Debug.Log("类已加载:" + path); return(m_AllExcelData[path] as T); } T data = BinarySerializeOpt.BinaryDeserializationRun <T>(path); #if UNITY_EDITOR if (data == null) { Debug.Log(path + "二进制加载失败,改为xml加载"); path = path.Replace("Binary", "Xml").Replace(".bytes", ".xml"); data = BinarySerializeOpt.XmlDeserialization <T>(path); } #endif if (data != null) { data.Init(); } m_AllExcelData.Add(path, data); return(data); }
/// <summary> /// 将xml转换为二进制 /// </summary> /// <param name="className"></param> public static void XmlToBinary(string className) { if (string.IsNullOrEmpty(className)) { return; } try { Type type = null; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { Type tempType = asm.GetType(className); if (tempType != null) { type = tempType; break; } } if (type != null) { string xmlPath = XMLPATH + className + ".xml"; string binaryPath = BINARYPATH + className + ".bytes"; object obj = BinarySerializeOpt.XmlDeserialization(xmlPath, type); BinarySerializeOpt.BinarySerialize(binaryPath, obj); Debug.Log(className + "xml转二进制成功!"); } } catch (Exception e) { Debug.LogError(className + "xml转二进制失败!" + e); } }
/// <summary> /// 加载data /// </summary> /// <param name="path">路径</param> /// <typeparam name="T">ExcelBase类</typeparam> /// <returns></returns> public T LoadData <T>(string path) where T : ExcelBase { if (string.IsNullOrEmpty(path)) { return(null); } if (m_ExcelDataDic.ContainsKey(path)) { return(m_ExcelDataDic[path] as T); } T data = null; data = BinarySerializeOpt.BinaryDeserialize <T>(path); //如果在编译器中 未生成二进制 转为xml读取 #if UNITY_EDITOR if (data == null) { Debug.LogWarning("LoadData 未生成Binary文件 path:" + path); path = path.Replace("Binary", "Xml").Replace(".bytes", ".xml"); data = BinarySerializeOpt.XmlDeserialize <T>(path); } #endif if (data != null) { data.Init(); } return(data); }
/// <summary> /// xml转 protobuf /// </summary> /// <param name="name"></param> private static void XmlToProtoBuf(string name) { if (string.IsNullOrEmpty(name)) { return; } try { Type type = null; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { Type tempType = asm.GetType(name); if (tempType != null) { type = tempType; break; } } if (type != null) { string xmlPath = XmlPath + name + ".xml"; string protobufPath = ProtobufPath + name + ".bytes"; object obj = BinarySerializeOpt.XmlDeserialize(xmlPath, type); BinarySerializeOpt.ProtoSerialize(protobufPath, obj); Debug.Log(name + "xml转Protobuf成功,xml转Protobuf成功路径为:" + protobufPath); } } catch { Debug.LogError(name + "xml转二进制失败!"); } }
static void XmlToBinary(string name) { try { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); Type type = null; foreach (Assembly assembly in assemblies) { type = assembly.GetType(name); if (type != null) { break; } } if (type != null) { string xmlPath = XML_DATA_PATH + name + ".xml"; object obj = BinarySerializeOpt.XmlDeserialize(type, xmlPath); string binaryPath = BINARY_DATA_PATH + name + ".bytes"; BinarySerializeOpt.BinarySerialize(binaryPath, obj); Debug.Log("Xml转Binary成功 path:" + binaryPath); } } catch (System.Exception) { Debug.LogError("XmlToBinary 转换失败 name:" + name); } }
public void ResponseData(byte[] data) { RegisterData register = BinarySerializeOpt.ProtoDeSerialize <RegisterData>(data); //Debug.Log("请求注册:" + register.name + register.password); PlayerPrefs.SetString(DataConst.PlayerName, register.name); PlayerPrefs.SetString(DataConst.PlayerPassword, register.password); }
void Start() { string path = Application.streamingAssetsPath + "/phone.bytes"; myPhone.name = "Apple"; myPhone.price = 7200; BinarySerializeOpt.ProtoSerialize(path, myPhone); Phone loadedPhone = BinarySerializeOpt.ProtoDeSerialize <Phone>(path); Debug.Log("loadedPhone.name " + loadedPhone.name); Debug.Log("loadedPhone.price " + loadedPhone.price); }
public static void TestProtobuf() { string path = "Assets/GameData/Data/ProtobufData/MonsterData.bytes"; MonsterData data = BinarySerializeOpt.ProtoDeSerialize <MonsterData>(path); foreach (var monster in data.AllMonster) { Debug.Log(monster.Id + " " + monster.Name + " " + monster.OutLook); } foreach (var monster in data.AllKing) { Debug.Log(monster.Id + " " + monster.Name + " " + monster.HP); } }
/// <summary> /// 实际的类转XML /// </summary> /// <param name="name"></param> static void ClassToXml(string name) { if (string.IsNullOrEmpty(name)) { return; } try { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); Type type = null; foreach (Assembly assembly in assemblies) { type = assembly.GetType(name); if (type != null) { break; } } if (type != null) { //反射创建类 var tempClass = Activator.CreateInstance(type); if (tempClass is ExcelBase) { //执行编译器下构造方法 (tempClass as ExcelBase).Construction(); } string xmlPath = XML_DATA_PATH + name + ".xml"; if (File.Exists(xmlPath)) { Debug.LogError("该类已生成过Xml,需删除后重新生成 path:" + xmlPath); } else { BinarySerializeOpt.XmlSerialize(xmlPath, tempClass); Debug.Log("类转Xml成功 path:" + xmlPath); } } } catch (System.Exception) { Debug.LogError("ClassToXml 转换失败 name:" + name); } }
//xml反序列化转成类 private static object XmlToObject(string name) { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); Type type = null; foreach (Assembly assembly in assemblies) { type = assembly.GetType(name); if (type != null) { break; } } if (type != null) { string xmlPath = XML_DATA_PATH + name + ".xml"; return(BinarySerializeOpt.XmlDeserialize(type, xmlPath)); } return(null); }
/// <summary> /// 反序列化xml到类 /// </summary> /// <param name="name"></param> /// <returns></returns> private static object GetObjectFromXml(string name) { Type type = null; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { Type tempType = asm.GetType(name); if (tempType != null) { type = tempType; break; } } if (type != null) { string xmlPath = XmlPath + name + ".xml"; return(BinarySerializeOpt.XmlDeserialize(xmlPath, type)); } return(null); }
/// <summary> /// xml反序列化 /// </summary> /// <param name="className"></param> /// <param name="dataObj"></param> /// <returns></returns> private static object GetObjFromXml(string className) { object dataObj = null; Type type = null; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { Type tempType = asm.GetType(className); if (tempType != null) { type = tempType; break; } } if (type != null) { string xmlPath = XMLPATH + className + ".xml"; dataObj = BinarySerializeOpt.XmlDeserialization(xmlPath, type); } return(dataObj); }
/// <summary> /// 检查本地热更信息与服务器热更信息比较 /// </summary> /// <returns></returns> private bool CheckLocalAndServerPatch() { if (!File.Exists(m_LocalXmlPath)) { return(true); } m_LocalInfo = BinarySerializeOpt.XmlDeserialize(m_LocalXmlPath, typeof(ServerInfo)) as ServerInfo; VersionInfo versionInfo1 = (VersionInfo)null; if (m_LocalInfo != null) { foreach (VersionInfo versionInfo2 in m_LocalInfo.GameVersion) { if (versionInfo2.Version == m_CurVersion) { versionInfo1 = versionInfo2; break; } } } return(versionInfo1 != null && m_GameVersion.Patches != null && (versionInfo1.Patches != null && m_GameVersion.Patches.Length != 0) && m_GameVersion.Patches[m_GameVersion.Patches.Length - 1].Version != versionInfo1.Patches[versionInfo1.Patches.Length - 1].Version); }
public void ResponseData(byte[] data) { LoginData loginData = BinarySerializeOpt.ProtoDeSerialize <LoginData>(data); string savedName = PlayerPrefs.GetString(DataConst.PlayerName); string savedPassword = PlayerPrefs.GetString(DataConst.PlayerPassword); LoginResult result = new LoginResult(); if (!string.IsNullOrEmpty(savedName) && loginData.name == savedName) { //用户存在 if (loginData.password == savedPassword) { //登陆成功 result.result = true; result.reason = "ojbk"; } else { //密码错误 result.result = false; result.reason = "密码错误"; } } else { //用户不存在 result.result = false; result.reason = "该用户暂未注册"; } byte[] buffer = BinarySerializeOpt.ProtoSerialize(result); AppFacade.instance.GetNetworkManager().OnReceiveData(ProtobufID.S_LoginResult, buffer); //Debug.Log("name " + loadedPhone.name); //Debug.Log("password " + loadedPhone.password); }
/// <summary> /// 实际的类转XML /// </summary> /// <param name="name"></param> private static void ClassToXml(string name) { if (string.IsNullOrEmpty(name)) { return; } try { Type type = null; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { Type tempType = asm.GetType(name); if (tempType != null) { type = tempType; break; } } if (type != null) { var temp = Activator.CreateInstance(type); if (temp is ExcelBase) { (temp as ExcelBase).Construction(); } string xmlPath = XmlPath + name + ".xml"; BinarySerializeOpt.Xmlserialize(xmlPath, temp); Debug.Log(name + "类转xml成功,xml路径为:" + xmlPath); } } catch { Debug.LogError(name + "类转xml失败!"); } }
/// <summary> /// 将运行中的实际类转成Xml /// </summary> /// <param name="className"></param> public static void ClassToXml(string className) { if (string.IsNullOrEmpty(className)) { return; } try { Type type = null; foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) { Type tempType = asm.GetType(className); if (tempType != null) { type = tempType; break; } } if (type != null) { var obj = Activator.CreateInstance(type); if (obj is ExcelBase) { (obj as ExcelBase).Construction(); } BinarySerializeOpt.XmlSerialize(XMLPATH + className + ".xml", obj); Debug.Log(className + "类转成Xml成功!"); } } catch (Exception e) { Debug.LogError(className + "类转xml失败!" + e); } }
private static void ExcelToXml(string name) { string className = ""; string xmlName = ""; string excelName = ""; //第一步,读取Reg文件,确定类的结构 Dictionary <string, SheetClass> allSheetClassDic = ReadReg(name, ref excelName, ref xmlName, ref className); //第二步,读取excel里面的数据 string excelPath = ExcelPath + excelName; Dictionary <string, SheetData> sheetDataDic = new Dictionary <string, SheetData>(); try { using (FileStream stream = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (ExcelPackage package = new ExcelPackage(stream)) { ExcelWorksheets worksheetArray = package.Workbook.Worksheets; for (int i = 0; i < worksheetArray.Count; i++) { SheetData sheetData = new SheetData(); ExcelWorksheet worksheet = worksheetArray[i + 1]; SheetClass sheetClass = allSheetClassDic[worksheet.Name]; int colCount = worksheet.Dimension.End.Column; int rowCount = worksheet.Dimension.End.Row; for (int n = 0; n < sheetClass.VarList.Count; n++) { sheetData.AllName.Add(sheetClass.VarList[n].Name); sheetData.AllType.Add(sheetClass.VarList[n].Type); } for (int m = 1; m < rowCount; m++) { RowData rowData = new RowData(); int n = 0; if (string.IsNullOrEmpty(sheetClass.SplitStr) && sheetClass.ParentVar != null && !string.IsNullOrEmpty(sheetClass.ParentVar.Foregin)) { rowData.ParnetVlue = worksheet.Cells[m + 1, 1].Value.ToString().Trim(); n = 1; } for (; n < colCount; n++) { ExcelRange range = worksheet.Cells[m + 1, n + 1]; string value = ""; if (range.Value != null) { value = range.Value.ToString().Trim(); } string colValue = worksheet.Cells[1, n + 1].Value.ToString().Trim(); rowData.RowDataDic.Add(GetNameFormCol(sheetClass.VarList, colValue), value); } sheetData.AllData.Add(rowData); } sheetDataDic.Add(worksheet.Name, sheetData); } } } } catch (Exception e) { Debug.LogError(e); return; } //根据类的结构,创建类,并且给每个变量赋值(从excel里读出来的值) object objClass = CreateClass(className); List <string> outKeyList = new List <string>(); foreach (string str in allSheetClassDic.Keys) { SheetClass sheetClass = allSheetClassDic[str]; if (sheetClass.Depth == 1) { outKeyList.Add(str); } } for (int i = 0; i < outKeyList.Count; i++) { ReadDataToClass(objClass, allSheetClassDic[outKeyList[i]], sheetDataDic[outKeyList[i]], allSheetClassDic, sheetDataDic, null); } BinarySerializeOpt.Xmlserialize(XmlPath + xmlName, objClass); //BinarySerializeOpt.BinarySerilize(BinaryPath + className + ".bytes", objClass); Debug.Log(excelName + "表导入unity完成!"); AssetDatabase.Refresh(); }
public static void ExcelToXml(string xmlRegName) { string className = ""; string excelName = ""; string xmlName = ""; //读取reg文件数据 Dictionary <string, SheetClass> allSheetClassDic = ReadReg(xmlRegName, ref className, ref excelName, ref xmlName); //读取excel文件数据 string excelPath = EXCELPATH + excelName; Dictionary <string, SheetData> allSheetDataDic = new Dictionary <string, SheetData>(); try { using (FileStream stream = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (ExcelPackage excelPackage = new ExcelPackage(stream)) { ExcelWorksheets worksheetArray = excelPackage.Workbook.Worksheets; for (int i = 0; i < worksheetArray.Count; i++) { SheetData sheetData = new SheetData(); ExcelWorksheet worksheet = worksheetArray[i + 1];//索引从1开始 string key = GetDicKey(allSheetClassDic, worksheet.Name); if (string.IsNullOrEmpty(key)) { Debug.Log("配置表在reg文件中不存在!请检查"); return; } SheetClass sheetClass = allSheetClassDic[key]; int colCount = worksheet.Dimension.End.Column; int rowCount = worksheet.Dimension.End.Row; for (int j = 0; j < sheetClass.VarList.Count; j++) { sheetData.AllName.Add(sheetClass.VarList[j].Name); sheetData.AllType.Add(sheetClass.VarList[j].Type); } for (int j = 1; j < rowCount; j++) { RowData rowData = new RowData(); int k = 0; if (string.IsNullOrEmpty(sheetClass.SplitStr) && sheetClass.ParentVar != null && !string.IsNullOrEmpty(sheetClass.ParentVar.Foregin)) { rowData.parentValue = worksheet.Cells[j + 1, 1].Value.ToString().Trim(); k = 1; } for (; k < colCount; k++) { ExcelRange range = worksheet.Cells[j + 1, k + 1];//索引从1开始 string value = ""; if (range.Value != null) { value = range.Value.ToString().Trim(); } string colValue = worksheet.Cells[1, k + 1].Value.ToString().Trim(); rowData.RowDataDic.Add(GetNameFormCol(sheetClass.VarList, colValue), value); } sheetData.AllData.Add(rowData); } allSheetDataDic.Add(worksheet.Name, sheetData); } } } } catch (Exception e) { Console.WriteLine(e); throw; } //根据类结构创建类,并将execl数据将变量赋值,然后调用xml序列化 object objClass = CreatClass(className); List <SheetClass> outKeyList = new List <SheetClass>(); foreach (string key in allSheetClassDic.Keys) { SheetClass sheet = allSheetClassDic[key]; if (sheet.Depth == 1) { outKeyList.Add(sheet); } } for (int i = 0; i < outKeyList.Count; i++) { ReadDataToClass(objClass, allSheetClassDic[ParseDicKey(outKeyList[i])], allSheetDataDic[outKeyList[i].SheetName], allSheetClassDic, allSheetDataDic, null); } BinarySerializeOpt.XmlSerialize(XMLPATH + xmlName, objClass); Debug.Log(excelName + "表导入完成!"); AssetDatabase.Refresh(); }
private static void ExcelToXml(string name) { string className = ""; string from = ""; string to = ""; //储存所有变量 sheet名 SheetClass Dictionary <string, SheetClass> allSheetClassDic = ReadReg(name, ref className, ref from, ref to); //储存所有data sheet名 sheetdata Dictionary <string, SheetData> allSheetDataDic = new Dictionary <string, SheetData>(); string excelPath = EXCEL_DATA_PATH + from; // string xmlPath = XML_DATA_PATH+to; //读取excel文件 try{ using (FileStream fs = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (ExcelPackage package = new ExcelPackage(fs)){ ExcelWorksheets workSheets = package.Workbook.Worksheets; for (int i = 1; i <= workSheets.Count; i++) { ExcelWorksheet workSheet = workSheets[i]; int colCount = workSheet.Dimension.End.Column; int rowCount = workSheet.Dimension.End.Row; SheetData sheetData = new SheetData();//储存这一页的信息 //Reg中sheet的信息 SheetClass sheetClass = allSheetClassDic[workSheet.Name]; for (int j = 0; j < sheetClass.VarList.Count; j++) { VarClass curVarClass = sheetClass.VarList[j]; //储存 变量名 类型 sheetData.AllName.Add(curVarClass.Name); sheetData.AllType.Add(curVarClass.Type); } //读取excel中的数据 //第一行是标题 所以跳过 for (int row = 1; row < rowCount; row++) { RowData data = new RowData(); int col = 0; //如果这页sheet是外键数据 则第一列对应mainKey 数据从第二列开始 if (string.IsNullOrEmpty(sheetClass.SplitStr) && !string.IsNullOrEmpty(sheetClass.ParentVar.Foreign)) { data.ParentKey = workSheet.Cells[row + 1, 1].Value.ToString().Trim(); col = 1; } for (; col < colCount; col++) { //每一行的信息 ExcelRange range = workSheet.Cells[row + 1, col + 1]; string colName = workSheet.Cells[1, col + 1].Value.ToString(); string value = range.Value != null?range.Value.ToString().Trim() : ""; data.RowDataDic.Add(GetNameFormCol(sheetClass.VarList, colName), value); } sheetData.AllRowData.Add(data); } allSheetDataDic.Add(workSheet.Name, sheetData); } } } catch (System.Exception e) { Debug.LogError("XmlToExcel:Excel写入错误 " + e); return; } //创建类 并赋值 object objClass = CreateClass(className); List <string> outKeyList = new List <string>(); foreach (var item in allSheetClassDic) { SheetClass sheetClass = item.Value; if (sheetClass.Depth == 1) { outKeyList.Add(item.Key); } } for (int i = 0; i < outKeyList.Count; i++) { string key = outKeyList[i]; ReadDataToClass(objClass, allSheetClassDic[key], allSheetDataDic[key], allSheetClassDic, allSheetDataDic); } string xmlPath = XML_DATA_PATH + name + ".xml"; object obj = BinarySerializeOpt.XmlSerialize(xmlPath, objClass); //转成二进制 // BinarySerializeOpt.BinarySerialize(BINARY_DATA_PATH, obj); Debug.Log("Excel转Xml完成!" + from + "-->" + to); AssetDatabase.Refresh(); }
/// <summary> /// 检查本地热更信息与服务器热更信息比较 /// </summary> /// <returns> false 不需要更新, true 需要更新</returns> private bool CheckLocalAndServerPatch() { #if JSON //if (!File.Exists(m_localJsonPath)) // return true; Branches branch = null; var branches = m_serverInfoDataModule.GameVersionInfos.FirstOrDefault(i => i.GameVersion == m_curVersion); Debug.Assert(null != branches, "未找到与当前App Version 所匹配的更新信息 : App Version " + m_curVersion); branch = branches.Branches.FirstOrDefault(i => i.BranchName == m_branchName); Debug.Assert(null != branches, "未找到与当前Branch Name 所匹配的更新信息 : Branch Name " + m_branchName); var assetVersion = LocalVersionInfoManager.Instance.GetVersionInfo(m_curVersion, m_branchName); if (null != assetVersion && "" != assetVersion) { // 获取与本地版本相匹配的版本序号 var localPatch = branch.Patches.FirstOrDefault(i => i.Version == assetVersion); m_localVersionIndex = branch.Patches.IndexOf(localPatch) + 1; m_localVersion = branch.Patches[m_localVersionIndex - 1].Version; Debug.Log("当前本地资源版本 : " + m_localVersion); } m_targetVersion = branch.Patches[m_currentBranch.Patches.Count - 1].Version; return("" != assetVersion && null != m_currentBranch.Patches && assetVersion != m_currentBranch.Patches[m_currentBranch.Patches.Count - 1].Version); // // 读取本地版本信息 // StreamReader sr = new StreamReader(m_localJsonPath); // var content = sr.ReadToEnd(); // m_localInfoDataModule = new ServerInfoDataModule(JsonMapper.ToObject(content)); // sr.Close(); // // 匹配与当前APP Version所匹配的资源信息 // //GameVersionInfo matchInfo = null; // if (null != m_localInfoDataModule) // { // var info = m_localInfoDataModule.GameVersionInfos.FirstOrDefault(i => i.GameVersion == m_curVersion); // if (null != info) // { // // 获取当前本地资源版本 // branch = info.Branches.FirstOrDefault(i => i.BranchName == m_branchName); // m_localVersionIndex = branch.Patches.Count; // m_localVersion = branch.Patches[m_localVersionIndex - 1].Version; // Debug.Log("当前本地资源版本 : " + m_localVersion); // } //} // // 与服务端对应的APP Version相关信息进行比对, 如果当前的热更好与服务器端最新的一样就返回true; // return null != branch && // null != m_currentBranch.Patches && // (null != branch.Patches && m_currentBranch.Patches.Count != 0) && // m_currentBranch.Patches[m_currentBranch.Patches.Count - 1].Version != m_localVersion; #elif XML if (!File.Exists(m_localXmlPath)) { return(true); } m_localInfo = BinarySerializeOpt.XmlDeserialize(m_localXmlPath, typeof(ServerInfo)) as ServerInfo; VersionInfo versionInfo1 = null; if (m_localInfo != null) { foreach (VersionInfo versionInfo2 in m_localInfo.GameVersion) { if (versionInfo2.Version == m_curVersion) { versionInfo1 = versionInfo2; break; } } } return(versionInfo1 != null && m_gameVersion.Patches != null && (versionInfo1.Patches != null && m_gameVersion.Patches.Length != 0) && m_gameVersion.Patches[m_gameVersion.Patches.Length - 1].Version != versionInfo1.Patches[versionInfo1.Patches.Length - 1].Version); #endif }