Пример #1
0
        public static string FixLocation(BSPParser bspParser, VPKParser vpkParser, string rawPath)
        {
            string fixedLocation = rawPath.Replace("\\", "/").ToLower();

            if ((bspParser == null || !bspParser.HasPakFile(fixedLocation)) && (vpkParser == null || !vpkParser.FileExists(fixedLocation)))
            {
                fixedLocation += ".vmt";
            }
            if ((bspParser == null || !bspParser.HasPakFile(fixedLocation)) && (vpkParser == null || !vpkParser.FileExists(fixedLocation)))
            {
                fixedLocation = Path.Combine("materials", fixedLocation).Replace("\\", "/");
            }

            return(fixedLocation);
        }
Пример #2
0
        public static SourceTexture GrabTexture(BSPParser bspParser, VPKParser vpkParser, string rawPath)
        {
            SourceTexture srcTexture = null;

            if (!string.IsNullOrEmpty(rawPath))
            {
                string vtfFilePath = FixLocation(bspParser, vpkParser, rawPath);
                if (!string.IsNullOrEmpty(vtfFilePath))
                {
                    if (loadedTextures.ContainsKey(vtfFilePath))
                    {
                        srcTexture = loadedTextures[vtfFilePath];
                    }
                    else
                    {
                        if (bspParser != null && bspParser.HasPakFile(vtfFilePath))
                        {
                            //Debug.Log("Loaded " + vtfFilePath + " from pakfile");
                            srcTexture = ReadAndCache(bspParser.GetPakFile(vtfFilePath), vtfFilePath);
                        }
                        else if (vpkParser != null && vpkParser.FileExists(vtfFilePath))
                        {
                            try
                            {
                                vpkParser.LoadFileAsStream(vtfFilePath, (stream, origOffset, fileLength) => { srcTexture = ReadAndCache(stream, origOffset, vtfFilePath); });
                            }
                            catch (Exception e)
                            {
                                Debug.LogError("SourceTexture: " + e.ToString());
                            }
                        }
                        else
                        {
                            Debug.LogError("SourceTexture: Could not find Texture FixedPath(" + vtfFilePath + ") RawPath(" + rawPath + ")");
                        }
                    }
                }
                else
                {
                    Debug.LogError("SourceTexture: Could not find texture at " + rawPath);
                }
            }
            else
            {
                Debug.LogError("SourceTexture: Texture string path is null or empty");
            }

            return(srcTexture);
        }
