Skip to content
This repository has been archived by the owner on Sep 16, 2019. It is now read-only.

Commit

Permalink
Add texture blacklist and whitelist, and add error handling for inval…
Browse files Browse the repository at this point in the history
…id WAD files
  • Loading branch information
LogicAndTrick committed May 26, 2014
1 parent 8a15b3f commit 0bd4641
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Sledge.Editor/Documents/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Document(string mapFile, Map map, Game game)
GameData.MapSizeHigh = game.OverrideMapSizeHigh;
}

TextureCollection = TextureProvider.CreateCollection(Environment.GetGameDirectories(), Game.AdditionalPackages);
TextureCollection = TextureProvider.CreateCollection(Environment.GetGameDirectories(), Game.AdditionalPackages, Game.GetTextureBlacklist(), Game.GetTextureWhitelist());

var texList = Map.GetAllTextures();
var items = TextureCollection.GetItems(texList);
Expand Down
147 changes: 109 additions & 38 deletions Sledge.Editor/Settings/SettingsForm.Designer.cs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Sledge.Editor/Settings/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ private void BindConfigControls()
SelectedGameDefaultBrushEnt.SelectedIndexChanged += (s, e) => CheckNull(_selectedGame, x => x.DefaultBrushEntity = SelectedGameDefaultBrushEnt.Text);
SelectedGameTextureScale.ValueChanged += (s, e) => CheckNull(_selectedGame, x => x.DefaultTextureScale = SelectedGameTextureScale.Value);
SelectedGameLightmapScale.ValueChanged += (s, e) => CheckNull(_selectedGame, x => x.DefaultLightmapScale = SelectedGameLightmapScale.Value);
SelectedGameBlacklistTextbox.TextChanged += (s, e) => CheckNull(_selectedGame, x => x.PackageBlacklist = SelectedGameBlacklistTextbox.Text.Trim());
SelectedGameWhitelistTextbox.TextChanged += (s, e) => CheckNull(_selectedGame, x => x.PackageWhitelist = SelectedGameWhitelistTextbox.Text.Trim());
SelectedGameIncludeFgdDirectoriesInEnvironment.CheckedChanged += (s, e) => CheckNull(_selectedGame, x => x.IncludeFgdDirectoriesInEnvironment = SelectedGameIncludeFgdDirectoriesInEnvironment.Checked);
SelectedGameOverrideMapSize.CheckedChanged += (s, e) => CheckNull(_selectedGame, x => x.OverrideMapSize = SelectedGameOverrideMapSize.Checked);
var sizes = new[] {4096, 8192, 16384, 32768, 65536};
Expand Down Expand Up @@ -1059,6 +1061,8 @@ private void UpdateSelectedGame()
SelectedGameDefaultBrushEnt.SelectedText = _selectedGame.DefaultBrushEntity;
SelectedGameTextureScale.Value = _selectedGame.DefaultTextureScale;
SelectedGameLightmapScale.Value = _selectedGame.DefaultLightmapScale;
SelectedGameBlacklistTextbox.Text = _selectedGame.PackageBlacklist ?? "";
SelectedGameWhitelistTextbox.Text = _selectedGame.PackageWhitelist ?? "";
SelectedGameIncludeFgdDirectoriesInEnvironment.Checked = _selectedGame.IncludeFgdDirectoriesInEnvironment;
SelectedGameOverrideMapSize.Checked = _selectedGame.OverrideMapSize;
var sizes = new[] { 4096, 8192, 16384, 32768, 65536 };
Expand Down
29 changes: 24 additions & 5 deletions Sledge.Editor/Settings/SettingsForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,33 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="label45.Text" xml:space="preserve">
<value>Only add entries in this section if you want to load textures that do not exist on your game path. They will not appear in-game unless you include them in the BSP using the compile tools.
<metadata name="HelpTooltip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<data name="label46.ToolTip" xml:space="preserve">
<value>Type the names of all the packages or folders you
want to include in the texture list. If packages are
specified in this list, no other packages will be loaded.
Put each package name on a line. The name must match
exactly: "test" will only match "test.wad", not "test1.wad".

If this list is not blank, all other texture packages will be ignored!</value>
</data>
<data name="lblGameWAD.ToolTip" xml:space="preserve">
<value>Only add entries in this section if you want to load textures that
do not exist on your game path. They will not appear in-game
unless you include them in the BSP using the compile tools.

For Goldsource: Add WAD files or a folder containing WAD files (subfolders are not scanned). Auto-detection of wads is automatically performed when saving your map.
For Goldsource: Add WAD files or a folder containing WAD files
(subfolders are not scanned). Auto-detection of wads is
automatically performed when saving your map.

