Skip to content

Commit 318d750

Browse files
committed
add index to graphics element, fix gog g1 groups, hide none value for graphicselementflag
1 parent 2e8d78c commit 318d750

File tree

8 files changed

+86
-43
lines changed

8 files changed

+86
-43
lines changed

Dat/Converters/GraphicsElementConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static DatG1Element32 Convert(this GraphicsElement graphicsElement)
1919
ImageData = graphicsElement.ImageData,
2020
};
2121

22-
public static GraphicsElement Convert(this DatG1Element32 graphicsElement)
22+
public static GraphicsElement Convert(this DatG1Element32 graphicsElement, string name, int index)
2323
=> new()
2424
{
2525
Width = graphicsElement.Width,
@@ -29,5 +29,7 @@ public static GraphicsElement Convert(this DatG1Element32 graphicsElement)
2929
Flags = (GraphicsElementFlags)graphicsElement.Flags,
3030
ZoomOffset = graphicsElement.ZoomOffset,
3131
ImageData = graphicsElement.ImageData,
32+
Name = name,
33+
ImageTableIndex = index,
3234
};
3335
}

Dat/FileParsing/SawyerStreamReader.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,7 @@ public static StringTable ReadStringTableStream(Stream stream, string[] stringNa
219219

220220
public static G1Dat? LoadG1(string filename, ILogger logger)
221221
{
222-
ReadOnlySpan<byte> fullData = File.ReadAllBytes(filename);
223-
224-
using (var ms = new MemoryStream(fullData.ToArray()))
222+
using (var ms = new FileStream(filename, FileMode.Open))
225223
using (var br = new LocoBinaryReader(ms))
226224
{
227225
var (g1Header, imageTable) = ReadImageTable(br);
@@ -282,9 +280,7 @@ public static (G1Header Header, List<GraphicsElement> Table) ReadImageTable(Loco
282280
currElement.ImageData = DecodeRLEImageData(currElement);
283281
}
284282

285-
var ge = currElement.Convert();
286-
ge.Name = DefaultImageTableNameProvider.GetImageName(i);
287-
graphicsElements.Add(ge);
283+
graphicsElements.Add(currElement.Convert(DefaultImageTableNameProvider.GetImageName(i), i));
288284
}
289285

290286
return (g1Header, graphicsElements);

Dat/Types/G1Dat.cs

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Definitions.ObjectModels;
22
using Definitions.ObjectModels.Types;
33
using System.ComponentModel;
4+
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
56

67
namespace Dat.Types;
@@ -12,33 +13,67 @@ public class G1Dat
1213
public G1Header G1Header { get; set; }
1314
public ImageTable ImageTable { get; set; }
1415

16+
public const int SteamImageCount = 3896;
17+
public const int GoGImageCount = 4122;
18+
1519
public bool IsSteamG1
16-
=> ImageTable.GraphicsElements.Count == 3896;
20+
=> ImageTable.GraphicsElements.Count == SteamImageCount;
1721

1822
public G1Dat(G1Header g1Header, List<GraphicsElement> graphicsElements)
1923
{
2024
G1Header = g1Header;
21-
ImageTable = new ImageTable
25+
if (graphicsElements.Count == SteamImageCount)
26+
{
27+
ImageTable = new ImageTable
28+
{
29+
// Note: The grouping below is good but its for steam only, and because elements are out of order, saving is not possible unless each element stores its index
30+
Groups =
31+
[
32+
("terrain-masks", [.. graphicsElements[0..417], .. graphicsElements[3629..3896]]),
33+
("palettes", [.. graphicsElements[417..428], .. graphicsElements[2170..2305]]),
34+
("arrows", [.. graphicsElements[428..444], .. graphicsElements[449..457], .. graphicsElements[3492..3504]]),
35+
("unk", graphicsElements[444..449]),
36+
("supports", graphicsElements[457..1117]),
37+
("glyphs", graphicsElements[1117..2170]),
38+
("loading-bar", graphicsElements[2326..2335]),
39+
("interface", [.. graphicsElements[2335..2470], .. graphicsElements[3477..3479], .. graphicsElements[2305..2326], .. graphicsElements[3539..3547]]),
40+
("height-markers", graphicsElements[2470..3238]),
41+
("numerical-markers", graphicsElements[3238..3302]),
42+
("unk", graphicsElements[3302..3362]),
43+
("particles", graphicsElements[3362..3477]),
44+
("masks", [.. graphicsElements[3479..3492], graphicsElements[3504]]),
45+
("object-types", graphicsElements[3505..3539]),
46+
("title", graphicsElements[3547..3629]),
47+
]
48+
};
49+
}
50+
else
2251
{
23-
Groups =
24-
[
25-
("terrain-masks", [.. graphicsElements[0..417], .. graphicsElements[3629..3896]]),
26-
("palettes", [.. graphicsElements[417..428], .. graphicsElements[2170..2304]]),
27-
("arrows", [.. graphicsElements[428..444], .. graphicsElements[449..457], .. graphicsElements[3492..3504]]),
28-
("unk", graphicsElements[444..449]),
29-
("supports", graphicsElements[457..1117]),
30-
("glyphs", graphicsElements[1117..2169]),
31-
("loading-bar", graphicsElements[2326..2335]),
32-
("interface", [.. graphicsElements[2335..2470], .. graphicsElements[3477..3479], .. graphicsElements[2305..2326], .. graphicsElements[3539..3547]]),
33-
("height-markers", graphicsElements[2470..3238]),
34-
("numerical-markers", graphicsElements[3238..3302]),
35-
("unk", graphicsElements[3302..3362]),
36-
("particles", graphicsElements[3362..3477]),
37-
("masks", [.. graphicsElements[3479..3492], graphicsElements[3504]]),
38-
("object-types", graphicsElements[3505..3539]),
39-
("title", graphicsElements[3547..3629]),
40-
]
41-
};
52+
ImageTable = new ImageTable()
53+
{
54+
Groups =
55+
[
56+
("terrain-masks", [.. graphicsElements[0..417], .. graphicsElements[3631..3898]]),
57+
("palettes", [.. graphicsElements[417..428], .. graphicsElements[2170..2305]]),
58+
("arrows", [.. graphicsElements[428..444], .. graphicsElements[449..457], .. graphicsElements[3492..3504]]),
59+
("unk", graphicsElements[444..449]),
60+
("supports", graphicsElements[457..1117]),
61+
("glyphs", [.. graphicsElements[1117..2170], .. graphicsElements[3898..4122]]),
62+
("loading-bar", graphicsElements[2326..2335]),
63+
("interface", [.. graphicsElements[2335..2470], .. graphicsElements[3477..3479], .. graphicsElements[2305..2326], .. graphicsElements[3539..3547]]),
64+
("height-markers", graphicsElements[2470..3238]),
65+
("numerical-markers", graphicsElements[3238..3302]),
66+
("unk", graphicsElements[3302..3362]),
67+
("particles", graphicsElements[3362..3477]),
68+
("masks", [.. graphicsElements[3479..3492], graphicsElements[3504]]),
69+
("object-types", graphicsElements[3505..3539]),
70+
("title", graphicsElements[3547..3631]),
71+
]
72+
};
73+
}
74+
75+
Debug.Assert(G1Header.NumEntries == ImageTable.GraphicsElements.Count);
76+
Debug.Assert(ImageTable.GraphicsElements.Count == graphicsElements.Count);
4277
}
4378

4479
public bool TryGetImageName(int id, [MaybeNullWhen(false)] out string value)

Definitions/ObjectModels/ImageTable.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public PaletteMap PaletteMap
2929
// public/old interface
3030
public List<GraphicsElement> GraphicsElements
3131
{
32-
get => [.. Groups.SelectMany(x => x.GraphicsElements)];
32+
get => [.. Groups
33+
.SelectMany(x => x.GraphicsElements)
34+
.OrderBy(x => x.ImageTableIndex)];
3335
set => Groups.Add(("All", value));
3436
}
3537

Definitions/ObjectModels/Types/GraphicsElement.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ public class GraphicsElement // follows G1Element32, except XOffset and YOffset
3636

3737
[JsonIgnore] // this is calculated based on object type, its not really a part of the object definition itself
3838
public string Name { get; set; } // taken from IImageNameProvider
39+
40+
public int ImageTableIndex { get; init; }
3941
}

