예제 #1
0
        private static DWordTexture ConvertTexture(WordTexture t)
        {
            var ret = new DWordTexture(new uint[256][]);
            for (var y = 0; y < 256; y++)
            {
                ret.Pixels[y] = new uint[256];

                for (var x = 0; x < 256; x++)
                {
                    var color = t.Pixels[y][x];
                    uint val = 0;
                    if ((color & 0x8000) != 0)
                    {
                        val = (uint)(
                            ((color & 0x00007c00) >> 7) 
                            | (((color & 0x000003e0) >> 2) << 8) 
                            | ((color & 0x0000001f) << 3 << 16) 
                            | 0xff000000);
                    }
                    ret.Pixels[y][x] = val;
                }
            }
            return ret;
        }
예제 #2
0
        /*private static ByteTexture ConvertTexture(DWordTexture tex, out Palette p)
        {
            var top = GetTopColors(tex).Select(x => new ByteColor(x));
            p = new Palette() {Colour = top.ToArray()};
            var res = new ByteTexture(new byte[256][]);
            for (var y = 0; y < 256; y++)
            {
                res.Pixels[y] = new byte[256];

                for (var x = 0; x < 256; x++)
                {
                    res.Pixels[y][x] = (byte) closestColor1(top, new ByteColor(tex.Pixels[y][x]));
                }
            }
            return res;
        }*/

        private unsafe void Gen8bitPaletteFrom32bitTex(DWordTexture[] texs)
        {
            foreach (var bt in texs)
            {
                var bmp = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
                var bmpData = bmp.LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.WriteOnly,
                    PixelFormat.Format32bppArgb);
                var scan0 = (uint*) bmpData.Scan0;
                for (var y = 0; y < 256; y++)
                {
                    var scanline = bt.Pixels[y];
                    fixed (uint* ptr = scanline)
                    {
                        for (var i = 0; i < 256; i++)
                            scan0[y * 256 + i] = (ptr[i] & 0xff00ff00) |
                                                 ((ptr[i] & 0x00ff0000) >> 16) |
                                                 ((ptr[i] & 0x000000ff) << 16);
                    }
                }
                var bmp8 = bmp.Clone(new Rectangle(0, 0, 256, 256), PixelFormat.Format8bppIndexed);
                Palette = new Palette() {Colour = bmp8.Palette.Entries.Select(x => (ByteColor) x).ToArray()};
            }
            /*var top = GetTopColors(texs).Select(x => new ByteColor(x)).ToArray();
            Palette = new Palette() { Colour = top };*/
        }
예제 #3
0
        private static DWordTexture ConvertTexture(Palette p, ByteTexture t)
        {
            var ret = new DWordTexture(new uint[256][]);
            for (var y = 0; y < 256; y++)
            {
                ret.Pixels[y] = new uint[256];

                for (var x = 0; x < 256; x++)
                {
                    var color = t.Pixels[y][x];
                    uint val = 0;
                    if(color > 0)
                    {
                        var palc = p.Colour[color];
                        val = (uint)(palc.R | (palc.G << 8) | (palc.B << 16) | (0xff << 24));
                    }
                    ret.Pixels[y][x] = val;
                }
            }
            return ret;
        }
예제 #4
0
 /// <summary>
 /// Reads a <see cref="DWordTexture"/>
 /// </summary>
 /// <param name="br">The <see cref="BinaryReader"/> used to read the <see cref="DWordTexture"/></param>
 /// <returns>A <see cref="DWordTexture"/></returns>
 public static DWordTexture Read(BinaryReader br)
 {
     var ret = new DWordTexture
     {
         Pixels = new uint[256][]
     };
     for (var i = 0; i < 256; i++)
     {
         ret.Pixels[i] = new uint[256];
         for (var j = 0; j < 256; j++)
         {
             var tmp = br.ReadUInt32();
             ret.Pixels[i][j] = (tmp & 0xff00ff00) | ((tmp & 0x00ff0000) >> 16) | ((tmp & 0x000000ff) << 16);
         }
     }
     return ret;
 }
예제 #5
0
 private static uint[] GetTopColors(DWordTexture[] texs, int n = 256)
 {
     var res = new Dictionary<uint, int>();
     foreach (var tex in texs)
     {
         for (var y = 0; y < 256; y++)
         {
             for (var x = 0; x < 256; x++)
             {
                 if (!res.ContainsKey(tex.Pixels[y][x])) res[tex.Pixels[y][x]] = 1;
                 else res[tex.Pixels[y][x]]++;
             }
         }
     }
     return res.OrderByDescending(x => x.Value).Distinct().Select(x => x.Key).Take(n).ToArray();
 }
