Exemple #1
0
 public unsafe void FinalizeTracker(Tracker tracker, AssetBuffer buffer)
 {
     using (Chunk chunk = new Chunk())
     {
         tracker.MakeRelocatable(chunk);
         buffer.InstanceData = new byte[chunk.InstanceBufferSize];
         if (chunk.InstanceBufferSize > 0)
         {
             fixed(byte *pBuffer = &buffer.InstanceData[0])
             {
                 MarshalUtil.CopyMemory((IntPtr)pBuffer, chunk.InstanceBuffer, chunk.InstanceBufferSize);
             }
         }
         buffer.RelocationData = new byte[chunk.RelocationBufferSize];
         if (chunk.RelocationBufferSize > 0)
         {
             fixed(byte *pBuffer = &buffer.RelocationData[0])
             {
                 MarshalUtil.CopyMemory((IntPtr)pBuffer, chunk.RelocationBuffer, chunk.RelocationBufferSize);
             }
         }
         buffer.ImportsData = new byte[chunk.ImportsBufferSize];
         if (chunk.ImportsBufferSize > 0)
         {
             fixed(byte *pBuffer = &buffer.ImportsData[0])
             {
                 MarshalUtil.CopyMemory((IntPtr)pBuffer, chunk.ImportsBuffer, chunk.ImportsBufferSize);
             }
         }
     }
 }
 public AssetDownInfo()
 {
     Buffer    = new AssetBuffer();
     AssetName = string.Empty;
     MD5       = string.Empty;
     Version   = string.Empty;
     TotalSize = 0;
 }
 public void Reset()
 {
     Buffer.Clear();
     if (DownloadedBuffer != null)
     {
         DownloadedBuffer.Clear();
         DownloadedBuffer = null;
     }
     BufferNumber = 0;
 }
Exemple #4
0
 private unsafe void FinalizeTracker(Tracker tracker, AssetBuffer buffer)
 {
     Relo.Chunk chunk = new Relo.Chunk();
     tracker.MakeRelocatable(chunk);
     buffer.InstanceData = chunk.InstanceBuffer;
     if (chunk.RelocationBuffer.Length > 0)
     {
         buffer.RelocationData = chunk.RelocationBuffer;
     }
     else
     {
         buffer.RelocationData = new byte[0];
     }
     if (chunk.ImportsBuffer.Length > 0)
     {
         buffer.ImportsData = chunk.ImportsBuffer;
     }
     else
     {
         buffer.ImportsData = new byte[0];
     }
 }
