/// <summary> /// 初始化接口 /// </summary> public static void Init() { mAttrib2Item = LPCMapping.Empty; mItem2Attrib = LPCMapping.Empty; // 载入字段配置表 mFieldsCsv = CsvFileMgr.Load("fields"); int itemClassId; foreach (CsvRow data in mFieldsCsv.rows) { // 获取属性对应的item_class_id itemClassId = data.Query <LPCValue>("item_class_id").AsInt; // 该属性没有绑定道具 if (itemClassId == 0) { continue; } // 记录数据 mAttrib2Item.Add(data.Query <string>("field"), itemClassId); mItem2Attrib.Add(itemClassId, data.Query <string>("field")); } }
/// <summary> /// 加载某个csv /// </summary> public static CsvFile Load(string csvName) { try { // 载入资源 string assetPath = ConfigMgr.ETC_PATH + "/" + csvName + CSV_EXT; byte[] csvBytes = ResourceMgr.Instance.LoadByte(assetPath); // 资源不存在 if (csvBytes == null || csvBytes.Length == 0) { return(null); } // 反序列化 MemoryStream csvStr = new MemoryStream(csvBytes, 0, csvBytes.Length, true, true); CsvFile csv = CsvFileMgr.Deserialize(csvStr); csvStr.Close(); // 返回数据 return(csv); } catch (Exception e) { NIDebug.Log(e.Message); return(null); } finally { // do something } }
/// <summary> /// 载入地资源映射目录 /// </summary> private static IEnumerator LoadResourceDictFile() { // 本地版本文件不存在 string filePath = string.Format("{0}/{1}.bytes", ConfigMgr.ASSETBUNDLES_PATH, RESOURCE_DICT); if (!File.Exists(filePath)) { NIDebug.Log("找不到本地资源映射目录。"); yield break; } // 载入本地版本文件 try { // 读取文件 byte[] verData = File.ReadAllBytes(filePath); // 反序列化 MemoryStream csvStr = new MemoryStream(verData, 0, verData.Length, true, true); mResourceDictCsv = CsvFileMgr.Deserialize(csvStr); csvStr.Close(); verData = null; } catch (Exception e) { NIDebug.LogException(e); } }
/// <summary> /// 载入随包资源版本 /// </summary> /// <returns>The streaming version file.</returns> public static IEnumerator LoadStreamingVersionFile() { // 重新new一个数据 mNewVersionCsv = new CsvFile("new_version"); // 载入文件 WWW www = new WWW(ConfigMgr.GetStreamingPathWWW(string.Format("{0}.bytes", VERSION_NAME))); yield return(www); // 等待资源加载完成 while (!www.isDone) { yield return(null); } // 文件载入失败, 获取文件不存在 if (!string.IsNullOrEmpty(www.error) || www.bytes.Length <= 0) { yield break; } // 反序列化 MemoryStream csvStr = new MemoryStream(www.bytes, 0, www.bytes.Length, true, true); mNewVersionCsv = CsvFileMgr.Deserialize(csvStr); csvStr.Close(); // 释放www www.Dispose(); }
/// <summary> /// 初始化开始场景中的资源 /// </summary> /// <param name="_files">Files.</param> private void InitStartRes() { // 初始化开始场景语言 // LocalizationMgr.InitStartRes(); // 初始化start场景csv CsvFileMgr.InitStartCsv(); }
/// <summary> /// 生成资源-->AssetBundle映射表 /// </summary> private static void GenResourceDictFile(string sourcePath, AssetBundleManifest manifest) { // 保存的路径是否存在,不存在则创建一个 // csv保存的地址 string dirpath = string.Format("{0}resource_dict.csv", sourcePath); FileInfo fi = new FileInfo(dirpath); if (!fi.Directory.Exists) { fi.Directory.Create(); } // csv文件写入 FileStream fs = new FileStream(dirpath, System.IO.FileMode.Create, System.IO.FileAccess.Write); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); // 创建csv文件前面几行 sw.WriteLine(WriteRow(new string[] { "# resource_dict.csv", "" })); sw.WriteLine(WriteRow(new string[] { "# 资源目录", "" })); sw.WriteLine(WriteRow(new string[] { "# 资源路径", "bundle名" })); sw.WriteLine(WriteRow(new string[] { "string", "string" })); sw.WriteLine(WriteRow(new string[] { "path", "bundle" })); // 遍历该文件下的所有AssetBundles foreach (string abName in manifest.GetAllAssetBundles()) { // 载入assetBundle string abPath = string.Format("{0}{1}", sourcePath, abName); AssetBundle assetBundle = AssetBundle.LoadFromFile(abPath); // 遍历该assetBundle下所有资源 foreach (string bundle in assetBundle.GetAllAssetNames()) { // 根据路径载入资源 UnityEngine.Object ob = AssetDatabase.LoadMainAssetAtPath(bundle); string path = AssetDatabase.GetAssetPath(ob); // 写入数据 sw.WriteLine(WriteRow(new string[] { path, abName })); } // 释放资源 assetBundle.Unload(true); } //关闭写入 sw.Close(); fs.Close(); // 讲csv文件转成byte文件 CsvFileMgr.Save(dirpath, false, sourcePath); }
/// <summary> /// 更新本地版本信息 /// </summary> /// <param name="ab">Ab.</param> public static void SyncVersion(List <string> abs) { // 遍历需要更新的ab信息 foreach (string ab in abs) { // 新版树中没有该版本信息 CsvRow data = mNewVersionCsv.FindByKey(ab); if (data == null) { return; } // 修正本地版本信息 CsvRow oldData = mVersionCsv.FindByKey(ab); // 如果是新增资源 if (oldData == null) { // 构建一列数据 CsvRow row = new CsvRow(mNewVersionCsv); row.Add("bundle", data.Query <LPCValue>("bundle")); row.Add("md5", data.Query <LPCValue>("md5")); row.Add("patch", data.Query <LPCValue>("patch")); row.Add("unzip_size", data.Query <LPCValue>("unzip_size")); row.Add("zip_size", data.Query <LPCValue>("zip_size")); // 添加新数据 mVersionCsv.AddNewRow(row); } else { oldData.Add("md5", data.Query <LPCValue>("md5")); oldData.Add("patch", data.Query <LPCValue>("patch")); oldData.Add("unzip_size", data.Query <LPCValue>("unzip_size")); oldData.Add("zip_size", data.Query <LPCValue>("zip_size")); } } // 序列化 MemoryStream ms = new MemoryStream(); CsvFileMgr.Serialize(ms, mVersionCsv); // 版本文件存放路径 string versionFilePath = string.Format("{0}/{1}.bytes", ConfigMgr.ASSETBUNDLES_PATH, VERSION_NAME); FileStream fs = new FileStream(versionFilePath, FileMode.Create, FileAccess.Write); fs.Write(ms.GetBuffer(), 0, (int)ms.Length); fs.Close(); }
/// <summary> /// 生成某个目录下的版本控制文件 /// </summary> private static void GenVersionFile(string sourcePath, string publishPath, Dictionary <string, List <object> > assetBundleInfo, Dictionary <string, string> abPatchDic) { // 保存的路径是否存在,不存在则创建一个 // csv保存的地址 string dirpath = string.Format("{0}version_tree.csv", sourcePath); FileInfo fi = new FileInfo(dirpath); if (!fi.Directory.Exists) { fi.Directory.Create(); } // csv文件写入 FileStream fs = new FileStream(dirpath, System.IO.FileMode.Create, System.IO.FileAccess.Write); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); //创建csv文件前面几行 sw.WriteLine(WriteRow(new string[] { "# version_tree.csv", "", "", "", "" })); sw.WriteLine(WriteRow(new string[] { "# 版本树", "", "", "", "" })); sw.WriteLine(WriteRow(new string[] { "# assetbundle名", "MD5编号", "所在patch", "未压缩资源大小", "压缩资源大小" })); sw.WriteLine(WriteRow(new string[] { "string", "string", "string", "int", "int" })); sw.WriteLine(WriteRow(new string[] { "bundle", "md5", "patch", "unzip_size", "zip_size" })); // 遍历该文件下的所有AssetBundles foreach (string abName in assetBundleInfo.Keys) { List <object> info = assetBundleInfo[abName]; string patch = abPatchDic.ContainsKey(abName) ? abPatchDic[abName] : "0"; // 写入数据 sw.WriteLine(WriteRow(new string[] { abName, info[0].ToString(), patch, info[1].ToString(), info[2].ToString() })); } //关闭写入 sw.Close(); fs.Close(); // 讲csv文件转成byte文件 CsvFileMgr.Save(dirpath, false, sourcePath); // 讲csv文件转成byte文件 CsvFileMgr.Save(dirpath, false, publishPath); }
/// <summary> /// 下载远程版本文件 /// </summary> private static IEnumerator UpdateNewVersionFile(int urlIndex) { // 获取配置的ab下载地址 IList abUrls = (IList)ConfigMgr.Get <JsonData>("ab_urls", null); if (abUrls == null || abUrls.Count == 0) { NIDebug.Log("获取assetbundle资源路径失败"); isVersionFileLoadSuccess = true; yield break; } // 尝试载入服务器上的版本文件 string versionUrl = string.Format("{0}/{1}.bytes", abUrls[urlIndex], VERSION_NAME); WWW www = new WWW(versionUrl); yield return(www); // 下载文件失败,切换其他地址下载 if (!string.IsNullOrEmpty(www.error)) { // 切换ab更新地址 Coroutine.DispatchService(UpdateNewVersionFile(urlIndex >= abUrls.Count - 1 ? 0 : ++urlIndex)); NIDebug.Log("{0}超时或错误", versionUrl); yield break; } // 反序列化 MemoryStream csvStr = new MemoryStream(www.bytes, 0, www.bytes.Length, true, true); mNewVersionCsv = CsvFileMgr.Deserialize(csvStr); csvStr.Close(); isVersionFileLoadSuccess = true; // 释放www www.Dispose(); NIDebug.Log("下载版本文件成功"); yield break; }
/// <summary> /// 载入版本文件 /// </summary> private static CsvFile LoadVersion(string path) { string versionPath = string.Format("{0}version_tree.bytes", path); // 如果文件不存在不处理 if (!File.Exists(versionPath)) { return(new CsvFile("version_tree")); } // 获取byte信息 byte[] csvBytes = File.ReadAllBytes(versionPath); // 反序列化 MemoryStream csvStr = new MemoryStream(csvBytes, 0, csvBytes.Length, true, true); CsvFile csv = CsvFileMgr.Deserialize(csvStr); csvStr.Close(); // 返回配置信息 return(csv); }
/// <summary> /// 载入本地版本文件 /// </summary> public static IEnumerator LoadVersionFile() { // 本地版本文件不存在 string filePath = string.Format("{0}/{1}.bytes", ConfigMgr.ASSETBUNDLES_PATH, VERSION_NAME); if (!File.Exists(filePath)) { NIDebug.Log("找不到本地版本文件"); yield break; } // 载入文件 WWW www = new WWW(ConfigMgr.GetLocalRootPathWWW(string.Format("{0}/{1}.bytes", ConfigMgr.ASSETBUNDLES_NAME, VERSION_NAME))); yield return(www); // 等待资源加载完成 while (!www.isDone) { yield return(null); } // 文件载入失败, 获取文件不存在 if (!string.IsNullOrEmpty(www.error) || www.bytes.Length <= 0) { yield break; } // 反序列化 MemoryStream csvStr = new MemoryStream(www.bytes, 0, www.bytes.Length, true, true); mVersionCsv = CsvFileMgr.Deserialize(csvStr); csvStr.Close(); // 释放www www.Dispose(); }
/// <summary> /// 序列化csv文件操作. /// </summary> public static void Save(string filePath, bool checkValid, string dir) { CsvFile csv = new CsvFile(System.IO.Path.GetFileNameWithoutExtension(filePath)); string[] lines = FileMgr.ReadLines(filePath); if (lines.Length == 0) { NIDebug.Log("空文件 {0}", lines); return; } // 解析csv文件 CsvParser cp = new CsvParser(); LPCValue m; try { cp.Load(filePath, checkValid); if (cp.Fields.Count == 0 || cp.Records.Count == 0) { return; } m = ImportMgr.Read(cp); } catch (Exception e) { NIDebug.Log("读取csv错误 {0}\n{1}", filePath, e.ToString()); return; } // 主key csv.primaryKey = cp.PrimaryKey; // 列名字对应的索引 for (int i = 0; i < cp.Fields.Count; i++) { csv.columns.Add(cp.Fields[i].name, i); } // 每列值 csv.rows = new CsvRow[m.AsArray.Count]; for (int i = 0; i < m.AsArray.Count; i++) { LPCValue v = m.AsArray[i]; CsvRow row = new CsvRow(csv); for (int idx = 0; idx < cp.Fields.Count; idx++) { row.Add(idx, v.AsMapping[cp.Fields[idx].name]); } csv.AddRow(i, row); } // 序列化 MemoryStream ms = new MemoryStream(); CsvFileMgr.Serialize(ms, csv); // 写入数据 string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath); // 确保路径存在 Directory.CreateDirectory(dir); FileStream fs = new FileStream(dir + "/" + fileName + CSV_EXT, FileMode.Create, FileAccess.Write); fs.Write(ms.GetBuffer(), 0, (int)ms.Length); fs.Close(); }