Example #1
0
 /// <summary>
 /// Writes the spotlight to a stream
 /// </summary>
 /// <param name="writer">Output stream</param>
 public void Write(EndianWriter writer)
 {
     writer.WriteSingle(near);
     writer.WriteSingle(far);
     writer.WriteInt32(DegToBAMS(insideAngle));
     writer.WriteInt32(DegToBAMS(outsideAngle));
 }
Example #2
0
 public static void WritePropHeader(this EndianWriter stream, IMEPackage pcc, NameReference propName, PropertyType type, int size, int staticArrayIndex)
 {
     stream.WriteNameReference(propName, pcc);
     stream.WriteNameReference(type.ToString(), pcc);
     stream.WriteInt32(size);
     stream.WriteInt32(staticArrayIndex);
 }
Example #3
0
        /// <summary>
        /// Writes a Vector3 to a stream
        /// </summary>
        /// <param name="writer">Output stream</param>
        /// <param name="type">Datatype to write object as</param>
        public static void Write(this Vector3 vector, EndianWriter writer, IOType type)
        {
            switch (type)
            {
            case IOType.Short:
                writer.WriteInt16((short)vector.X);
                writer.WriteInt16((short)vector.Y);
                writer.WriteInt16((short)vector.Z);
                break;

            case IOType.Float:
                writer.WriteSingle(vector.X);
                writer.WriteSingle(vector.Y);
                writer.WriteSingle(vector.Z);
                break;

            case IOType.BAMS16:
                writer.WriteInt16((short)DegToBAMS(vector.X));
                writer.WriteInt16((short)DegToBAMS(vector.Y));
                writer.WriteInt16((short)DegToBAMS(vector.Z));
                break;

            case IOType.BAMS32:
                writer.WriteInt32(DegToBAMS(vector.X));
                writer.WriteInt32(DegToBAMS(vector.Y));
                writer.WriteInt32(DegToBAMS(vector.Z));
                break;

            default:
                throw new ArgumentException($"Type {type} not available for struct Vector3");
            }
        }
Example #4
0
        public static void WriteStringRefProperty(this EndianWriter stream, IMEPackage pcc, NameReference propName, int value, int staticArrayIndex)
        {
            //Debug.WriteLine("Writing stringref property " + propName + ", value: " + value + " at 0x" + stream.Position.ToString("X6"));

            stream.WritePropHeader(pcc, propName, PropertyType.StringRefProperty, 4, staticArrayIndex);
            stream.WriteInt32(value);
        }
Example #5
0
 public static void WriteArrayProperty(this EndianWriter stream, IMEPackage pcc, NameReference propName, int count, Stream value, int staticArrayIndex)
 {
     //Debug.WriteLine("Writing array property " + propName + ", count: " + count + " at 0x" + stream.Position.ToString("X6")+", length: "+value.Length);
     stream.WritePropHeader(pcc, propName, PropertyType.ArrayProperty, 4 + (int)value.Length, staticArrayIndex);
     stream.WriteInt32(count);
     stream.BaseStream.WriteStream(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);
                }
            }
        }
Example #7
0
        public static void WriteGuid(this EndianWriter stream, Guid value)
        {
            var data = value.ToByteArray();

            Debug.Assert(data.Length == 16);

            stream.WriteInt32(BitConverter.ToInt32(data, 0));
            stream.WriteInt16(BitConverter.ToInt16(data, 4));
            stream.WriteInt16(BitConverter.ToInt16(data, 6));
            stream.Write(data, 8, 8);
        }
Example #8
0
        private void SaveData(IStream stream)
        {
            // Create a memory buffer and write the strings there
            var buffer       = new MemoryStream();
            var bufferWriter = new EndianWriter(buffer, stream.Endianness);

            try
            {
                int count            = _strings.Count();
                int start_offset     = 0x8 + count * 4; // Start data offset of the first string
                int string_data_size = 0;
                foreach (string str in _strings)
                {
                    string_data_size += str.Length + 1;
                }
                int data_size = start_offset + string_data_size;

                // Headder
                bufferWriter.WriteInt32(count);
                bufferWriter.WriteInt32(data_size);

                // Offsets
                SaveOffsets(stream);

                // Write the strings to the buffer
                foreach (string str in _strings)
                {
                    if (str != null)
                    {
                        bufferWriter.WriteAscii(str);
                    }
                }
            }
            finally
            {
                bufferWriter.Close();
            }
        }
Example #9
0
        public override void Serialize(Stream stream)
        {
            using (var writer = new EndianWriter(stream, Endian.BigEndian))
            {
                var buildNameBytes = System.Text.Encoding.UTF8.GetBytes(BuildName);
                writer.WriteInt32(buildNameBytes.Length);
                writer.WriteBlock(buildNameBytes);

                var cacheNameBytes = System.Text.Encoding.UTF8.GetBytes(CacheName);
                writer.WriteInt32(cacheNameBytes.Length);
                writer.WriteBlock(cacheNameBytes);

                var count = Actions.Count;
                writer.WriteInt32(count);

                foreach (var action in Actions)
                {
                    writer.WriteInt64(action.Position);
                    writer.WriteInt32(action.Buffer.Length);
                    writer.WriteBlock(action.Buffer);
                }
            }
        }
