Beispiel #1
0
 public static void WriteByteProperty(this EndianWriter stream, IMEPackage pcc, NameReference propName, byte value, int staticArrayIndex)
 {
     //Debug.WriteLine("Writing byte property " + propName + ", value: " + value + " at 0x" + stream.Position.ToString("X6"));
     stream.WritePropHeader(pcc, propName, PropertyType.ByteProperty, 1, staticArrayIndex);
     if (pcc.Game >= MEGame.ME3)
     {
         stream.WriteNameReference("None", pcc);
     }
     stream.WriteByte(value);
 }
        /// <summary>
        /// Reads a shader from a stream and then serializes it into a byte array which can then be exported.
        /// </summary>
        /// <param name="reader">The stream to read from. It should be positioned at the beginning of the shader pointer.</param>
        /// <param name="type">The type of shader to read.</param>
        /// <returns>
        /// The serialized shader data, or <c>null</c> if reading failed.
        /// </returns>
        public byte[] ExportShader(IReader reader, ShaderType type)
        {
            var info = ReadShaderInfo(reader, type);

            if (info == null)
            {
                return(null);
            }

            using (var memStream = new MemoryStream())
            {
                using (var writer = new EndianWriter(memStream, Endian.BigEndian))
                {
                    writer.WriteInt32(SerializationMagic);
                    writer.WriteByte((byte)type);

                    // Write the layout size for compatibility checking when deserializing
                    if (type == ShaderType.Pixel)
                    {
                        writer.WriteInt32(_pixelShaderInfoLayout.Size);
                    }
                    else
                    {
                        writer.WriteInt32(_vertexShaderInfoLayout.Size);
                    }

                    // Write the raw debug info
                    reader.SeekTo(info.DebugInfoOffset);
                    writer.WriteUInt32(info.DebugInfoSize);
                    writer.WriteBlock(reader.ReadBlock((int)info.DebugInfoSize));

                    // Write the raw shader data
                    var dataOffset = _cacheFile.MetaArea.PointerToOffset(info.DataAddress);
                    reader.SeekTo(dataOffset);
                    writer.WriteUInt32(info.DataSize);
                    writer.WriteBlock(reader.ReadBlock((int)info.DataSize));

                    // Create the result from the memory stream's buffer
                    var result = new byte[memStream.Length];
                    Buffer.BlockCopy(memStream.ToArray(), 0, result, 0, (int)memStream.Length);
                    return(result);
                }
            }
        }
		/// <summary>
		/// Reads a shader from a stream and then serializes it into a byte array which can then be exported.
		/// </summary>
		/// <param name="reader">The stream to read from. It should be positioned at the beginning of the shader pointer.</param>
		/// <param name="type">The type of shader to read.</param>
		/// <returns>
		/// The serialized shader data, or <c>null</c> if reading failed.
		/// </returns>
		public byte[] ExportShader(IReader reader, ShaderType type)
		{
			var info = ReadShaderInfo(reader, type);
			if (info == null)
				return null;

			using (var memStream = new MemoryStream())
			{
				using (var writer = new EndianWriter(memStream, Endian.BigEndian))
				{
					writer.WriteInt32(SerializationMagic);
					writer.WriteByte((byte)type);

					// Write the layout size for compatibility checking when deserializing
					if (type == ShaderType.Pixel)
						writer.WriteInt32(_pixelShaderInfoLayout.Size);
					else
						writer.WriteInt32(_vertexShaderInfoLayout.Size);

					// Write the raw debug info
					reader.SeekTo(info.DebugInfoOffset);
					writer.WriteUInt32(info.DebugInfoSize);
					writer.WriteBlock(reader.ReadBlock((int)info.DebugInfoSize));

					// Write the raw shader data
					var dataOffset = _cacheFile.MetaArea.PointerToOffset(info.DataAddress);
					reader.SeekTo(dataOffset);
					writer.WriteUInt32(info.DataSize);
					writer.WriteBlock(reader.ReadBlock((int)info.DataSize));

					// Create the result from the memory stream's buffer
					var result = new byte[memStream.Length];
					Buffer.BlockCopy(memStream.GetBuffer(), 0, result, 0, (int)memStream.Length);
					return result;
				}
			}
		}