Пример #3
0
        private void Parse(BSPParser bspParser, VPKParser vpkParser)
        {
            if (vpkParser != null)
            {
                string mdlPath = modelPath + ".mdl";
                string vvdPath = modelPath + ".vvd";
                string vtxPath = modelPath + ".vtx";

                if ((bspParser != null && bspParser.HasPakFile(mdlPath)) || (vpkParser != null && vpkParser.FileExists(mdlPath)))
                {
                    if ((bspParser != null && bspParser.HasPakFile(vvdPath)) || (vpkParser != null && vpkParser.FileExists(vvdPath)))
                    {
                        if ((bspParser == null || !bspParser.HasPakFile(vtxPath)) && (vpkParser == null || !vpkParser.FileExists(vtxPath)))
                        {
                            vtxPath = modelPath + ".dx90.vtx";
                        }

                        if ((bspParser != null && bspParser.HasPakFile(vtxPath)) || (vpkParser != null && vpkParser.FileExists(vtxPath)))
                        {
                            using (MDLParser mdl = new MDLParser())
                                using (VVDParser vvd = new VVDParser())
                                    using (VTXParser vtx = new VTXParser())
                                    {
                                        try
                                        {
                                            if (bspParser != null && bspParser.HasPakFile(mdlPath))
                                            {
                                                bspParser.LoadPakFileAsStream(mdlPath, (stream, origOffset, byteCount) => { mdl.ParseHeader(stream, origOffset); });
                                            }
                                            else
                                            {
                                                vpkParser.LoadFileAsStream(mdlPath, (stream, origOffset, byteCount) => { mdl.ParseHeader(stream, origOffset); });
                                            }

                                            if (bspParser != null && bspParser.HasPakFile(vvdPath))
                                            {
                                                bspParser.LoadPakFileAsStream(vvdPath, (stream, origOffset, byteCount) => { vvd.ParseHeader(stream, origOffset); });
                                            }
                                            else
                                            {
                                                vpkParser.LoadFileAsStream(vvdPath, (stream, origOffset, byteCount) => { vvd.ParseHeader(stream, origOffset); });
                                            }

                                            int mdlChecksum = mdl.header1.checkSum;
                                            int vvdChecksum = (int)vvd.header.checksum;

                                            if (mdlChecksum == vvdChecksum)
                                            {
                                                if (bspParser != null && bspParser.HasPakFile(vtxPath))
                                                {
                                                    bspParser.LoadPakFileAsStream(vtxPath, (stream, origOffset, byteCount) => { vtx.ParseHeader(stream, origOffset); });
                                                }
                                                else
                                                {
                                                    vpkParser.LoadFileAsStream(vtxPath, (stream, origOffset, byteCount) => { vtx.ParseHeader(stream, origOffset); });
                                                }

                                                int vtxChecksum = vtx.header.checkSum;

                                                if (mdlChecksum == vtxChecksum)
                                                {
                                                    if (bspParser != null && bspParser.HasPakFile(mdlPath))
                                                    {
                                                        bspParser.LoadPakFileAsStream(mdlPath, (stream, origOffset, byteCount) => { mdl.Parse(stream, origOffset); });
                                                    }
                                                    else
                                                    {
                                                        vpkParser.LoadFileAsStream(mdlPath, (stream, origOffset, byteCount) => { mdl.Parse(stream, origOffset); });
                                                    }

                                                    if (bspParser != null && bspParser.HasPakFile(vvdPath))
                                                    {
                                                        bspParser.LoadPakFileAsStream(vvdPath, (stream, origOffset, byteCount) => { vvd.Parse(stream, mdl.header1.rootLod, origOffset); });
                                                    }
                                                    else
                                                    {
                                                        vpkParser.LoadFileAsStream(vvdPath, (stream, origOffset, byteCount) => { vvd.Parse(stream, mdl.header1.rootLod, origOffset); });
                                                    }

                                                    if (bspParser != null && bspParser.HasPakFile(vtxPath))
                                                    {
                                                        bspParser.LoadPakFileAsStream(vtxPath, (stream, origOffset, byteCount) => { vtx.Parse(stream, origOffset); });
                                                    }
                                                    else
                                                    {
                                                        vpkParser.LoadFileAsStream(vtxPath, (stream, origOffset, byteCount) => { vtx.Parse(stream, origOffset); });
                                                    }

                                                    version = mdl.header1.version;
                                                    id      = mdl.header1.id;

                                                    if (mdl.bodyParts != null)
                                                    {
                                                        ReadFaceMeshes(mdl, vvd, vtx, bspParser, vpkParser);
                                                    }
                                                    else
                                                    {
                                                        Debug.LogError("SourceModel: Could not find body parts of " + modelPath);
                                                    }
                                                }
                                                else
                                                {
                                                    Debug.LogError("SourceModel: MDL and VTX checksums don't match (" + mdlChecksum + " != " + vtxChecksum + ") vtxver(" + vtx.header.version + ") for " + modelPath);
                                                }
                                            }
                                            else
                                            {
                                                Debug.LogError("SourceModel: MDL and VVD checksums don't match (" + mdlChecksum + " != " + vvdChecksum + ") for " + modelPath);
                                            }
                                        }
                                        catch (System.Exception e)
                                        {
                                            Debug.LogError("SourceModel: " + e.ToString());
                                        }
                                    }
                        }
                        else
                        {
                            Debug.LogError("SourceModel: Could not find vtx file (" + vtxPath + ")");
                        }
                    }
                    else
                    {
                        Debug.LogError("SourceModel: Could not find vvd file (" + vvdPath + ")");
                    }
                }
                else
                {
                    Debug.LogError("SourceModel: Could not find mdl file (" + mdlPath + ")");
                }
            }
            else
            {
                Debug.LogError("SourceModel: VPK parser is null");
            }
        }
