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

Commit

Permalink
Added the WAD provider back, with the new texture provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
LogicAndTrick committed May 18, 2014
1 parent acb7e17 commit 3279c89
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 140 deletions.
6 changes: 3 additions & 3 deletions Sledge.Editor/Documents/Document.cs
Expand Up @@ -113,11 +113,11 @@ public Document(string mapFile, Map map, Game game)
// new [] {new NativeFile(@"F:\Steam\SteamApps\common\Team Fortress 2\tf").TraversePath("materials/concrete")}
//));
//SpriteCollection = TextureProvider.CreateCollection(Environment.GetEditorRoot().GetChildren("sprites"));
//TextureCollection = TextureProvider.CreateCollection(Environment.GetGameDirectories());
TextureCollection = TextureProvider.CreateCollection(new[]
TextureCollection = TextureProvider.CreateCollection(Environment.GetGameDirectories());
/*TextureCollection = TextureProvider.CreateCollection(new[]
{
@"F:\Steam\SteamApps\common\Half-life 2\hl2"
});
});*/
SpriteCollection = new TextureCollection(new List<TexturePackage>());

var texList = Map.GetAllTextures();
Expand Down
7 changes: 3 additions & 4 deletions Sledge.Editor/Editor.cs
Expand Up @@ -152,13 +152,12 @@ private void EditorLoad(object sender, EventArgs e)
MapProvider.Register(new VmfProvider());
MapProvider.Register(new ObjProvider());
GameDataProvider.Register(new FgdProvider());
//TextureProvider.Register(new WadProvider());
TextureProvider.Register(new WadProvider());
//TextureProvider.Register(new SprProvider());
//TextureProvider.Register(new VtfProvider());
TextureProvider.Register(new VmtProvider());
ModelProvider.Register(new MdlProvider());

//WadProvider.ReplaceTransparentPixels = !Sledge.Settings.View.DisableWadTransparency && !Sledge.Settings.View.GloballyDisableTransparency;
WadProvider.ReplaceTransparentPixels = !Sledge.Settings.View.DisableWadTransparency && !Sledge.Settings.View.GloballyDisableTransparency;
TextureHelper.EnableTransparency = !Sledge.Settings.View.GloballyDisableTransparency;
TextureHelper.DisableTextureFiltering = Sledge.Settings.View.DisableTextureFiltering;
TextureHelper.ForceNonPowerOfTwoResize = Sledge.Settings.View.ForcePowerOfTwoTextureResizing;
Expand Down Expand Up @@ -481,7 +480,7 @@ private static void SettingsChanged()
vp.Camera.ClipDistance = Sledge.Settings.View.BackClippingPane;
}
ViewportManager.RefreshClearColour();
//WadProvider.ReplaceTransparentPixels = !Sledge.Settings.View.DisableWadTransparency && !Sledge.Settings.View.GloballyDisableTransparency;
WadProvider.ReplaceTransparentPixels = !Sledge.Settings.View.DisableWadTransparency && !Sledge.Settings.View.GloballyDisableTransparency;
TextureHelper.EnableTransparency = !Sledge.Settings.View.GloballyDisableTransparency;
TextureHelper.DisableTextureFiltering = Sledge.Settings.View.DisableTextureFiltering;
TextureHelper.ForceNonPowerOfTwoResize = Sledge.Settings.View.ForcePowerOfTwoTextureResizing;
Expand Down
8 changes: 7 additions & 1 deletion Sledge.Packages/Wad/WadPackage.cs
Expand Up @@ -35,10 +35,16 @@ public WadPackage(FileInfo packageFile)
// Read all the entries from the wad
ReadTextureEntries(br);
SetAdditionalEntryData(br);
RemoveInvalidEntries();
BuildDirectories();
}
}


private void RemoveInvalidEntries()
{
Entries.RemoveAll(e => (e.PaletteDataOffset + e.PaletteSize * 3) - e.Offset > e.Length);
}

