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

Commit

Permalink
Tweak invalid solid numbers to closer match VHE
Browse files Browse the repository at this point in the history
Fixes #275 (maybe?)
  • Loading branch information
LogicAndTrick committed Oct 15, 2018
1 parent a9cb138 commit 70e55a7
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 33 deletions.
52 changes: 40 additions & 12 deletions Sledge.BspEditor/Documents/BspSourceDocumentLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public class BspSourceDocumentLoader : IDocumentLoader

/// <inheritdoc />
public string FileTypeDescription { get; set; }

public string UntitledDocumentName { get; set; }
public string UnsupportedFileFormat { get; set; }
public string FileTypeNotSupported { get; set; }
public string LoadResultTitle { get; set; }
public string InvalidObjectsWereDiscarded { get; set; }
public string OK { get; set; }

private static int _untitled = 1;

Expand Down Expand Up @@ -116,7 +120,7 @@ public async Task<IDocument> CreateBlank()
Name = string.Format(UntitledDocumentName, _untitled++),
HasUnsavedChanges = true
};
await ProcessAfterLoad(env, md);
await ProcessAfterLoad(env, md, new BspFileLoadResult { Map = md.Map });
return md;
}

Expand All @@ -133,9 +137,15 @@ public async Task<IDocument> Load(string location)
{
try
{
var map = await provider.Value.Load(stream, env);
var md = new MapDocument(map, env) { FileName = location };
await ProcessAfterLoad(env, md);
var result = await provider.Value.Load(stream, env);
if (result.Map == null)
{
stream.Seek(0, SeekOrigin.Begin);
continue;
}

var md = new MapDocument(result.Map, env) { FileName = location };
await ProcessAfterLoad(env, md, result);
return md;
}
catch (NotSupportedException e)
Expand All @@ -149,25 +159,37 @@ public async Task<IDocument> Load(string location)
if (ex != null)
{
// This file type is explicitly supported, but the provider rejected it.
MessageBox.Show(ex.Message, "Unsupported file format", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(ex.Message, UnsupportedFileFormat, MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
throw new NotSupportedException("This file type is not supported.");
throw new NotSupportedException(FileTypeNotSupported);
}

/// <summary>
/// Process a document after it has been loaded from a file.
/// </summary>
/// <param name="env">The document's environment</param>
/// <param name="document">The document to process</param>
private async Task ProcessAfterLoad(IEnvironment env, MapDocument document)
/// <param name="result">The result of the load operation</param>
private async Task ProcessAfterLoad(IEnvironment env, MapDocument document, BspFileLoadResult result)
{
await env.UpdateDocumentData(document);

foreach (var p in _processors.Select(x => x.Value).OrderBy(x => x.OrderHint))
{
await p.AfterLoad(document);
}

if (result.InvalidObjects.Any() || result.Messages.Any())
{
var messages = new List<string>();
if (result.InvalidObjects.Any()) messages.Add(String.Format(InvalidObjectsWereDiscarded, result.InvalidObjects.Count));
foreach (var m in result.Messages) messages.Add(m);
_shell.Value.InvokeSync(() =>
{
MessageBox.Show(String.Join(System.Environment.NewLine, messages), LoadResultTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
});
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -205,7 +227,7 @@ public async Task Save(IDocument document, string location)
}
}
}
throw new NotSupportedException("This file type is not supported.");
throw new NotSupportedException(FileTypeNotSupported);
}

/// <summary>
Expand Down Expand Up @@ -252,9 +274,15 @@ public async Task<IDocument> Load(DocumentPointer documentPointer)
{
try
{
var map = await provider.Value.Load(stream, env);
var md = new MapDocument(map, env) { FileName = fileName };
await ProcessAfterLoad(env, md);
var result = await provider.Value.Load(stream, env);
if (result.Map == null)
{
stream.Seek(0, SeekOrigin.Begin);
continue;
}

var md = new MapDocument(result.Map, env) { FileName = fileName };
await ProcessAfterLoad(env, md, result);
return md;
}
catch (NotSupportedException)
Expand Down
29 changes: 29 additions & 0 deletions Sledge.BspEditor/Providers/BspFileLoadResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using Sledge.BspEditor.Primitives;

namespace Sledge.BspEditor.Providers
{
public class BspFileLoadResult
{
/// <summary>
/// A list of messages to present to the user after loading the file
/// </summary>
public List<string> Messages { get; set; }

/// <summary>
/// A list of invalid objects that were present in the file
/// </summary>
public List<object> InvalidObjects { get; set; }

/// <summary>
/// The map that was loaded from the file. If the load wasn't successful, this should be null.
/// </summary>
public Map Map { get; set; }

public BspFileLoadResult()
{
Messages = new List<string>();
InvalidObjects = new List<object>();
}
}
}
3 changes: 2 additions & 1 deletion Sledge.BspEditor/Providers/IBspSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Sledge.BspEditor.Environment;
using Sledge.BspEditor.Primitives;
using Sledge.BspEditor.Primitives.MapObjects;
using Sledge.Common.Shell.Documents;

namespace Sledge.BspEditor.Providers
Expand Down Expand Up @@ -31,7 +32,7 @@ public interface IBspSourceProvider
/// <param name="stream">The stream</param>
/// <param name="environment">The environment to load the map into</param>
/// <returns>Completion task for the map</returns>
Task<Map> Load(Stream stream, IEnvironment environment);
Task<BspFileLoadResult> Load(Stream stream, IEnvironment environment);

/// <summary>
/// Save the map to a stream
Expand Down
28 changes: 15 additions & 13 deletions Sledge.BspEditor/Providers/MapBspSourceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -40,14 +39,16 @@ public class MapBspSourceProvider : IBspSourceProvider
new FileExtensionInfo("Quake map formats", ".map", ".max"),
};

public async Task<Map> Load(Stream stream, IEnvironment environment)
public async Task<BspFileLoadResult> Load(Stream stream, IEnvironment environment)
{
return await Task.Factory.StartNew(() =>
{
using (var reader = new StreamReader(stream, Encoding.ASCII, true, 1024, false))
{
var result = new BspFileLoadResult();

var map = new Map();
var entities = ReadAllEntities(reader, map.NumberGenerator);
var entities = ReadAllEntities(reader, map.NumberGenerator, result);

var worldspawn = entities.FirstOrDefault(x => x.EntityData?.Name == "worldspawn")
?? new Entity(0) { Data = { new EntityData { Name = "worldspawn" } } };
Expand All @@ -70,14 +71,16 @@ public async Task<Map> Load(Stream stream, IEnvironment environment)
}

map.Root.DescendantsChanged();
return map;

result.Map = map;
return result;
}
});
}

#region Reading

private string CleanLine(string line)
private static string CleanLine(string line)
{
if (line == null) return null;
var ret = line;
Expand Down Expand Up @@ -177,7 +180,7 @@ private Face ReadFace(string line, UniqueNumberGenerator generator)
return face;
}

private Solid ReadSolid(StreamReader rdr, UniqueNumberGenerator generator)
private Solid ReadSolid(StreamReader rdr, UniqueNumberGenerator generator, BspFileLoadResult result)
{
var faces = new List<Face>();
string line;
Expand All @@ -194,11 +197,10 @@ private Solid ReadSolid(StreamReader rdr, UniqueNumberGenerator generator)

foreach (var face in faces)
{
var pg = poly.Polygons.FirstOrDefault(x => x.Plane.Normal.EquivalentTo(face.Plane.Normal));
var pg = poly.Polygons.FirstOrDefault(x => x.Plane.Normal.EquivalentTo(face.Plane.Normal, 0.0075f)); // Magic number that seems to match VHE
if (pg == null)
{
// TODO: Report invalid solids
Debug.WriteLine("Invalid solid!");
result.InvalidObjects.Add(poly);
return null;
}
face.Vertices.AddRange(pg.Vertices);
Expand Down Expand Up @@ -257,7 +259,7 @@ private static void ReadProperty(Entity ent, string line)
}
}

private Entity ReadEntity(StreamReader rdr, UniqueNumberGenerator generator)
private Entity ReadEntity(StreamReader rdr, UniqueNumberGenerator generator, BspFileLoadResult result)
{
var ent = new Entity(generator.Next("Face"))
{
Expand All @@ -274,7 +276,7 @@ private Entity ReadEntity(StreamReader rdr, UniqueNumberGenerator generator)
if (line[0] == '"') ReadProperty(ent, line);
else if (line[0] == '{')
{
var s = ReadSolid(rdr, generator);
var s = ReadSolid(rdr, generator, result);
if (s != null) s.Hierarchy.Parent = ent;
}
else if (line[0] == '}') break;
Expand All @@ -283,14 +285,14 @@ private Entity ReadEntity(StreamReader rdr, UniqueNumberGenerator generator)
return ent;
}

private List<Entity> ReadAllEntities(StreamReader rdr, UniqueNumberGenerator generator)
private List<Entity> ReadAllEntities(StreamReader rdr, UniqueNumberGenerator generator, BspFileLoadResult result)
{
var list = new List<Entity>();
string line;
while ((line = CleanLine(rdr.ReadLine())) != null)
{
if (String.IsNullOrWhiteSpace(line)) continue;
if (line == "{") list.Add(ReadEntity(rdr, generator));
if (line == "{") list.Add(ReadEntity(rdr, generator, result));
}
return list;
}
Expand Down
8 changes: 6 additions & 2 deletions Sledge.BspEditor/Providers/NativeBspSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ public NativeBspSourceProvider([Import] Lazy<SerialisedObjectFormatter> formatte
new FileExtensionInfo("Sledge map format", ".smf"),
};

public async Task<Map> Load(Stream stream, IEnvironment environment)
public async Task<BspFileLoadResult> Load(Stream stream, IEnvironment environment)
{
return await Task.Factory.StartNew(() =>
{
var result = new BspFileLoadResult();

var map = new Map();
var so = _formatter.Deserialize(stream);
foreach (var o in so)
Expand All @@ -60,7 +62,9 @@ public async Task<Map> Load(Stream stream, IEnvironment environment)
}
}
map.Root.DescendantsChanged();
return map;

result.Map = map;
return result;
});
}

