public override void Convert(ResourceLocation source, ResourceLocation dest) { ContentBinaryReader br = new ContentBinaryReader(source); BinaryDataReader data = br.ReadBinaryData(); float xllcorner = data.GetDataSingle("xllcorner"); float yllcorner = data.GetDataSingle("yllcorner"); int width = data.GetDataInt32("width"); int height = data.GetDataInt32("height"); float[] demData = new float[height * width]; int bits = data.GetDataInt32("bits", 32); ContentBinaryReader br2 = data.GetData("data"); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { demData[i * width + j] = br2.ReadSingle(); } } br2.Close(); data.Close(); Half[] demData16 = Half.ConvertToHalf(demData); // ========================================================= BinaryDataWriter result = new BinaryDataWriter(); result.AddEntry("xllcorner", xllcorner); result.AddEntry("yllcorner", yllcorner); result.AddEntry("width", width); result.AddEntry("height", height); result.AddEntry("bits", 16); Stream dataStream = result.AddEntryStream("data"); ContentBinaryWriter bw = new ContentBinaryWriter(dataStream); for (int i = 0; i < demData.Length; i++) { bw.Write(demData16[i].InternalValue); } bw.Close(); bw = new ContentBinaryWriter(dest); bw.Write(result); bw.Close(); }
public override byte[] Serialize(object obj) { Type type = obj.GetType(); if (!MainEntity && typeof(EntityBase).IsAssignableFrom(type)) { return ((EntityBase)obj).Index.ToByteArray(); } MainEntity = false; BinaryDataWriter writer = new BinaryDataWriter(); var properties = type.GetProperties(); for (byte i = 0; i < properties.Length; i++) { var property = properties[i]; #region 判断是否有效数据 if (!property.CanRead || !property.CanWrite) continue; PropertyAuthenticationAttribute authenticate = property.GetCustomAttributes(typeof(PropertyAuthenticationAttribute), false).FirstOrDefault() as PropertyAuthenticationAttribute; if (authenticate != null) { if (!authenticate.AllowAnonymous && !MemberManager.IsSigned) continue; if (!RoleManager.HasRoles(authenticate.ViewRolesRequired)) continue; } #endregion writer.WriteByte(i); var data = Serialize(property.GetValue(obj, null)); writer.WriteBytes(data); } MainEntity = true; return writer.ToArray(); }
protected static void WriteNonLengthPrefixedAsciiString(BinaryDataWriter dataWriter, string s) { var bytes = Encoding.ASCII.GetBytes(s); dataWriter.Write(bytes); }
protected static void Write16BitLengthPrefixedByteArray(BinaryDataWriter dataWriter, byte[] bytes) { dataWriter.Write <UInt16>(checked ((ushort)bytes.Length)); dataWriter.Output.WriteBytes(bytes); }
private static void Write_32_32_32_Single(this BinaryDataWriter self, Vector4F value) { self.Write(value.X); self.Write(value.Y); self.Write(value.Z); }
/// <summary> /// Writes a <see cref="Half"/> instance into the current stream. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="value">The <see cref="Half"/> instance.</param> public static void Write(this BinaryDataWriter self, Half value) { self.Write(value.Raw); }
private static void Write_16_16_SIntToSingle(this BinaryDataWriter self, Vector4F value) { self.Write((short)value.X); self.Write((short)value.Y); }
private static void Write_16_16_SNorm(this BinaryDataWriter self, Vector4F value) { self.Write((short)(Algebra.Clamp(value.X, -1, 1) * 32767)); self.Write((short)(Algebra.Clamp(value.Y, -1, 1) * 32767)); }
// ---- 32-bit (16 x 2) ---- private static void Write_16_16_UNorm(this BinaryDataWriter self, Vector4F value) { self.Write((ushort)(Algebra.Clamp(value.X, 0, 1) * 65535)); self.Write((ushort)(Algebra.Clamp(value.Y, 0, 1) * 65535)); }
/// <summary> /// Writes a <see cref="Vector3U"/> instance into the current stream. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="value">The <see cref="Vector3U"/> instance.</param> public static void Write(this BinaryDataWriter self, Vector3U value) { self.Write(value.X); self.Write(value.Y); self.Write(value.Z); }
// ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- /// <summary> /// Writes a <see cref="AnimConstant"/> instance into the current stream. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="value">The <see cref="AnimConstant"/> instance.</param> public static void Write(this BinaryDataWriter self, AnimConstant value) { self.Write(value.AnimDataOffset); self.Write(value.Value); }
/// <summary> /// Writes a <see cref="Vector2Bool"/> instance into the current stream. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="value">The <see cref="Vector2Bool"/> instance.</param> /// <param name="format">The <see cref="BinaryBooleanFormat"/> in which values are stored.</param> public static void Write(this BinaryDataWriter self, Vector2Bool value, BinaryBooleanFormat format = BinaryBooleanFormat.NonZeroByte) { self.Write(value.X, format); self.Write(value.Y, format); }
/// <summary> /// Writes a <see cref="Matrix3x4"/> instance into the current stream. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="value">The <see cref="Matrix3x4"/> instance.</param> public static void Write(this BinaryDataWriter self, Matrix3x4 value) { self.Write(value.M11); self.Write(value.M12); self.Write(value.M13); self.Write(value.M14); self.Write(value.M21); self.Write(value.M22); self.Write(value.M23); self.Write(value.M24); self.Write(value.M31); self.Write(value.M32); self.Write(value.M33); self.Write(value.M34); }
/// <summary> /// Convert file to bytes. /// </summary> /// <param name="endian"></param> /// <returns></returns> public byte[] ToBytes(UInt16 endian, bool forceFstm = false) { //Update file. Update(endian); if (forceFstm) { fileHeader.magic = "FSTM".ToCharArray(); } //Writer. MemoryStream o = new MemoryStream(); BinaryDataWriter bw = new BinaryDataWriter(o); //Write header. fileHeader.Write(ref bw); //Info block. bw.Write(info.magic); bw.Write(info.blockSize); info.streamSoundInfoRef.Write(ref bw); info.trackInfoTableRef.Write(ref bw); info.channelInfoTableRef.Write(ref bw); //Stream sound info. bw.Write(info.streamSoundInfo.encoding); bw.Write(info.streamSoundInfo.isLoop); bw.Write(info.streamSoundInfo.channelCount); bw.Write(info.streamSoundInfo.regionCount); bw.Write(info.streamSoundInfo.sampleRate); bw.Write(info.streamSoundInfo.loopStart); bw.Write(info.streamSoundInfo.sampleCount); bw.Write(info.streamSoundInfo.blockCount); bw.Write(info.streamSoundInfo.oneBlockBytesize); bw.Write(info.streamSoundInfo.oneBlockSamples); bw.Write(info.streamSoundInfo.lastBlockBytesize); bw.Write(info.streamSoundInfo.lastBlockSamples); bw.Write(info.streamSoundInfo.lastBlockPaddedBytesize); bw.Write(info.streamSoundInfo.sizeOfSeekInfo); bw.Write(info.streamSoundInfo.seekInfoIntervalSamples); info.streamSoundInfo.sampleDataOffset.Write(ref bw); if (fileHeader.vMajor >= regionInfo) { bw.Write(info.streamSoundInfo.regionInfoBytesize); bw.Write(info.streamSoundInfo.padding); info.streamSoundInfo.regionDataOffset.Write(ref bw); } if (fileHeader.vMajor >= originalLoopInfo) { bw.Write(info.streamSoundInfo.originalLoopStart); bw.Write(info.streamSoundInfo.originalLoopEnd); } if (fileHeader.vMajor >= secretInfo) { bw.Write(info.streamSoundInfo.secretInfo); } //Write tables. if (info.trackInfoRefTable != null) { info.trackInfoRefTable.Write(ref bw); } if (info.channelInfoRefTable != null) { info.channelInfoRefTable.Write(ref bw); } //Write tracks. if (info.tracks != null) { foreach (TrackInfo t in info.tracks) { bw.Write(t.volume); bw.Write(t.pan); bw.Write(t.span); bw.Write(t.surroundMode); t.globalChannelIndexTableRef.Write(ref bw); if (t.globalChannelIndexTable != null) { bw.Write(t.globalChannelIndexTable.count); foreach (byte b in t.globalChannelIndexTable.entries) { bw.Write(b); } while (bw.Position % 4 != 0) { bw.Write((byte)0); } } } } //Write channels. if (info.channels != null) { foreach (ChannelInfo c in info.channels) { c.dspAdpcmInfoRef.Write(ref bw); } foreach (ChannelInfo c in info.channels) { if (c.dspAdpcmInfo != null) { bw.Write(c.dspAdpcmInfo.coefs[0]); bw.Write(c.dspAdpcmInfo.coefs[1]); bw.Write(c.dspAdpcmInfo.coefs[2]); bw.Write(c.dspAdpcmInfo.coefs[3]); bw.Write(c.dspAdpcmInfo.coefs[4]); bw.Write(c.dspAdpcmInfo.coefs[5]); bw.Write(c.dspAdpcmInfo.coefs[6]); bw.Write(c.dspAdpcmInfo.coefs[7]); bw.Write(c.dspAdpcmInfo.pred_scale); bw.Write(c.dspAdpcmInfo.yn1); bw.Write(c.dspAdpcmInfo.yn2); bw.Write(c.dspAdpcmInfo.loop_pred_scale); bw.Write(c.dspAdpcmInfo.loop_yn1); bw.Write(c.dspAdpcmInfo.loop_yn2); bw.Write((UInt16)0); } } } //Write padding. while (bw.Position % 0x20 != 0) { bw.Write((byte)0); } //Seek block (if needed). if (info.streamSoundInfo.encoding >= EncodingTypes.DSP_ADPCM) { seek.Write(ref bw, fileHeader); } //Region block (if needed). if (region != null) { region.Write(ref bw); } //Data block. data.WriteSTM(ref bw, info, info.streamSoundInfo.sampleCount); //Return finished file. return(o.ToArray()); }
/// <summary> /// Update the file. /// </summary> public void Update(UInt16 endian) { //Update info. info.magic = "INFO".ToCharArray(); info.streamSoundInfoRef = new Reference(ReferenceTypes.STRM_Info_StreamSound, 0x18); //Stream sound info. UInt32 infoBaseOffet = 0x18; info.streamSoundInfo.channelCount = (byte)info.channels.Count; info.streamSoundInfo.regionCount = (byte)(region != null ? region.regions.Length : 0); info.streamSoundInfo.regionDataOffset = new Reference(0, 0x18); info.streamSoundInfo.regionInfoBytesize = 0x100; info.streamSoundInfo.sampleDataOffset = new Reference(ReferenceTypes.General_ByteStream, 0x18); info.streamSoundInfo.padding = 0; info.streamSoundInfo.sizeOfSeekInfo = 4; infoBaseOffet += 56; if (fileHeader.vMajor >= regionInfo) { infoBaseOffet += 12; } if (fileHeader.vMajor >= originalLoopInfo) { infoBaseOffet += 8; } if (fileHeader.vMajor >= secretInfo) { infoBaseOffet += 4; } //Reference table for track info ref. info.trackInfoTableRef = new Reference(0x0101, (int)infoBaseOffet); //Skip ref table coords. infoBaseOffet += (uint)(info.tracks != null ? (info.tracks.Count * 8 + 4) : 0); info.channelInfoTableRef = new Reference(0x0101, (int)infoBaseOffet); List <Reference> trackRefs = new List <Reference>(); //Track info. UInt32 trackBaseOffset = (uint)((info.tracks != null ? (info.tracks.Count * 8 + 4) : 0) + (info.channels.Count * 8 + 4)); if (info.tracks != null) { for (int i = 0; i < info.tracks.Count; i++) { trackRefs.Add(new Reference(ReferenceTypes.STRM_Info_Track, (int)trackBaseOffset)); infoBaseOffet += 12; trackBaseOffset += 12; if (info.tracks[i].globalChannelIndexTable != null) { info.tracks[i].globalChannelIndexTableRef = new Reference(0x100, 0xC); info.tracks[i].globalChannelIndexTable.count = (uint)info.tracks[i].globalChannelIndexTable.entries.Count; infoBaseOffet += 4 + info.tracks[i].globalChannelIndexTable.count; trackBaseOffset += 4 + info.tracks[i].globalChannelIndexTable.count; while (infoBaseOffet % 4 != 0) { infoBaseOffet++; trackBaseOffset++; } } else { info.tracks[i].globalChannelIndexTableRef = new Reference(0, Reference.NULL_PTR); } } info.trackInfoRefTable = new ReferenceTable(trackRefs); } else { info.trackInfoTableRef = new Reference(0, Reference.NULL_PTR); info.trackInfoRefTable = null; } //Channel info. UInt32 channelBaseOffset = (uint)(4 + 8 * info.channels.Count() + trackBaseOffset - ((uint)((info.tracks != null ? (info.tracks.Count * 8 + 4) : 0) + (info.channels.Count * 8 + 4)))); infoBaseOffet += (uint)(4 + 8 * info.channels.Count()); List <Reference> channelRefs = new List <Reference>(); for (int i = 0; i < info.channels.Count; i++) { channelRefs.Add(new Reference(ReferenceTypes.STRM_Info_Channel, (int)channelBaseOffset)); channelBaseOffset += 8; infoBaseOffet += 8; } info.channelInfoRefTable = new ReferenceTable(channelRefs); channelBaseOffset = (uint)(8 * info.channels.Count()); for (int i = 0; i < info.channels.Count; i++) { info.channels[i].dspAdpcmInfoRef = new Reference(0, Reference.NULL_PTR); if (info.channels[i].dspAdpcmInfo != null) { info.channels[i].dspAdpcmInfoRef = new Reference(0x300, (int)channelBaseOffset); channelBaseOffset += 0x2E; infoBaseOffet += 0x2E; } channelBaseOffset -= 8; } //Make offsets, depeding on what blocks exist, and on the sizes of the blocks. List <SizedReference> blocks = new List <SizedReference>(); //Can't forget about magic and size. infoBaseOffet += 8; while (infoBaseOffet % 0x20 != 0) { infoBaseOffet++; } info.blockSize = infoBaseOffet; blocks.Add(new SizedReference(ReferenceTypes.STRM_Block_Info, 0, info.blockSize)); if (seek != null) { blocks.Add(new SizedReference(ReferenceTypes.STRM_Block_Seek, (int)info.blockSize, seek.GetSize())); } if (region != null) { blocks.Add(new SizedReference(ReferenceTypes.STRM_Block_Region, (int)info.blockSize + (seek != null ? (int)seek.GetSize() : 0), region.GetSize())); } blocks.Add(new SizedReference(ReferenceTypes.STRM_Block_Data, (int)info.blockSize + (seek != null ? (int)seek.GetSize() : 0) + (region != null ? (int)region.GetSize() : 0), data.GetSize(info.streamSoundInfo.encoding, ref info))); fileHeader = new FileHeader(endian == ByteOrder.LittleEndian ? "CSTM" : "FSTM", endian, fileHeader.vMajor, fileHeader.vMinor, fileHeader.vRevision, (uint)(info.blockSize + (seek != null ? (int)seek.GetSize() : 0) + (region != null ? (int)region.GetSize() : 0) + data.GetSize(info.streamSoundInfo.encoding, ref info)), blocks); if (region == null) { info.streamSoundInfo.regionDataOffset = new Reference(0, Reference.NULL_PTR); } //Update stuff. MemoryStream o = new MemoryStream(); BinaryDataWriter bw = new BinaryDataWriter(o); if (seek != null) { seek.Write(ref bw, fileHeader); } if (region != null) { region.Write(ref bw); } data.WriteSTM(ref bw, info, info.streamSoundInfo.sampleCount); }
private void WriteDictionary(BinaryDataWriter writer, object obj) { // Create a string-object dictionary out of the members. Dictionary <string, object> dictionary = new Dictionary <string, object>(); // Add the custom members if any have been created when collecting node contents previously. Dictionary <string, object> customMembers; if (_customMembers.TryGetValue(obj, out customMembers)) { foreach (KeyValuePair <string, object> customMember in customMembers) { dictionary.Add(customMember.Key, customMember.Value); } } // Add the ByamlMemberAttribute decorated members. ByamlObjectInfo objectInfo = _byamlObjectInfos[obj.GetType()]; foreach (KeyValuePair <string, ByamlMemberInfo> member in objectInfo.Members) { object value = member.Value.GetValue(obj); if (value != null || !member.Value.Optional) { dictionary.Add(member.Key, value); } } // Dictionaries need to be sorted ordinally by key. var sortedDict = dictionary.Values.Zip(dictionary.Keys, (Value, Key) => new { Key, Value }) .OrderBy(x => x.Key, StringComparer.Ordinal).ToList(); WriteTypeAndElementCount(writer, ByamlNodeType.Dictionary, dictionary.Count); // Write the key-value pairs. Dictionary <Offset, object> offsetElements = new Dictionary <Offset, object>(); foreach (var keyValuePair in sortedDict) { string key = keyValuePair.Key; object element = keyValuePair.Value; // Get the index of the key string in the file's name array and write it together with the type. uint keyIndex = (uint)_nameArray.IndexOf(key); ByamlNodeType nodeType = element == null ? ByamlNodeType.Null : GetNodeType(element.GetType()); if (Settings.ByteOrder == ByteOrder.BigEndian) { writer.Write(keyIndex << 8 | (uint)nodeType); } else { writer.Write(keyIndex | (uint)nodeType << 24); } // Write the elements. Complex types are just offsets, primitive types are directly written as values. if (nodeType == ByamlNodeType.Array || nodeType == ByamlNodeType.Dictionary) { offsetElements.Add(writer.ReserveOffset(), element); } else { WritePrimitiveType(writer, nodeType, element); } } // Write the array or dictionary elements and satisfy their offsets. foreach (KeyValuePair <Offset, object> offsetElement in offsetElements) { WriteArrayOrDictionary(writer, offsetElement.Key, offsetElement.Value); } }
/// <summary> /// Convert the file to bytes. /// </summary> /// <returns></returns> public byte[] ToBytes(UInt16 byteOrder, bool forceFwav = false, Int32 paddingAmount = 0x20) { //Update file. Update(byteOrder, paddingAmount); if (forceFwav) { fileHeader.magic = "FWAV".ToCharArray(); } //New stream. MemoryStream o = new MemoryStream(); BinaryDataWriter bw = new BinaryDataWriter(o); //File header. fileHeader.Write(ref bw); //Write info. bw.Write(info.magic); bw.Write(info.blockSize); bw.Write(info.encoding); bw.Write(info.isLoop); bw.Write(info.padding); bw.Write(info.sampleRate); bw.Write(info.loopStart); bw.Write(info.loopEnd); bw.Write(info.originalLoopStart); info.channelInfoRefTable.Write(ref bw); //Write channel info reference table. foreach (InfoBlock.ChannelInfo c in info.channelInfo) { c.samplesRef.Write(ref bw); c.dspAdpcmInfoRef.Write(ref bw); bw.Write(c.reserved); } //Write channel info reference table. foreach (InfoBlock.ChannelInfo c in info.channelInfo) { if (c.dspAdpcmInfo != null) { bw.Write(c.dspAdpcmInfo.coefs[0]); bw.Write(c.dspAdpcmInfo.coefs[1]); bw.Write(c.dspAdpcmInfo.coefs[2]); bw.Write(c.dspAdpcmInfo.coefs[3]); bw.Write(c.dspAdpcmInfo.coefs[4]); bw.Write(c.dspAdpcmInfo.coefs[5]); bw.Write(c.dspAdpcmInfo.coefs[6]); bw.Write(c.dspAdpcmInfo.coefs[7]); bw.Write(c.dspAdpcmInfo.pred_scale); bw.Write(c.dspAdpcmInfo.yn1); bw.Write(c.dspAdpcmInfo.yn2); bw.Write(c.dspAdpcmInfo.loop_pred_scale); bw.Write(c.dspAdpcmInfo.loop_yn1); bw.Write(c.dspAdpcmInfo.loop_yn2); bw.Write((UInt16)0); } } //Padding. while ((bw.Position % 0x20) != 0) { bw.Write((byte)0); } //Write data block. data.WriteWAV(ref bw, info, paddingAmount); //Return file. return(o.ToArray()); }
/// <summary> /// Returns the conversion delegate for converting data available in the given <paramref name="attribFormat"/> /// from a <see cref="Vector4F"/> instance. Useful to prevent repetitive lookup for multiple values. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="attribFormat">The <see cref="GX2AttribFormat"/> of the data.</param> /// <returns>A conversion delegate for the data.</returns> public static Action <BinaryDataWriter, Vector4F> GetGX2AttribCallback(this BinaryDataWriter self, GX2AttribFormat attribFormat) { switch (attribFormat) { // 8-bit (8 x 1) case GX2AttribFormat.Format_8_UNorm: return(Write_8_UNorm); case GX2AttribFormat.Format_8_UInt: return(Write_8_UInt); case GX2AttribFormat.Format_8_SNorm: return(Write_8_SNorm); case GX2AttribFormat.Format_8_SInt: return(Write_8_SInt); case GX2AttribFormat.Format_8_UIntToSingle: return(Write_8_UIntToSingle); case GX2AttribFormat.Format_8_SIntToSingle: return(Write_8_SIntToSingle); // 8-bit (4 x 2) case GX2AttribFormat.Format_4_4_UNorm: return(Write_4_4_UNorm); // 16-bit (16 x 1) case GX2AttribFormat.Format_16_UNorm: return(Write_16_UNorm); case GX2AttribFormat.Format_16_UInt: return(Write_16_UInt); case GX2AttribFormat.Format_16_SNorm: return(Write_16_SNorm); case GX2AttribFormat.Format_16_SInt: return(Write_16_SInt); case GX2AttribFormat.Format_16_Single: return(Write_16_Single); case GX2AttribFormat.Format_16_UIntToSingle: return(Write_16_UIntToSingle); case GX2AttribFormat.Format_16_SIntToSingle: return(Write_16_SIntToSingle); // 16-bit (8 x 2) case GX2AttribFormat.Format_8_8_UNorm: return(Write_8_8_UNorm); case GX2AttribFormat.Format_8_8_UInt: return(Write_8_8_UInt); case GX2AttribFormat.Format_8_8_SNorm: return(Write_8_8_SNorm); case GX2AttribFormat.Format_8_8_SInt: return(Write_8_8_SInt); case GX2AttribFormat.Format_8_8_UIntToSingle: return(Write_8_8_UIntToSingle); case GX2AttribFormat.Format_8_8_SIntToSingle: return(Write_8_8_SIntToSingle); // 32-bit (32 x 1) case GX2AttribFormat.Format_32_UInt: return(Write_32_UInt); case GX2AttribFormat.Format_32_SInt: return(Write_32_SInt); case GX2AttribFormat.Format_32_Single: return(Write_32_Single); // 32-bit (16 x 2) case GX2AttribFormat.Format_16_16_UNorm: return(Write_16_16_UNorm); case GX2AttribFormat.Format_16_16_UInt: return(Write_16_16_UInt); case GX2AttribFormat.Format_16_16_SNorm: return(Write_16_16_SNorm); case GX2AttribFormat.Format_16_16_SInt: return(Write_16_16_SInt); case GX2AttribFormat.Format_16_16_Single: return(Write_16_16_Single); case GX2AttribFormat.Format_16_16_UIntToSingle: return(Write_16_16_UIntToSingle); case GX2AttribFormat.Format_16_16_SIntToSingle: return(Write_16_16_SIntToSingle); // 32-bit (10/11 x 3) case GX2AttribFormat.Format_10_11_11_Single: return(Write_10_11_11_Single); // 32-bit (8 x 4) case GX2AttribFormat.Format_8_8_8_8_UNorm: return(Write_8_8_8_8_UNorm); case GX2AttribFormat.Format_8_8_8_8_UInt: return(Write_8_8_8_8_UInt); case GX2AttribFormat.Format_8_8_8_8_SNorm: return(Write_8_8_8_8_SNorm); case GX2AttribFormat.Format_8_8_8_8_SInt: return(Write_8_8_8_8_SInt); case GX2AttribFormat.Format_8_8_8_8_UIntToSingle: return(Write_8_8_8_8_UIntToSingle); case GX2AttribFormat.Format_8_8_8_8_SIntToSingle: return(Write_8_8_8_8_SIntToSingle); // 32-bit (10 x 3 + 2) case GX2AttribFormat.Format_10_10_10_2_UNorm: return(Write_10_10_10_2_UNorm); case GX2AttribFormat.Format_10_10_10_2_UInt: return(Write_10_10_10_2_UInt); case GX2AttribFormat.Format_10_10_10_2_SNorm: return(Write_10_10_10_2_SNorm); case GX2AttribFormat.Format_10_10_10_2_SInt: return(Write_10_10_10_2_SInt); // 64-bit (32 x 2) case GX2AttribFormat.Format_32_32_UInt: return(Write_32_32_UInt); case GX2AttribFormat.Format_32_32_SInt: return(Write_32_32_SInt); case GX2AttribFormat.Format_32_32_Single: return(Write_32_32_Single); // 64-bit (16 x 4) case GX2AttribFormat.Format_16_16_16_16_UNorm: return(Write_16_16_16_16_UNorm); case GX2AttribFormat.Format_16_16_16_16_UInt: return(Write_16_16_16_16_UInt); case GX2AttribFormat.Format_16_16_16_16_SNorm: return(Write_16_16_16_16_SNorm); case GX2AttribFormat.Format_16_16_16_16_SInt: return(Write_16_16_16_16_SInt); case GX2AttribFormat.Format_16_16_16_16_Single: return(Write_16_16_16_16_Single); case GX2AttribFormat.Format_16_16_16_16_UIntToSingle: return(Write_16_16_16_16_UIntToSingle); case GX2AttribFormat.Format_16_16_16_16_SIntToSingle: return(Write_16_16_16_16_SIntToSingle); // 96-bit (32 x 3) case GX2AttribFormat.Format_32_32_32_UInt: return(Write_32_32_32_UInt); case GX2AttribFormat.Format_32_32_32_SInt: return(Write_32_32_32_SInt); case GX2AttribFormat.Format_32_32_32_Single: return(Write_32_32_32_Single); // 128-bit (32 x 4) case GX2AttribFormat.Format_32_32_32_32_UInt: return(Write_32_32_32_32_UInt); case GX2AttribFormat.Format_32_32_32_32_SInt: return(Write_32_32_32_32_SInt); case GX2AttribFormat.Format_32_32_32_32_Single: return(Write_32_32_32_32_Single); // Invalid default: throw new ArgumentException($"Invalid {nameof(GX2AttribFormat)} {attribFormat}.", nameof(attribFormat)); } }
private static void Write_16_16_UInt(this BinaryDataWriter self, Vector4F value) { self.Write((ushort)value.X); self.Write((ushort)value.Y); }
/// <summary> /// Writes a <see cref="Bounding"/> instance into the current stream. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="value">The <see cref="Bounding"/> instance.</param> public static void Write(this BinaryDataWriter self, Bounding value) { self.Write(value.Center); self.Write(value.Extent); }
private static void Write_16_16_Single(this BinaryDataWriter self, Vector4F value) { Write(self, (Half)value.X); Write(self, (Half)value.Y); }
/// <summary> /// Writes a <see cref="Vector4U"/> instance into the current stream with the given /// <paramref name="attribFormat"/>. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="value">The <see cref="Vector4F"/> instance.</param> /// <param name="attribFormat">The <see cref="GX2AttribFormat"/> of the data.</param> public static void Write(this BinaryDataWriter self, Vector4F value, GX2AttribFormat attribFormat) { self.GetGX2AttribCallback(attribFormat).Invoke(self, value); }
// ---- 32-bit (10/11 x 3) ---- private static void Write_10_11_11_Single(this BinaryDataWriter self, Vector4F value) { throw new NotImplementedException("10-bit and 11-bit Single values have not yet been implemented."); }
// ---- 16-bit (8 x 2) ---- private static void Write_8_8_UNorm(this BinaryDataWriter self, Vector4F value) { self.Write((byte)(Algebra.Clamp(value.X, 0, 1) * 255)); self.Write((byte)(Algebra.Clamp(value.Y, 0, 1) * 255)); }
private static void Write_32_32_32_SInt(this BinaryDataWriter self, Vector4F value) { self.Write((int)value.X); self.Write((int)value.Y); self.Write((int)value.Z); }
/// <summary> /// Writes a <see cref="Decimal10x5"/> instance into the current stream. /// </summary> /// <param name="self">The extended <see cref="BinaryDataWriter"/>.</param> /// <param name="value">The <see cref="Decimal10x5"/> instance.</param> public static void Write(this BinaryDataWriter self, Decimal10x5 value) { self.Write(value.Raw); }
internal override void WriteContentLength(BinaryDataWriter dataWriter, long length) { dataWriter.Write <UInt16>(checked ((ushort)length)); }
private static void Write_8_8_SNorm(this BinaryDataWriter self, Vector4F value) { self.Write((sbyte)(Algebra.Clamp(value.X, -1, 1) * 127)); self.Write((sbyte)(Algebra.Clamp(value.Y, -1, 1) * 127)); }
protected static void Write16BitLengthPrefixedAsciiString(BinaryDataWriter dataWriter, string s) { var bytes = Encoding.ASCII.GetBytes(s); Write16BitLengthPrefixedByteArray(dataWriter, bytes); }
private static void Write_8_8_SInt(this BinaryDataWriter self, Vector4F value) { self.Write((sbyte)value.X); self.Write((sbyte)value.Y); }
public PackedDataWriteVisitor(Stream stream) { _stream = stream; _writer = new BinaryDataWriter(stream); _reservations = new Stack<WriteReservation>(); }
private static void Write_8_8_UIntToSingle(this BinaryDataWriter self, Vector4F value) { self.Write((byte)value.X); self.Write((byte)value.Y); }
// ---- 32-bit (32 x 1) ---- private static void Write_32_UInt(this BinaryDataWriter self, Vector4F value) { self.Write((uint)value.X); }
/// <summary> /// Decompresses the Yaz0-compressed contents of the input <see cref="Stream"/> and writes them directly into /// the given output <see cref="MemoryStream"/>. Both streams stay open after this method returned the number of /// decompressed bytes written. /// </summary> /// <param name="input">The input <see cref="Stream"/> from which the Yaz0-compressed data will be read.</param> /// <param name="output">The output <see cref="MemoryStream"/> to which the decompressed data will be written /// directly.</param> /// <returns>The number of decompressed bytes written to the output stream.</returns> public static int Decompress(Stream input, MemoryStream output) { using (BinaryDataReader reader = new BinaryDataReader(input, true)) using (BinaryDataWriter writer = new BinaryDataWriter(output, true)) { reader.ByteOrder = ByteOrder.BigEndian; // Read and check the header. if (reader.ReadString(4) != "Yaz0") { throw new Yaz0Exception("Invalid Yaz0 header."); } uint decompressedSize = reader.ReadUInt32(); reader.Position += 8; // Padding // Decompress the data. int decompressedBytes = 0; while (decompressedBytes < decompressedSize) { // Read the configuration byte of a decompression setting group, and go through each bit of it. byte groupConfig = reader.ReadByte(); for (int i = 7; i >= 0; i--) { // Check if bit of the current chunk is set. if ((groupConfig & (1 << i)) == (1 << i)) { // Bit is set, copy 1 raw byte to the output. writer.Write(reader.ReadByte()); decompressedBytes++; } else if (decompressedBytes < decompressedSize) // This does not make sense for last byte. { // Bit is not set and data copying configuration follows, either 2 or 3 bytes long. ushort dataBackSeekOffset = reader.ReadUInt16(); int dataSize; // If the nibble of the first back seek offset byte is 0, the config is 3 bytes long. byte nibble = (byte)(dataBackSeekOffset >> 12/*1 byte (8 bits) + 1 nibble (4 bits)*/); if (nibble == 0) { // Nibble is 0, the number of bytes to read is in third byte, which is (size + 0x12). dataSize = reader.ReadByte() + 0x12; } else { // Nibble is not 0, and determines (size + 0x02) of bytes to read. dataSize = nibble + 0x02; // Remaining bits are the real back seek offset. dataBackSeekOffset &= 0x0FFF; } // Since bytes can be reread right after they were written, write and read bytes one by one. for (int j = 0; j < dataSize; j++) { // Read one byte from the current back seek position. writer.Position -= dataBackSeekOffset + 1; byte readByte = (byte)writer.BaseStream.ReadByte(); // Write the byte to the end of the memory stream. writer.Seek(0, SeekOrigin.End); writer.Write(readByte); decompressedBytes++; } } } } return decompressedBytes; } }