Esempio n. 1
0
        public override void ExportBinary(IExportContainer container, Stream stream)
        {
            if (CompleteImageSize == 0)
            {
                return;
            }

            if (IsReadStreamData(container.Version))
            {
                string path = StreamData.Path;
                if (path != string.Empty)
                {
                    if (m_imageData.Length != 0)
                    {
                        throw new Exception("Texture contains data and resource path");
                    }

                    using (ResourcesFile res = File.Collection.FindResourcesFile(File, path))
                    {
                        if (res == null)
                        {
                            Logger.Log(LogType.Warning, LogCategory.Export, $"Can't export '{ValidName}' because resources file '{path}' wasn't found");
                            return;
                        }

                        using (PartialStream resStream = new PartialStream(res.Stream, res.Offset, res.Size))
                        {
                            resStream.Position = StreamData.Offset;
                            Export(container, stream, resStream, StreamData.Size);
                        }
                    }
                    return;
                }
            }

            using (MemoryStream memStream = new MemoryStream(m_imageData))
            {
                Export(container, stream, memStream, m_imageData.Length);
            }
        }
Esempio n. 2
0
        public IReadOnlyList <byte> GetImageData()
        {
            byte[] data = m_imageData;
            if (IsReadStreamData(File.Version))
            {
                if (StreamData.Path != string.Empty)
                {
                    if (m_imageData.Length != 0)
                    {
                        throw new Exception($"Texture '{ValidName}' contains both data and resource path");
                    }

                    using (ResourcesFile res = File.Collection.FindResourcesFile(File, StreamData.Path))
                    {
                        if (res != null)
                        {
                            data = new byte[StreamData.Size];
                            using (PartialStream resStream = new PartialStream(res.Stream, res.Offset, res.Size))
                            {
                                resStream.Position = StreamData.Offset;
                                resStream.ReadBuffer(data, 0, data.Length);
                            }
                        }
                    }
                }
            }

            if (IsSwapBytes(File.Platform, TextureFormat))
            {
                for (int i = 0; i < data.Length; i += 2)
                {
                    byte b = data[i];
                    data[i]     = data[i + 1];
                    data[i + 1] = b;
                }
            }

            return(data);
        }
Esempio n. 3
0
        Stream LoadBaml(Resource res, string name)
        {
            if (res.ResourceType != ResourceType.Embedded)
            {
                return(null);
            }
            Stream s = res.TryOpenStream();

            if (s == null)
            {
                return(null);
            }
            s.Position = 0;
            ResourcesFile resources;

            try
            {
                resources = new ResourcesFile(s);
            }
            catch (ArgumentException)
            {
                return(null);
            }
            foreach (var entry in resources.OrderBy(e => e.Key))
            {
                if (entry.Key == name)
                {
                    if (entry.Value is Stream)
                    {
                        return((Stream)entry.Value);
                    }
                    if (entry.Value is byte[])
                    {
                        return(new MemoryStream((byte[])entry.Value));
                    }
                }
            }
            return(null);
        }
Esempio n. 4
0
        public byte[] GetContent(ISerializedFile file)
        {
            using (ResourcesFile res = file.Collection.FindResourcesFile(file, Source))
            {
                if (res == null)
                {
                    return(null);
                }
                if (Size == 0)
                {
                    return(null);
                }

                byte[] data = new byte[Size];
                using (PartialStream resStream = new PartialStream(res.Stream, res.Offset, res.Size))
                {
                    resStream.Position = Offset;
                    resStream.ReadBuffer(data, 0, data.Length);
                }
                return(data);
            }
        }