Exemple #5
0
        private unsafe AssetBuffer ProcessAudioFileInstance(InstanceDeclaration declaration)
        {
            AssetBuffer       result      = new AssetBuffer();
            bool              isBigEndian = _platform != TargetPlatform.Win32;
            Node              node        = new Node(declaration.Node.CreateNavigator(), declaration.Document.NamespaceManager);
            AudioFileRuntime *audioFileRuntime;

            using (Tracker tracker = new Tracker((void **)&audioFileRuntime, (uint)sizeof(AudioFileRuntime), isBigEndian))
            {
                AudioFile audioFile = new AudioFile();
                audioFile.Marshal(node);
                string file    = declaration.XmlNode.Attributes["File"].Value;
                bool   isSound = Path.GetDirectoryName(file).ToLower().EndsWith("\\sounds");
                int    codec;
                PCAudioCompressionSetting compression = audioFile.PCCompression ?? (isSound ? PCAudioCompressionSetting.XAS : PCAudioCompressionSetting.NONE);
                switch (compression)
                {
                case PCAudioCompressionSetting.NONE:     // would become '1'
                    codec = 1;
                    break;

                case PCAudioCompressionSetting.XAS:     // would become '29', xbox XMA would be '28'
                    codec = 29;
                    break;

                case PCAudioCompressionSetting.EALAYER3:     // would become '31', '30' in TW (-ealayer3_int)
                    codec = 31;
                    break;

                default:
                    throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Internal error: xml compiler returned bad PC audio compression type of {0}", compression);
                }
                bool isStreamed = audioFile.IsStreamedOnPC ?? !isSound;
                if (audioFile.SubtitleStringName is null)
                {
                    audioFile.SubtitleStringName = $"DIALOGEVENT:{Path.GetFileNameWithoutExtension(file)}SubTitle";
                }
                Marshaler.Marshal(audioFile.SubtitleStringName, &audioFileRuntime->SubtitleStringName, tracker);
                int type = SIMEX_id(file, 0);
                if (type < 0)
                {
                    Console.WriteLine("Warning: Unable to identify format of \"{0}\"; cannot process. (Error: {1})", file, SIMEX_getlasterr());
                    return(null);
                }
                if (type != 1)
                {
                    Console.WriteLine("Warning: Input files must be WAVE format. Cannot process \"{0}\"", file);
                    return(null);
                }
                SINSTANCE *instance = null;
                int        count    = SIMEX_open(file, 0, type, &instance);
                using (AutoSINSTANCECloser closer = new AutoSINSTANCECloser(instance))
                {
                    if (count <= 0 || instance == null)
                    {
                        Console.WriteLine("Warning: Could not begin audio processing of \"{0}\": {1}.", file, SIMEX_getlasterr());
                        return(null);
                    }
                    string tempFile = declaration.CustomDataPath + _tempFilenameSuffix;
                    using (AutoCleanUpTempFiles tempFiles = new AutoCleanUpTempFiles(tempFile))
                    {
                        SINSTANCE *instance2 = null;
                        if (SIMEX_create(tempFile, 39, &instance2) == 0)
                        {
                            throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Internal error preparing audio output file \"{0}\" (SIMEX_create(): {1}).", tempFile, SIMEX_getlasterr());
                        }
                        using (AutoSINSTANCEWCloser closer2 = new AutoSINSTANCEWCloser(instance2))
                        {
                            for (int i = 0; i < count; i++)
                            {
                                SINFO *info = null;
                                if (SIMEX_info(instance, &info, i) == 0)
                                {
                                    throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Internal error reading element {0} of \"{1}\" (SIMEX_info(): {2}).", file, SIMEX_getlasterr());
                                }
                                using (AutoSINFOFreer freer = new AutoSINFOFreer(info))
                                {
                                    if (info != null)
                                    {
                                        if (SIMEX_read(instance, info, i) == 0)
                                        {
                                            throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Internal error reading element {0} of \"{1}\" (SIMEX_read(): {2}).", file, SIMEX_getlasterr());
                                        }
                                        if (isStreamed)
                                        {
                                            SIMEX_setplayloc(info, 4096);
                                            _tracer.TraceNote("Setting play location to streamed");
                                        }
                                        else
                                        {
                                            SIMEX_setplayloc(info, 2048);
                                            _tracer.TraceNote("Setting play location to RAM");
                                        }
                                        SIMEX_setcodec(info, codec);
                                        _tracer.TraceNote("Setting compression type to {0}. ", SIMEX_getsamplerepname(codec));
                                    }
                                    if (codec == 31 || codec == 38)
                                    {
                                        int quality = audioFile.PCQuality;
                                        if (quality < 0 || quality > 100)
                                        {
                                            throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Audio file {0}: Quality parameter must be between 0 and 100", file);
                                        }
                                        SIMEX_setvbrquality(info, quality);
                                        _tracer.TraceNote("Setting compression quality to {0}", quality);
                                    }
                                    if (audioFile.PCSampleRate.HasValue)
                                    {
                                        int oldrate = SIMEX_getsamplerate(info);
                                        int newrate = audioFile.PCSampleRate.Value;
                                        if (oldrate != newrate)
                                        {
                                            if (newrate < 400 || newrate > 96000)
                                            {
                                                throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Audio file {0}: Sample rate must be between 400 and 96000", file);
                                            }
                                            _tracer.TraceNote("Resampling from {0} to {1} ", oldrate, newrate);
                                            SIMEX_resample(info, newrate);
                                            int rate = SIMEX_getsamplerate(info);
                                            if (newrate != rate)
                                            {
                                                Console.WriteLine("Warning: Downsampling of audio file {0} not completely sucessful. Wanted final sample of {1}Hz but got {2}Hz", file, newrate, rate);
                                            }
                                        }
                                    }
                                    audioFileRuntime->NumberOfChannels = SIMEX_getchannelconfig(info);
                                    audioFileRuntime->NumberOfSamples  = SIMEX_getnumsamples(info);
                                    audioFileRuntime->SampleRate       = SIMEX_getsamplerate(info);
                                    if (audioFileRuntime->NumberOfChannels != 1 && audioFileRuntime->NumberOfChannels != 2 && audioFileRuntime->NumberOfChannels != 4 && audioFileRuntime->NumberOfChannels != 6)
                                    {
                                        Console.WriteLine("Warning: Audio file {0} has {1} channels. The only supported channel counts are 1, 2, 4, and 6; sample will probably use only the first channel in the engine", file, audioFileRuntime->NumberOfChannels);
                                    }
                                    if (SIMEX_write(instance2, info, i) == 0)
                                    {
                                        throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Internal error writing element {0} of \"{1}\" (SIMEX_write(): {2}).", i, tempFile, SIMEX_getlasterr());
                                    }
                                }
                            }
                            string snr = tempFile + ".snr";
                            byte[] buffer;
                            using (Stream stream = File.Open(snr, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                buffer = new byte[stream.Length];
                                stream.Read(buffer, 0, buffer.Length);
                            }
                            if (isStreamed)
                            {
                                string sns = tempFile + ".sns";
                                if (File.Exists(declaration.CustomDataPath))
                                {
                                    File.Delete(declaration.CustomDataPath);
                                }
                                _tracer.TraceNote("Creating output file {0}\n", declaration.CustomDataPath);
                                File.Move(sns, declaration.CustomDataPath);
                                audioFileRuntime->HeaderDataSize = buffer.Length;
                                fixed(byte *pBuffer = &buffer[0])
                                {
                                    using (Tracker.Context context = tracker.Push(&audioFileRuntime->HeaderData, 1u, (uint)buffer.Length))
                                    {
                                        Native.MsVcRt.MemCpy(new IntPtr(audioFileRuntime->HeaderData), new IntPtr(pBuffer), new Native.SizeT(buffer.Length));
                                    }
                                }
                            }
                            else
                            {
                                if (File.Exists(declaration.CustomDataPath))
                                {
                                    File.Delete(declaration.CustomDataPath);
                                }
                                _tracer.TraceNote("Creating output file {0}\n", declaration.CustomDataPath);
                                File.Move(snr, declaration.CustomDataPath);
                            }
                            FinalizeTracker(tracker, result);
                        }
                    }
                }
            }
            return(result);
        }
