Esempio n. 1
0
        private static bool LoadSprites(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, ref Dictionary <long, WadSprite> outSprites)
        {
            if (idOuter != Wad2Chunks.Sprites)
            {
                return(false);
            }

            var  sprites       = new Dictionary <long, WadSprite>();
            long obsoleteIndex = 0; // Move this into each chunk once we got rid of old style *.wad2 files.

            chunkIO.ReadChunks((id, chunkSize) =>
            {
                if (id != Wad2Chunks.Sprite)
                {
                    return(false);
                }

                int width          = LEB128.ReadInt(chunkIO.Raw);
                int height         = LEB128.ReadInt(chunkIO.Raw);
                byte[] imageData   = null;
                RectangleInt2 rect = new RectangleInt2();

                chunkIO.ReadChunks((id2, chunkSize2) =>
                {
                    if (id2 == Wad2Chunks.SpriteIndex)
                    {
                        obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2);
                    }
                    else if (id2 == Wad2Chunks.SpriteData)
                    {
                        imageData = chunkIO.ReadChunkArrayOfBytes(chunkSize2);
                    }
                    else if (id2 == Wad2Chunks.SpriteSides)
                    {
                        rect.X0 = chunkIO.Raw.ReadInt32();
                        rect.Y0 = chunkIO.Raw.ReadInt32();
                        rect.X1 = chunkIO.Raw.ReadInt32();
                        rect.Y1 = chunkIO.Raw.ReadInt32();
                    }
                    else
                    {
                        return(false);
                    }
                    return(true);
                });

                sprites.Add(obsoleteIndex++, new WadSprite {
                    Texture   = new WadTexture(ImageC.FromByteArray(imageData, width, height)),
                    Alignment = rect
                })
                ;
                return(true);
            });

            outSprites = sprites;
            return(true);
        }
Esempio n. 2
0
        private static bool LoadTextures(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, ref Dictionary <long, WadTexture> outTextures)
        {
            if (idOuter != Wad2Chunks.Textures)
            {
                return(false);
            }

            Dictionary <long, WadTexture> textures = new Dictionary <long, WadTexture>();
            long obsoleteIndex = 0; // Move this into each chunk once we got rid of old style *.wad2 files.

            chunkIO.ReadChunks((id, chunkSize) =>
            {
                if (id != Wad2Chunks.Texture)
                {
                    return(false);
                }

                var width          = LEB128.ReadInt(chunkIO.Raw);
                var height         = LEB128.ReadInt(chunkIO.Raw);
                byte[] textureData = null;
                chunkIO.ReadChunks((id2, chunkSize2) =>
                {
                    if (id2 == Wad2Chunks.TextureIndex)
                    {
                        obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2);
                    }
                    if (id2 == Wad2Chunks.TextureData)
                    {
                        textureData = chunkIO.ReadChunkArrayOfBytes(chunkSize2);
                    }
                    else
                    {
                        return(false);
                    }
                    return(true);
                });

                var texture = ImageC.FromByteArray(textureData, width, height);
                texture.ReplaceColor(new ColorC(255, 0, 255, 255), new ColorC(0, 0, 0, 0));

                textures.Add(obsoleteIndex++, new WadTexture(texture));
                return(true);
            });

            outTextures = textures;
            return(true);
        }
Esempio n. 3
0
        private static Wad2 LoadWad2(ChunkReader chunkIO, bool obsolete)
        {
            if (obsolete)
            {
                LEB128.ReadUInt(chunkIO.Raw);
            }
            var wad = new Wad2();

            Dictionary <long, WadTexture>   textures   = null;
            Dictionary <long, WadSample>    samples    = null;
            Dictionary <long, WadSoundInfo> soundInfos = null;
            Dictionary <long, WadSprite>    sprites    = null;

            wad.SoundSystem = SoundSystem.Dynamic;

            chunkIO.ReadChunks((id, chunkSize) =>
            {
                if (id == Wad2Chunks.GameVersion)
                {
                    wad.GameVersion = (TRVersion.Game)chunkIO.ReadChunkLong(chunkSize);
                    return(true);
                }
                else if (id == Wad2Chunks.SoundSystem)
                {
                    wad.SoundSystem = (SoundSystem)chunkIO.ReadChunkLong(chunkSize);
                    return(true);
                }
                else if (LoadTextures(chunkIO, id, wad, ref textures))
                {
                    return(true);
                }
                else if (LoadSamples(chunkIO, id, wad, ref samples, obsolete))
                {
                    return(true);
                }
                else if (LoadSoundInfos(chunkIO, id, wad, ref soundInfos, samples))
                {
                    return(true);
                }
                else if (LoadFixedSoundInfos(chunkIO, id, wad, soundInfos))
                {
                    return(true);
                }
                else if (LoadAdditionalSoundInfos(chunkIO, id, wad, soundInfos, samples))
                {
                    return(true);
                }
                else if (LoadSprites(chunkIO, id, wad, ref sprites))
                {
                    return(true);
                }
                else if (LoadSpriteSequences(chunkIO, id, wad, sprites))
                {
                    return(true);
                }
                else if (LoadMoveables(chunkIO, id, wad, soundInfos, textures))
                {
                    return(true);
                }
                else if (LoadStatics(chunkIO, id, wad, textures))
                {
                    return(true);
                }
                return(false);
            });

            if (obsolete)
            {
                foreach (KeyValuePair <long, WadSoundInfo> soundInfo in soundInfos)
                {
                    if (TrCatalog.IsSoundFixedByDefault(TRVersion.Game.TR4, checked ((uint)soundInfo.Key)))
                    {
                        var Id = new WadFixedSoundInfoId(checked ((uint)soundInfo.Key));
                        wad.FixedSoundInfosObsolete.Add(Id, new WadFixedSoundInfo(Id)
                        {
                            SoundInfo = soundInfo.Value
                        });
                    }
                }
            }

            // XML_SOUND_SYSTEM: Used for conversion of Wad2 to new sound system
            wad.AllLoadedSoundInfos = soundInfos;

            // Force wad to be xml wad in case there's no sound infos at all
            if (wad.SoundSystem != SoundSystem.Xml && wad.AllLoadedSoundInfos?.Count == 0)
            {
                wad.SoundSystem = SoundSystem.Xml;
            }

            return(wad);
        }