Example #10
0
        private string ConvertToAudioFile(ICollection <byte> data, string path = null)
        {
            var tempFile = Path.GetTempFileName();

            byte[] footer;
            var    codec = _soundResourceGestalt.SoundPlatformCodecs[_sound.CodecIndex];

            switch (codec.Channel)
            {
            case Channel.Mono:
                footer = _monoFooter;
                break;

            case Channel.Stereo:
                footer = _stereoFooter;
                break;

            default:
                throw new NotImplementedException();
            }

            switch (_sound.Encoding)
            {
            case Encoding.XMA:
                using (var fileStream = new FileStream(tempFile, FileMode.OpenOrCreate))
                {
                    using (var writer = new EndianWriter(fileStream, Endian.BigEndian))
                    {
                        // Generate an XMA header
                        // ADAPTED FROM wwisexmabank - I DO NOT TAKE ANY CREDIT WHATSOEVER FOR THE FOLLOWING CODE.
                        // See http://hcs64.com/vgm_ripping.html for more information

                        // 'riff' chunk
                        writer.WriteInt32(0x52494646);                                 // 'RIFF'
                        writer.Endianness = Endian.LittleEndian;
                        writer.WriteInt32(data.Count + 0x34);
                        writer.Endianness = Endian.BigEndian;
                        writer.WriteInt32(0x57415645);

                        // 'data' chunk
                        writer.Endianness = Endian.BigEndian;
                        writer.WriteInt32(0x64617461);                                 // 'data'
                        writer.Endianness = Endian.LittleEndian;
                        writer.WriteInt32(data.Count);
                        writer.WriteBlock(data.ToArray());

                        // footer
                        writer.WriteBlock(footer);

                        // size
                        writer.SeekTo(0x04);
                        writer.WriteInt32((Int32)writer.Length - 0x08);
                    }
                }

                VariousFunctions.RunProgramSilently(@"A:\Xbox\Games\towav.exe",
                                                    string.Format("\"{0}\"", Path.GetFileName(tempFile)),
                                                    Path.GetDirectoryName(tempFile));

                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }

                tempFile = Path.ChangeExtension(tempFile, "wav");

                if (path != null)
                {
                    File.Move(tempFile, path);
                }

                return(path ?? tempFile);

            default:
                throw new NotImplementedException();
            }
        }
Example #11
0
        private string ConvertToAudioFile(ICollection <byte> data, string path = null)
        {
            string towav = Path.Combine(VariousFunctions.GetApplicationLocation(), "helpers", "towav.exe");

            if (!File.Exists(towav))
            {
                MetroMessageBox.Show("Cannot Convert Sound", "Sounds cannot be converted because towav.exe is not present. Copy it to the \\helpers folder inside your Assembly installation.");
                return(null);
            }

            var tempFile = Path.GetTempFileName();

            byte[] footer;
            var    codec = _soundResourceTable.Codecs[_sound.CodecIndex];

            switch ((SoundEncoding)codec.Encoding)
            {
            case SoundEncoding.Mono:
                footer = _monoFooter;
                break;

            case SoundEncoding.Stereo:
                footer = _stereoFooter;
                break;

            default:
                throw new NotImplementedException();
            }

            switch ((SoundCompression)codec.Compression)
            {
            case SoundCompression.XMA2:
                using (var fileStream = new FileStream(tempFile, FileMode.OpenOrCreate))
                {
                    using (var writer = new EndianWriter(fileStream, Endian.BigEndian))
                    {
                        // Generate an XMA header
                        // ADAPTED FROM wwisexmabank - I DO NOT TAKE ANY CREDIT WHATSOEVER FOR THE FOLLOWING CODE.
                        // See http://hcs64.com/vgm_ripping.html for more information

                        // 'riff' chunk
                        writer.WriteInt32(0x52494646);                                 // 'RIFF'
                        writer.Endianness = Endian.LittleEndian;
                        writer.WriteInt32(data.Count + 0x34);
                        writer.Endianness = Endian.BigEndian;
                        writer.WriteInt32(0x57415645);

                        // 'data' chunk
                        writer.Endianness = Endian.BigEndian;
                        writer.WriteInt32(0x64617461);                                 // 'data'
                        writer.Endianness = Endian.LittleEndian;
                        writer.WriteInt32(data.Count);
                        writer.WriteBlock(data.ToArray());

                        // footer
                        writer.WriteBlock(footer);

                        // size
                        writer.SeekTo(0x04);
                        writer.WriteInt32((Int32)writer.Length - 0x08);
                    }
                }

                VariousFunctions.RunProgramSilently(towav,
                                                    string.Format("\"{0}\"", Path.GetFileName(tempFile)),
                                                    Path.GetDirectoryName(tempFile));

                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }

                tempFile = Path.ChangeExtension(tempFile, "wav");

                if (path != null)
                {
                    File.Move(tempFile, path);
                }

                return(path ?? tempFile);

            default:
                throw new NotImplementedException();
            }
        }
Example #12
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);
            }
        }
Example #13
0
 public static void WriteDelegateProperty(this EndianWriter stream, IMEPackage pcc, NameReference propName, ScriptDelegate value, int staticArrayIndex)
 {
     stream.WritePropHeader(pcc, propName, PropertyType.DelegateProperty, 12, staticArrayIndex);
     stream.WriteInt32(value.Object);
     stream.WriteNameReference(value.FunctionName, pcc);
 }