Exemple #6
0
        private unsafe bool EncodeEALayer3(InstanceDeclaration declaration, AudioFile audioFile, out AssetBuffer result)
        {
            if (_platform != TargetPlatform.Win32)
            {
                throw new InvalidOperationException("Critical: EALayer3 audio compiler should not be called on non Win32 platforms.");
            }
            if (audioFile.PCCompression != PCAudioCompressionSetting.EALAYER3)
            {
                throw new InvalidOperationException("Critical: EALayer3 audio compiler should not be called on non EALayer3 compression.");
            }
            AudioFileRuntime *audioFileRuntime;
            Tracker           tracker  = new Tracker(&audioFileRuntime, _platform == TargetPlatform.Xbox360);
            string            filePath = audioFile.File;

            if (!filePath.EndsWith(".mp3"))
            {
                throw new InvalidOperationException("Critical: EALayer3 audio compiler should not be called on non MP3 files.");
            }
            bool isPathEndingWithSound = Path.GetDirectoryName(filePath).ToLower().EndsWith("\\sounds"); // flag2
            bool?isStreamedBox         = audioFile.IsStreamedOnPC;                                       // flagPtr
            int? sampleRateBox         = audioFile.PCSampleRate;                                         // numPtr1
            int  quality = audioFile.PCQuality;                                                          // num3
            // compressionType, 1 == none, 29 == xas, 28 == xma (xbox, will actually set to none or same as xas depending on the isPathEndingWithSound flag)
            MpegConverterCompressionType compression;                                                    // num4

            switch (audioFile.PCCompression)
            {
            case PCAudioCompressionSetting.NONE:
                compression = !isPathEndingWithSound ? MpegConverterCompressionType.None : MpegConverterCompressionType.XAS;
                break;

            case PCAudioCompressionSetting.XAS:
                compression = MpegConverterCompressionType.XAS;
                break;

            case PCAudioCompressionSetting.EALAYER3:
                compression = MpegConverterCompressionType.EALayer3;
                break;

            default:
                throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Internal error: xml compiler returned bad PC audio compression type of {0}.", audioFile.PCCompression);
            }
            bool isStreamed = isStreamedBox ?? !isPathEndingWithSound; // flag1

            if (audioFile.SubtitleStringName != null)
            {
                Relo.Marshaler.Marshal(audioFile.SubtitleStringName, &audioFileRuntime->SubtitleStringName, tracker);
            }
            else
            {
                Relo.Marshaler.Marshal($"DIALOGEVENT:{Path.GetFileNameWithoutExtension(filePath)}SubTitle", &audioFileRuntime->SubtitleStringName, tracker);
            }
            audioFileRuntime->NumberOfChannels = 0;
            audioFileRuntime->NumberOfSamples  = 0;
            audioFileRuntime->SampleRate       = 0;
            audioFileRuntime->HeaderData       = IntPtr.Zero;
            audioFileRuntime->HeaderDataSize   = 0;
            // identify format (SIMEX_id(streamPtr))            - we don't need that, though we might want to check for mp3
            // check format needs to be 0 or 1 aka WAVE or AIFF - we don't need that, though we might want to check for mp3
            // open audio file (SIMEX_open(streamPtr, &instancePtr, format)
            using (MpegConverter converter = new MpegConverter(filePath))
            {
                string tempFile = declaration.CustomDataPath + _tempFilenameSuffix; // str2
                _tracer.TraceNote("Creating temp file {0}", tempFile);
                using (AutoCleanUpTempFiles cleanUpTempFiles = new AutoCleanUpTempFiles(tempFile))
                {
                    // set temp filename SIMEX_setfilename(str2ptr)
                    converter.SetOutputFilePath(tempFile);
                    // create temp output file SIMEX_create(null, &instancePtr, 39)
                    try
                    {
                        converter.CreateOutputFiles();
                        MpegConverterSettings converterSettings = converter.GetSettings();
                        // start iterating through elements (num elements is returned by SIMEX_open)
                        // get info SIMEX_info(instancePtr, &infoPtr, idx)
                        // read element SIMEX_read(instancePtr, infoPtr, idx)
                        if (isStreamed)
                        {
                            // infoPtr + 526 = 0x1000;
                            converterSettings.IsStreamed = true;
                            _tracer.TraceNote("Setting play location to streamed.");
                        }
                        else
                        {
                            // infoPtr + 526 = 0x0800;
                            converterSettings.IsStreamed = false;
                            _tracer.TraceNote("Setting play location to RAM.");
                        }
                        // infoPtr + 509 = compression;
                        converterSettings.CompressionType = compression;
                        _tracer.TraceNote("Setting compression type to {0}.", compression); // SIMEX_getsamplerepname(compression)
                        if (compression == MpegConverterCompressionType.XMA || compression == MpegConverterCompressionType.EALayer3)
                        {
                            if (quality < 0 || quality > 100)
                            {
                                throw new BinaryAssetBuilderException(ErrorCode.InvalidArgument, "Audio file {0}: Quality parameter must be between 0 and 100.", declaration);
                            }
                            // infoPtr + 536 = quality
                            converterSettings.CompressionQuality = quality;
                            _tracer.TraceNote("Setting compression quality to {0}.", quality);
                        }
                        // if (sampleRateBox.HasValue && sampleRateBox.Value != *(int*)(*(infoPtr + 540))
                        if (sampleRateBox.HasValue && sampleRateBox.Value != converterSettings.SampleRate)
                        {
                            int sampleRate = sampleRateBox.Value;
                            if (sampleRate < 400 || sampleRate > 96000)
                            {
                                throw new BinaryAssetBuilderException(ErrorCode.InvalidArgument, "Audio file {0}: Sample rate must be between 400 and 96000.", declaration);
                            }
                            _tracer.TraceNote("Resampling from {0} to {1}", converterSettings.SampleRate, sampleRate); // infoPtr + 540
                            // TODO: resampling
                            _tracer.TraceWarning("Warning: Resampling is currently not implemented.");
                        }
                        // if (*(int*)infoPtr != 0)
                        audioFileRuntime->NumberOfChannels = (byte)converterSettings.NumberOfChannels; // infoPtr + 510
                        audioFileRuntime->NumberOfSamples  = converterSettings.NumberOfSamples;        // infoPtr + 544
                        audioFileRuntime->SampleRate       = converterSettings.SampleRate;             // infoPtr + 540
                        if (audioFileRuntime->NumberOfChannels != 1 && audioFileRuntime->NumberOfChannels != 2 && audioFileRuntime->NumberOfChannels != 4 && audioFileRuntime->NumberOfChannels != 6)
                        {
                            _tracer.TraceWarning("Warning: Audio file {0} has {1} channels. The only supported channel counts are 1, 2, 4, and 6; sample will probably use only the first channel in the engine.", declaration, audioFileRuntime->NumberOfChannels);
                        }
                        // TODO if (!SIMEX_write(tmpFile, infoPtr, idx))
                        if (converter.WriteOutput(converterSettings))
                        {
                            throw new BinaryAssetBuilderException(ErrorCode.InternalError, "Internal error writing element of \"{0}\".", tempFile);
                        }
                    }
                    finally
                    {
                        converter.CloseOutputFiles();
                    }
                    string tempFileSnr = tempFile + ".snr";
                    if (isStreamed)
                    {
                        string tempFileSns = tempFile + ".sns";
                        if (File.Exists(declaration.CustomDataPath))
                        {
                            File.Delete(declaration.CustomDataPath);
                        }
                        _tracer.TraceNote("Creating output file {0}\n", declaration.CustomDataPath);
                        File.Move(tempFileSns, declaration.CustomDataPath);
                        using (Stream headerStream = new FileStream(tempFileSnr, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                        {
                            int     length     = (int)headerStream.Length;
                            sbyte **headerData = (sbyte **)audioFileRuntime->HeaderData;
                            tracker.Push((void **)headerData, 1, length);
                            headerStream.Read((IntPtr)(*(int *)audioFileRuntime->HeaderData), length);
                            audioFileRuntime->HeaderDataSize = length;
                            tracker.Pop();
                        }
                    }
                    else
                    {
                        if (File.Exists(declaration.CustomDataPath))
                        {
                            File.Delete(declaration.CustomDataPath);
                        }
                        _tracer.TraceNote("Creating output file {0}\n", declaration.CustomDataPath);
                        File.Move(tempFileSnr, declaration.CustomDataPath);
                    }
                    if (tracker.IsBigEndian)
                    {
                        Tracker.ByteSwap32((uint *)&audioFileRuntime->NumberOfSamples);
                        Tracker.ByteSwap32((uint *)&audioFileRuntime->SampleRate);
                        Tracker.ByteSwap32((uint *)&audioFileRuntime->HeaderDataSize);
                    }
                    result = new AssetBuffer();
                    FinalizeTracker(tracker, result);
                }
            }
            return(true);
        }