예제 #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) { }
            }
        }
예제 #2
0
파일: MdlxTests.cs 프로젝트: xorllc/OpenKh
        public void ReadAlb1t2Table() => File.OpenRead(MapFileName).Using(stream =>
        {
            var alb1t2 = Mdlx.Read(stream).MapModel.alb1t2;
            Assert.Equal(97, alb1t2.Count);

            Assert.Equal(0, alb1t2[0]);
            Assert.Equal(1, alb1t2[1]);
            Assert.Equal(2, alb1t2[2]);
            Assert.Equal(96, alb1t2[96]);
        });
예제 #3
0
        public void ReadRemapTable() => File.OpenRead(MapFileName).Using(stream =>
        {
            var remapTable = Mdlx.Read(stream).MapModel.DmaChainIndexRemapTable;
            Assert.Equal(97, remapTable.Count);

            Assert.Equal(0, remapTable[0]);
            Assert.Equal(1, remapTable[1]);
            Assert.Equal(2, remapTable[2]);
            Assert.Equal(96, remapTable[96]);
        });
예제 #4
0
파일: MdlxTests.cs 프로젝트: xorllc/OpenKh
        public void ShouldWriteBackTheExactSameFile() => File.OpenRead(FileName).Using(stream =>
        {
            Helpers.AssertStream(stream, inStream =>
            {
                var mdlx = Mdlx.Read(inStream);

                var outStream = new MemoryStream();
                mdlx.Write(outStream);

                return(outStream);
            });
        });
예제 #5
0
파일: MdlxTests.cs 프로젝트: xorllc/OpenKh
        public void WriteMapBack() => File.OpenRead(MapFileName).Using(stream =>
        {
            Helpers.AssertStream(stream, inStream =>
            {
                var mdlx = Mdlx.Read(inStream);

                var outStream = new MemoryStream();
                mdlx.Write(outStream);

                return(outStream);
            });
        });
예제 #6
0
파일: MdlxTests.cs 프로젝트: xorllc/OpenKh
        public void ReadAlb2() => File.OpenRead(MapFileName).Using(stream =>
        {
            var alb2 = Mdlx.Read(stream).MapModel.alb2;
            Assert.Equal(9, alb2.Count);

            Assert.Equal(20, alb2[0].Length);
            Assert.Equal(1, alb2[0][0]);
            Assert.Equal(91, alb2[0][19]);

            Assert.Equal(3, alb2[8].Length);
            Assert.Equal(30, alb2[8][0]);
            Assert.Equal(68, alb2[8][2]);
        });
예제 #7
0
        public void ReadRenderingGroup() => File.OpenRead(MapFileName).Using(stream =>
        {
            var renderingGroup = Mdlx.Read(stream).MapModel.vifPacketRenderingGroup;
            Assert.Equal(9, renderingGroup.Count);

            Assert.Equal(20, renderingGroup[0].Length);
            Assert.Equal(1, renderingGroup[0][0]);
            Assert.Equal(91, renderingGroup[0][19]);

            Assert.Equal(3, renderingGroup[8].Length);
            Assert.Equal(30, renderingGroup[8][0]);
            Assert.Equal(68, renderingGroup[8][2]);
        });
예제 #8
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);
            }
        }
예제 #9
0
파일: MdlxTests.cs 프로젝트: xorllc/OpenKh
        public void ShouldReadVifPackets() => File.OpenRead(FileName).Using(stream =>
        {
            var dmaChain = Mdlx.Read(stream).SubModels[0].DmaChains;
            Assert.Equal(26, dmaChain[0].DmaVifs.Count);
            Assert.Equal(4, dmaChain.Count);
            Assert.Equal(58, dmaChain.Sum(x => x.DmaVifs.Count));

            var dmaVif = dmaChain[0].DmaVifs[0];
            Assert.Equal(0, dmaVif.TextureIndex);
            Assert.Equal(10, dmaVif.Alaxi.Length);
            Assert.Equal(53, dmaVif.Alaxi[0]);
            Assert.Equal(22, dmaVif.Alaxi[9]);
            Assert.Equal(1600, dmaVif.VifPacket.Length);
            Assert.Equal(1, dmaVif.VifPacket[0]);
            Assert.Equal(248, dmaVif.VifPacket[324]);
        });