internal Stream OpenFile(FileInfo file)
{
return Stream.Synchronized(new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, FileOptions.RandomAccess));
Expand Down
1 change: 0 additions & 1 deletion Sledge.Providers/Sledge.Providers.csproj
Expand Up @@ -70,7 +70,6 @@
<Compile Include="Texture\TextureProvider.cs" />
<Compile Include="Texture\ITextureStreamSource.cs" />
<Compile Include="Texture\TextureSubItem.cs" />
<Compile Include="Texture\VtfProvider.cs" />
<Compile Include="Texture\Vtf\VtfFile.cs" />
<Compile Include="Texture\WadProvider.cs" />
</ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions Sledge.Providers/Texture/VmtProvider.cs
Expand Up @@ -171,8 +171,9 @@ public QuickRoot(IEnumerable<string> roots, string baseFolder, string extension)
_streams = streams.Select(x => x.Stream).ToList();
_files = _streams
.SelectMany(x => x.SearchFiles(baseFolder, "\\" + extension + "$", true))
.Union(_roots.SelectMany(x => Directory.GetFiles(Path.Combine(x, baseFolder), "*" + extension, SearchOption.AllDirectories)
.Select(f => MakeRelative(x, f))))
.Union(_roots.Where(x => Directory.Exists(Path.Combine(x, baseFolder)))
.SelectMany(x => Directory.GetFiles(Path.Combine(x, baseFolder), "*" + extension, SearchOption.AllDirectories)
.Select(f => MakeRelative(x, f))))
.GroupBy(x => x)
.Select(x => StripBase(x.First()))
.ToList();
Expand Down
105 changes: 0 additions & 105 deletions Sledge.Providers/Texture/VtfProvider.cs

This file was deleted.

44 changes: 21 additions & 23 deletions Sledge.Providers/Texture/WadProvider.cs
Expand Up @@ -4,13 +4,11 @@
using System.Globalization;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using Sledge.FileSystem;
using Sledge.Graphics.Helpers;
using System.Drawing;
using Sledge.Packages;
using Sledge.Packages.Wad;
/*

namespace Sledge.Providers.Texture
{
public class WadProvider : TextureProvider
Expand Down Expand Up @@ -38,7 +36,7 @@ private static Bitmap PostProcessBitmap(string packageName, string name, Bitmap
blueTransparency = true;
break;
}
}* /
}*/

// Can't be clever and detect the transparency type automatically - Goldsource is too unpredictable
var blueTransparency = packageName.IndexOf("decal", StringComparison.CurrentCultureIgnoreCase) < 0;
Expand Down Expand Up @@ -80,7 +78,7 @@ private class WadStreamSource : ITextureStreamSource

public WadStreamSource(IEnumerable<TexturePackage> packages)
{
_packages = packages.Select(x => new WadPackage(new FileInfo(x.PackageFile.FullPathName))).ToList();
_packages = packages.Select(x => new WadPackage(new FileInfo(x.PackageRoot))).ToList();
_streams = _packages.Select(x => x.GetStreamSource()).ToList();
}

Expand All @@ -94,7 +92,7 @@ public Bitmap GetImage(TextureItem item)
using (var stream = _streams.First(x => x.HasFile(item.Name)).OpenFile(item.Name))
{
bool hasTransparency;
return PostProcessBitmap(item.Package.PackageFile.NameWithoutExtension, item.Name, new Bitmap(stream), out hasTransparency);
return PostProcessBitmap(item.Package.PackageRelativePath, item.Name, new Bitmap(stream), out hasTransparency);
}
}

Expand All @@ -105,18 +103,13 @@ public void Dispose()
}
}

public override bool IsValidForPackageFile(IFile package)
{
return package.FullPathName.EndsWith(".wad", true, CultureInfo.InvariantCulture) && File.Exists(package.FullPathName);
}
private const char NullCharacter = (char) 0;