Beispiel #4
0
        private void WriteChunks(SerializingContainer2 sc)
        {
            EndianWriter writer = sc.ms.Writer;

            if (EmbeddedFiles.Count > 0)
            {
                writer.WriteUInt32(didx);
                writer.WriteInt32(EmbeddedFiles.Count * 12);
                var dataChunk = new MemoryStream();
                foreach ((uint id, byte[] bytes) in EmbeddedFiles)
                {
                    dataChunk.WriteZeros((int)(dataChunk.Position.Align(16) - dataChunk.Position)); //files must be 16-byte aligned in the data chunk
                    writer.WriteUInt32(id);                                                         //writing to DIDX
                    writer.WriteInt32((int)dataChunk.Position);                                     //Writing to DIDX
                    writer.WriteInt32(bytes.Length);                                                //Writing to DIDX
                    dataChunk.WriteFromBuffer(bytes);                                               //Writing to DATA
                }

                writer.WriteUInt32(data);
                writer.WriteInt32((int)dataChunk.Length);
                writer.WriteFromBuffer(dataChunk.ToArray());
            }

            if (sc.Game == MEGame.ME2 && ME2STMGFallback != null)
            {
                writer.WriteUInt32(stmg);
                writer.WriteInt32(ME2STMGFallback.Length);
                writer.WriteFromBuffer(ME2STMGFallback);
            }

            if (sc.Game == MEGame.ME3 && InitStateManagement != null)
            {
                writer.WriteUInt32(stmg);
                var lenPos = sc.ms.Position;
                writer.WriteUInt32(0);
                writer.WriteFloat(InitStateManagement.VolumeThreshold);
                writer.WriteUInt16(InitStateManagement.MaxVoiceInstances);
                writer.WriteInt32(InitStateManagement.StateGroups.Count);
                foreach ((uint _, WwiseStateManagement.StateGroup stateGroup) in InitStateManagement.StateGroups)
                {
                    writer.WriteUInt32(stateGroup.ID);
                    writer.WriteUInt32(stateGroup.DefaultTransitionTime);
                    writer.WriteInt32(stateGroup.CustomTransitionTimes.Count);
                    foreach (var transTime in stateGroup.CustomTransitionTimes)
                    {
                        writer.WriteUInt32(transTime.FromStateID);
                        writer.WriteUInt32(transTime.ToStateID);
                        writer.WriteUInt32(transTime.TransitionTime);
                    }
                }
                writer.WriteInt32(InitStateManagement.SwitchGroups.Count);
                foreach ((uint _, WwiseStateManagement.SwitchGroup switchGroup) in InitStateManagement.SwitchGroups)
                {
                    writer.WriteUInt32(switchGroup.ID);
                    writer.WriteUInt32(switchGroup.GameParamID);
                    writer.WriteInt32(switchGroup.Points.Count);
                    foreach (var point in switchGroup.Points)
                    {
                        writer.WriteFloat(point.GameParamValue);
                        writer.WriteUInt32(point.SwitchID);
                        writer.WriteUInt32(point.CurveShape);
                    }
                }
                writer.WriteInt32(InitStateManagement.GameParameterDefaultValues.Count);
                foreach ((uint id, float defaultValue) in InitStateManagement.GameParameterDefaultValues)
                {
                    writer.WriteUInt32(id);
                    writer.WriteFloat(defaultValue);
                }
                var endPos = sc.ms.Position;
                sc.ms.JumpTo(lenPos);
                writer.WriteInt32((int)(endPos - lenPos - 4));
                sc.ms.JumpTo(endPos);
            }

            if (HIRCObjects.Count > 0)
            {
                writer.WriteUInt32(hirc);
                var lengthPos = sc.ms.Position;
                writer.WriteUInt32(0);
                writer.WriteInt32(HIRCObjects.Count);
                foreach ((uint _, HIRCObject h) in HIRCObjects)
                {
                    writer.WriteFromBuffer(h.ToBytes(sc.Game));
                }

                var endPos = sc.ms.Position;
                sc.ms.JumpTo(lengthPos);
                writer.WriteInt32((int)(endPos - lengthPos - 4));
                sc.ms.JumpTo(endPos);
            }

            if (ReferencedBanks.Count > 0)
            {
                writer.WriteUInt32(stid);
                var lengthPos = sc.ms.Position;
                writer.WriteUInt32(0);
                writer.WriteUInt32(1);
                writer.WriteInt32(ReferencedBanks.Count);
                foreach ((uint id, string name) in ReferencedBanks)
                {
                    writer.WriteUInt32(id);
                    writer.WriteByte((byte)name.Length);
                    writer.WriteStringASCII(name);
                }

                var endPos = sc.ms.Position;
                sc.ms.JumpTo(lengthPos);
                writer.WriteInt32((int)(endPos - lengthPos - 4));
                sc.ms.JumpTo(endPos);
            }

            if (FXPR_Chunk != null)
            {
                writer.WriteUInt32(fxpr);
                writer.WriteInt32(FXPR_Chunk.Length);
                writer.WriteFromBuffer(FXPR_Chunk);
            }

            if (ENVS_Chunk != null)
            {
                writer.WriteUInt32(envs);
                writer.WriteInt32(ENVS_Chunk.Length);
                writer.WriteFromBuffer(ENVS_Chunk);
            }
        }