Skip to content

Commit

Permalink
Merge pull request sabresaurus#119 from Henry00IS/VmfImprovement
Browse files Browse the repository at this point in the history
Valve map format importer improvements.
  • Loading branch information
Henry00IS authored May 20, 2018
2 parents 1cd8ba8 + 7aa3882 commit b5ab002
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Scripts/Editor/Inspectors/CSGModelInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ public override void OnInspectorGUI()
catch (Exception ex)
{
EditorUtility.ClearProgressBar();
EditorUtility.DisplayDialog("Source Engine Map Import", "An exception occurred while importing the map:\r\n" + ex.Message, "Ohno!");
EditorUtility.DisplayDialog("Source Engine Map Import", "An exception occurred while importing the map:\r\n" + ex.Message + ex.StackTrace, "Ohno!");
}
}

if (GUILayout.Button("?", GUILayout.Width(16)))
{
EditorUtility.DisplayDialog("Source Engine 2006 Importer", "This importer is an incomplete proof of concept! While it has internal data structures with UV and texture details none of it is currently used by SabreCSG.\n\nKnown Issues:\n* NODRAW solid sides are not excluded.\n* No imported UV data is applied on the brushes.\n* There is no automatic material search.", "Okay");
EditorUtility.DisplayDialog("Source Engine 2006 Importer", "This importer was created using Source SDK maps and Hammer 4.1.\n\nImportant Notes:\n* It will try to find the materials in your project automatically. First it looks for the full name with forward slashes '/' replaced by periods '.' like 'BRICK.BRICKFLOOR001A' then the last word 'BRICKFLOOR001A'. The latter option could cause some false positives, try creating a material with the full name if this happens.\n\nKnown Issues:\n* Some UVs' panning appears to be completely off but I have been unable to replicate it. If anyone knows why it happens please do contact me!", "Okay");
}
EditorGUILayout.EndHorizontal();

Expand Down
36 changes: 36 additions & 0 deletions Scripts/Importers/ValveMapFormat2006/VmfEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#if UNITY_EDITOR || RUNTIME_CSG

using System;
using System.Collections.Generic;

namespace Sabresaurus.SabreCSG.Importers.ValveMapFormat2006
{
/// <summary>
/// Represents a Hammer Entity.
/// </summary>
public class VmfEntity
{
public int Id = -1;

/// <summary>
/// The class name of the entity.
/// </summary>
public string ClassName;

/// <summary>
/// The solids in the entity if available.
/// </summary>
public List<VmfSolid> Solids = new List<VmfSolid>();

/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString()
{
return "VmfEntity " + ClassName + " " + Id + " (" + Solids.Count + " Solids)";
}
}
}

#endif
13 changes: 13 additions & 0 deletions Scripts/Importers/ValveMapFormat2006/VmfEntity.cs.meta

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

83 changes: 80 additions & 3 deletions Scripts/Importers/ValveMapFormat2006/VmfImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public VmfWorld Import(string path)
object value;
VmfSolid solid = null;
VmfSolidSide solidSide = null;
VmfEntity entity = null;
while (!reader.EndOfStream)
{
line = reader.ReadLine().Trim();
Expand Down Expand Up @@ -99,7 +100,7 @@ public VmfWorld Import(string path)
}
}

// parse solid.
// parse world solid.
if (closures[0] == "world" && closures[1] == "solid" && closures[2] == null)
{
// create a new solid and add it to the world.
Expand All @@ -119,7 +120,7 @@ public VmfWorld Import(string path)
}
}