Esempio n. 5
0
        private void Read(Stream baseStream, bool isClosable)
        {
            m_files.Clear();
            m_resources.Clear();

            using (EndianStream stream = new EndianStream(baseStream, baseStream.Position, EndianType.BigEndian))
            {
                long position = stream.BaseStream.Position;
                Header.Read(stream);
                if (Header.Generation < BundleGeneration.BF_530_x)
                {
                    ReadPre530Metadata(stream, isClosable);
                }
                else
                {
                    Read530Metadata(stream, isClosable, position);
                }

                foreach (BundleMetadata metadata in Metadatas)
                {
                    foreach (BundleFileEntry entry in metadata.AssetsEntries)
                    {
                        if (!IsSerializedFileLoaded(entry.Name))
                        {
                            SerializedFile file = entry.ReadSerializedFile(m_fileCollection, OnRequestDependency);
                            m_files.Add(file);
                        }
                    }
                    foreach (BundleFileEntry entry in metadata.ResourceEntries)
                    {
                        ResourcesFile resesFile = entry.ReadResourcesFile(m_filePath);
                        m_resources.Add(resesFile);
                    }
                }
            }
        }
Esempio n. 6
0
 public bool CheckAssetIntegrity()
 {
     if (IsReadLoadType(File.Version))
     {
         using (ResourcesFile res = File.Collection.FindResourcesFile(File, FSBResource.Source))
         {
             return(res != null);
         }
     }
     else if (IsReadStreamingInfo(File.Version))
     {
         if (LoadType == AudioClipLoadType.Streaming)
         {
             if (m_audioData == null)
             {
                 using (ResourcesFile res = File.Collection.FindResourcesFile(File, StreamingInfo.Path))
                 {
                     return(res != null);
                 }
             }
         }
     }
     return(true);
 }