For Source: Add a folder containing VMT/VTF files (optionally, in a subfolder called "materials"). All subfolders are scanned. The VMT and VTF files must be in their correct relative locations within the folder, or they will not be loaded.</value>
For Source: Add a folder containing VMT/VTF files (optionally,
in a subfolder called "materials"). All subfolders are scanned.
The VMT and VTF files must be in their correct relative locations
within the folder, or they will not be loaded.</value>
</data>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>36</value>
<value>66</value>
</metadata>
</root>
2 changes: 1 addition & 1 deletion Sledge.Editor/Tools/TextureApplicationForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Sledge.Packages/Wad/WadPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private void ReadTextureEntries(BinaryReader br)
var name = br.ReadFixedLengthString(Encoding.ASCII, 16).ToLowerInvariant();

if (!validTypes.Contains(type)) continue; // Skip unsupported types
if (Entries.Any(x => x.Name == name)) continue; // Don't add duplicates
Entries.Add(new WadEntry(this, name, (WadEntryType) type, offset, compressionType, compressedLength, fullLength));
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sledge.Providers/Texture/SprProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ public override void LoadTextures(IEnumerable<TextureItem> items)
}
}

public override IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages)
public override IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages, IEnumerable<string> blacklist, IEnumerable<string> whitelist)
{
// Sprite provider ignores the black/whitelists
var dirs = sourceRoots.Union(additionalPackages).Where(Directory.Exists).Select(Path.GetFullPath).Select(x => x.ToLowerInvariant()).Distinct().ToList();
var tp = new TexturePackage(String.Join(";", dirs), "sprites", this) {IsBrowsable = false};
foreach (var dir in dirs)
Expand Down
8 changes: 5 additions & 3 deletions Sledge.Providers/Texture/TextureProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ public static void Deregister(TextureProvider provider)
#endregion

protected string CachePath { get; private set; }
public abstract IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages);
public abstract IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages, IEnumerable<string> blacklist, IEnumerable<string> whitelist);
public abstract void DeletePackages(IEnumerable<TexturePackage> packages);
public abstract void LoadTextures(IEnumerable<TextureItem> items);
public abstract ITextureStreamSource GetStreamSource(int maxWidth, int maxHeight, IEnumerable<TexturePackage> packages);

public static TextureCollection CreateCollection(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages = null)
public static TextureCollection CreateCollection(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages, IEnumerable<string> blacklist, IEnumerable<string> whitelist)
{
var list = sourceRoots.ToList();
var additional = additionalPackages == null ? new List<string>() : additionalPackages.ToList();
var wl = whitelist == null ? new List<string>() : whitelist.ToList();
var bl = blacklist == null ? new List<string>() : blacklist.ToList();
var pkgs = new List<TexturePackage>();
foreach (var provider in RegisteredProviders)
{
pkgs.AddRange(provider.CreatePackages(list, additional));
pkgs.AddRange(provider.CreatePackages(list, additional, bl, wl));
}
var tc = new TextureCollection(pkgs);
Packages.AddRange(pkgs);
Expand Down
16 changes: 14 additions & 2 deletions Sledge.Providers/Texture/VmtProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ namespace Sledge.Providers.Texture
{
public class VmtProvider : TextureProvider
{
private Dictionary<TexturePackage, QuickRoot> _roots = new Dictionary<TexturePackage, QuickRoot>();
private readonly Dictionary<TexturePackage, QuickRoot> _roots = new Dictionary<TexturePackage, QuickRoot>();

public override IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages)
public override IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages, IEnumerable<string> blacklist, IEnumerable<string> whitelist)
{
var blist = blacklist.Select(x => x.TrimEnd('/', '\\')).Where(x => !String.IsNullOrWhiteSpace(x)).Select(x => x + '/').ToList();
var wlist = whitelist.Select(x => x.TrimEnd('/', '\\')).Where(x => !String.IsNullOrWhiteSpace(x)).Select(x => x + '/').ToList();

var roots = sourceRoots.ToList();
var packages = new Dictionary<string, TexturePackage>();

Expand Down Expand Up @@ -62,6 +65,15 @@ public override IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> s
packages[dir].AddTexture(new TextureItem(packages[dir], vmt, baseTexture, size.Width, size.Height));
}

foreach (var key in packages.Keys.ToList())
{
if ((blist.Any(x => x.StartsWith(key, StringComparison.InvariantCultureIgnoreCase))) ||
(wlist.Any() && !wlist.Any(x => x.StartsWith(key, StringComparison.InvariantCultureIgnoreCase))))
{
packages.Remove(key);
}
}

vmtRoot.Dispose();

foreach (var tp in packages.Values)
Expand Down
42 changes: 28 additions & 14 deletions Sledge.Providers/Texture/WadProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,33 +155,47 @@ private void SaveToCache(TexturePackage package)

private TexturePackage CreatePackage(string package)
{
if (!File.Exists(package)) return null;
try
{
if (!File.Exists(package)) return null;

var tp = new TexturePackage(package, Path.GetFileNameWithoutExtension(package), this);
if (LoadFromCache(tp)) return tp;
var tp = new TexturePackage(package, Path.GetFileNameWithoutExtension(package), this);
if (LoadFromCache(tp)) return tp;

var list = new List<TextureItem>();
using (var pack = new WadPackage(new FileInfo(package)))
{
list.AddRange(pack.GetEntries().OfType<WadEntry>().Select(x => new TextureItem(tp, x.Name, (int) x.Width, (int) x.Height)));
var list = new List<TextureItem>();
using (var pack = new WadPackage(new FileInfo(package)))
{
list.AddRange(pack.GetEntries().OfType<WadEntry>().Select(x => new TextureItem(tp, x.Name, (int) x.Width, (int) x.Height)));
}
foreach (var ti in list)
{
tp.AddTexture(ti);
}
SaveToCache(tp);
return tp;
}
foreach (var ti in list)
catch
{
tp.AddTexture(ti);
return null;
}
SaveToCache(tp);
return tp;
}

public override IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages)
public override IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots, IEnumerable<string> additionalPackages, IEnumerable<string> blacklist, IEnumerable<string> whitelist)
{
var blist = blacklist.Select(x => x.EndsWith(".wad") ? x.Substring(0, x.Length - 4) : x).Where(x => !String.IsNullOrWhiteSpace(x)).ToList();
var wlist = whitelist.Select(x => x.EndsWith(".wad") ? x.Substring(0, x.Length - 4) : x).Where(x => !String.IsNullOrWhiteSpace(x)).ToList();
var wads = sourceRoots.Union(additionalPackages)
.Where(Directory.Exists)
.SelectMany(x => Directory.GetFiles(x, "*.wad", SearchOption.TopDirectoryOnly))
.Union(additionalPackages.Where(x => x.EndsWith(".wad") && File.Exists(x)))
.GroupBy(Path.GetFileNameWithoutExtension)
.Select(x => x.First());
return wads.AsParallel().Select(CreatePackage);
.Select(x => x.First())
.Where(x => !blist.Any(b => String.Equals(Path.GetFileNameWithoutExtension(x) ?? x, b, StringComparison.InvariantCultureIgnoreCase)));
if (wlist.Any())
{
wads = wads.Where(x => wlist.Contains(Path.GetFileNameWithoutExtension(x) ?? x, StringComparer.InvariantCultureIgnoreCase));
}
return wads.AsParallel().Select(CreatePackage).Where(x => x != null);
}

