Skip to content

Commit

Permalink
Support JMF v122
Browse files Browse the repository at this point in the history
Closes #28
  • Loading branch information
LogicAndTrick committed Jan 20, 2024
1 parent 3d61142 commit 59e569a
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 17 deletions.
41 changes: 25 additions & 16 deletions Sledge.Formats.Map.Tests/Formats/TestJackhammerFormat.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
using System;
using System.IO;
using System.Linq;
using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sledge.Formats.Map.Formats;
using Sledge.Formats.Map.Objects;

namespace Sledge.Formats.Map.Tests.Formats
{
[TestClass]
public class TestJackhammerFormat
{
[TestMethod]
public void TestJmfFormatLoading()
public void TestJmf121()
{
using var file = typeof(TestJackhammerFormat).Assembly.GetManifestResourceStream("Sledge.Formats.Map.Tests.Resources.jmf.default-room-121.jmf");
var format = new JackhammerJmfFormat();
foreach (var file in Directory.GetFiles(@"D:\Downloads\formats\jmf", "1group.jmf"))
{
using (var r = File.OpenRead(file))
{
try
{
format.Read(r);
}
catch (Exception ex)
{
Assert.Fail($"Unable to read file: {Path.GetFileName(file)}. {ex.Message}");
}
}
}
var map = format.Read(file);
Assert.AreEqual("worldspawn", map.Worldspawn.ClassName);
}

[TestMethod]
public void TestJmf122()
{
using var file = typeof(TestJackhammerFormat).Assembly.GetManifestResourceStream("Sledge.Formats.Map.Tests.Resources.jmf.default-room-122.jmf");
var format = new JackhammerJmfFormat();
var map = format.Read(file);
Assert.AreEqual("worldspawn", map.Worldspawn.ClassName);
Assert.AreEqual(3, map.BackgroundImages.Count);

var front = map.BackgroundImages.Single(x => x.Viewport == ViewportType.OrthographicFront);
Assert.AreEqual("C:/Test/Viewport.png", front.Path);
Assert.IsTrue(Math.Abs(front.Scale - 2.5) < 0.0001);
Assert.AreEqual(175, front.Luminance);
Assert.AreEqual(FilterMode.Linear, front.Filter);
Assert.AreEqual(true, front.InvertColours);
Assert.AreEqual(new Vector2(6, -7), front.Offset);
}
}
}
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions Sledge.Formats.Map.Tests/Sledge.Formats.Map.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="Resources\jmf\default-room-121.jmf" />
<None Remove="Resources\jmf\default-room-122.jmf" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\jmf\default-room-121.jmf" />
<EmbeddedResource Include="Resources\jmf\default-room-122.jmf" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
Expand Down
36 changes: 35 additions & 1 deletion Sledge.Formats.Map/Formats/JackhamerJmfFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MapFile Read(Stream stream)

// Only JHMF version 121 is supported for the moment.
var version = br.ReadInt32();
Util.Assert(version == 121, $"Unsupported JMF version number. Expected 121, got {version}.");
Util.Assert(version == 121 || version == 122, $"Unsupported JMF version number. Expected 121 or 122, got {version}.");

// Appears to be an array of locations to export to .map
var numExportStrings = br.ReadInt32();
Expand All @@ -41,6 +41,7 @@ public MapFile Read(Stream stream)

var map = new MapFile();

if (version >= 122) ReadBackgroundImages(map, br);
var groups = ReadGroups(map, br);
ReadVisgroups(map, br);
map.CordonBounds = (br.ReadVector3(), br.ReadVector3());
Expand Down Expand Up @@ -413,6 +414,39 @@ private void ReadSurfaceProperties(Surface surface, BinaryReader br)
surface.TextureName = br.ReadFixedLengthString(Encoding.ASCII, 64);
}

private static void ReadBackgroundImages(MapFile map, BinaryReader br)
{
var viewports = new[]
{
ViewportType.OrthographicFront,
ViewportType.OrthographicSide,
ViewportType.OrthographicTop
};
foreach (var vp in viewports)
{
var bgi = new BackgroundImage
{
Viewport = vp,
Path = ReadString(br),
Scale = br.ReadDouble(),
Luminance = (byte) br.ReadInt32(),
Filter = ConvertFilterMode(br.ReadInt32()),
InvertColours = br.ReadInt32() != 0,
Offset = new Vector2(br.ReadInt32(), br.ReadInt32())
};
br.ReadBytes(4); // padding
map.BackgroundImages.Add(bgi);
}

return;

FilterMode ConvertFilterMode(int num)
{
if (num == 0) return FilterMode.Nearest;
return FilterMode.Linear;
}
}

#endregion

public void Write(Stream stream, MapFile map, string styleHint)
Expand Down
18 changes: 18 additions & 0 deletions Sledge.Formats.Map/Objects/BackgroundImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace Sledge.Formats.Map.Objects
{
public class BackgroundImage
{
public ViewportType Viewport { get; set; }
public string Path { get; set; }
public double Scale { get; set; }
public byte Luminance { get; set; }
public FilterMode Filter { get; set; }
public bool InvertColours { get; set; }
public Vector2 Offset { get; set; }
}
}
8 changes: 8 additions & 0 deletions Sledge.Formats.Map/Objects/FilterMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Sledge.Formats.Map.Objects
{
public enum FilterMode
{
Nearest = 0,
Linear = 1,
}
}
2 changes: 2 additions & 0 deletions Sledge.Formats.Map/Objects/MapFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MapFile
public List<Camera> Cameras { get; set; }
public List<SerialisedObject> AdditionalObjects { get; set; }
public (Vector3 min, Vector3 max) CordonBounds { get; set; }
public List<BackgroundImage> BackgroundImages { get; set; }

public MapFile()
{
Expand All @@ -21,6 +22,7 @@ public MapFile()
Cameras = new List<Camera>();
AdditionalObjects = new List<SerialisedObject>();
CordonBounds = (Vector3.Zero, Vector3.Zero);
BackgroundImages = new List<BackgroundImage>();
}
}
}
25 changes: 25 additions & 0 deletions Sledge.Formats.Map/Objects/ViewportType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Sledge.Formats.Map.Objects
{
public enum ViewportType
{
/// <summary>
/// 3D view
/// </summary>
Perspective,

/// <summary>
/// X/Y
/// </summary>
OrthographicTop,

/// <summary>
/// X/Z
/// </summary>
OrthographicSide,

/// <summary>
/// Y/Z
/// </summary>
OrthographicFront,
}
}

0 comments on commit 59e569a

Please sign in to comment.