예제 #10
0
파일: MdlxTests.cs 프로젝트: xorllc/OpenKh
        public void ReadVifPackets() => File.OpenRead(MapFileName).Using(stream =>
        {
            var alvifpkt = Mdlx.Read(stream).MapModel.VifPackets;
            Assert.Equal(97, alvifpkt.Count);

            var packet1 = alvifpkt[0];
            Assert.Equal(1, packet1.TextureId);
            Assert.Equal(1184, packet1.VifPacket.Length);
            Assert.Equal(1, packet1.VifPacket[0]);
            Assert.Equal(0, packet1.VifPacket[10]);
            Assert.Equal(64, packet1.VifPacket[100]);
            Assert.Equal(76, packet1.VifPacket[1170]);

            var packet2 = alvifpkt[96];
            Assert.Equal(2, packet2.TextureId);
            Assert.Equal(960, packet2.VifPacket.Length);
            Assert.Equal(1, packet2.VifPacket[0]);
            Assert.Equal(117, packet2.VifPacket[500]);
        });
예제 #11
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");
            }
        }
예제 #12
0
파일: MdlxTests.cs 프로젝트: xorllc/OpenKh
        public void ShouldReadBones() => File.OpenRead(FileName).Using(stream =>
        {
            var bones = Mdlx.Read(stream).SubModels[0].Bones;
            Assert.Equal(228, bones.Count);

            var bone = bones[0];
            Assert.Equal(0, bone.Index);
            Assert.Equal(-1, bone.Parent);
            Assert.Equal(0, bone.Unk08);
            Assert.Equal(3, bone.Unk0c);
            Assert.Equal(1, bone.ScaleX);
            Assert.Equal(0, bone.RotationX);
            Assert.Equal(0, bone.TranslationX);
            Assert.Equal(1, bone.ScaleY);
            Assert.Equal(4.71, bone.RotationY, 2);
            Assert.Equal(102.62, bone.TranslationY, 2);
            Assert.Equal(1, bone.ScaleZ);
            Assert.Equal(4.71, bone.RotationZ, 2);
            Assert.Equal(0, bone.TranslationZ);
            Assert.Equal(0, bone.ScaleW);
            Assert.Equal(0, bone.RotationW);
            Assert.Equal(0, bone.TranslationW);
        });
예제 #13
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);
                        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);
            }
        }
예제 #14
0
            protected int OnExecute(CommandLineApplication app)
            {
                MapOut = Path.GetFullPath(MapOut ?? Path.GetFileName(MapIn));

                Console.WriteLine($"Output map file: {MapOut}");

                var entries = File.OpenRead(MapIn).Using(s => Bar.Read(s).ToArray());

                var mapModel = Mdlx.Read(entries.Single(IsMapModel).Stream);

                var numVifPackets = mapModel.MapModel.VifPackets.Count;
                var numAlb2Groups = mapModel.MapModel.vifPacketRenderingGroup.Count;

                Console.WriteLine($"numVifPackets: {numVifPackets:#,##0}");
                Console.WriteLine($"numAlb2Groups: {numAlb2Groups:#,##0}");

                Console.WriteLine($"Note: this tool will build a unoptimized doct that renders all ALB2 {numAlb2Groups:#,##0} groups.");

                var doctStream = new MemoryStream();

                {
                    var doct = new Doct();

                    doct.Entry1List.Add(
                        new Doct.Entry1
                    {
                        Entry2Index     = 0,
                        Entry2LastIndex = Convert.ToUInt16(numAlb2Groups),     // max 65535
                    }
                        );

                    const float WorldBounds = 18000;

                    doct.Entry2List.AddRange(
                        Enumerable.Range(0, numAlb2Groups)
                        .Select(
                            index => new Doct.Entry2
                    {
                        BoundingBox = new BoundingBox(
                            new System.Numerics.Vector3(-WorldBounds),
                            new System.Numerics.Vector3(WorldBounds)
                            )
                    }
                            )
                        );

                    Doct.Write(doctStream, doct);

                    doctStream.Position = 0;
                }

                foreach (var entry in entries.Where(IsDoct))
                {
                    Console.WriteLine("DOCT entry replaced.");
                    entry.Stream = doctStream;
                }

                File.Create(MapOut).Using(s => Bar.Write(s, entries));

                Console.WriteLine("Output map file is written successfully.");
                return(0);
            }
예제 #15
0
        internal static bool IsModelFile(List <Bar.Entry> barEntries)
        {
            var model = barEntries.First(it => it.Type == Bar.EntryType.Model);

            return(!Mdlx.Read(model.Stream).IsMap);
        }
예제 #16
0
파일: MdlxTests.cs 프로젝트: xorllc/OpenKh
        public void ShouldReadTheCorrectAmountOfSubModels() => File.OpenRead(FileName).Using(stream =>
        {
            var model = Mdlx.Read(stream).SubModels;

            Assert.Equal(2, model.Count);
        });