public override void DeletePackages(IEnumerable<TexturePackage> packages)
Expand Down
25 changes: 25 additions & 0 deletions Sledge.Settings/Models/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class Game

public List<Fgd> Fgds { get; set; }
public List<string> AdditionalPackages { get; set; }
public string PackageBlacklist { get; set; }
public string PackageWhitelist { get; set; }

public Game()
{
Expand Down Expand Up @@ -89,6 +91,9 @@ public void Read(GenericStructure gs)
}
}

PackageBlacklist = gs["PackageBlacklist"] ?? "";
PackageWhitelist = gs["PackageWhitelist"] ?? "";

var fgds = gs.Children.FirstOrDefault(x => x.Name == "Fgds");
if (fgds != null)
{
Expand Down Expand Up @@ -138,6 +143,9 @@ public void Write(GenericStructure gs)
}
gs.Children.Add(additional);

gs["PackageBlacklist"] = PackageBlacklist ?? "";
gs["PackageWhitelist"] = PackageWhitelist ?? "";

var fgds = new GenericStructure("Fgds");
i = 1;
foreach (var fgd in Fgds)
Expand Down Expand Up @@ -194,6 +202,23 @@ public string GetGameLaunchArgument()
}
}

public IEnumerable<string> GetTextureBlacklist()
{
var bl = new List<string>();
if (Engine == Engine.Goldsource)
{
bl.Add("cached");
bl.Add("gfx");
}
bl.AddRange((PackageBlacklist ?? "").Trim().Split('\n').Select(x => x.Trim()).Where(x => !String.IsNullOrWhiteSpace(x)));
return bl;
}

public IEnumerable<string> GetTextureWhitelist()
{
return (PackageWhitelist ?? "").Trim().Split('\n').Select(x => x.Trim()).Where(x => !String.IsNullOrWhiteSpace(x));
}

private int GetSteamAppId()
{
if (Engine == Engine.Goldsource)
Expand Down
2 changes: 1 addition & 1 deletion Sledge.Tests/Vtf/VtfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void VpkVtfCollectionTest()
var collection = TextureProvider.CreateCollection(new[]
{
@"F:\Steam\SteamApps\common\Team Fortress 2\tf"
});
}, null, null, null);
}

[TestMethod]
Expand Down

0 comments on commit 0bd4641

Please sign in to comment.