private bool LoadFromCache(TexturePackage package)
{
if (CachePath == null || !Directory.Exists(CachePath)) return false;

var fi = new FileInfo(package.PackageFile.FullPathName);
var fi = new FileInfo(package.PackageRoot);
var cacheFile = Path.Combine(CachePath, fi.Name + "_" + (fi.LastWriteTime.Ticks));
if (!File.Exists(cacheFile)) return false;

Expand Down Expand Up @@ -147,7 +140,7 @@ private bool LoadFromCache(TexturePackage package)
private void SaveToCache(TexturePackage package)
{
if (CachePath == null || !Directory.Exists(CachePath)) return;
var fi = new FileInfo(package.PackageFile.FullPathName);
var fi = new FileInfo(package.PackageRoot);
var cacheFile = Path.Combine(CachePath, fi.Name + "_" + (fi.LastWriteTime.Ticks));
var lines = new List<string>();
lines.Add(fi.FullName);
Expand All @@ -160,15 +153,15 @@ private void SaveToCache(TexturePackage package)
File.WriteAllLines(cacheFile, lines);
}

public override TexturePackage CreatePackage(IFile package)
private TexturePackage CreatePackage(string package)
{
if (!File.Exists(package.FullPathName)) throw new ProviderException("The WAD Provider only supports the native file system.");
if (!File.Exists(package)) return null;

var tp = new TexturePackage(package, this);
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.FullPathName)))
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)));
}
Expand All @@ -180,16 +173,22 @@ public override TexturePackage CreatePackage(IFile package)
return tp;
}

public override void LoadTexture(TextureItem item)
public override IEnumerable<TexturePackage> CreatePackages(IEnumerable<string> sourceRoots)
{
LoadTextures(new[] {item});
var wads = sourceRoots.Where(Directory.Exists).SelectMany(x => Directory.GetFiles(x, "*.wad", SearchOption.TopDirectoryOnly));
return wads.AsParallel().Select(CreatePackage).Where(x => x != null);
}

public override void DeletePackages(IEnumerable<TexturePackage> packages)
{

}

public override void LoadTextures(IEnumerable<TextureItem> items)
{
var list = items.ToList();
var packages = list.Select(x => x.Package).Distinct().ToList();
var packs = packages.Select(x => new WadPackage(new FileInfo(x.PackageFile.FullPathName))).ToList();
var packs = packages.Select(x => new WadPackage(new FileInfo(x.PackageRoot))).ToList();
var streams = packs.Select(x => x.GetStreamSource()).ToList();

// Process the bitmaps in parallel
Expand All @@ -201,7 +200,7 @@ public override void LoadTextures(IEnumerable<TextureItem> items)
if (open == null) continue;
var bmp = new Bitmap(open);
bool hasTransparency;
bmp = PostProcessBitmap(ti.Package.PackageFile.NameWithoutExtension, ti.Name, bmp, out hasTransparency);
bmp = PostProcessBitmap(ti.Package.PackageRelativePath, ti.Name, bmp, out hasTransparency);
open.Dispose();
return new
{
Expand All @@ -222,10 +221,9 @@ public override void LoadTextures(IEnumerable<TextureItem> items)
foreach (var pack in packs) pack.Dispose();
}

public override ITextureStreamSource GetStreamSource(IEnumerable<TexturePackage> packages)
public override ITextureStreamSource GetStreamSource(int maxWidth, int maxHeight, IEnumerable<TexturePackage> packages)
{
return new WadStreamSource(packages);
}
}
}
*/
2 changes: 1 addition & 1 deletion Sledge.Sandbox/QuickStartBootstrap.cs
Expand Up @@ -23,7 +23,7 @@ public static void Start()
MapProvider.Register(new RmfProvider());
MapProvider.Register(new VmfProvider());
GameDataProvider.Register(new FgdProvider());
//TextureProvider.Register(new WadProvider());
TextureProvider.Register(new WadProvider());

// var editor = new Editor.Editor();
// editor.Load += (sender, e) => PostStart(sender as Editor.Editor);
Expand Down

0 comments on commit 3279c89

Please sign in to comment.