Skip to content

Commit

Permalink
Merge pull request #151 from bottiger1/fix_slashes
Browse files Browse the repository at this point in the history
Improved keyvalue parsing in vmtPathParser
closes #149
  • Loading branch information
Exactol authored Jun 18, 2021
2 parents 5ff6a65 + c24e646 commit 0c99292
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 28 deletions.
68 changes: 59 additions & 9 deletions CompilePalX/Compilers/BSPPack/AssetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ public static Tuple<List<string>, List<string>> findMdlMaterialsAndModels(string

mdl.Close();
}

for(int i=0;i<materials.Count;i++)
{
materials[i] = Regex.Replace(materials[i], "/+", "/"); // remove duplicate slashes
}

return new Tuple<List<string>, List<string>>(materials, models);
}

Expand Down Expand Up @@ -358,9 +364,9 @@ public static List<string> findVmtTextures(string fullpath)

if (Keys.vmtTextureKeyWords.Any(key => param.ToLower().StartsWith(key + " ")))
{
vtfList.Add("materials/" + vmtPathParser(param) + ".vtf");
vtfList.Add("materials/" + vmtPathParser2(line) + ".vtf");
if (param.ToLower().StartsWith("$envmap" + " "))
vtfList.Add("materials/" + vmtPathParser(param) + ".hdr.vtf");
vtfList.Add("materials/" + vmtPathParser2(line) + ".hdr.vtf");
}
}
return vtfList;
Expand All @@ -376,7 +382,7 @@ public static List<string> findVmtMaterials(string fullpath)
string param = line.Replace("\"", " ").Replace("\t", " ").Trim();
if (Keys.vmtMaterialKeyWords.Any(key => param.StartsWith(key + " ")))
{
vmtList.Add("materials/" + vmtPathParser(param) + ".vmt");
vmtList.Add("materials/" + vmtPathParser2(line) + ".vmt");
}
}
return vmtList;
Expand All @@ -392,7 +398,7 @@ public static List<string> findResMaterials(string fullpath)
string param = line.Replace("\"", " ").Replace("\t", " ").Trim();
if (param.StartsWith("image ", StringComparison.CurrentCultureIgnoreCase))
{
string path = "materials/vgui/" + vmtPathParser(param) + ".vmt";
string path = "materials/vgui/" + vmtPathParser2(line) + ".vmt";
path = path.Replace("/vgui/..", "");
vmtList.Add(path);
}
Expand Down Expand Up @@ -447,6 +453,49 @@ public static string vmtPathParser(string vmtline, bool needsSplit = true)
return vmtline;
}

// same as above but quotes are not replaced and line does not need to be trimmed, quotes are needed to tell if // are comments or not
public static string vmtPathParser2(string vmtline)
{
vmtline = vmtline.Trim(new char[] {' ', '\t'});

// remove key
if(vmtline[0] == '"')
{
vmtline = Regex.Match(vmtline, "\"[^\"]+\"(.*)$").Groups[1].Value;
}
else
{
vmtline = Regex.Match(vmtline, "[^ \t]+(.*)$").Groups[1].Value;
}

vmtline = vmtline.TrimStart(new char[] { ' ', '\t' });
// process value
if (vmtline[0] == '"')
{
vmtline = Regex.Match(vmtline, "\"([^\"]+)\"").Groups[1].Value;
}
else
{
// strip c style comments like this one
var commentIndex = vmtline.IndexOf("//");
if(commentIndex > -1)
{
vmtline = vmtline.Substring(0, commentIndex);
}
vmtline = Regex.Match(vmtline, "[^ \t]+").Groups[0].Value;
}

vmtline = vmtline.Trim(new char[] { ' ', '/', '\\' }); // removes leading slashes
vmtline = vmtline.Replace('\\', '/'); // normalize slashes
vmtline = Regex.Replace(vmtline, "/+", "/"); // remove duplicate slashes

if (vmtline.StartsWith("materials/"))
vmtline = vmtline.Remove(0, "materials/".Length); // removes materials/ if its the beginning of the string for consistency
if (vmtline.EndsWith(".vmt") || vmtline.EndsWith(".vtf")) // removes extentions if present for consistency
vmtline = vmtline.Substring(0, vmtline.Length - 4);
return vmtline;
}