예제 #6
0
        private void Load_TR2()
        {
            var version = reader.ReadUInt32();

            if(version != 0x0000002d)
                throw new ArgumentException("Load_TR2: Wrong level version");

            Palette = Palette.Read(reader, Engine.TR1);

            Palette16 = Palette.Read(reader, Engine.TR2);

            var numTextiles = reader.ReadUInt32();
            Texture8 = reader.ReadArray(numTextiles, () => ByteTexture.Read(reader));
            Texture16 = reader.ReadArray(numTextiles, () => WordTexture.Read(reader));

            var unused = reader.ReadUInt32();
            if(unused != 0)
                Cerr.Write("Load_TR2: unused: Expected 0, Found " + unused.ToString("X8"));

            var numRooms = reader.ReadUInt16();
            Rooms = reader.ReadArray(numRooms, () => Room.Read(reader, Engine.TR2));

            var numFloorData = reader.ReadUInt32();
            FloorData = reader.ReadUInt16Array(numFloorData);

            ReadMeshData();

            var numAnimations = reader.ReadUInt32();
            Animations = reader.ReadArray(numAnimations, () => Animation.Read(reader, Engine.TR2));

            var numStateChanges = reader.ReadUInt32();
            StateChanges = reader.ReadArray(numStateChanges, () => StateChange.Read(reader));

            var numAnimDispatches = reader.ReadUInt32();
            AnimDispatches = reader.ReadArray(numAnimDispatches, () => AnimDispatch.Read(reader));

            var numAnimCommands = reader.ReadUInt32();
            AnimCommands = reader.ReadInt16Array(numAnimCommands);

            var numMeshTreeData = reader.ReadUInt32();
            MeshTreeData = reader.ReadUInt32Array(numMeshTreeData);

            ReadFrameMoveableData();

            var numStaticMeshes = reader.ReadUInt32();
            StaticMeshes = reader.ReadArray(numStaticMeshes, () => StaticMesh.Read(reader));

            var numObjectTextures = reader.ReadUInt32();
            ObjectTextures = reader.ReadArray(numObjectTextures, () => ObjectTexture.Read(reader, Engine.TR2));

            var numSpriteTextures = reader.ReadUInt32();
            SpriteTextures = reader.ReadArray(numSpriteTextures, () => SpriteTexture.Read(reader, Engine.TR2));

            var numSpriteSequences = reader.ReadUInt32();
            SpriteSequences = reader.ReadArray(numSpriteSequences, () => SpriteSequence.Read(reader));

            if(IsDemoOrUb)
                LightMap = LightMap.Read(reader);

            var numCameras = reader.ReadUInt32();
            Cameras = reader.ReadArray(numCameras, () => Camera.Read(reader));

            var numSoundSources = reader.ReadUInt32();
            SoundSources = reader.ReadArray(numSoundSources, () => SoundSource.Read(reader));

            var numBoxes = reader.ReadUInt32();
            Boxes = reader.ReadArray(numBoxes, () => Box.Read(reader, Engine.TR2));

            var numOverlaps = reader.ReadUInt32();
            Overlaps = reader.ReadUInt16Array(numOverlaps);

            Zones = reader.ReadArray(numBoxes, () => Zone.Read(reader, Engine.TR2));

            var numAnimatedTextures = reader.ReadUInt32();
            AnimatedTextures = reader.ReadUInt16Array(numAnimatedTextures);

            var numItems = reader.ReadUInt32();
            Items = reader.ReadArray(numItems, () => Item.Read(reader, Engine.TR2));

            if (!IsDemoOrUb)
                LightMap = LightMap.Read(reader);

            var numCinematicFrames = reader.ReadUInt16();
            CinematicFrames = reader.ReadArray(numCinematicFrames, () => CinematicFrame.Read(reader));

            var numDemoData = reader.ReadUInt16();
            DemoData = reader.ReadBytes(numDemoData);

            SoundMap = reader.ReadInt16Array(Constants.TR_AUDIO_MAP_SIZE_TR2);

            var numSoundDetails = reader.ReadUInt32();
            SoundDetails = reader.ReadArray(numSoundDetails, () => Loader.SoundDetails.Read(reader, Engine.TR2));

            var numSampleIndices = reader.ReadUInt32();
            SampleIndices = reader.ReadUInt32Array(numSampleIndices);

            for (uint i = 0; i < numSoundDetails; i++)
            {
                if (SoundDetails[i].Sample < numSampleIndices)
                    SoundDetails[i].Sample = (ushort)SampleIndices[SoundDetails[i].Sample];
            }

            if(!File.Exists(SfxPath))
                Cerr.Write("Load_TR2: '" + SfxPath + "' not found, no samples loaded");
            else
            {
                using (var fs = new FileStream(SfxPath, FileMode.Open))
                {
                    using (var br = new BinaryReader(fs))
                    {
                        SamplesData = new byte[fs.Length];

                        for(long i = 0; i < SamplesData.Length; i++)
                        {
                            SamplesData[i] = br.ReadByte();

                            if(i >= 4)
                            {
                                if (SamplesData[i - 4] == 82
                                    && SamplesData[i - 3] == 73
                                    && SamplesData[i - 2] == 70
                                    && SamplesData[i - 1] == 70)
                                {
                                    SamplesCount++;
                                }
                            }
                        }
                    }
                }
            }

            Textures = new DWordTexture[numTextiles];
            for(uint i = 0; i < numTextiles; i++)
            {
                Textures[i] = ConvertTexture(Texture16[i]);
            }
        }
