TerrainDisplay.MPQ.WMO.WMORootParser.Process C# (CSharp) Method

Process() public static method

public static Process ( WCell.MPQTool.MpqManager mpqManager, string filePath ) : WMORoot
mpqManager WCell.MPQTool.MpqManager
filePath string
return WMORoot
        public static WMORoot Process(MpqManager mpqManager, string filePath)
        {
            var root = new WMORoot(filePath);

            if (!mpqManager.FileExists(filePath))
            {
                log.Error("WMO file does not exist: ", filePath);
            }

            using (var stream = mpqManager.OpenFile(filePath))
            using (var fileReader = new BinaryReader(stream))
            {
                uint type = 0;
                uint size = 0;
                long curPos = AdvanceToNextChunk(fileReader, 0, ref type, ref size);

                if (type == Signatures.MVER)
                {
                    // Version
                    ReadMVER(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MVER");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOHD)
                {
                    // Root Header
                    ReadMOHD(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOHD");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOTX)
                {
                    // Texture Names
                    ReadMOTX(fileReader, root, size);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOTX");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOMT)
                {
                    // Materials
                    ReadMOMT(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOMT");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOGN)
                {
                    ReadMOGN(fileReader, root, size);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOGN");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOGI)
                {
                    // Group Information
                    ReadMOGI(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOGI");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOSB)
                {
                    // Skybox (always 0 now, its no longer handled in WMO)
                    ReadMOSB(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOSB");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOPV)
                {
                    // Portal Vertices
                    ReadMOPV(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOPV");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOPT)
                {
                    // Portal Information
                    ReadMOPT(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOPT");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOPR)
                {
                    // Portal Relations
                    ReadMOPR(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOPR");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOVV)
                {
                    // Visible Vertices
                    ReadMOVV(fileReader, root, size);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOVV");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOVB)
                {
                    // Visible Blocks
                    ReadMOVB(fileReader, root, size);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOVB");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MOLT)
                {
                    // Lights
                    ReadMOLT(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MOLT");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MODS)
                {
                    // Doodad Set
                    ReadMODS(fileReader, root);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MODS");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MODN)
                {
                    // Doodad Names
                    ReadMODN(fileReader, root, size);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MODN");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MODD)
                {
                    // Doodad Definitions
                    ReadMODD(fileReader, root, size);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MODD");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                if (type == Signatures.MFOG)
                {
                    // Fog info
                    ReadMFOG(fileReader, root, size);
                }
                else
                {
                    Console.WriteLine("WMO Root missing required chunk MFOG");
                }
                curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);

                // Theres only 1 optional chunk in a WMO root
                if (fileReader.BaseStream.Position < fileReader.BaseStream.Length)
                {
                    if (type == Signatures.MCVP)
                    {
                        ReadMCVP(fileReader, root, size);
                    }

                    //curPos = AdvanceToNextChunk(fileReader, curPos, ref type, ref size);
                }

            }

            return root;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Adds a WMO to the manager
        /// </summary>
        /// <param name="currentMODF">MODF (placement information for this WMO)</param>
        public void AddWMO(MapObjectDefinition currentMODF)
        {
            _fileNames.Add(currentMODF.FilePath);

            // Parse the WMORoot
            var wmoRoot = WMORootParser.Process(MpqTerrainManager.MpqManager, currentMODF.FilePath);

            // Parse the WMOGroups
            for (var wmoGroup = 0; wmoGroup < wmoRoot.Header.GroupCount; wmoGroup++)
            {
                var newFile         = wmoRoot.FilePath.Substring(0, wmoRoot.FilePath.LastIndexOf('.'));
                var currentFilePath = String.Format("{0}_{1:000}.wmo", newFile, wmoGroup);

                var group = WMOGroupParser.Process(MpqTerrainManager.MpqManager, currentFilePath, wmoRoot, wmoGroup);

                wmoRoot.Groups[wmoGroup] = group;
            }

            //wmoRoot.DumpLiqChunks();

            // Parse in the WMO's M2s
            var curDoodadSet = currentMODF.DoodadSetId;

            var setIndices = new List <int> {
                0
            };

            if (curDoodadSet > 0)
            {
                setIndices.Add(curDoodadSet);
            }

            foreach (var index in setIndices)
            {
                var doodadSetOffset = wmoRoot.DoodadSets[index].FirstInstanceIndex;
                var doodadSetCount  = wmoRoot.DoodadSets[index].InstanceCount;
                wmoRoot.WMOM2s = new List <M2.M2>((int)doodadSetCount);
                for (var i = doodadSetOffset; i < (doodadSetOffset + doodadSetCount); i++)
                {
                    var curDoodadDef = wmoRoot.DoodadDefinitions[i];
                    var curM2        = M2ModelParser.Process(MpqTerrainManager.MpqManager, curDoodadDef.FilePath);

                    var tempIndices = new List <int>();
                    for (var j = 0; j < curM2.BoundingTriangles.Length; j++)
                    {
                        var tri = curM2.BoundingTriangles[j];

                        tempIndices.Add(tri.Index2);
                        tempIndices.Add(tri.Index1);
                        tempIndices.Add(tri.Index0);
                    }

                    var rotatedM2 = TransformWMOM2(curM2, tempIndices, curDoodadDef);
                    wmoRoot.WMOM2s.Add(rotatedM2);
                }
            }

            TransformWMO(currentMODF, wmoRoot);

            var bounds = new BoundingBox(wmoRoot.WmoVertices);

            wmoRoot.Bounds = bounds;

            WMOs.Add(wmoRoot);
        }