Esempio n. 4
0
        private static WadMesh LoadMesh(ChunkReader chunkIO, long chunkSize, Dictionary <long, WadTexture> textures)
        {
            var  mesh          = new WadMesh();
            long obsoleteIndex = 0;

            chunkIO.ReadChunks((id2, chunkSize2) =>
            {
                if (id2 == Wad2Chunks.MeshIndex)
                {
                    obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2);
                }
                else if (id2 == Wad2Chunks.MeshName)
                {
                    mesh.Name = chunkIO.ReadChunkString(chunkSize2);
                }
                else if (id2 == Wad2Chunks.MeshSphere)
                {
                    // Read bounding sphere
                    float radius   = 0;
                    Vector3 center = Vector3.Zero;
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshSphereCenter)
                        {
                            center = chunkIO.ReadChunkVector3(chunkSize3);
                        }
                        else if (id3 == Wad2Chunks.MeshSphereRadius)
                        {
                            radius = chunkIO.ReadChunkFloat(chunkSize3);
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                    mesh.BoundingSphere = new BoundingSphere(center, radius);
                }
                else if (id2 == Wad2Chunks.MeshBoundingBox)
                {
                    // Read bounding box
                    Vector3 min = Vector3.Zero;
                    Vector3 max = Vector3.Zero;
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshBoundingBoxMin)
                        {
                            min = chunkIO.ReadChunkVector3(chunkSize3);
                        }
                        else if (id3 == Wad2Chunks.MeshBoundingBoxMax)
                        {
                            max = chunkIO.ReadChunkVector3(chunkSize3);
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                    mesh.BoundingBox = new BoundingBox(min, max);
                }
                else if (id2 == Wad2Chunks.MeshVertexPositions)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexPosition)
                        {
                            mesh.VerticesPositions.Add(chunkIO.ReadChunkVector3(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshVertexNormals)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexNormal)
                        {
                            mesh.VerticesNormals.Add(chunkIO.ReadChunkVector3(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshVertexShades)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexShade)
                        {
                            mesh.VerticesShades.Add(chunkIO.ReadChunkShort(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshPolygons)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshQuad ||
                            id3 == Wad2Chunks.MeshTriangle)
                        {
                            var polygon    = new WadPolygon();
                            polygon.Shape  = id3 == Wad2Chunks.MeshQuad ? WadPolygonShape.Quad : WadPolygonShape.Triangle;
                            polygon.Index0 = LEB128.ReadInt(chunkIO.Raw);
                            polygon.Index1 = LEB128.ReadInt(chunkIO.Raw);
                            polygon.Index2 = LEB128.ReadInt(chunkIO.Raw);
                            if (id3 == Wad2Chunks.MeshQuad)
                            {
                                polygon.Index3 = LEB128.ReadInt(chunkIO.Raw);
                            }
                            polygon.ShineStrength = LEB128.ReadByte(chunkIO.Raw);

                            TextureArea textureArea = new TextureArea();
                            textureArea.Texture     = textures[LEB128.ReadInt(chunkIO.Raw)];
                            textureArea.TexCoord0   = chunkIO.Raw.ReadVector2();
                            textureArea.TexCoord1   = chunkIO.Raw.ReadVector2();
                            textureArea.TexCoord2   = chunkIO.Raw.ReadVector2();
                            if (id3 == Wad2Chunks.MeshQuad)
                            {
                                textureArea.TexCoord3 = chunkIO.Raw.ReadVector2();
                            }
                            else
                            {
                                textureArea.TexCoord3 = textureArea.TexCoord2;
                            }
                            textureArea.BlendMode   = (BlendMode)LEB128.ReadLong(chunkIO.Raw);
                            textureArea.DoubleSided = chunkIO.Raw.ReadBoolean();
                            polygon.Texture         = textureArea;

                            chunkIO.ReadChunks((id4, chunkSize4) =>
                            {
                                return(false);
                            });

                            mesh.Polys.Add(polygon);
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else
                {
                    return(false);
                }

                return(true);
            });

            return(mesh);
        }
Esempio n. 5
0
        private static bool LoadSoundInfo(ChunkReader chunkIO, Wad2 wad, Dictionary <long, WadSample> samples,
                                          out WadSoundInfo soundInfo, out long index)
        {
            var   tempSoundInfo = new WadSoundInfo(0);
            long  tempIndex     = 0;
            float volume        = 0;
            float chance        = 0;
            float pitch         = 0;
            float range         = 0;

            chunkIO.ReadChunks((id2, chunkSize2) =>
            {
                // XML_SOUND_SYSTEM
                if (id2 == Wad2Chunks.SoundInfoIndex)
                {
                    tempIndex = chunkIO.ReadChunkLong(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoVolume)
                {
                    volume = chunkIO.ReadChunkFloat(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoRange)
                {
                    range = chunkIO.ReadChunkFloat(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoPitch)
                {
                    pitch = chunkIO.ReadChunkFloat(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoChance)
                {
                    chance = chunkIO.ReadChunkFloat(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoDisablePanning)
                {
                    tempSoundInfo.DisablePanning = chunkIO.ReadChunkBool(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoRandomizePitch)
                {
                    tempSoundInfo.RandomizePitch = chunkIO.ReadChunkBool(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoRandomizeVolume)
                {
                    tempSoundInfo.RandomizeVolume = chunkIO.ReadChunkBool(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoLoopBehaviour)
                {
                    tempSoundInfo.LoopBehaviour = (WadSoundLoopBehaviour)(3 & chunkIO.ReadChunkByte(chunkSize2));
                }
                else if (id2 == Wad2Chunks.SoundInfoName || id2 == Wad2Chunks.SoundInfoNameObsolete)
                {
                    tempSoundInfo.Name = chunkIO.ReadChunkString(chunkSize2);
                }
                else if (id2 == Wad2Chunks.SoundInfoSampleIndex)
                {
                    tempSoundInfo.Samples.Add(samples[chunkIO.ReadChunkInt(chunkSize2)]); // Legacy
                }
                else
                {
                    return(false);
                }
                return(true);
            });

            // Convert from floats to ints
            tempSoundInfo.Volume         = (int)Math.Round(volume * 100.0f);
            tempSoundInfo.RangeInSectors = (int)range;
            tempSoundInfo.Chance         = (int)Math.Round(chance * 100.0f);
            tempSoundInfo.PitchFactor    = (int)Math.Round((pitch - 1.0f) * 100.0f);

            // Try to get the old ID
            tempSoundInfo.Id = TrCatalog.TryGetSoundInfoIdByDescription(wad.GameVersion, tempSoundInfo.Name);

            if (string.IsNullOrWhiteSpace(tempSoundInfo.Name))
            {
                tempSoundInfo.Name = TrCatalog.GetOriginalSoundName(wad.GameVersion, unchecked ((uint)tempIndex));
            }

            index     = tempIndex;
            soundInfo = tempSoundInfo;

            return(true);
        }
Esempio n. 6
0
        private static bool LoadSamples(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, ref Dictionary <long, WadSample> outSamples, bool obsolete)
        {
            if (idOuter != Wad2Chunks.Samples)
            {
                return(false);
            }

            var  samples       = new Dictionary <long, WadSample>();
            long obsoleteIndex = 0; // Move this into each chunk once we got rid of old style *.wad2 files.

            chunkIO.ReadChunks((id, chunkSize) =>
            {
                if (id != Wad2Chunks.Sample)
                {
                    return(false);
                }

                string FilenameObsolete = null;
                byte[] data             = null;

                chunkIO.ReadChunks((id2, chunkSize2) =>
                {
                    if (id2 == Wad2Chunks.SampleIndex)
                    {
                        obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2);
                    }
                    else if (id2 == Wad2Chunks.SampleFilenameObsolete)
                    {
                        FilenameObsolete = chunkIO.ReadChunkString(chunkSize2);
                    }
                    else if (id2 == Wad2Chunks.SampleData)
                    {
                        data = chunkIO.ReadChunkArrayOfBytes(chunkSize2);
                    }
                    else
                    {
                        return(false);
                    }
                    return(true);
                });

                if (data == null && !string.IsNullOrEmpty(FilenameObsolete))
                {
                    string fullPath = Path.Combine(PathC.GetDirectoryNameTry(Assembly.GetEntryAssembly().Location), "Sounds\\TR4\\Samples", FilenameObsolete + ".wav");
                    data            = File.ReadAllBytes(fullPath);
                }

                samples.Add(obsoleteIndex++, new WadSample("", WadSample.ConvertSampleFormat(data,
                                                                                             sampleRate => obsolete ?
                                                                                             new WadSample.ResampleInfo {
                    Resample = false, SampleRate = WadSample.GameSupportedSampleRate
                } :
                                                                                             new WadSample.ResampleInfo {
                    Resample = true, SampleRate = sampleRate
                })));
                return(true);
            });

            outSamples = samples;
            return(true);
        }