예제 #7
0
        private void Load_TR4()
        {
            var version = reader.ReadUInt32();

            if (!version.IsAnyOf((uint)0x00345254, (uint)0x63345254, 0xFFFFFFF0))
                throw new ArgumentException("Load_TR4: Wrong level version");

            Texture16 = new WordTexture[0];

            var numRoomTextiles = reader.ReadUInt16();
            var numObjTextiles = reader.ReadUInt16();
            var numBumpTextiles = reader.ReadUInt16();
            var numMiscTextiles = 2;
            var numTextiles = numRoomTextiles + numObjTextiles + numBumpTextiles + numMiscTextiles;

            var uncompSize = reader.ReadUInt32();
            if (uncompSize == 0)
                throw new ArgumentException("Load_TR4: Textiles32 uncompSize == 0", nameof(uncompSize));

            var compSize = reader.ReadUInt32();
            if (compSize > 0)
            {
                var compBuffer = reader.ReadBytes((int) compSize);

                var newsrc_ = Helper.Decompress(compBuffer);
                Textures = newsrc_.ReadArray(numTextiles - numMiscTextiles, () => DWordTexture.Read(newsrc_));
            }

            uncompSize = reader.ReadUInt32();
            if (uncompSize == 0)
                throw new ArgumentException("Load_TR4: Textiles16 uncompSize == 0", nameof(uncompSize));

            compSize = reader.ReadUInt32();
            if (compSize > 0)
            {
                if (Textures.Length == 0)
                {
                    var compBuffer = reader.ReadBytes((int) compSize);

                    var newsrc_ = Helper.Decompress(compBuffer);
                    Texture16 = newsrc_.ReadArray(numTextiles - numMiscTextiles, () => WordTexture.Read(newsrc_));
                }
                else
                {
                    reader.BaseStream.Position += compSize;
                }
            }

            uncompSize = reader.ReadUInt32();
            if (uncompSize == 0)
                throw new ArgumentException("Load_TR4: Textiles32d uncompSize == 0", nameof(uncompSize));

            compSize = reader.ReadUInt32();
            if (compSize > 0)
            {
                if (Textures.Length == 0)
                {
                    // 262144 = 256*256*4
                    if (uncompSize / 262144 > 2)
                        Cerr.Write("Load_TR4: NumMiscTextiles > 2");

                    Array.Resize(ref Textures, numTextiles);

                    var compBuffer = reader.ReadBytes((int) compSize);

                    var newsrc_ = Helper.Decompress(compBuffer);
                    Textures = Textures.AddArray(newsrc_.ReadArray(numMiscTextiles, () => DWordTexture.Read(newsrc_)));
                }
                else
                {
                    reader.BaseStream.Position += compSize;
                }
            }

            uncompSize = reader.ReadUInt32();
            if (uncompSize == 0)
                throw new ArgumentException("Load_TR4: Packed geometry uncompSize == 0", nameof(uncompSize));

            compSize = reader.ReadUInt32();
            if (compSize == 0)
                throw new ArgumentException("Load_TR4: Packed geometry", nameof(compSize));

            var compBuffer_ = reader.ReadBytes((int) compSize);

            var newsrc = Helper.Decompress(compBuffer_);

            var unused = newsrc.ReadUInt32();
            if (unused != 0)
                Cerr.Write("Load_TR4: unused: Expected 0, Found " + unused.ToString("X8"));

            var numRooms = newsrc.ReadUInt16();
            Rooms = newsrc.ReadArray(numRooms, () => Room.Read(newsrc, Engine.TR4));

            var numFloorData = newsrc.ReadUInt32();
            FloorData = newsrc.ReadUInt16Array(numFloorData);

            var tmpr = reader;
            reader = newsrc;
            ReadMeshData();
            reader = tmpr;

            var numAnimations = newsrc.ReadUInt32();
            Animations = newsrc.ReadArray(numAnimations, () => Animation.Read(newsrc, Engine.TR4));

            var numStateChanges = newsrc.ReadUInt32();
            StateChanges = newsrc.ReadArray(numStateChanges, () => StateChange.Read(newsrc));

            var numAnimDispatches = newsrc.ReadUInt32();
            AnimDispatches = newsrc.ReadArray(numAnimDispatches, () => AnimDispatch.Read(newsrc));

            var numAnimCommands = newsrc.ReadUInt32();
            AnimCommands = newsrc.ReadInt16Array(numAnimCommands);

            var numMeshTreeData = newsrc.ReadUInt32();
            MeshTreeData = newsrc.ReadUInt32Array(numMeshTreeData);

            tmpr = reader;
            reader = newsrc;
            ReadFrameMoveableData();
            reader = tmpr;

            var numStaticMeshes = newsrc.ReadUInt32();
            StaticMeshes = newsrc.ReadArray(numStaticMeshes, () => StaticMesh.Read(newsrc));

            var spr1 = (char)newsrc.ReadSByte();
            var spr2 = (char)newsrc.ReadSByte();
            var spr3 = (char)newsrc.ReadSByte();
            var spr = "" + spr1 + spr2 + spr3;
            if(spr != "SPR")
                throw new ArgumentException("Load_TR4: Expected 'SPR', Found '" + spr + "'", nameof(spr));

            var numSpriteTextures = newsrc.ReadUInt32();
            SpriteTextures = newsrc.ReadArray(numSpriteTextures, () => SpriteTexture.Read(newsrc, Engine.TR4));

            var numSpriteSequences = newsrc.ReadUInt32();
            SpriteSequences = newsrc.ReadArray(numSpriteSequences, () => SpriteSequence.Read(newsrc));

            var numCameras = newsrc.ReadUInt32();
            Cameras = newsrc.ReadArray(numCameras, () => Camera.Read(newsrc));

            var numFlybyCameras = newsrc.ReadUInt32();
            FlybyCameras = newsrc.ReadArray(numFlybyCameras, () => FlybyCamera.Read(newsrc));

            var numSoundSources = newsrc.ReadUInt32();
            SoundSources = newsrc.ReadArray(numSoundSources, () => SoundSource.Read(newsrc));

            var numBoxes = newsrc.ReadUInt32();
            Boxes = newsrc.ReadArray(numBoxes, () => Box.Read(newsrc, Engine.TR2));

            var numOverlaps = newsrc.ReadUInt32();
            Overlaps = newsrc.ReadUInt16Array(numOverlaps);

            Zones = newsrc.ReadArray(numBoxes, () => Zone.Read(newsrc, Engine.TR2));

            var numAnimatedTextures = newsrc.ReadUInt32();
            AnimatedTextures = newsrc.ReadUInt16Array(numAnimatedTextures);

            AnimatedTexturesUVCount = newsrc.ReadByte();

            var tex1 = (char)newsrc.ReadSByte();
            var tex2 = (char)newsrc.ReadSByte();
            var tex3 = (char)newsrc.ReadSByte();
            var tex = "" + tex1 + tex2 + tex3;
            if (tex != "TEX")
                throw new ArgumentException("Load_TR4: Expected 'TEX', Found '" + tex + "'", nameof(tex));

            var numObjectTextures = newsrc.ReadUInt32();
            ObjectTextures = newsrc.ReadArray(numObjectTextures, () => ObjectTexture.Read(newsrc, Engine.TR4));

            var numItems = newsrc.ReadUInt32();
            Items = newsrc.ReadArray(numItems, () => Item.Read(newsrc, Engine.TR4));

            var numAiObjects = newsrc.ReadUInt32();
            AIObjects = newsrc.ReadArray(numAiObjects, () => AIObject.Read(newsrc));

            var numDemoData = newsrc.ReadUInt16();
            DemoData = newsrc.ReadBytes(numDemoData);

            SoundMap = newsrc.ReadInt16Array(Constants.TR_AUDIO_MAP_SIZE_TR4);

            var numSoundDetails = newsrc.ReadUInt32();
            SoundDetails = newsrc.ReadArray(numSoundDetails, () => Loader.SoundDetails.Read(newsrc, Engine.TR3));

            var numSampleIndices = newsrc.ReadUInt32();
            SampleIndices = newsrc.ReadUInt32Array(numSampleIndices);

            var numSamples = reader.ReadUInt32();
            if(numSamples > 0)
            {
                SamplesCount = (int) numSamples;
                SamplesData = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position));
            }

            if (Textures.Length == 0)
            {
                Textures = new DWordTexture[Texture16.Length];
                for (uint i = 0; i < Texture16.Length; i++)
                {
                    Textures[i] = ConvertTexture(Texture16[i]);
                }
            }
        }
