Example #1
0
        public void LoadSubFiles()
        {
            foreach (Bar.Entry barEntry in BarFile)
            {
                try
                {
                    switch (barEntry.Type)
                    {
                    case Bar.EntryType.Model:
                        ModelFile = Mdlx.Read(barEntry.Stream);
                        break;

                    case Bar.EntryType.ModelTexture:
                        TextureFile = ModelTexture.Read(barEntry.Stream);
                        break;

                    case Bar.EntryType.ModelCollision:
                        CollisionFile = new ModelCollision(barEntry.Stream);
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception e) { }
            }
        }
Example #2
0
        public int Execute()
        {
            var outDir   = Path.GetDirectoryName(MapFile);
            var baseName = Path.GetFileNameWithoutExtension(MapFile);

            var perTexture = new DeserializerBuilder()
                             .IgnoreUnmatchedProperties()
                             .WithAttributeOverride <TextureFrame>(it => it.Data, new YamlIgnoreAttribute())
                             .Build()
                             .Deserialize <PerTexture>(File.ReadAllText(Path.Combine(outDir, YmlFile ?? $"{baseName}.footer.yml")));

            var anyChanges = false;

            var barEntries = File.OpenRead(MapFile).Using(Bar.Read);

            foreach (var entry in barEntries
                     .Where(entry => entry.Type == Bar.EntryType.ModelTexture &&
                            ModelTexture.IsValid(entry.Stream) &&
                            perTexture.Textures.ContainsKey(entry.Name)
                            )
                     )
            {
                var model = perTexture.Textures[entry.Name];

                entry.Stream.SetPosition(0);

                var modelTexture = ModelTexture.Read(entry.Stream);

                model.ConvertBackTo(
                    pngFile =>
                {
                    return(new SpriteBitmap(Path.Combine(outDir, pngFile)));
                },
                    modelTexture.TextureFooterData
                    );

                {
                    var buffer = new MemoryStream();
                    modelTexture.Write(buffer);
                    buffer.Position = 0;

                    entry.Stream = buffer;
                }

                anyChanges |= true;
            }

            if (anyChanges)
            {
                var buffer = new MemoryStream();
                Bar.Write(buffer, barEntries);

                File.WriteAllBytes(MapFile, buffer.ToArray());
            }

            return(0);
        }
Example #3
0
        public void Read8bitPaletteCorrectly() => File.OpenRead(FileName2).Using(stream =>
        {
            var images = ModelTexture.Read(stream).Images;

            AssertPalette(images, 0, 0, 0, 0, 0);
            AssertPalette(images, 0, 4, 10, 10, 10);
            AssertPalette(images, 0, 8, 11, 15, 23);
            AssertPalette(images, 0, 16, 23, 19, 29);
            AssertPalette(images, 0, 32, 23, 35, 56);
            AssertPalette(images, 0, 64, 57, 51, 71);
            AssertPalette(images, 0, 128, 153, 96, 7);

            AssertPalette(images, 1, 3, 22, 18, 27);
        });
Example #4
0
        public MeshGroup this[int objId]
        {
            get
            {
                if (_meshGroups.TryGetValue(objId, out var meshGroup))
                {
                    return(meshGroup);
                }

                var objEntryName = _objEntryLookupReversed[objId];

                var modelPath     = Path.Combine(_objPath, objEntryName);
                var modelFileName = modelPath + ".mdlx";
                if (File.Exists(modelFileName))
                {
                    var mdlxEntries = File.OpenRead(modelFileName).Using(Bar.Read);
                    var modelEntry  = mdlxEntries.FirstOrDefault(x => x.Type == Bar.EntryType.Model);
                    if (modelEntry != null)
                    {
                        var          model    = Mdlx.Read(modelEntry.Stream);
                        ModelTexture textures = null;

                        var textureEntry = mdlxEntries.FirstOrDefault(x => x.Type == Bar.EntryType.ModelTexture);
                        if (textureEntry != null)
                        {
                            textures = ModelTexture.Read(textureEntry.Stream);
                        }

                        var modelMotion = MeshLoader.FromKH2(model);
                        modelMotion.ApplyMotion(modelMotion.InitialPose);
                        meshGroup = new MeshGroup
                        {
                            MeshDescriptors = modelMotion.MeshDescriptors,
                            Textures        = textures == null ? new IKingdomTexture[0] : textures.LoadTextures(_graphics).ToArray()
                        };
                    }
                    else
                    {
                        meshGroup = EmptyMeshGroup;
                    }
                }
                else
                {
                    meshGroup = EmptyMeshGroup;
                }

                _meshGroups[objId] = meshGroup;
                return(meshGroup);
            }
        }
Example #5
0
        static ImageFormatService()
        {
            imageFormat = new IImageFormat[]
            {
                GetImageFormat("PNG", "png", true, Png.IsValid, Png.Read, (stream, image) => Png.Write(stream, image)),
                GetImageFormat("BMP", "bmp", true, Bmp.IsValid, Bmp.Read, (stream, image) => Bmp.Write(stream, image)),
                GetImageFormat("TIFF", "tiff", true, Tiff.IsValid, Tiff.Read, (stream, image) => Tiff.Write(stream, image)),

                GetImageFormat("FAC", "fac", true, Imgd.IsFac, s => Imgd.ReadAsFac(s), (stream, images) =>
                               Imgd.WriteAsFac(stream, images.Select(x => x.AsImgd()))),

                GetImageFormat("IMGD", "imd", true, Imgd.IsValid, Imgd.Read, (stream, image) => image.AsImgd().Write(stream)),

                GetImageFormat("IMGZ", "imz", true, Imgz.IsValid, s => Imgz.Read(s), (stream, images) =>
                               Imgz.Write(stream, images.Select(x => x.AsImgd()))),

                GetImageFormat("KH2 Font", "bar", true, IsKh2Font, ReadKh2Font, WriteKh2Font),

                GetImageFormat("Font ARC", "arc", false, FontsArc.IsValid, s =>
                {
                    var fonts = FontsArc.Read(s);
                    return(new[]
                    {
                        fonts.FontCmd.Image1,
                        fonts.FontCmd.Image2,
                        fonts.FontHelp.Image1,
                        fonts.FontHelp.Image2,
                        fonts.FontMenu.Image1,
                        fonts.FontMenu.Image2,
                        fonts.FontMes.Image1,
                        fonts.FontMes.Image2,
                        fonts.FontNumeral.Image1,
                        fonts.FontNumeral.Image2,
                        fonts.FontIcon,
                    });
                }, (stream, images) =>
                               throw new NotImplementedException()),

                GetImageFormat("TIM2", "tm2", false, Tm2.IsValid, s => Tm2.Read(s), (stream, images) =>
                               throw new NotImplementedException()),

                GetImageFormat("KH2TIM", "tex", false, _ => true,
                               s => ModelTexture.Read(s).Images.Cast <IImageRead>(),
                               (stream, images) => throw new NotImplementedException()),
            };
Example #6
0
        public int Execute()
        {
            var perTexture = new PerTexture();

            var barEntries = File.OpenRead(MapFile).Using(Bar.Read);

            foreach (var entry in barEntries
                     .Where(entry => entry.Type == Bar.EntryType.ModelTexture && ModelTexture.IsValid(entry.Stream))
                     )
            {
                entry.Stream.SetPosition(0);

                var modelTexture = ModelTexture.Read(entry.Stream);

                if (modelTexture.Images == null || !modelTexture.Images.Any())
                {
                    return(1);
                }

                var footerData = modelTexture.TextureFooterData;

                perTexture.Textures[entry.Name] = new TextureFooterDataIMEx(footerData);
            }

            var outDir = Path.Combine(
                Path.GetDirectoryName(MapFile),
                OutputDir ?? "."
                );
            var baseName = Path.GetFileNameWithoutExtension(MapFile);

            Directory.CreateDirectory(outDir);

            File.WriteAllText(
                Path.Combine(outDir, $"{baseName}.footer.yml"),
                new SerializerBuilder()
                .WithTypeConverter(new UseJsonStyleArray <short>())
                .WithTypeConverter(new UseJsonStyleArray <byte>())
                .WithAttributeOverride <TextureFrame>(it => it.Data, new YamlIgnoreAttribute())
                .Build()
                .Serialize(ExportHelper.AlsoExportImages(outDir, baseName, perTexture))
                );

            return(0);
        }
Example #7
0
        static ImageFormatService()
        {
            imageFormat = new IImageFormat[]
            {
                GetImageFormat("PNG", "png", true, Png.IsValid, Png.Read, (stream, image) => Png.Write(stream, image)),
                GetImageFormat("BMP", "bmp", true, Bmp.IsValid, Bmp.Read, (stream, image) => Bmp.Write(stream, image)),
                GetImageFormat("TIFF", "tiff", true, Tiff.IsValid, Tiff.Read, (stream, image) => Tiff.Write(stream, image)),
                GetImageFormat("IMGD", "imd", true, Imgd.IsValid, Imgd.Read, (stream, image) => image.AsImgd().Write(stream)),

                GetImageFormat("IMGZ", "imz", true, Imgz.IsValid, s => Imgz.Read(s), (stream, images) =>
                               Imgz.Write(stream, images.Select(x => x.AsImgd()))),

                GetImageFormat("TIM2", "tm2", false, Tm2.IsValid, s => Tm2.Read(s), (stream, images) =>
                               throw new NotImplementedException()),

                GetImageFormat("KH2TIM", "tex", true, _ => true,
                               s => ModelTexture.Read(s).Images.Cast <IImageRead>(),
                               (stream, images) => throw new NotImplementedException()),
            };
Example #8
0
        public void OpenMdlxFile(string filePath)
        {
            using var stream = File.Open(filePath, FileMode.Open);
            Bar binarc = Bar.Read(stream);

            ObservableCollection <Bar.Entry> testColl = new ObservableCollection <Bar.Entry>(binarc);

            testColl[0].Name = "Changed";
            Debug.WriteLine("TEST: " + binarc[0].Name);
            Debug.WriteLine("TEST: " + testColl[0].Name);

            if (binarc != null)
            {
                Mdlx           modelFile;
                ModelTexture   textureFile;
                ModelCollision collisionFile;

                foreach (Bar.Entry barEntry in binarc)
                {
                    switch (barEntry.Type)
                    {
                    case Bar.EntryType.Model:
                        modelFile = Mdlx.Read(barEntry.Stream);
                        break;

                    case Bar.EntryType.ModelTexture:
                        textureFile = ModelTexture.Read(barEntry.Stream);
                        break;

                    case Bar.EntryType.ModelCollision:
                        collisionFile = new ModelCollision(barEntry.Stream);
                        break;

                    default:
                        break;
                    }
                }
                Debug.WriteLine("BREAKPOINT");
            }
        }
Example #9
0
        public void CreateImagesWithTheCorrectInformation() => File.OpenRead(FileName2).Using(stream =>
        {
            var modelTexture = ModelTexture.Read(stream);

            var image0 = modelTexture.Images[0];
            Assert.Equal(256, image0.Size.Width);
            Assert.Equal(256, image0.Size.Height);
            Assert.Equal(PixelFormat.Indexed8, image0.PixelFormat);
            Assert.Equal(256 * 256, image0.GetData().Length);

            var image1 = modelTexture.Images[1];
            Assert.Equal(256, image1.Size.Width);
            Assert.Equal(256, image1.Size.Height);
            Assert.Equal(PixelFormat.Indexed8, image1.PixelFormat);
            Assert.Equal(256 * 256, image1.GetData().Length);

            var image2 = modelTexture.Images[2];
            Assert.Equal(128, image2.Size.Width);
            Assert.Equal(64, image2.Size.Height);
            Assert.Equal(PixelFormat.Indexed8, image2.PixelFormat);
            Assert.Equal(128 * 64, image2.GetData().Length);
        });
        public MeshGroup this[int objId]
        {
            get
            {
                if (_meshGroups.TryGetValue(objId, out var meshGroup))
                {
                    return(meshGroup);
                }

                var objEntryName = _objEntryLookupReversed[objId];

                var modelPath     = Path.Combine(_objPath, objEntryName);
                var modelFileName = modelPath + ".mdlx";
                if (File.Exists(modelFileName))
                {
                    var mdlxEntries = File.OpenRead(modelFileName).Using(Bar.Read);
                    var modelEntry  = mdlxEntries.FirstOrDefault(x => x.Type == Bar.EntryType.Model);
                    if (modelEntry != null)
                    {
                        var model    = Mdlx.Read(modelEntry.Stream);
                        var textures = ModelTexture.Read(mdlxEntries.First(x => x.Type == Bar.EntryType.ModelTexture).Stream);
                        meshGroup = MeshLoader.FromKH2(_graphics, model, textures);
                    }
                    else
                    {
                        meshGroup = EmptyMeshGroup;
                    }
                }
                else
                {
                    meshGroup = EmptyMeshGroup;
                }

                _meshGroups[objId] = meshGroup;
                return(meshGroup);
            }
        }
Example #11
0
        public void Read4bitPaletteCorrectly() => File.OpenRead(FileName1).Using(stream =>
        {
            var clut = ModelTexture.Read(stream).Images.First().GetClut();

            Assert.Equal(87, clut[0]);
            Assert.Equal(98, clut[1]);
            Assert.Equal(106, clut[2]);
            Assert.Equal(255, clut[3]);

            Assert.Equal(95, clut[4]);
            Assert.Equal(105, clut[5]);
            Assert.Equal(114, clut[6]);
            Assert.Equal(255, clut[7]);

            Assert.Equal(108, clut[16]);
            Assert.Equal(118, clut[17]);
            Assert.Equal(128, clut[18]);
            Assert.Equal(255, clut[19]);

            Assert.Equal(134, clut[32]);
            Assert.Equal(147, clut[33]);
            Assert.Equal(158, clut[34]);
            Assert.Equal(255, clut[35]);
        });
Example #12
0
 public void WriteBackTheSameFile(string fileName) => File.OpenRead(fileName).Using(stream => Helpers.AssertStream(stream, inStream =>
 {
     var outStream = new MemoryStream();
     ModelTexture.Read(inStream).Write(outStream);
     return(outStream);
 }));
Example #13
0
        public void ReadCorrectAmountOfTextures(string fileName, int expectedCount) => File.OpenRead(fileName).Using(stream =>
        {
            var modelTexture = ModelTexture.Read(stream);

            Assert.Equal(expectedCount, modelTexture.Images.Count);
        });
Example #14
0
        public void CreateAndRead(string baseName)
        {
            var image = File.OpenRead($"kh2/res/image-{baseName}.imd").Using(s => Imgd.Read(s));

            {
                var outStream = new MemoryStream();
                {
                    var textures = new ModelTexture(new Imgd[] { image });
                    textures.Write(outStream);
                }
                {
                    outStream.Position = 0;
                    var textures = ModelTexture.Read(outStream);
                    Assert.Single(textures.Images);

                    var converted = textures.Images.Single();

                    Assert.Equal(image.PixelFormat, converted.PixelFormat);
                    Assert.Equal(image.Size, converted.Size);
                    Assert.Equal(image.GetClut(), converted.GetClut());
                    Assert.Equal(image.GetData(), converted.GetData());
                }
            }

            {
                var outStream = new MemoryStream();
                {
                    var textures = new ModelTexture(new Imgd[] { image, image });
                    textures.Write(outStream);
                }
                {
                    outStream.Position = 0;
                    var textures = ModelTexture.Read(outStream);
                    Assert.Equal(2, textures.Images.Count);

                    foreach (var converted in textures.Images)
                    {
                        Assert.Equal(image.PixelFormat, converted.PixelFormat);
                        Assert.Equal(image.Size, converted.Size);
                        Assert.Equal(image.GetClut(), converted.GetClut());
                        Assert.Equal(image.GetData(), converted.GetData());
                    }
                }
            }

            {
                var outStream = new MemoryStream();
                {
                    var textures = new ModelTexture(new Imgd[] { image, image, image, image });
                    textures.Write(outStream);
                }
                {
                    outStream.Position = 0;
                    var textures = ModelTexture.Read(outStream);
                    Assert.Equal(4, textures.Images.Count);

                    foreach (var converted in textures.Images)
                    {
                        Assert.Equal(image.PixelFormat, converted.PixelFormat);
                        Assert.Equal(image.Size, converted.Size);
                        Assert.Equal(image.GetClut(), converted.GetClut());
                        Assert.Equal(image.GetData(), converted.GetData());
                    }
                }
            }
        }