public static void Load(ResFileSwitchLoader loader, ResFile resFile) { loader.CheckSignature("FRES"); uint padding = loader.ReadUInt32(); resFile.Version = loader.ReadUInt32(); resFile.SetVersionInfo(resFile.Version); resFile.ByteOrder = loader.ReadByteOrder(); resFile.Alignment = loader.ReadByte(); resFile.TargetAddressSize = loader.ReadByte(); //Thanks MasterF0X for pointing out the layout of the these uint OffsetToFileName = loader.ReadUInt32(); resFile.Flag = loader.ReadUInt16(); resFile.BlockOffset = loader.ReadUInt16(); uint RelocationTableOffset = loader.ReadUInt32(); uint sizFile = loader.ReadUInt32(); resFile.Name = loader.LoadString(); long modelOffset = loader.ReadOffset(); long modelDictOffset = loader.ReadOffset(); if (loader.ResFile.VersionMajor2 == 9) { loader.ReadBytes(32); //reserved } resFile.SkeletalAnims = loader.LoadDictValues <SkeletalAnim>(); resFile.MaterialAnims = loader.LoadDictValues <MaterialAnim>(); resFile.BoneVisibilityAnims = loader.LoadDictValues <VisibilityAnim>(); resFile.ShapeAnims = loader.LoadDictValues <ShapeAnim>(); resFile.SceneAnims = loader.LoadDictValues <SceneAnim>(); resFile.MemoryPool = loader.Load <MemoryPool>(); resFile.BufferInfo = loader.Load <BufferInfo>(); resFile.ExternalFiles = loader.LoadDictValues <ExternalFile>(); long padding1 = loader.ReadInt64(); resFile.StringTable = loader.Load <StringTable>(); uint StringPoolSize = loader.ReadUInt32(); ushort numModel = loader.ReadUInt16(); //Read models after buffer data resFile.Models = loader.LoadDictValues <Model>(modelDictOffset, modelOffset); if (loader.ResFile.VersionMajor2 == 9) { //Count for 2 new sections ushort unkCount = loader.ReadUInt16(); ushort unk2Count = loader.ReadUInt16(); if (unkCount != 0) { throw new System.Exception("unk1 has section!"); } if (unk2Count != 0) { throw new System.Exception("unk2 has section!"); } } ushort numSkeletalAnim = loader.ReadUInt16(); ushort numMaterialAnim = loader.ReadUInt16(); ushort numBoneVisibilityAnim = loader.ReadUInt16(); ushort numShapeAnim = loader.ReadUInt16(); ushort numSceneAnim = loader.ReadUInt16(); ushort numExternalFile = loader.ReadUInt16(); uint padding2 = loader.ReadUInt16(); uint padding3 = loader.ReadUInt32(); resFile.Textures = new ResDict <TextureShared>(); foreach (var ext in resFile.ExternalFiles) { if (ext.Key.Contains(".bntx")) { BntxFile bntx = new BntxFile(new MemoryStream(ext.Value.Data)); ext.Value.LoadedFileData = bntx; foreach (var tex in bntx.Textures) { resFile.Textures.Add(tex.Name, new SwitchTexture(bntx, tex)); } } } resFile.TexPatternAnims = new ResDict <MaterialAnim>(); resFile.MatVisibilityAnims = new ResDict <MaterialAnim>(); resFile.ShaderParamAnims = new ResDict <MaterialAnim>(); resFile.ColorAnims = new ResDict <MaterialAnim>(); resFile.TexSrtAnims = new ResDict <MaterialAnim>(); //Split material animations into shader, texture, and visual animation lists foreach (var anim in resFile.MaterialAnims.Values) { if (anim.Name.Contains("_ftp")) { resFile.TexPatternAnims.Add(anim.Name, anim); } else if (anim.Name.Contains("_fts")) { resFile.ShaderParamAnims.Add(anim.Name, anim); } else if (anim.Name.Contains("_fcl")) { resFile.ColorAnims.Add(anim.Name, anim); } else if (anim.Name.Contains("_fst")) { resFile.TexSrtAnims.Add(anim.Name, anim); } else if (anim.Name.Contains("_fvt")) { resFile.MatVisibilityAnims.Add(anim.Name, anim); } else if (anim.MaterialAnimDataList != null && anim.MaterialAnimDataList.Any(x => x.VisibilyCount > 0)) { resFile.MatVisibilityAnims.Add(anim.Name, anim); } else if (anim.MaterialAnimDataList != null && anim.MaterialAnimDataList.Any(x => x.TexturePatternCount > 0)) { resFile.TexPatternAnims.Add(anim.Name, anim); } else { resFile.ShaderParamAnims.Add(anim.Name, anim); } } }
public static void Load(ResFileSwitchLoader loader, VertexBuffer vertexBuffer) { if (loader.ResFile.VersionMajor2 == 9) { vertexBuffer.Flags = loader.ReadUInt32(); } else { loader.LoadHeaderBlock(); } vertexBuffer.Attributes = loader.LoadDictValues <VertexAttrib>(); vertexBuffer.MemoryPool = loader.Load <MemoryPool>(); long unk = loader.ReadOffset(); if (loader.ResFile.VersionMajor2 > 2 || loader.ResFile.VersionMajor > 0) { loader.ReadOffset();// unk2 } long VertexBufferSizeOffset = loader.ReadOffset(); long VertexStrideSizeOffset = loader.ReadOffset(); long padding = loader.ReadInt64(); int BufferOffset = loader.ReadInt32(); byte numVertexAttrib = loader.ReadByte(); byte numBuffer = loader.ReadByte(); ushort Idx = loader.ReadUInt16(); uint vertexCount = loader.ReadUInt32(); vertexBuffer.VertexSkinCount = (byte)loader.ReadUInt32(); //Buffers use the index buffer offset from memory info section //This goes to a section in the memory pool which stores all the buffer data, including faces //To obtain a list of all the buffer data, it would be by the index buffer offset + BufferOffset var StrideArray = loader.LoadList <VertexBufferStride>(numBuffer, (uint)VertexStrideSizeOffset); var VertexBufferSizeArray = loader.LoadList <VertexBufferSize>(numBuffer, (uint)VertexBufferSizeOffset); vertexBuffer.Buffers = new List <Buffer>(); for (int buff = 0; buff < numBuffer; buff++) { Buffer data = new Buffer(); data.Flags = VertexBufferSizeArray[buff].GpuAccessFlags; data.Stride = (ushort)StrideArray[buff].Stride; uint size = VertexBufferSizeArray[buff].Size; if (buff == 0) { data.BufferOffset = ((int)BufferInfo.BufferOffset + BufferOffset); } if (buff > 0) { data.BufferOffset = vertexBuffer.Buffers[buff - 1].BufferOffset + vertexBuffer.Buffers[buff - 1].Size; } if (data.BufferOffset % 8 != 0) { data.BufferOffset = data.BufferOffset + (8 - (data.BufferOffset % 8)); } data.Data = new byte[1][]; data.Data[0] = loader.LoadCustom(() => //Load buffer data from mem block { return(loader.ReadBytes((int)size)); }, (uint)data.BufferOffset); vertexBuffer.Buffers.Add(data); } }
public static void Load(ResFileSwitchLoader loader, Material mat) { if (loader.ResFile.VersionMajor2 == 9) { mat.Flags = loader.ReadEnum <MaterialFlags>(true); } else { loader.LoadHeaderBlock(); } mat.Name = loader.LoadString(); mat.RenderInfos = loader.LoadDictValues <RenderInfo>(); mat.ShaderAssign = loader.Load <ShaderAssign>(); long TextureArrayOffset = loader.ReadInt64(); long TextureNameArray = loader.ReadInt64(); long SamplerArrayOffset = loader.ReadInt64(); mat.Samplers = loader.LoadDictValues <Sampler>(); mat.ShaderParams = loader.LoadDictValues <ShaderParam>(); long SourceParamOffset = loader.ReadInt64(); mat.UserData = loader.LoadDictValues <UserData>(); long VolatileFlagsOffset = loader.ReadInt64(); long userPointer = loader.ReadInt64(); long SamplerSlotArrayOffset = loader.ReadInt64(); long TexSlotArrayOffset = loader.ReadInt64(); if (loader.ResFile.VersionMajor2 != 9) { mat.Flags = loader.ReadEnum <MaterialFlags>(true); } ushort idx = loader.ReadUInt16(); ushort numRenderInfo = loader.ReadUInt16(); byte numTextureRef = loader.ReadByte(); byte numSampler = loader.ReadByte(); ushort numShaderParam = loader.ReadUInt16(); ushort numShaderParamVolatile = loader.ReadUInt16(); ushort sizParamSource = loader.ReadUInt16(); ushort sizParamRaw = loader.ReadUInt16(); ushort numUserData = loader.ReadUInt16(); if (loader.ResFile.VersionMajor2 != 9) { loader.ReadUInt32(); //Padding } var textures = loader.LoadCustom(() => loader.LoadStrings(numTextureRef), (uint)TextureNameArray); mat.TextureRefs = new List <TextureRef>(); if (textures != null) { foreach (var tex in textures) { mat.TextureRefs.Add(new TextureRef() { Name = tex }); } } //Add names to the value as switch does not store any foreach (var sampler in mat.Samplers) { sampler.Value.Name = sampler.Key; } mat.ShaderParamData = loader.LoadCustom(() => loader.ReadBytes(sizParamSource), (uint)SourceParamOffset); mat.VolatileFlags = loader.LoadCustom(() => loader.ReadBytes((int)Math.Ceiling(numShaderParam / 8f)), (uint)VolatileFlagsOffset); mat.TextureSlotArray = loader.LoadCustom(() => loader.ReadInt64s(numTextureRef), (uint)SamplerSlotArrayOffset); mat.SamplerSlotArray = loader.LoadCustom(() => loader.ReadInt64s(numSampler), (uint)TexSlotArrayOffset); }