Esempio n. 7
0
        protected virtual IEnumerable <Tuple <string, string> > WriteResourceFilesInProject(Metadata.PEFile module)
        {
            foreach (var r in module.Resources.Where(r => r.ResourceType == Metadata.ResourceType.Embedded))
            {
                Stream stream = r.TryOpenStream();
                stream.Position = 0;

                if (r.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
                {
                    bool decodedIntoIndividualFiles;
                    var  individualResources = new List <Tuple <string, string> >();
                    try {
                        var resourcesFile = new ResourcesFile(stream);
                        if (resourcesFile.AllEntriesAreStreams())
                        {
                            foreach (var(name, value) in resourcesFile)
                            {
                                string fileName = Path.Combine(name.Split('/').Select(p => CleanUpFileName(p)).ToArray());
                                string dirName  = Path.GetDirectoryName(fileName);
                                if (!string.IsNullOrEmpty(dirName) && directories.Add(dirName))
                                {
                                    Directory.CreateDirectory(Path.Combine(targetDirectory, dirName));
                                }
                                Stream entryStream = (Stream)value;
                                entryStream.Position = 0;
                                individualResources.AddRange(
                                    WriteResourceToFile(fileName, (string)name, entryStream));
                            }
                            decodedIntoIndividualFiles = true;
                        }
                        else
                        {
                            decodedIntoIndividualFiles = false;
                        }
                    } catch (BadImageFormatException) {
                        decodedIntoIndividualFiles = false;
                    } catch (EndOfStreamException) {
                        decodedIntoIndividualFiles = false;
                    }
                    if (decodedIntoIndividualFiles)
                    {
                        foreach (var entry in individualResources)
                        {
                            yield return(entry);
                        }
                    }
                    else
                    {
                        stream.Position = 0;
                        string fileName = GetFileNameForResource(r.Name);
                        foreach (var entry in WriteResourceToFile(fileName, r.Name, stream))
                        {
                            yield return(entry);
                        }
                    }
                }
                else
                {
                    string fileName = GetFileNameForResource(r.Name);
                    using (FileStream fs = new FileStream(Path.Combine(targetDirectory, fileName), FileMode.Create, FileAccess.Write)) {
                        stream.Position = 0;
                        stream.CopyTo(fs);
                    }
                    yield return(Tuple.Create("EmbeddedResource", fileName));
                }
            }
        }
Esempio n. 8
0
        public override byte[] ExportBinary()
        {
            byte[] data   = m_imageData;
            int    offset = 0;
            int    length = data.Length;

            if (IsReadStreamData)
            {
                string path = StreamData.Path;
                if (path != string.Empty)
                {
                    if (data.Length != 0)
                    {
                        throw new Exception("Texture contains data and resource path");
                    }

                    const string archivePrefix = "archive:/";
                    if (path.StartsWith(archivePrefix))
                    {
                        path = path.Substring(archivePrefix.Length);
                    }

                    ResourcesFile res = AssetsFile.Collection.FindResourcesFile(AssetsFile, path);
                    if (res == null)
                    {
                        Logger.Log(LogType.Warning, LogCategory.Export, $"Can't export '{Name}' because resources file '{path}' hasn't been found");
                        return(null);
                    }
                    data = res.Data;
                    long longOffset = StreamData.Offset;
                    long longSize   = StreamData.Size;
                    if (longOffset > int.MaxValue)
                    {
                        throw new Exception($"Unsupported offset value {longOffset}");
                    }
                    if (longSize > int.MaxValue)
                    {
                        throw new Exception($"Unsupported size value {longSize}");
                    }
                    offset = (int)longOffset;
                    length = (int)longSize;
                }
            }

            switch (TextureFormat.ToContainerType())
            {
            case ContainerType.None:
                return(m_imageData);

            case ContainerType.DDS:
                return(ExportDDS(data, offset, length));

            case ContainerType.PVR:
                return(ExportPVR(data, offset, length));

            case ContainerType.KTX:
                return(ExportKTX(data, offset, length));

            default:
                throw new NotSupportedException($"Unsupported texture container {TextureFormat.ToContainerType()}");
            }
        }
Esempio n. 9
0
        public override void Read(AssetReader reader)
        {
            base.Read(reader);

            if (IsReadLoadType(reader.Version))
            {
                LoadType      = (AudioClipLoadType)reader.ReadInt32();
                Channels      = reader.ReadInt32();
                Frequency     = reader.ReadInt32();
                BitsPerSample = reader.ReadInt32();
                Length        = reader.ReadSingle();

                if (IsReadIsTrackerFormat(reader.Version))
                {
                    IsTrackerFormat = reader.ReadBoolean();
                }
                if (IsReadAmbisonic(reader.Version))
                {
                    Ambisonic = reader.ReadBoolean();
                }
                if (IsAlignTrackerFormat(reader.Version))
                {
                    reader.AlignStream(AlignType.Align4);
                }

                if (IsReadAudioClipFlags(reader.Version))
                {
                    AudioClipFlags = reader.ReadInt32();
                }
                if (IsReadFSBResourceFirst(reader.Version))
                {
                    FSBResource.Read(reader);
                }

                SubsoundIndex    = reader.ReadInt32();
                PreloadAudioData = reader.ReadBoolean();
                LoadInBackground = reader.ReadBoolean();
                Legacy3D         = reader.ReadBoolean();
                reader.AlignStream(AlignType.Align4);

                if (!IsReadFSBResourceFirst(reader.Version))
                {
                    FSBResource.Read(reader);
                }

                if (IsReadType(reader.Version))
                {
                    Type = (FMODSoundType)reader.ReadInt32();
                }
                if (IsReadCompressionFormat(reader.Version))
                {
                    CompressionFormat = (AudioCompressionFormat)reader.ReadInt32();
                }
                reader.AlignStream(AlignType.Align4);

#if UNIVERSAL
                if (IsReadEditorResource(reader.Flags))
                {
                    EditorResource.Read(reader);
                    if (IsReadCompressionFormat(reader.Version))
                    {
                        EditorCompressionFormat = (AudioCompressionFormat)reader.ReadInt32();
                    }
                }
#endif
            }
            else
            {
                if (IsReadDecompressOnLoadFirst(reader.Version))
                {
                    DecompressOnLoad = reader.ReadBoolean();
                }

                Format = (FMODSoundFormat)reader.ReadInt32();
                if (IsReadType(reader.Version))
                {
                    Type = (FMODSoundType)reader.ReadInt32();
                }
                if (IsReadLength(reader.Version))
                {
                    Length    = reader.ReadSingle();
                    Frequency = reader.ReadInt32();
                    Size      = reader.ReadInt32();
                }

                if (IsReadDecompressOnLoadSecond(reader.Version))
                {
                    DecompressOnLoad = reader.ReadBoolean();
                }
                if (IsRead3D(reader.Version))
                {
                    Legacy3D = reader.ReadBoolean();
                }
                if (IsReadUseHardware(reader.Version))
                {
                    UseHardware = reader.ReadBoolean();
                }
                if (IsAlignBools(reader.Version))
                {
                    reader.AlignStream(AlignType.Align4);
                }

                if (IsStreamInt32(reader.Version))
                {
                    LoadType = (AudioClipLoadType)reader.ReadInt32();
                }

                if (IsReadStreamingInfo(reader.Version))
                {
                    bool isInnerData = true;
                    if (LoadType == AudioClipLoadType.Streaming)
                    {
                        using (ResourcesFile res = File.Collection.FindResourcesFile(File, StreamingFileName))
                        {
                            isInnerData = res == null;
                        }
                    }
                    if (isInnerData)
                    {
                        m_audioData = reader.ReadByteArray();
                        reader.AlignStream(AlignType.Align4);
                    }
                    else
                    {
                        StreamingInfo.Read(reader, StreamingFileName);
                    }
                }
                else
                {
                    m_audioData = reader.ReadByteArray();
                    if (IsAlignAudioData(reader.Version))
                    {
                        reader.AlignStream(AlignType.Align4);
                    }
                }

                if (IsReadDecompressOnLoadThird(reader.Version))
                {
                    DecompressOnLoad = reader.ReadBoolean();
                }

                if (IsReadStream(reader.Version))
                {
                    if (!IsStreamInt32(reader.Version))
                    {
                        LoadType = reader.ReadBoolean() ? AudioClipLoadType.CompressedInMemory : AudioClipLoadType.DecompressOnLoad;
                    }
                }
            }
        }
Esempio n. 10
0
        public override void ExportBinary(IExportContainer container, Stream stream)
        {
            if (IsReadLoadType(container.Version))
            {
                using (ResourcesFile res = File.Collection.FindResourcesFile(File, FSBResource.Source))
                {
                    if (res == null)
                    {
                        Logger.Log(LogType.Warning, LogCategory.Export, $"Can't export '{ValidName}' because resources file '{FSBResource.Source}' hasn't been found");
                        return;
                    }

                    if (StreamedResource.IsReadSize(container.Version))
                    {
                        using (PartialStream resStream = new PartialStream(res.Stream, res.Offset, res.Size))
                        {
                            resStream.Position = FSBResource.Offset;
                            resStream.CopyStream(stream, FSBResource.Size);
                        }
                    }
                    else
                    {
                        // I think they read data by its type for this verison, so I can't even export raw data :/
                        Logger.Log(LogType.Warning, LogCategory.Export, $"Can't export '{ValidName}' because of unknown size");
                    }
                }
            }
            else
            {
                if (IsReadStreamingInfo(container.Version))
                {
                    if (LoadType == AudioClipLoadType.Streaming)
                    {
                        if (m_audioData == null)
                        {
                            using (ResourcesFile res = File.Collection.FindResourcesFile(File, StreamingInfo.Path))
                            {
                                if (res == null)
                                {
                                    Logger.Log(LogType.Warning, LogCategory.Export, $"Can't export '{ValidName}' because resources file '{StreamingInfo.Path}' hasn't been found");
                                    return;
                                }

                                using (PartialStream resStream = new PartialStream(res.Stream, res.Offset, res.Size))
                                {
                                    resStream.Position = StreamingInfo.Offset;
                                    resStream.CopyStream(stream, StreamingInfo.Size);
                                }
                            }
                        }
                        else
                        {
                            stream.Write(m_audioData, 0, m_audioData.Length);
                        }
                    }
                    else
                    {
                        stream.Write(m_audioData, 0, m_audioData.Length);
                    }
                }
                else
                {
                    stream.Write(m_audioData, 0, m_audioData.Length);
                }
            }
        }