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);
        }
Esempio n. 2
0
        private void GrabDependants(BSPParser bspParser, VPKParser vpkParser)
        {
            if (!string.IsNullOrEmpty(includedVmtPath))
            {
                includedVmt = VMTData.GrabVMT(bspParser, vpkParser, includedVmtPath);
            }

            if (!string.IsNullOrEmpty(baseTexturePath))
            {
                baseTexture = SourceTexture.GrabTexture(bspParser, vpkParser, baseTexturePath);
            }
            if (!string.IsNullOrEmpty(bumpMapPath))
            {
                bumpMap = SourceTexture.GrabTexture(bspParser, vpkParser, bumpMapPath);
            }
            if (!string.IsNullOrEmpty(detailMapPath))
            {
                detailMap = SourceTexture.GrabTexture(bspParser, vpkParser, detailMapPath);
            }
        }
        public static SourceTexture ReadAndCache(Stream stream, long origOffset, string location)
        {
            string fixedLocation = location.Replace("\\", "/").ToLower();

            SourceTexture srcTexture = null;

            if (loadedTextures.ContainsKey(fixedLocation))
            {
                srcTexture = loadedTextures[fixedLocation];
            }
            else
            {
                srcTexture = new SourceTexture(fixedLocation);

                Color[] pixels = null;
                int     width  = 0;
                int     height = 0;

                pixels = LoadVTFFile(stream, origOffset, out width, out height);

                if (pixels != null)
                {
                    if (averageTextures)
                    {
                        pixels = MakePlain(AverageTexture(pixels), 4, 4);
                        width  = 4;
                        height = 4;
                    }
                    else
                    {
                        pixels = DecreaseTextureSize(pixels, width, height, maxTextureSize, out width, out height);
                    }
                }

                srcTexture.pixels = pixels;
                srcTexture.width  = width;
                srcTexture.height = height;
            }

            return(srcTexture);
        }
Esempio n. 4
0
 public void SetBumpMap(SourceTexture texture)
 {
     bumpMap = texture;
 }
Esempio n. 5
0
 public void SetBaseTexture(SourceTexture texture)
 {
     baseTexture = texture;
 }
Esempio n. 6
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);
        }
Esempio n. 7
0
        public List <string> GetDependencies(CancellationToken cancelToken)
        {
            List <string> dependencies = new List <string>();

            using (VPKParser vpkParser = new VPKParser(vpkLoc))
                using (BSPParser bspParser = new BSPParser(Path.Combine(mapDir, mapName + ".bsp")))
                {
                    bool validVPK = vpkParser.IsValid();
                    if (!validVPK)
                    {
                        return(null);
                    }

                    bspParser.ParseData(cancelToken);

                    if (cancelToken.IsCancellationRequested)
                    {
                        return(null);
                    }

                    //Note: If there are materials that point to textures in separate archives or there are textures used by the models whose vpk archive is not already
                    //      added by other dependencies, those archives will not be added. That would require us to read the materials and models to get what textures they use.

                    #region Map face textures dependencies
                    if (FaceLoadPercent > 0)
                    {
                        foreach (dface_t face in bspParser.faces)
                        {
                            if (cancelToken.IsCancellationRequested)
                            {
                                return(null);
                            }

                            texflags currentTexFlags    = GetFaceTextureFlags(face, bspParser);
                            string   rawTextureLocation = GetFaceTextureLocation(face, bspParser);

                            if (!IsUndesiredTexture(rawTextureLocation, currentTexFlags))
                            {
                                string fixedLocation = VMTData.FixLocation(bspParser, vpkParser, rawTextureLocation);
                                if (!vpkParser.FileExists(fixedLocation))
                                {
                                    fixedLocation = SourceTexture.FixLocation(bspParser, vpkParser, rawTextureLocation);
                                }

                                string dependency = vpkParser.LocateInArchive(fixedLocation);

                                if (!string.IsNullOrEmpty(dependency) && !dependencies.Contains(dependency))
                                {
                                    dependencies.Add(dependency);
                                }
                            }
                        }
                    }
                    #endregion

                    #region Model dependencies
                    if (ModelLoadPercent > 0)
                    {
                        for (int i = 0; i < bspParser.staticProps.staticPropInfo.Length; i++)
                        {
                            if (cancelToken.IsCancellationRequested)
                            {
                                return(null);
                            }

                            var currentPropInfo = bspParser.staticProps.staticPropInfo[i];

                            ushort propType      = currentPropInfo.PropType;
                            string modelFullPath = bspParser.staticProps.staticPropDict.names[propType];
                            modelFullPath = modelFullPath.Substring(0, modelFullPath.LastIndexOf("."));

                            string mdlPath = modelFullPath + ".mdl";
                            string vvdPath = modelFullPath + ".vvd";
                            string vtxPath = modelFullPath + ".vtx";

                            if (!vpkParser.FileExists(vtxPath))
                            {
                                vtxPath = modelFullPath + ".dx90.vtx";
                            }

                            string dependency = vpkParser.LocateInArchive(mdlPath);
                            if (!string.IsNullOrEmpty(dependency) && !dependencies.Contains(dependency))
                            {
                                dependencies.Add(dependency);
                            }
                            dependency = vpkParser.LocateInArchive(vvdPath);
                            if (!string.IsNullOrEmpty(dependency) && !dependencies.Contains(dependency))
                            {
                                dependencies.Add(dependency);
                            }
                            dependency = vpkParser.LocateInArchive(vtxPath);
                            if (!string.IsNullOrEmpty(dependency) && !dependencies.Contains(dependency))
                            {
                                dependencies.Add(dependency);
                            }
                        }
                    }
                    #endregion
                }
            return(dependencies);
        }