public void BNTXInjectionSameSize(Syroot.BinaryData.BinaryDataWriter writer, Syroot.BinaryData.BinaryDataReader reader) { foreach (ExternalFile ext in TargetSwitchBFRES.ExternalFiles) { reader.Seek(ext.ofsData, SeekOrigin.Begin); char[] emchar = reader.ReadChars(4); string em = new string(emchar); if (em == "BNTX") { Console.WriteLine("Found BNTX"); long OriginalBNTXSize = ext.sizData; long InjectedBNTXSize = BNTX.BNTXFile.Length; if (OriginalBNTXSize == InjectedBNTXSize) { writer.Seek((int)ext.ofsData, SeekOrigin.Begin); writer.Write(BNTX.BNTXFile); } else { MessageBox.Show("BNTX Is too big or small! Must be original Size!"); } } } }
public static byte[] WriteShaderParams(this BFRES.MaterialData m) { using (var mem = new MemoryStream()) using (Syroot.BinaryData.BinaryDataWriter writer = new Syroot.BinaryData.BinaryDataWriter(mem)) { writer.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian; foreach (var prm in m.matparam.Values) { switch (prm.Type) { case Syroot.NintenTools.Bfres.ShaderParamType.Float: writer.Write(prm.Value_float); break; case Syroot.NintenTools.Bfres.ShaderParamType.Float2: writer.Write(prm.Value_float2.X); writer.Write(prm.Value_float2.Y); break; case Syroot.NintenTools.Bfres.ShaderParamType.Float3: writer.Write(prm.Value_float3.X); writer.Write(prm.Value_float3.Y); writer.Write(prm.Value_float3.Z); break; case Syroot.NintenTools.Bfres.ShaderParamType.Float4: writer.Write(prm.Value_float4.X); writer.Write(prm.Value_float4.Y); writer.Write(prm.Value_float4.Z); writer.Write(prm.Value_float4.W); break; case Syroot.NintenTools.Bfres.ShaderParamType.TexSrt: writer.Write(prm.Value_TexSrt.Mode); writer.Write(prm.Value_TexSrt.scale.X); writer.Write(prm.Value_TexSrt.scale.Y); writer.Write(prm.Value_TexSrt.rotate); writer.Write(prm.Value_TexSrt.translate.X); writer.Write(prm.Value_TexSrt.translate.Y); break; case Syroot.NintenTools.Bfres.ShaderParamType.Float4x4: foreach (float f in prm.Value_float4x4) { writer.Write(f); } break; case Syroot.NintenTools.Bfres.ShaderParamType.UInt: writer.Write(prm.Value_UInt); break; default: writer.Write(prm.UnkownTypeData); break; } } return(mem.ToArray()); } }
public void FSKLInjection(Syroot.BinaryData.BinaryDataWriter writer) { int mdl = 0; foreach (Model fmdl in TargetSwitchBFRES.Models) { int CurBn = 0; foreach (Syroot.NintenTools.NSW.Bfres.Bone bn in fmdl.Skeleton.Bones) { Bone bone = models[mdl].skeleton.bones[CurBn]; writer.Seek(bn.SRTPos, SeekOrigin.Begin); writer.Write(bone.scale[0]); writer.Write(bone.scale[1]); writer.Write(bone.scale[2]); writer.Write(bone.rotation[0]); writer.Write(bone.rotation[1]); writer.Write(bone.rotation[2]); writer.Write(bone.rotation[3]); writer.Write(bone.position[0]); writer.Write(bone.position[1]); writer.Write(bone.position[2]); CurBn++; } mdl++; } }
public void WriteBlocks(Syroot.BinaryData.BinaryDataWriter writer) { foreach (KeyValuePair <object, BlockEntry> entry in _savedBlocks) { if (entry.Value.Alignment != 0) { writer.Align((int)entry.Value.Alignment); } // Write the data. entry.Value.Callback.Invoke(); } }
public void InjectToWiiUBFRES(string FileName) { using (Syroot.BinaryData.BinaryDataWriter writer = new Syroot.BinaryData.BinaryDataWriter(File.Open(FileName, FileMode.Create))) { writer.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian; writer.Write(BFRESFile); int mdl = 0; foreach (Model fmdl in TargetWiiUBFRES.Models.Values) { int s = 0; foreach (Shape shp in fmdl.Shapes.Values) { s++; } mdl++; } } }
public void InjectToFile(string FileName) { //Reparse and inject buffer data using (Syroot.BinaryData.BinaryDataReader reader = new Syroot.BinaryData.BinaryDataReader(new MemoryStream(BFRESFile))) using (Syroot.BinaryData.BinaryDataWriter writer = new Syroot.BinaryData.BinaryDataWriter(File.Open(FileName, FileMode.Create))) { writer.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian; reader.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian; BufferInjection(writer, reader); BNTXInjectionSameSize(writer, reader); // FSKAInjection(writer); FSKLInjection(writer); int mdl = 0; foreach (Model fmdl in TargetSwitchBFRES.Models) { int s = 0; foreach (Shape shp in fmdl.Shapes) { byte[] data = fmdl.Materials[shp.MaterialIndex].ShaderParamData; byte[] NewParamData = WriteShaderParams(data, models[mdl].poly[s]); if (SaveShaderParam == true) { writer.Seek(fmdl.Materials[shp.MaterialIndex].SourceParamOffset, SeekOrigin.Begin); writer.Write(NewParamData); } s++; } mdl++; } writer.Flush(); writer.Close(); } }
private void writeBuffer(Syroot.BinaryData.BinaryDataWriter writer, Vector4 value, AttribFormat format) { if (format == AttribFormat.Format_32_32_32_Single) { writer.Write(value.X); writer.Write(value.Y); writer.Write(value.Z); } else if (format == AttribFormat.Format_16_16_16_16_Single) { writer.Write((short)BinaryDataWriterExtensions.fromFloat(value.X)); writer.Write((short)BinaryDataWriterExtensions.fromFloat(value.Y)); writer.Write((short)BinaryDataWriterExtensions.fromFloat(value.Z)); writer.Write((short)BinaryDataWriterExtensions.fromFloat(value.W)); } else if (format == AttribFormat.Format_16_16_Single) { writer.Write((short)BinaryDataWriterExtensions.fromFloat(value.X)); writer.Write((short)BinaryDataWriterExtensions.fromFloat(value.Y)); } else if (format == AttribFormat.Format_16_16_UNorm) { writer.Write((ushort)(Syroot.Maths.Algebra.Clamp(value.X, 0, 1) * 65535)); writer.Write((ushort)(Syroot.Maths.Algebra.Clamp(value.Y, 0, 1) * 65535)); } else if (format == AttribFormat.Format_10_10_10_2_SNorm) { int x = BinaryDataWriterExtensions.FloatToInt10(Syroot.Maths.Algebra.Clamp(value.X, -1, 1) * 511); int y = BinaryDataWriterExtensions.FloatToInt10(Syroot.Maths.Algebra.Clamp(value.Y, -1, 1) * 511); int z = BinaryDataWriterExtensions.FloatToInt10(Syroot.Maths.Algebra.Clamp(value.Z, -1, 1) * 511); int w = BinaryDataWriterExtensions.FloatToInt10(Syroot.Maths.Algebra.Clamp(0, 0, 1)); writer.Write(x | (y << 10) | (z << 20) | (w << 30)); } else if (format == AttribFormat.Format_8_8_8_8_SNorm) { writer.Write((sbyte)(Syroot.Maths.Algebra.Clamp(value.X, -1, 1) * 127)); writer.Write((sbyte)(Syroot.Maths.Algebra.Clamp(value.Y, -1, 1) * 127)); writer.Write((sbyte)(Syroot.Maths.Algebra.Clamp(value.Z, -1, 1) * 127)); writer.Write((sbyte)(Syroot.Maths.Algebra.Clamp(value.W, -1, 1) * 127)); } else if (format == AttribFormat.Format_32_32_Single) { writer.Write(value.X); writer.Write(value.Y); } else if (format == AttribFormat.Format_16_16_SNorm) { writer.Write((short)(Syroot.Maths.Algebra.Clamp(value.X, -1, 1) * 32767)); writer.Write((short)(Syroot.Maths.Algebra.Clamp(value.Y, -1, 1) * 32767)); } else if (format == AttribFormat.Format_8_8_UNorm) { writer.Write((byte)(Syroot.Maths.Algebra.Clamp(value.X, 0, 1) * 255)); writer.Write((byte)(Syroot.Maths.Algebra.Clamp(value.Y, 0, 1) * 255)); } else if (format == AttribFormat.Format_8_8_8_8_UNorm) { writer.Write((byte)(Syroot.Maths.Algebra.Clamp(value.X, 0, 1) * 255)); writer.Write((byte)(Syroot.Maths.Algebra.Clamp(value.Y, 0, 1) * 255)); writer.Write((byte)(Syroot.Maths.Algebra.Clamp(value.Z, 0, 1) * 255)); writer.Write((byte)(Syroot.Maths.Algebra.Clamp(value.W, 0, 1) * 255)); } else if (format == AttribFormat.Format_8_8_8_8_UInt) { writer.Write((byte)value.X); writer.Write((byte)value.Y); writer.Write((byte)value.Z); writer.Write((byte)value.W); } else if (format == AttribFormat.Format_8_8_UInt) { writer.Write((byte)value.X); writer.Write((byte)value.Y); } else if (format == AttribFormat.Format_8_UInt) { writer.Write((byte)value.X); } else { MessageBox.Show("Unsupported format for weights " + format); } }
public void BufferInjection(Syroot.BinaryData.BinaryDataWriter writer, Syroot.BinaryData.BinaryDataReader reader) { writer.Write(BFRESFile); int mdl = 0; foreach (Model fmdl in TargetSwitchBFRES.Models) { int s = 0; foreach (Shape shp in fmdl.Shapes) { Mesh msh = models[mdl].poly[s]; writer.Seek(shp.Meshes[0].DataOffset, SeekOrigin.Begin); int[] Faces = msh.lodMeshes[msh.DisplayLODIndex].getDisplayFace().ToArray(); foreach (int f in Faces) { if (shp.Meshes[0].IndexFormat == IndexFormat.UInt16) { writer.Write((ushort)f); } if (shp.Meshes[0].IndexFormat == IndexFormat.UInt32) { writer.Write((uint)f); } } foreach (VertexAttrib att in fmdl.VertexBuffers[shp.VertexBufferIndex].Attributes) { int test = (int)fmdl.VertexBuffers[shp.VertexBufferIndex].Buffers[att.BufferIndex].DataOffset + att.Offset + fmdl.VertexBuffers[shp.VertexBufferIndex].Buffers[att.BufferIndex].Stride; } for (int vt = 0; vt < fmdl.VertexBuffers[shp.VertexBufferIndex].VertexCount; vt++) { if (vt < msh.vertices.Count) { } else { Vertex vtx = new Vertex(); vtx.pos = new Vector3(0); vtx.nrm = new Vector3(0); vtx.col = new Vector4(1); vtx.uv0 = new Vector2(0); vtx.uv1 = new Vector2(0); vtx.uv2 = new Vector2(0); vtx.boneWeights.Add(1); vtx.boneIds.Add(0); vtx.tan = new Vector4(0); vtx.bitan = new Vector4(0); msh.vertices.Add(vtx); } Vertex v = msh.vertices[vt]; int at = 0; foreach (VertexAttrib att in fmdl.VertexBuffers[shp.VertexBufferIndex].Attributes) { writer.Seek(fmdl.VertexBuffers[shp.VertexBufferIndex].Buffers[att.BufferIndex].DataOffset + att.Offset + fmdl.VertexBuffers[shp.VertexBufferIndex].Buffers[att.BufferIndex].Stride * vt, SeekOrigin.Begin); if (att.Name == "_p0") { writeBuffer(writer, new Vector4(v.pos, 0), att.Format); } if (att.Name == "_n0") { writeBuffer(writer, new Vector4(v.nrm, 0), att.Format); } if (att.Name == "_t0") { writeBuffer(writer, new Vector4(v.tan), att.Format); } if (att.Name == "_b0") { writeBuffer(writer, new Vector4(v.bitan), att.Format); } if (att.Name == "_c0") { writeBuffer(writer, new Vector4(v.col), att.Format); } if (att.Name == "_u0") { writeBuffer(writer, new Vector4(v.uv0.X, v.uv0.Y, 0, 0), att.Format); } if (att.Name == "_u1") { writeBuffer(writer, new Vector4(v.uv1.X, v.uv1.Y, 0, 0), att.Format); } if (att.Name == "_u2") { writeBuffer(writer, new Vector4(v.uv2.X, v.uv2.Y, 0, 0), att.Format); } if (att.Name == "_i0") { Vector4 bufferdata = new Vector4(0); bufferdata.X = v.boneIds.Count > 0 ? v.boneIds[0] : 0; bufferdata.Y = v.boneIds.Count > 1 ? v.boneIds[1] : 0; bufferdata.Z = v.boneIds.Count > 2 ? v.boneIds[2] : 0; bufferdata.W = v.boneIds.Count > 3 ? v.boneIds[3] : 0; writeBuffer(writer, bufferdata, att.Format); } if (att.Name == "_w0") { Vector4 bufferdata = new Vector4(0); bufferdata.X = v.boneWeights.Count > 0 ? v.boneWeights[0] : 0; bufferdata.Y = v.boneWeights.Count > 1 ? v.boneWeights[1] : 0; bufferdata.Z = v.boneWeights.Count > 2 ? v.boneWeights[2] : 0; bufferdata.W = v.boneWeights.Count > 3 ? v.boneWeights[3] : 0; writeBuffer(writer, bufferdata, att.Format); } } at++; } s++; } mdl++; } }
public byte[] WriteShaderParams(byte[] data, Mesh m) { //Write data to this byte array. using (Syroot.BinaryData.BinaryDataWriter writer = new Syroot.BinaryData.BinaryDataWriter(new MemoryStream(data))) { writer.ByteOrder = Syroot.BinaryData.ByteOrder.LittleEndian; foreach (var prm in m.material.matparam.Values) { switch (prm.Type) { case Syroot.NintenTools.Bfres.ShaderParamType.Float: writer.Write(prm.Value_float); break; case Syroot.NintenTools.Bfres.ShaderParamType.Float2: writer.Write(prm.Value_float2.X); writer.Write(prm.Value_float2.Y); break; case Syroot.NintenTools.Bfres.ShaderParamType.Float3: writer.Write(prm.Value_float3.X); writer.Write(prm.Value_float3.Y); writer.Write(prm.Value_float3.Z); break; case Syroot.NintenTools.Bfres.ShaderParamType.Float4: writer.Write(prm.Value_float4.X); writer.Write(prm.Value_float4.Y); writer.Write(prm.Value_float4.Z); writer.Write(prm.Value_float4.W); break; case Syroot.NintenTools.Bfres.ShaderParamType.TexSrt: writer.Write(prm.Value_TexSrt.Mode); writer.Write(prm.Value_TexSrt.scale.X); writer.Write(prm.Value_TexSrt.scale.Y); writer.Write(prm.Value_TexSrt.rotate); writer.Write(prm.Value_TexSrt.translate.X); writer.Write(prm.Value_TexSrt.translate.Y); break; case Syroot.NintenTools.Bfres.ShaderParamType.Float4x4: foreach (float f in prm.Value_float4x4) { writer.Write(f); } break; case Syroot.NintenTools.Bfres.ShaderParamType.UInt: writer.Write(prm.Value_UInt); break; default: MessageBox.Show("Format not added to shader param saving " + prm.Type); MessageBox.Show("Shader param will not save!"); SaveShaderParam = false; break; } } return(data); } }