/*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); }
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); }