/// <summary> /// 作为本地正式缓存写入本地文件 /// </summary> public void WriteAsCached() { Refresh(); SaveUpdatings(); string versions = JsonUtility.ToJson(this); string path = AppPaths.PersistentResDataRoot.Append(FILE_RES_DATA_VERSIONS_NAME); #if !RELEASE FileOperater.WriteUTF8Text(versions, path.Append(".json~"));//写入一个用于查看的文件 #endif "todo".Log("资源版本写入设备时需要加密"); FileOperater.WriteBytes(versions, path); }
/// <summary> /// 创建资源版本 /// </summary> /// <param name="remoteVers"></param> private void BuildVersionConfig(ref ResDataVersion remoteVers) { string versions = default; ResDataVersion resDataVersion = default; GetVersionDataFromRemote(ref remoteVers, ref versions, ref resDataVersion); string[] abNamesValue = ABNamesWillBuild != default ? ABNamesWillBuild.ToArray() : new string[0]; resDataVersion.CreateNewResVersion(ref resRemoteGateWay, isUpdateVersion, isUpdateResVersion, isSyncAppVersion, ref remoteVers, ref abNamesValue); resDataVersion.Refresh(); bool applyClientGateway = false; if (isSyncClientVersions) { List <ScriptableObject> list = default; ShipDockEditorUtils.FindAssetInEditorProject(ref list, "t:ScriptableObject", @"Assets\Prefabs"); ClientResVersion clientRes = default;// = (ClientResVersion)list[0]; foreach (ScriptableObject i in list) { if (i.name == ClientVersionFileName) { clientRes = (ClientResVersion)i; break; } else { } } applyClientGateway = clientRes.ApplyCurrentResGateway; if (applyClientGateway) { resDataVersion.res_gateway = clientRes.ClientResVersionGateway(); } clientRes.Versions.CloneVersionsFrom(ref resDataVersion); clientRes.SetChanges(resDataVersion.ResChanges); UnityEditor.EditorUtility.SetDirty(clientRes); } versions = JsonUtility.ToJson(resDataVersion); string stamp = "_".Append(System.DateTime.Now.ToShortDateString().Replace("/", "_")); FileOperater.WriteUTF8Text(versions, Application.dataPath.Append("/Prefabs/ClientResVersions_Previews/", ResDataVersion.FILE_RES_DATA_VERSIONS_NAME, stamp, ".json")); //仅用于查看 FileOperater.WriteBytes(versions, AppPaths.ABBuildOutputRoot.Append(ResDataVersion.FILE_RES_DATA_VERSIONS_NAME)); //位于资源主目录里的正式文件 resDataVersion.Clean(); remoteVers?.Clean(); }
/// <summary> /// 读取表数据,生成对应的数组 /// </summary> /// <param name="filePath">excel文件全路径</param> /// <returns>Item数组</returns> public static void CreateItemArrayWithExcel(string filePath, out string relativeName, ref string log) { InitExeclDefs(); log += filePath + "开始解析... \r\n"; DataRowCollection collect = ReadExcel(filePath, out int rowSize, out int colSize);//获得表数据 ExeclDefination defination = Setting.classDefine; int col = defination.column, row = defination.row; string className = collect[row][col].ToString();//类名 defination = Setting.generateFileName; col = defination.column; row = defination.row; string fileName = collect[row][col].ToString();//生成的配置文件名 fileName = string.IsNullOrEmpty(fileName) ? className : fileName; defination = Setting.classType; col = defination.column; row = defination.row; string classType = collect[row][col].ToString();//类模板的类别 defination = Setting.IDFieldName; col = defination.column; row = defination.row; string IDName = collect[row][col].ToString();//id字段的名称 defination = Setting.dataStart; int dataStartRow = defination.row; int dataStartCol = defination.column; Debug.Log(rowSize); string cellData; for (int i = dataStartRow; i < rowSize; i++) { cellData = collect[i][dataStartCol].ToString(); if (string.IsNullOrEmpty(cellData)) { rowSize = i;//找到数据起始列为空的一行,作为数据最终行 Debug.Log(rowSize); break; } else { } } StringBuilder sb = new StringBuilder(); int typeRow = Setting.dataType.row; //类型名所在行 int keyRow = Setting.keyFieldDef.row; //字段名所在行 int notesRow = Setting.noteFieldDef.row; //注释所在行 DataRow rowData = collect[typeRow]; object colData = rowData[dataStartCol]; object IDTypeCell = collect[typeRow][dataStartCol]; string typeValue = GetCSharpForTypeValueCode(IDTypeCell.ToString()); ClassTemplateInfo info = new ClassTemplateInfo { sb = sb, collect = collect, className = className, notesRow = notesRow, dataStartCol = dataStartCol, typeValue = typeValue, IDName = IDName, }; switch (classType) { case "mapper": TranslateConfigCSharpClassCode(info); break; case "const": //常量类 sb.Append("namespace StaticConfig"); sb.Append("{"); sb.Append(" public static class %cls%".Replace(info.rplClsName, className)); sb.Append(" {"); sb.Append(" }"); sb.Append("}"); break; } string field, notes, valueInitCode; string CSharpScript = sb.ToString(); sb.Clear(); object item; string parserCode = IDName.Append(GetCSharpForSetValueCode(IDTypeCell.ToString())); //获取到翻译后的 id 赋值语句 for (int i = dataStartCol + 1; i < colSize; i++) //从id列顺延下一个字段开始构建剩余的脚本 { item = collect[typeRow][i]; //类型 typeValue = item.ToString(); if (string.IsNullOrEmpty(typeValue)) { colSize = i;//读取到无效的表头,此前的所有字段为完整的类成员字段,并更新到列数 break; } else { } typeValue = GetCSharpForTypeValueCode(typeValue); item = collect[keyRow][i];//字段名 field = item.ToString(); notes = collect[notesRow][i].ToString(); valueInitCode = collect[typeRow][i].ToString(); sb.Append("/// <summary>"); sb.Append("\r\n /// %notes%") .Replace(info.notes, notes); sb.Append("\r\n /// <summary>\r\n "); sb.Append("public %type% %fieldName%;\r\n") .Replace(info.typeName, typeValue) .Replace(info.fieldName, field); sb.Append(" "); parserCode = parserCode.Append(field, GetCSharpForSetValueCode(valueInitCode));//获取到翻译后的其他成员字段赋值语句 } CSharpScript = CSharpScript.Replace(info.codes, sb.ToString()); //合成类成员字段的声明语句 CSharpScript = CSharpScript.Replace(info.parseCode, parserCode); //合成类成员字段的初始化赋值语句 sb.Clear(); object ID; string IDValue; DataRow itemRow; ByteBuffer byteBuffer = ByteBuffer.Allocate(0); int dataRealSize = rowSize - dataStartRow; byteBuffer.WriteInt(dataRealSize); for (int i = dataStartRow; i < rowSize; i++) { itemRow = collect[i]; ID = itemRow[dataStartCol]; IDValue = ID.ToString(); if (string.IsNullOrEmpty(IDValue)) { Debug.LogWarning("Config ID invalid in row " + i + ", it do not allow empty, config bytes parse will be break."); break; } else { Debug.Log("Writing in ID = " + IDValue.ToString()); for (int j = dataStartCol; j < colSize; j++)//从id列顺延下一个字段开始构建剩余的脚本 { item = collect[typeRow][j]; typeValue = item.ToString(); //字段类型 field = itemRow[j].ToString(); //字段数据 WriteField(ref byteBuffer, typeValue, field, collect[keyRow][j].ToString()); } } } Debug.Log("Write finished.. length = " + byteBuffer.GetCapacity()); string path = GetCSharpCodeFilePath(className); FileOperater.WriteUTF8Text(CSharpScript, path);//生成代码 path = GetConfigFilePath(fileName, out relativeName); FileOperater.WriteBytes(byteBuffer.ToArray(), path);//生成配置数据文件 }