// parse solid side.
// parse world solid side.
if (closures[0] == "world" && closures[1] == "solid" && closures[2] == "side" && closures[3] == null)
{
// create a new solid side and add it to the solid.
Expand All @@ -137,7 +138,7 @@ public VmfWorld Import(string path)
case "id": solidSide.Id = (int)value; break;
case "plane": solidSide.Plane = (VmfPlane)value; break;
case "material": solidSide.Material = (string)value; break;
case "rotation": solidSide.Rotation = (int)value; break;
//case "rotation": solidSide.Rotation = (float)value; break;
case "uaxis": solidSide.UAxis = (VmfAxis)value; break;
case "vaxis": solidSide.VAxis = (VmfAxis)value; break;
case "lightmapscale": solidSide.LightmapScale = (int)value; break;
Expand All @@ -152,6 +153,74 @@ public VmfWorld Import(string path)
solidSide.HasDisplacement = true;
}

// parse entity.
if (closures[0] == "entity" && closures[1] == null)
{
// create a new entity and add it to the world.
if (justEnteredClosure)
{
entity = new VmfEntity();
world.Entities.Add(entity);
}

// parse entity properties.
if (TryParsekeyValue(line, out key, out value))
{
switch (key)
{
case "id": entity.Id = (int)value; break;
case "classname": entity.ClassName = (string)value; break;
}
}
}

// parse entity solid.
if (closures[0] == "entity" && closures[1] == "solid" && closures[2] == null)
{
// create a new solid and add it to the entity.
if (justEnteredClosure)
{
solid = new VmfSolid();
entity.Solids.Add(solid);
}

// parse solid properties.
if (TryParsekeyValue(line, out key, out value))
{
switch (key)
{
case "id": solid.Id = (int)value; break;
}
}
}

// parse entity solid side.
if (closures[0] == "entity" && closures[1] == "solid" && closures[2] == "side" && closures[3] == null)
{
// create a new solid side and add it to the solid.
if (justEnteredClosure)
{
solidSide = new VmfSolidSide();
solid.Sides.Add(solidSide);
}

// parse solid side properties.
if (TryParsekeyValue(line, out key, out value))
{
switch (key)
{
case "id": solidSide.Id = (int)value; break;
case "plane": solidSide.Plane = (VmfPlane)value; break;
case "material": solidSide.Material = (string)value; break;
//case "rotation": solidSide.Rotation = (float)value; break;
case "uaxis": solidSide.UAxis = (VmfAxis)value; break;
case "vaxis": solidSide.VAxis = (VmfAxis)value; break;
case "lightmapscale": solidSide.LightmapScale = (int)value; break;
case "smoothing_groups": solidSide.SmoothingGroups = (int)value; break;
}
}
}

previousLine = line;
justEnteredClosure = false;
}
Expand All @@ -177,8 +246,10 @@ private bool TryParsekeyValue(string line, out string key, out object value)

key = line.Substring(1, idx - 1);
string rawvalue = line.Substring(idx + 3, line.Length - idx - 4);
if (rawvalue.Length == 0) return false;

int vi;
float vf;
// detect plane definition.
if (rawvalue[0] == '(')
{
Expand All @@ -196,6 +267,12 @@ private bool TryParsekeyValue(string line, out string key, out object value)
value = new VmfAxis(new VmfVector3(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2])), float.Parse(values[3]), float.Parse(values[4]));
return true;
}
// detect floating point value.
else if (rawvalue.Contains('.') && float.TryParse(rawvalue, out vf))
{
value = vf;
return true;
}
// detect integer value.
else if (Int32.TryParse(rawvalue, out vi))
{
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Importers/ValveMapFormat2006/VmfSolidSide.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class VmfSolidSide
public int Id = -1;
public VmfPlane Plane;
public string Material;
public int Rotation;
public float Rotation;
public VmfAxis UAxis;
public VmfAxis VAxis;
public int LightmapScale;
Expand Down
5 changes: 5 additions & 0 deletions Scripts/Importers/ValveMapFormat2006/VmfWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class VmfWorld
/// The solids in the world.
/// </summary>
public List<VmfSolid> Solids = new List<VmfSolid>();

/// <summary>
/// The entities in the world.
/// </summary>
public List<VmfEntity> Entities = new List<VmfEntity>();
}
}

Expand Down
Loading

0 comments on commit b5ab002

Please sign in to comment.