Пример #4
0
        /*public static VMTData GrabVMT(byte[] data, string rawPath)
         * {
         *  VMTData vmtData = null;
         *
         *  if (!string.IsNullOrEmpty(rawPath))
         *  {
         *      string vmtFilePath = rawPath.Replace("\\", "/").ToLower();
         *
         *      if (vmtCache.ContainsKey(vmtFilePath))
         *      {
         *          vmtData = vmtCache[vmtFilePath];
         *      }
         *      else
         *      {
         *          vmtData = new VMTData(vmtFilePath);
         *          vmtData.Parse(data);
         *      }
         *  }
         *  else
         *      Debug.LogError("VMTData: Given path is null or empty");
         *
         *  return vmtData;
         * }*/
        public static VMTData GrabVMT(BSPParser bspParser, VPKParser vpkParser, string rawPath, bool grabDependants = true)
        {
            VMTData vmtData = null;

            if (!string.IsNullOrEmpty(rawPath))
            {
                string vmtFilePath = FixLocation(bspParser, vpkParser, rawPath);
                if (vmtCache.ContainsKey(vmtFilePath))
                {
                    vmtData = vmtCache[vmtFilePath];
                    if (grabDependants)
                    {
                        vmtData.GrabDependants(bspParser, vpkParser);
                    }
                }
                else
                {
                    if (bspParser != null && bspParser.HasPakFile(vmtFilePath))
                    {
                        vmtData = new VMTData(vmtFilePath);
                        //Debug.Log("Loaded " + vmtFilePath + " from pakfile");
                        vmtData.Parse(bspParser.GetPakFile(vmtFilePath));
                        if (grabDependants)
                        {
                            vmtData.GrabDependants(bspParser, vpkParser);
                        }
                    }
                    else if (vpkParser != null && vpkParser.FileExists(vmtFilePath))
                    {
                        try
                        {
                            byte[] vmtByteData = null;
                            vpkParser.LoadFileAsStream(vmtFilePath, (stream, origOffset, fileLength) =>
                            {
                                vmtByteData     = new byte[fileLength];
                                stream.Position = origOffset;
                                stream.Read(vmtByteData, 0, vmtByteData.Length);
                            });
                            vmtData = new VMTData(vmtFilePath);
                            vmtData.Parse(vmtByteData);
                            if (grabDependants)
                            {
                                vmtData.GrabDependants(bspParser, vpkParser);
                            }
                        }
                        catch (System.Exception e)
                        {
                            Debug.LogError("VMTData: " + e.ToString());
                        }
                    }
                    else
                    {
                        string vtfFilePath = SourceTexture.FixLocation(bspParser, vpkParser, rawPath);
                        if ((bspParser != null && bspParser.HasPakFile(vtfFilePath)) || (vpkParser != null && vpkParser.FileExists(vtfFilePath)))
                        {
                            vmtData = new VMTData(vmtFilePath);
                            vmtData.baseTexturePath = rawPath;
                            if (grabDependants)
                            {
                                vmtData.GrabDependants(bspParser, vpkParser);
                            }
                        }
                        else
                        {
                            Debug.LogError("VMTData: Could not find VMT file at FixedPath(" + vmtFilePath + ") RawPath(" + rawPath + ")");
                        }
                    }
                }
            }
            else
            {
                Debug.LogError("VMTData: Given path is null or empty");
            }

            return(vmtData);
        }