예제 #8
0
        private void Load_TR5()
        {
            var version = reader.ReadUInt32();

            if (version != 0x00345254)
                throw new ArgumentException("Load_TR5: Wrong level version");

            var numRoomTextiles = reader.ReadUInt16();
            var numObjTextiles = reader.ReadUInt16();
            var numBumpTextiles = reader.ReadUInt16();
            var numMiscTextiles = 3;
            var numTextiles = numRoomTextiles + numObjTextiles + numBumpTextiles + numMiscTextiles;

            var uncompSize = reader.ReadUInt32();
            if (uncompSize == 0)
                throw new ArgumentException("Load_TR5: Textiles32 uncompSize == 0", nameof(uncompSize));

            var compSize = reader.ReadUInt32();
            if (compSize > 0)
            {
                var compBuffer = reader.ReadBytes((int)compSize);

                var newsrc = Helper.Decompress(compBuffer);
                Textures = newsrc.ReadArray(numTextiles - numMiscTextiles, () => DWordTexture.Read(newsrc));
            }

            uncompSize = reader.ReadUInt32();
            if (uncompSize == 0)
                throw new ArgumentException("Load_TR5: Textiles16 uncompSize == 0", nameof(uncompSize));

            compSize = reader.ReadUInt32();
            Texture16 = new WordTexture[0];
            if (compSize > 0)
            {
                if (Textures.Length == 0)
                {
                    var compBuffer = reader.ReadBytes((int)compSize);

                    var newsrc = Helper.Decompress(compBuffer);
                    Texture16 = newsrc.ReadArray(numTextiles - numMiscTextiles, () => WordTexture.Read(newsrc));
                }
                else
                {
                    reader.BaseStream.Position += compSize;
                }
            }

            uncompSize = reader.ReadUInt32();
            if (uncompSize == 0)
                throw new ArgumentException("Load_TR5: Textiles32d uncompSize == 0", nameof(uncompSize));

            compSize = reader.ReadUInt32();
            if (compSize > 0)
            {
                // 262144 = Width * Height * Depth
                //        =  256  *  256   *  4
                if (uncompSize / 262144 > 3)
                    Cerr.Write("Load_TR5: NumMiscTextiles > 3");

                var compBuffer = reader.ReadBytes((int) compSize);

                var newsrc = Helper.Decompress(compBuffer);
                var t = newsrc.ReadArray(numMiscTextiles, () => DWordTexture.Read(newsrc));

                Textures = Textures.AddArray(t);
            }

            LaraType = (LaraType) reader.ReadUInt16();
            WeatherType = (WeatherType) reader.ReadUInt16();

            var flags = reader.ReadUInt32Array(7); // 28 bytes zero padding
            for (var i = 0; i < flags.Length; i++)
            {
                if (flags[i] != 0)
                    Cerr.Write("Load_TR5: flags[" + (i + 1) + "]: Expected 0, Found 0x" + flags[i].ToString("X8"));
            }

            var levelUncompSize = reader.ReadUInt32();
            var levelCompSize = reader.ReadUInt32();

            var unused = reader.ReadUInt32();
            if(unused != 0)
                Cerr.Write("Load_TR5: unused: Expected 0, Found " + unused.ToString("X8"));

            var numRooms = reader.ReadUInt32();
            Rooms = reader.ReadArray(numRooms, () => Room.Read(reader, Engine.TR5));

            var numFloorData = reader.ReadUInt32();
            FloorData = reader.ReadUInt16Array(numFloorData);

            ReadMeshData();

            var numAnimations = reader.ReadUInt32();
            Animations = reader.ReadArray(numAnimations, () => Animation.Read(reader, Engine.TR4));

            var numStateChanges = reader.ReadUInt32();
            StateChanges = reader.ReadArray(numStateChanges, () => StateChange.Read(reader));

            var numAnimDispatches = reader.ReadUInt32();
            AnimDispatches = reader.ReadArray(numAnimDispatches, () => AnimDispatch.Read(reader));

            var numAnimCommands = reader.ReadUInt32();
            AnimCommands = reader.ReadInt16Array(numAnimCommands);

            var numMeshTreeData = reader.ReadUInt32();
            MeshTreeData = reader.ReadUInt32Array(numMeshTreeData);

            ReadFrameMoveableData();

            var numStaticMeshes = reader.ReadUInt32();
            StaticMeshes = reader.ReadArray(numStaticMeshes, () => StaticMesh.Read(reader));

            var spr1 = (char) reader.ReadSByte();
            var spr2 = (char) reader.ReadSByte();
            var spr3 = (char) reader.ReadSByte();
            var spr4 = (char) reader.ReadSByte();
            var spr = "" + spr1 + spr2 + spr3 + spr4;
            if (spr != "SPR\0")
                throw new ArgumentException("Load_TR5: Expected 'SPR', Found '" + spr + "'", nameof(spr));

            var numSpriteTextures = reader.ReadUInt32();
            SpriteTextures = reader.ReadArray(numSpriteTextures, () => SpriteTexture.Read(reader, Engine.TR4));

            var numSpriteSequences = reader.ReadUInt32();
            SpriteSequences = reader.ReadArray(numSpriteSequences, () => SpriteSequence.Read(reader));

            var numCameras = reader.ReadUInt32();
            Cameras = reader.ReadArray(numCameras, () => Camera.Read(reader));

            var numFlybyCameras = reader.ReadUInt32();
            FlybyCameras = reader.ReadArray(numFlybyCameras, () => FlybyCamera.Read(reader));

            var numSoundSources = reader.ReadUInt32();
            SoundSources = reader.ReadArray(numSoundSources, () => SoundSource.Read(reader));

            var numBoxes = reader.ReadUInt32();
            Boxes = reader.ReadArray(numBoxes, () => Box.Read(reader, Engine.TR2));

            var numOverlaps = reader.ReadUInt32();
            Overlaps = reader.ReadUInt16Array(numOverlaps);

            Zones = reader.ReadArray(numBoxes, () => Zone.Read(reader, Engine.TR2));

            var numAnimatedTextures = reader.ReadUInt32();
            AnimatedTextures = reader.ReadUInt16Array(numAnimatedTextures);

            AnimatedTexturesUVCount = reader.ReadByte();

            var tex1 = (char)reader.ReadSByte();
            var tex2 = (char)reader.ReadSByte();
            var tex3 = (char)reader.ReadSByte();
            var tex4 = (char) reader.ReadSByte();
            var tex = "" + tex1 + tex2 + tex3 + tex4;
            if (tex != "TEX\0")
                throw new ArgumentException("Load_TR5: Expected 'TEX', Found '" + tex + "'", nameof(tex));

            var numObjectTextures = reader.ReadUInt32();
            ObjectTextures = reader.ReadArray(numObjectTextures, () => ObjectTexture.Read(reader, Engine.TR5));

            var numItems = reader.ReadUInt32();
            Items = reader.ReadArray(numItems, () => Item.Read(reader, Engine.TR4));

            var numAiObjects = reader.ReadUInt32();
            AIObjects = reader.ReadArray(numAiObjects, () => AIObject.Read(reader));

            var numDemoData = reader.ReadUInt16();
            DemoData = reader.ReadBytes(numDemoData);

            SoundMap = reader.ReadInt16Array(Constants.TR_AUDIO_MAP_SIZE_TR5);

            var numSoundDetails = reader.ReadUInt32();
            SoundDetails = reader.ReadArray(numSoundDetails, () => Loader.SoundDetails.Read(reader, Engine.TR3));

            var numSampleIndices = reader.ReadUInt32();
            SampleIndices = reader.ReadUInt32Array(numSampleIndices);

            reader.BaseStream.Position += 6;

            var numSamples = reader.ReadUInt32();
            if (numSamples > 0)
            {
                SamplesCount = (int)numSamples;
                SamplesData = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position));
            }

            if (Textures.Length == 0)
            {
                Textures = new DWordTexture[Texture16.Length];
                for (uint i = 0; i < Texture16.Length; i++)
                {
                    Textures[i] = ConvertTexture(Texture16[i]);
                }
            }
        }