Gui/ViewModels/Graphics/ImageViewModel.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Definitions.ObjectModels;
33
using Definitions.ObjectModels.Types;
44
using Gui.Models;
5+
using PropertyModels.ComponentModel.DataAnnotations;
56
using ReactiveUI;
67
using ReactiveUI.Fody.Helpers;
78
using SixLabors.ImageSharp;
@@ -15,15 +16,16 @@ namespace Gui.ViewModels.Graphics;
1516

1617
public class ImageViewModel : ReactiveObject
1718
{
19+
public string Name { get; set; }
20+
public int ImageTableIndex { get; init; }
1821
public int Width => UnderlyingImage.Width;
1922
public int Height => UnderlyingImage.Height;
20-
public string Name { get; set; }
21-
2223
[Reactive] public int XOffset { get; set; }
2324
[Reactive] public int YOffset { get; set; }
25+
public short ZoomOffset { get; set; }
2426

27+
[EnumProhibitValues<GraphicsElementFlags>(GraphicsElementFlags.None)]
2528
public GraphicsElementFlags Flags { get; set; }
26-
public short ZoomOffset { get; set; }
2729

2830
[Reactive, Browsable(false)]
2931
public Bitmap DisplayedImage { get; private set; }
@@ -50,6 +52,7 @@ public ImageViewModel(GraphicsElement graphicsElement, PaletteMap paletteMap)
5052
YOffset = graphicsElement.YOffset;
5153
Flags = graphicsElement.Flags;
5254
ZoomOffset = graphicsElement.ZoomOffset;
55+
ImageTableIndex = graphicsElement.ImageTableIndex;
5356
PaletteMap = paletteMap;
5457

5558
_ = this.WhenAnyValue(o => o.UnderlyingImage)
@@ -69,13 +72,15 @@ public void RecolourImage(ColourRemapSwatch primary, ColourRemapSwatch secondary
6972

7073
var dummyElement = new GraphicsElement
7174
{
75+
Name = Name, // not necessary for palette
7276
Width = (short)UnderlyingImage.Width,
7377
Height = (short)UnderlyingImage.Height,
7478
XOffset = (short)XOffset,
7579
YOffset = (short)YOffset,
7680
Flags = Flags,
7781
ZoomOffset = ZoomOffset,
78-
ImageData = rawData
82+
ImageData = rawData,
83+
ImageTableIndex = ImageTableIndex, // not necessary for palette
7984
};
8085

8186
if (!PaletteMap.TryConvertG1ToRgba32Bitmap(dummyElement, primary, secondary, out var image))

Tests/G1Tests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using NUnit.Framework;
21
using Common.Logging;
32
using Dat.FileParsing;
43
using Dat.Types;
5-
using SixLabors.ImageSharp;
64
using Definitions.ObjectModels.Types;
5+
using NUnit.Framework;
6+
using SixLabors.ImageSharp;
77

88
namespace Dat.Tests;
99

@@ -55,8 +55,8 @@ public void LoadSaveLoadG1(string g1File)
5555
[TestCase(GoG_G1, 3540)]
5656
[TestCase(GoG_G1, 3541)]
5757
[TestCase(GoG_G1, 3542)]
58-
[TestCase(GoG_G1, 3618)]
59-
[TestCase(GoG_G1, 3619)]
58+
[TestCase(GoG_G1, 3620)]
59+
[TestCase(GoG_G1, 3621)]
6060
public void LoadSaveLoadG1_RLERunsGreaterThan127(string g1File, int element)
6161
{
6262
var g1 = SawyerStreamReader.LoadG1(g1File, Logger);

Tests/IdempotenceTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Dat.Tests;
1313
[TestFixture]
1414
public class IdempotenceTests
1515
{
16+
static PaletteMap PaletteMap { get; } = new PaletteMap("C:\\Users\\bigba\\source\\repos\\OpenLoco\\ObjectEditor\\Gui\\Assets\\palette.png");
17+
1618
static string[] VanillaFiles => [
1719
.. Directory.GetFiles(TestConstants.BaseSteamObjDataPath, "*.dat"),
1820
.. Directory.GetFiles(TestConstants.BaseGoGObjDataPath, "*.dat")
@@ -62,12 +64,11 @@ public void LoadSaveLoad(string filename)
6264

6365
using (Assert.EnterMultipleScope())
6466
{
65-
Assert.That(JsonSerializer.Serialize((object)actual.Object), Is.EqualTo(JsonSerializer.Serialize((object)expected.Object)));
66-
Assert.That(JsonSerializer.Serialize(actual.StringTable), Is.EqualTo(JsonSerializer.Serialize(expected.StringTable)));
67+
Assert.That(JsonSerializer.Serialize((object)actual.Object), Is.EqualTo(JsonSerializer.Serialize((object)expected.Object)), "Object");
68+
Assert.That(JsonSerializer.Serialize(actual.StringTable), Is.EqualTo(JsonSerializer.Serialize(expected.StringTable)), "String Table");
6769

6870
if (actual.ImageTable != null && expected.ImageTable != null)
6971
{
70-
var pm = new PaletteMap("C:\\Users\\bigba\\source\\repos\\OpenLoco\\ObjectEditor\\Gui\\Assets\\palette.png");
7172
var i = 0;
7273
foreach (var ae in actual.ImageTable.GraphicsElements.Zip(expected.ImageTable.GraphicsElements))
7374
{
@@ -76,13 +77,13 @@ public void LoadSaveLoad(string filename)
7677

7778
if (ac != ex)
7879
{
79-
_ = pm.TryConvertG1ToRgba32Bitmap(ae.First, ColourRemapSwatch.PrimaryRemap, ColourRemapSwatch.SecondaryRemap, out var img1);
80-
_ = pm.TryConvertG1ToRgba32Bitmap(ae.Second, ColourRemapSwatch.PrimaryRemap, ColourRemapSwatch.SecondaryRemap, out var img2);
80+
_ = PaletteMap.TryConvertG1ToRgba32Bitmap(ae.First, ColourRemapSwatch.PrimaryRemap, ColourRemapSwatch.SecondaryRemap, out var img1);
81+
_ = PaletteMap.TryConvertG1ToRgba32Bitmap(ae.Second, ColourRemapSwatch.PrimaryRemap, ColourRemapSwatch.SecondaryRemap, out var img2);
8182
img1.SaveAsBmp($"{Path.GetFileName(filename)}-{i}-actual.bmp");
8283
img2.SaveAsBmp($"{Path.GetFileName(filename)}-{i}-expected.bmp");
8384
}
8485

85-
Assert.That(ac, Is.EqualTo(ex));
86+
Assert.That(ac, Is.EqualTo(ex), $"GraphicsTable[{i}]");
8687
i++;
8788
}
8889
}

0 commit comments

Comments
 (0)