private void OpenBaseStream() { bool wasCombined; var assetsFileStream = FileProvider.ReadCombinedAssets(AssetsRootPath.CombineFwdSlash(AssetsFilename), out wasCombined); if (!assetsFileStream.CanSeek) { throw new NotSupportedException("Stream must support seeking!"); } FileWasSplit = wasCombined; BaseStream = assetsFileStream; }
public DLCItem(DLCItemConfig config, AssetBundleManifest assetBundleManifest = null) { Config = config; Name = config.Name; IsActive = config.IsActive; ABManifest = assetBundleManifest; BuildRuleData.Clear(); CopyDirectory.Clear(); foreach (var item in DLCConfig.Config) { BuildRuleData.Add(item.Clone() as BuildRuleConfig); } CopyDirectory.AddRange(DLCConfig.CopyDirectory.ToArray()); //计算DLC的跟目录 AssetsRootPath = CalcAssetRootPath(Name); //计算出绝对路径(拷贝文件使用) AbsRootPath = Path.Combine(Application.dataPath, AssetsRootPath.Replace("Assets/", "")); //计算出目标路径 TargetPath = Path.Combine(Const.Path_StreamingAssets, Name); //计算Const路径 ConstPath = Path.Combine(Const.RPath_Resources, Const.Dir_Const, Name + "Const.cs"); //计算Manifest路径 if (IsEditorMode) { ManifestPath = Path.Combine(AssetsRootPath, Const.Dir_Config); } else { ManifestPath = Path.Combine(TargetPath, Const.Dir_Config); } //计算Config路径 if (IsEditorMode) { ConfigPath = Path.Combine(AssetsRootPath, Const.Dir_Config); } else { ConfigPath = Path.Combine(TargetPath, Const.Dir_Config); } //计算语言包路径 if (IsEditorMode) { LanguagePath = Path.Combine(AssetsRootPath, Const.Dir_Language); } else { LanguagePath = Path.Combine(TargetPath, Const.Dir_Language); } //计算lua路径 if (IsEditorMode) { LuaPath = Path.Combine(AssetsRootPath, Const.Dir_Lua); } else { LuaPath = Path.Combine(TargetPath, Const.Dir_Lua); } //计算Text路径 if (IsEditorMode) { TextPath = Path.Combine(AssetsRootPath, Const.Dir_TextAssets); } else { TextPath = Path.Combine(TargetPath, Const.Dir_TextAssets); } //计算CS路径 if (IsEditorMode) { CSPath = Path.Combine(AssetsRootPath, Const.Dir_CSharp); } else { CSPath = Path.Combine(TargetPath, Const.Dir_CSharp); } //计算Excel路径 if (IsEditorMode) { ExcelPath = Path.Combine(AssetsRootPath, Const.Dir_Excel); } else { ExcelPath = Path.Combine(TargetPath, Const.Dir_Excel); } #region func GenerateCopyPath(); GeneralPath(); //建立拷贝路径 void GenerateCopyPath() { AbsCopyDirectory.Clear(); if (CopyDirectory != null) { for (int i = 0; i < CopyDirectory.Count; ++i) { AbsCopyDirectory.Add(Path.Combine(AbsRootPath, CopyDirectory[i])); } } } //建立打包路径 void GeneralPath() { foreach (var item in BuildRuleData) { string tempRootPath = AssetsRootPath; if (!item.CustomRootPath.IsInv()) { tempRootPath = item.CustomRootPath; } item.FullSearchPath = tempRootPath + "/" + item.Name; } } #endregion }
public void Write() { try { using (Stream objectsMS = GetTempStream()) { using (Stream metaMS = GetTempStream()) { using (AssetsWriter writer = new AssetsWriter(objectsMS)) { int ctr = 0; foreach (var obj in Metadata.ObjectInfos) { ctr++; var offset = (int)objectsMS.Position; obj.GetObjectForWrite().Write(writer); writer.Flush(); obj.DataOffset = offset; var origSize = obj.DataSize; obj.DataSize = (int)(objectsMS.Position - obj.DataOffset); writer.AlignTo(8); } } using (AssetsWriter writer = new AssetsWriter(metaMS)) { Metadata.Write(writer); } Header.FileSize = Header.HeaderSize + (int)objectsMS.Length + (int)metaMS.Length; Header.ObjectDataOffset = Header.HeaderSize + (int)metaMS.Length; int diff; int alignment = 16; //or 32, I don't know which //data has to be at least 4096 inward from the start of the file if (Header.ObjectDataOffset < 4096) { diff = 4096 - Header.ObjectDataOffset; } else { diff = alignment - (Header.ObjectDataOffset % alignment); if (diff == alignment) { diff = 0; } } if (diff > 0) { Header.ObjectDataOffset += diff; Header.FileSize += diff; } Header.MetadataSize = (int)metaMS.Length; objectsMS.Seek(0, SeekOrigin.Begin); metaMS.Seek(0, SeekOrigin.Begin); lock (this) { try { CloseBaseStream(); FileProvider.DeleteFiles(AssetsRootPath.CombineFwdSlash(AssetsFilename + ".split*")); using (Stream outputStream = GetTempStream()) { using (AssetsWriter writer = new AssetsWriter(outputStream)) { Header.Write(writer); } metaMS.CopyTo(outputStream); if (diff > 0) { outputStream.Write(new byte[diff], 0, diff); } objectsMS.CopyTo(outputStream); outputStream.Seek(0, SeekOrigin.Begin); if (FileWasSplit) { int splitCtr = 0; byte[] buffer = new byte[1024 * 1024]; do { Stream outFile = FileProvider.GetWriteStream($"{AssetsRootPath.CombineFwdSlash(AssetsFilename)}.split{splitCtr}"); var readLen = (int)(outputStream.Length - outputStream.Position); if (readLen < buffer.Length) { outputStream.Read(buffer, 0, readLen); outFile.Write(buffer, 0, readLen); break; } outputStream.Read(buffer, 0, buffer.Length); outFile.Write(buffer, 0, buffer.Length); splitCtr++; } while (true); } else { Stream writeStream = FileProvider.GetWriteStream(AssetsRootPath.CombineFwdSlash(AssetsFilename)); outputStream.CopyTo(writeStream); } } FileProvider.Save(); _hasChanges = false; foreach (var ptr in _knownPointers.Where(x => x.Owner.ObjectInfo.ParentFile == this && x.IsNew)) { ptr.IsNew = false; } OpenBaseStream(); } catch (Exception ex) { throw new Exception("CRITICAL: writing and reopening the file failed, the file is possibly destroyed and this object is in an unknown state.", ex); } } } } } finally { CleanupTempFiles(); } }