예제 #9
0
        private void Load()
        {
            var version = reader.ReadUInt32();

            if (version != 0x00000020)
                throw new ArgumentException("TR1Level.Load: Wrong level version");

            //Palette = Palette.Read(reader, TRVersion.TR1);

            var numTextiles = reader.ReadUInt32();
            var texture8 = reader.ReadArray(numTextiles, () => ByteTexture.Read(reader));

            var unused = reader.ReadUInt32();
            if (unused != 0)
                Log.Write("TR1Level.Load: unused: Expected 0, Found " + unused.ToString("X8"));

            var numRooms = reader.ReadUInt16();
            Rooms = reader.ReadArray(numRooms, () => Room.Read(reader, Engine.TR1));

            var numFloorData = reader.ReadUInt32();
            FloorData = reader.ReadUInt16Array(numFloorData);

            ReadMeshData();

            var numAnimations = reader.ReadUInt32();
            Animations = reader.ReadArray(numAnimations, () => Animation.Read(reader, Engine.TR1));

            var numStateChanges = reader.ReadUInt32();
            StateChanges = reader.ReadArray(numStateChanges, () => StateChange.Read(reader));

            var numAnimDispatches = reader.ReadUInt32();
            AnimDispatches = reader.ReadArray(numAnimDispatches, () => AnimDispatch.Read(reader));

            var numAnimCommands = reader.ReadUInt32();
            AnimCommands = reader.ReadInt16Array(numAnimCommands);

            var numMeshTreeData = reader.ReadUInt32();
            MeshTreeData = reader.ReadUInt32Array(numMeshTreeData);

            ReadFrameMoveableData();

            for(uint i = 0; i < numAnimations; i++)
            {
                var frameOffset = Animations[i].FrameOffset / 2;
                Animations[i].FrameSize = (byte)(FrameData[frameOffset + 9] * 2 + 10);
            }

            var numStaticMeshes = reader.ReadUInt32();
            StaticMeshes = reader.ReadArray(numStaticMeshes, () => StaticMesh.Read(reader));

            var numObjectTextures = reader.ReadUInt32();
            ObjectTextures = reader.ReadArray(numObjectTextures, () => ObjectTexture.Read(reader, Engine.TR1));

            var numSpriteTextures = reader.ReadUInt32();
            SpriteTextures = reader.ReadArray(numSpriteTextures, () => SpriteTexture.Read(reader, Engine.TR1));

            var numSpriteSequences = reader.ReadUInt32();
            SpriteSequences = reader.ReadArray(numSpriteSequences, () => SpriteSequence.Read(reader));

            if (IsDemoOrUb)
                Palette = Palette.Read(reader, Engine.TR1);

            var numCameras = reader.ReadUInt32();
            Cameras = reader.ReadArray(numCameras, () => Camera.Read(reader));

            var numSoundSources = reader.ReadUInt32();
            SoundSources = reader.ReadArray(numSoundSources, () => SoundSource.Read(reader));

            var numBoxes = reader.ReadUInt32();
            Boxes = reader.ReadArray(numBoxes, () => Box.Read(reader, Engine.TR1));

            var numOverlaps = reader.ReadUInt32();
            Overlaps = reader.ReadUInt16Array(numOverlaps);

            Zones = reader.ReadArray(numBoxes, () => Zone.Read(reader, Engine.TR1));

            var numAnimatedTextures = reader.ReadUInt32();
            AnimatedTextures = reader.ReadUInt16Array(numAnimatedTextures);

            var numItems = reader.ReadUInt32();
            Items = reader.ReadArray(numItems, () => Item.Read(reader, Engine.TR1));

            LightMap = LightMap.Read(reader);

            if (!IsDemoOrUb)
                Palette = Palette.Read(reader, Engine.TR1);

            var numCinematicFrames = reader.ReadUInt16();
            reader.ReadArray(numCinematicFrames, () => CinematicFrame.Read(reader));

            var numDemoData = reader.ReadUInt16();
            DemoData = reader.ReadBytes(numDemoData);

            SoundMap = reader.ReadInt16Array(Constants.TR_AUDIO_MAP_SIZE_TR1);

            var numSoundDetails = reader.ReadUInt32();
            SoundDetails = reader.ReadArray(numSoundDetails, () => Loader.SoundDetails.Read(reader, Engine.TR1));

            if (false)
            {
                var numSampleData = reader.ReadUInt32();
                SamplesData = new byte[numSampleData];

                for (uint i = 0; i < numSampleData; i++)
                {
                    SamplesData[i] = reader.ReadByte();

                    if (i >= 4)
                    {
                        if (SamplesData[i - 4] == 82
                            && SamplesData[i - 3] == 73
                            && SamplesData[i - 2] == 70
                            && SamplesData[i - 1] == 70)
                        {
                            SamplesCount++;
                        }
                    }
                }

                var numSamplesIndices = reader.ReadUInt32();
                SampleIndices = reader.ReadUInt32Array(numSamplesIndices);
            }
            else
            {
                var numSampleData = reader.ReadUInt32();
                SamplesData = reader.ReadBytes((int)numSampleData);

                var numSamplesIndices = reader.ReadUInt32();
                SampleIndices = reader.ReadUInt32Array(numSamplesIndices);

                SamplesCount = SampleIndices.Length;
            }

            Textures = new DWordTexture[numTextiles];
            for (uint i = 0; i < numTextiles; i++)
            {
                Textures[i] = ConvertTexture(Palette, texture8[i]);
            }

            if(reader.BaseStream.Position < reader.BaseStream.Length)
            {
                Log.Write("Error: " + (reader.BaseStream.Length - reader.BaseStream.Position) + " bytes of data after end of level");
            }
        }