public static List<string> findSoundscapeSounds(string fullpath)
{
// finds audio files from soundscape file
Expand Down Expand Up @@ -721,11 +770,12 @@ public static void findBspUtilityFiles(BSP bsp, List<string> sourceDirectories,
foreach (FileInfo f in dir.GetFiles(searchPattern))
{
// particle files if particle manifest is not being generated
if (!genparticlemanifest)
if (f.Name.StartsWith(name + "_particles") || f.Name.StartsWith(name + "_manifest"))
bsp.particleManifest =
new KeyValuePair<string, string>(internalDir + f.Name, externalDir + f.Name);

if (f.Name.StartsWith(name + "_particles") || f.Name.StartsWith(name + "_manifest"))
{
if(!genparticlemanifest)
bsp.particleManifest = new KeyValuePair<string, string>(internalDir + f.Name, externalDir + f.Name);
continue;
}
// soundscript
if (f.Name.StartsWith(name + "_level_sounds"))
bsp.soundscript =
Expand Down
45 changes: 28 additions & 17 deletions CompilePalX/Compilers/BSPPack/BSP.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -18,6 +19,8 @@ class BSP

public List<Dictionary<string, string>> entityList { get; private set; }

public List<List<Tuple<string, string>>> entityListArrayForm { get; private set; }

public List<int>[] modelSkinList { get; private set; }

public List<string> ModelList { get; private set; }
Expand Down Expand Up @@ -113,6 +116,7 @@ public BSP(FileInfo file)
public void buildEntityList()
{
entityList = new List<Dictionary<string, string>>();
entityListArrayForm = new List<List<Tuple<string, string>>>();

bsp.Seek(offsets[0].Key, SeekOrigin.Begin);
byte[] ent = reader.ReadBytes(offsets[0].Value);
Expand Down Expand Up @@ -144,6 +148,7 @@ public void buildEntityList()

string rawent = Encoding.ASCII.GetString(ents.ToArray());
Dictionary<string, string> entity = new Dictionary<string, string>();
var entityArrayFormat = new List<Tuple<string, string>>();
// split on \n, ignore \n inside of quotes
foreach (string s in Regex.Split(rawent, "(?=(?:(?:[^\"]*\"){2})*[^\"]*$)\\n"))
{
Expand All @@ -152,9 +157,11 @@ public void buildEntityList()
string[] c = s.Split('"');
if (!entity.ContainsKey(c[1]))
entity.Add(c[1], c[3]);
entityArrayFormat.Add(Tuple.Create(c[1], c[3]));
}
}
entityList.Add(entity);
entityListArrayForm.Add(entityArrayFormat);
ents = new List<byte>();
}
}
Expand Down Expand Up @@ -264,22 +271,6 @@ public void buildEntTextureList()
}
}

// pack IO triggered screen_overlay materials
var screenOverlays = ent.Values.Where((e) => e.Contains("r_screenoverlay"));
if (screenOverlays.Any())
{
foreach (var screenOverlay in screenOverlays)
{
var overlay = screenOverlay.Split(',')
.Where((e) => e.Contains("r_screenoverlay"))
.Select((e) => e.Replace("r_screenoverlay ", ""))
.FirstOrDefault();

if (overlay != null)
materials.Add(overlay);
}
}

// format and add materials
foreach (string material in materials)
{
Expand All @@ -290,6 +281,26 @@ public void buildEntTextureList()
EntTextureList.Add("materials/" + materialpath);
}
}

// get all overlay mats
var uniqueMats = new HashSet<string>();
foreach (var ent in entityListArrayForm)
{
foreach(var kv in ent)
{
var match = Regex.Match(kv.Item2, @"r_screenoverlay ([^,]+),");
if(match.Success)
{
uniqueMats.Add(match.Groups[1].Value);
}
}
}

foreach(var mat in uniqueMats)
{
var path = string.Format("materials/{0}.vmt", mat);
EntTextureList.Add(path);
}
}

public void buildModelList()
Expand Down
7 changes: 5 additions & 2 deletions CompilePalX/Compilers/Utility/ParticleUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,12 @@ public ParticleManifest (List<string> sourceDirectories, List<string> ignoreDire

sw.WriteLine("}");
}

string internalDirectory = filepath.Replace(baseDirectory, "");

string internalDirectory = filepath;
if(filepath.ToLower().StartsWith(baseDirectory.ToLower()))
{
internalDirectory = filepath.Substring(baseDirectory.Length);
}
//Store internal/external dir so it can be packed
particleManifest = new KeyValuePair<string, string>(internalDirectory, filepath);
}
Expand Down

0 comments on commit 0c99292

Please sign in to comment.