Expand Down
7 changes: 5 additions & 2 deletions Sledge.BspEditor/Providers/RmfBspSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public class RmfBspSourceProvider : IBspSourceProvider
new FileExtensionInfo("Worldcraft map formats", ".rmf", ".rmx"),
};

public async Task<Map> Load(Stream stream, IEnvironment environment)
public async Task<BspFileLoadResult> Load(Stream stream, IEnvironment environment)
{
return await Task.Factory.StartNew(() =>
{
using (var br = new BinaryReader(stream, Encoding.ASCII, true))
{
var result = new BspFileLoadResult();

// Only RMF version 2.2 is supported for the moment.
var version = Math.Round(br.ReadSingle(), 1);
if (Math.Abs(version - 2.2) > 0.01)
Expand Down Expand Up @@ -86,7 +88,8 @@ public async Task<Map> Load(Stream stream, IEnvironment environment)
ReadCameras(map, br);
}

return map;
result.Map = map;
return result;
}
});
}
Expand Down
9 changes: 7 additions & 2 deletions Sledge.BspEditor/Providers/VmfBspSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public VmfBspSourceProvider([Import] Lazy<SerialisedObjectFormatter> formatter,
new FileExtensionInfo("Valve map format", ".vmf", ".vmx"),
};

public async Task<Map> Load(Stream stream, IEnvironment environment)
public async Task<BspFileLoadResult> Load(Stream stream, IEnvironment environment)
{
var task = await Task.Factory.StartNew(async () =>
{
var result = new BspFileLoadResult();

var map = new Map();
var so = _formatter.Deserialize(stream).ToList();

Expand Down Expand Up @@ -92,8 +94,11 @@ public async Task<Map> Load(Stream stream, IEnvironment environment)
}

map.Root.DescendantsChanged();
return map;

result.Map = map;
return result;
});

return await task;
}

Expand Down
1 change: 1 addition & 0 deletions Sledge.BspEditor/Sledge.BspEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Providers\BspFileLoadResult.cs" />
<Compile Include="Providers\Processors\AddDefaults.cs" />
<Compile Include="Providers\Processors\HandleSelection.cs" />
<Compile Include="Providers\Processors\HandleVisgroups.cs" />
Expand Down
7 changes: 6 additions & 1 deletion Sledge.BspEditor/Translations/Sledge.BspEditor.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@
"Documents": {
"BspSourceDocumentLoader": {
"FileTypeDescription": "BSP Source File",
"UntitledDocumentName": "Untitled {0}"
"UntitledDocumentName": "Untitled {0}",
"UnsupportedFileFormat": "Unsupported file format",
"FileTypeNotSupported": "This file type is not supported.",
"LoadResultTitle": "Load completed with warnings",
"InvalidObjectsWereDiscarded": "{0} object(s) in the file were found to be invalid and were discarded.",
"OK": "OK"
}
},
"Environment": {
Expand Down

0 comments on commit 70e55a7

Please sign in to comment.