Exemplo n.º 1
0
        internal override void Read(ResourceReader reader)
        {
            while ((reader.Position + ResourceChunkHeader.SIZE) < reader.BaseStream.Length)
            {
                var chunk = reader.ReadChunkHeader();
                if (chunk.Type == ResourceChunkType.Invalid || !Enum.IsDefined(typeof(ResourceChunkType), chunk.Type))
                {
                    break;
                }

                var chunkDataLength = chunk.Length - 16;
                var chunkDataStart  = reader.Position;

                switch (chunk.Type)
                {
                case ResourceChunkType.TextureDictionary:
                    Textures = reader.ReadResource <TextureDictionary>(chunk.Version);
                    break;

                case ResourceChunkType.MaterialDictionary:
                    Materials = reader.ReadResource <MaterialDictionary>(chunk.Version);
                    break;

                case ResourceChunkType.Scene:
                    Scene = reader.ReadResource <Scene>(chunk.Version);
                    break;

                case ResourceChunkType.ChunkType000100F9:
                    ChunkType000100F9 = reader.ReadResource <ChunkType000100F9>(chunk.Version);
                    break;

                case ResourceChunkType.ChunkType000100F8:
                    ChunkType000100F8 = new ChunkType000100F8(chunk.Version)
                    {
                        RawData = reader.ReadBytes(chunkDataLength)
                    };
                    break;

                case ResourceChunkType.AnimationPack:
                {
                    AnimationPack = reader.ReadResource <AnimationPack>(chunk.Version);

                    if (AnimationPack.ErrorsOccuredDuringLoad && Scene != null)
                    {
                        // Invalid data, let's not make model uneditable because the animation support sucks, now shall we?
                        reader.SeekBegin(chunkDataStart);
                        AnimationPack = new AnimationPack(chunk.Version)
                        {
                            RawData = reader.ReadBytes(chunkDataLength)
                        };
                    }
                }
                break;

                default:
                    reader.SeekCurrent(chunkDataLength);
                    continue;
                }
            }
        }
Exemplo n.º 2
0
        public static Resource Load(Stream stream, bool leaveOpen)
        {
            if (stream.Length < ResourceFileHeader.SIZE)
            {
                throw new InvalidDataException("Stream is too small to be a valid resource file.");
            }

            using (var reader = new ResourceReader(stream, leaveOpen))
            {
                var      header = reader.ReadFileHeader();
                Resource res;

                switch (header.Type)
                {
                case ResourceType.Model:
                    res = new Model(header.Version);
                    break;

                case ResourceType.ShaderCachePS3:
                    res = new ShaderCachePS3(header.Version);
                    break;

                case ResourceType.ShaderCachePSP2:
                    res = new ShaderCachePSP2(header.Version);
                    break;

                case ResourceType.ShaderCachePS4:
                    res = new ShaderCachePS4(header.Version);
                    break;

                // Custom
                case ResourceType.TextureMap:
                    res = new TextureMap(header.Version);
                    break;

                case ResourceType.TextureDictionary:
                    res = new TextureDictionary(header.Version);
                    break;

                case ResourceType.Texture:
                    res = new Texture(header.Version);
                    break;

                case ResourceType.ShaderPS3:
                    res = new ShaderPS3(header.Version);
                    break;

                case ResourceType.ShaderPSP2:
                    res = new ShaderPSP2(header.Version);
                    break;

                case ResourceType.ShaderPS4:
                    res = new ShaderPS4(header.Version);
                    break;

                case ResourceType.Scene:
                    res = new Scene(header.Version);
                    break;

                case ResourceType.Node:
                    res = new Node(header.Version);
                    break;

                case ResourceType.UserPropertyCollection:
                    res = new UserPropertyCollection(header.Version);
                    break;

                case ResourceType.Morph:
                    res = new Morph(header.Version);
                    break;

                case ResourceType.MorphTarget:
                    res = new MorphTarget(header.Version);
                    break;

                case ResourceType.MorphTargetList:
                    res = new MorphTargetList(header.Version);
                    break;

                case ResourceType.MaterialDictionary:
                    res = new MaterialDictionary(header.Version);
                    break;

                case ResourceType.ChunkType000100F9:
                    res = new ChunkType000100F9(header.Version);
                    break;

                case ResourceType.ChunkType000100F8:
                    res = new ChunkType000100F8(header.Version);
                    break;

                case ResourceType.AnimationPack:
                    res = new AnimationPack(header.Version);
                    break;

                case ResourceType.MaterialAttribute:
                    return(MaterialAttribute.Read(reader, header.Version));

                case ResourceType.Material:
                    res = new Material(header.Version);
                    break;

                case ResourceType.Light:
                    res = new Light(header.Version);
                    break;

                case ResourceType.Geometry:
                    res = new Geometry(header.Version);
                    break;

                case ResourceType.Camera:
                    res = new Camera(header.Version);
                    break;

                case ResourceType.Epl:
                {
                    return(new Epl
                        {
                            // Make sure to also read the extra boolean we wrote to the file to keep track of this.
                            IncludesProperties = reader.ReadBoolean(),
                            RawData = reader.ReadBytes(( int )(stream.Length - stream.Position))
                        });
                }

                case ResourceType.EplLeaf:
                    res = new EplLeaf(header.Version);
                    break;

                case ResourceType.Animation:
                    res = new Animation(header.Version);
                    break;

                case ResourceType.AnimationExtraData:
                    res = new AnimationExtraData(header.Version);
                    break;

                case ResourceType.KeyframeTrack:
                    res = new KeyframeTrack(header.Version);
                    break;

                case ResourceType.AnimationController:
                    res = new AnimationController(header.Version);
                    break;

                default:
                    throw new InvalidDataException("Unknown/Invalid resource type");
                }

                res.Read(reader);

                if (res.ResourceType == ResourceType.Model)
                {
                    // Identifier AnimationPack from a file with a model resource header
                    var model = ( Model )res;
                    if (model.AnimationPack != null && model.ChunkType000100F8 == null && model.ChunkType000100F9 == null &&
                        model.Materials == null && model.Scene == null && model.Textures == null)
                    {
                        res = model.AnimationPack;
                    }
                }

                return(res);
            }
        }