예제 #10
0
        private void Load()
        {
            var version = reader.ReadUInt32();

            if (!new[] {0xFF080038, 0xFF180038, 0xFF180034}.Contains(version))
                throw new ArgumentException("TR3Level.Load: Wrong level version");

            Palette = Palette.Read(reader, Engine.TR1);

            /* Palette16 = TODO: */
            Palette.Read(reader, Engine.TR2);

            var numTextiles = reader.ReadUInt32();
            var texture8 = reader.ReadArray(numTextiles, () => ByteTexture.Read(reader));
            var texture16 = reader.ReadArray(numTextiles, () => WordTexture.Read(reader));

            if (version == 0xFF180034)
                return; // VICT.TR2, only palette and textiles

            var unused = reader.ReadUInt32();
            if (unused != 0)
                Log.Write("TR3Level.Load: unused: Expected 0, Found " + unused.ToString("X8"));

            var numRooms = reader.ReadUInt16();
            Rooms = reader.ReadArray(numRooms, () => Room.Read(reader, Engine.TR3));

            var numFloorData = reader.ReadUInt32();
            FloorData = reader.ReadUInt16Array(numFloorData);

            ReadMeshData();

            var numAnimations = reader.ReadUInt32();
            Animations = reader.ReadArray(numAnimations, () => Animation.Read(reader, Engine.TR3));

            var numStateChanges = reader.ReadUInt32();
            StateChanges = reader.ReadArray(numStateChanges, () => StateChange.Read(reader));

            var numAnimDispatches = reader.ReadUInt32();
            AnimDispatches = reader.ReadArray(numAnimDispatches, () => AnimDispatch.Read(reader));

            var numAnimCommands = reader.ReadUInt32();
            AnimCommands = reader.ReadInt16Array(numAnimCommands);

            var numMeshTreeData = reader.ReadUInt32();
            MeshTreeData = reader.ReadUInt32Array(numMeshTreeData);

            ReadFrameMoveableData();

            var numStaticMeshes = reader.ReadUInt32();
            StaticMeshes = reader.ReadArray(numStaticMeshes, () => StaticMesh.Read(reader));

            var numSpriteTextures = reader.ReadUInt32();
            SpriteTextures = reader.ReadArray(numSpriteTextures, () => SpriteTexture.Read(reader, Engine.TR3));

            var numSpriteSequences = reader.ReadUInt32();
            SpriteSequences = reader.ReadArray(numSpriteSequences, () => SpriteSequence.Read(reader));

            var numCameras = reader.ReadUInt32();
            Cameras = reader.ReadArray(numCameras, () => Camera.Read(reader));

            var numSoundSources = reader.ReadUInt32();
            SoundSources = reader.ReadArray(numSoundSources, () => SoundSource.Read(reader));

            var numBoxes = reader.ReadUInt32();
            Boxes = reader.ReadArray(numBoxes, () => Box.Read(reader, Engine.TR3));

            var numOverlaps = reader.ReadUInt32();
            Overlaps = reader.ReadUInt16Array(numOverlaps);

            Zones = reader.ReadArray(numBoxes, () => Zone.Read(reader, Engine.TR3));

            var numAnimatedTextures = reader.ReadUInt32();
            AnimatedTextures = reader.ReadUInt16Array(numAnimatedTextures);

            var numObjectTextures = reader.ReadUInt32();
            ObjectTextures = reader.ReadArray(numObjectTextures, () => ObjectTexture.Read(reader, Engine.TR3));

            var numItems = reader.ReadUInt32();
            Items = reader.ReadArray(numItems, () => Item.Read(reader, Engine.TR3));

            LightMap = LightMap.Read(reader);

            var numCinematicFrames = reader.ReadUInt16();
            reader.ReadArray(numCinematicFrames, () => CinematicFrame.Read(reader));

            var numDemoData = reader.ReadUInt16();
            DemoData = reader.ReadBytes(numDemoData);

            SoundMap = reader.ReadInt16Array(Constants.TR_AUDIO_MAP_SIZE_TR3);

            var numSoundDetails = reader.ReadUInt32();
            SoundDetails = reader.ReadArray(numSoundDetails, () => Loader.SoundDetails.Read(reader, Engine.TR3));

            var numSampleIndices = reader.ReadUInt32();
            SampleIndices = reader.ReadUInt32Array(numSampleIndices);

            for (uint i = 0; i < numSoundDetails; i++)
            {
                if (SoundDetails[i].Sample < numSampleIndices)
                    SoundDetails[i].Sample = (ushort) SampleIndices[SoundDetails[i].Sample];
            }

            if (!File.Exists(SfxPath))
                Log.Write("TR3Level.Load: '" + SfxPath + "' not found, no samples loaded");
            else
            {
                using (var fs = new FileStream(SfxPath, FileMode.Open))
                {
                    using (var br = new BinaryReader(fs))
                    {
                        SamplesData = new byte[fs.Length];

                        for (long i = 0; i < SamplesData.Length; i++)
                        {
                            SamplesData[i] = br.ReadByte();

                            if (i >= 4)
                            {
                                if (SamplesData[i - 4] == 82
                                    && SamplesData[i - 3] == 73
                                    && SamplesData[i - 2] == 70
                                    && SamplesData[i - 1] == 70)
                                {
                                    SamplesCount++;
                                }
                            }
                        }
                    }
                }
            }

            Textures = new DWordTexture[numTextiles];
            for (uint i = 0; i < numTextiles; i++)
            {
                Textures[i] = ConvertTexture(texture16[i]);
            }

            if (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                Log.Write("Error: " + (reader.BaseStream.Length - reader.BaseStream.Position) + " bytes of data after end of level");
            }
        }