Skip to content

Commit 1854ad6

Browse files
authored
Improve string table view model (#105)
* move save/reload buttons to interface * remove old animation sequence record * progress * string table saves correctly, vehicle also can view arrays now * add g1 saving * restore non-vehicle view models * fix bgr image saving * fix g1 saving
1 parent 4159468 commit 1854ad6

24 files changed

+455
-220
lines changed

Dat/FileParsing/ObjectAnnotator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace OpenLoco.Dat.FileParsing
99
{
1010
public static class ObjectAnnotator
1111
{
12-
public static IList<HexAnnotation> Annotate(byte[] bytelist, out byte[] fullData)
12+
public static IList<HexAnnotation> Annotate(byte[] byteList)
1313
{
1414
var annotations = new List<HexAnnotation>();
1515
var runningCount = 0;
@@ -18,25 +18,25 @@ public static IList<HexAnnotation> Annotate(byte[] bytelist, out byte[] fullData
1818
var s5HeaderAnnotation = new HexAnnotation("S5 Header", 0, S5Header.StructLength);
1919
annotations.Add(s5HeaderAnnotation);
2020
annotations.Add(new HexAnnotation("Flags", s5HeaderAnnotation, 0, 4));
21-
annotations.Add(new HexAnnotation("Name: '" + Encoding.ASCII.GetString(bytelist[4..12]) + "'", s5HeaderAnnotation, 4, 8));
21+
annotations.Add(new HexAnnotation("Name: '" + Encoding.ASCII.GetString(byteList[4..12]) + "'", s5HeaderAnnotation, 4, 8));
2222
annotations.Add(new HexAnnotation("Checksum", s5HeaderAnnotation, 12, 4));
2323

24-
var s5Header = S5Header.Read(bytelist.AsSpan()[0..S5Header.StructLength]);
24+
var s5Header = S5Header.Read(byteList.AsSpan()[0..S5Header.StructLength]);
2525
runningCount += S5Header.StructLength;
2626

2727
// Object Header Annotations
2828
var objectHeaderAnnotation = new HexAnnotation("Object Header", runningCount, ObjectHeader.StructLength);
2929
annotations.Add(objectHeaderAnnotation);
3030
annotations.Add(new HexAnnotation("Encoding", objectHeaderAnnotation, runningCount, 1));
3131
annotations.Add(new HexAnnotation("Data Length", objectHeaderAnnotation, runningCount + 1, 4));
32-
var objectHeader = ObjectHeader.Read(bytelist.AsSpan()[runningCount..(runningCount + ObjectHeader.StructLength)]);
32+
var objectHeader = ObjectHeader.Read(byteList.AsSpan()[runningCount..(runningCount + ObjectHeader.StructLength)]);
3333
runningCount += ObjectHeader.StructLength;
3434

3535
// Decode Loco Struct
36-
fullData =
36+
byte[] fullData =
3737
[
38-
.. bytelist[..runningCount],
39-
.. SawyerStreamReader.Decode(objectHeader.Encoding, bytelist[runningCount..(int)(runningCount + objectHeader.DataLength)]),
38+
.. byteList[..runningCount],
39+
.. SawyerStreamReader.Decode(objectHeader.Encoding, byteList[runningCount..(int)(runningCount + objectHeader.DataLength)]),
4040
];
4141

4242
var locoStruct = SawyerStreamReader.GetLocoStruct(s5Header.ObjectType, fullData.AsSpan()[runningCount..]);

Dat/FileParsing/SawyerStreamWriter.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ public static MemoryStream WriteLocoObjectStream(string objName, ILocoObject obj
189189
return headerStream;
190190
}
191191

192-
private static void SaveImageTable(List<G1Element32> g1Elements, Stream objStream)
192+
static void SaveImageTable(List<G1Element32> g1Elements, Stream objStream)
193193
{
194194
if (g1Elements != null && g1Elements.Count != 0)
195195
{
196196
// write G1Header
197197
objStream.Write(BitConverter.GetBytes((uint32_t)g1Elements.Count));
198-
objStream.Write(BitConverter.GetBytes((uint32_t)g1Elements.Sum(x => G1Element32.StructLength + x.ImageData.Length)));
198+
objStream.Write(BitConverter.GetBytes((uint32_t)g1Elements.Sum(x => x.ImageData.Length)));
199199

200200
var offsetBytesIntoImageData = 0;
201201
// write G1Element headers
@@ -221,6 +221,14 @@ private static void SaveImageTable(List<G1Element32> g1Elements, Stream objStrea
221221
}
222222
}
223223

224+
public static void SaveG1(string filename, G1Dat g1)
225+
{
226+
using (var fs = File.OpenWrite(filename))
227+
{
228+
SaveImageTable(g1.G1Elements, fs);
229+
}
230+
}
231+
224232
//public static ReadOnlySpan<byte> Encode(SawyerEncoding encoding, ReadOnlySpan<byte> data, ILogger? logger = null)
225233
//{
226234
// switch (encoding)
@@ -313,13 +321,5 @@ private static void SaveImageTable(List<G1Element32> g1Elements, Stream objStrea
313321

314322
// return encodedSpan;
315323
//}
316-
317-
public static void SaveG1(G1Dat g1, string filename)
318-
{
319-
using (var fs = File.OpenWrite(filename))
320-
{
321-
SaveImageTable(g1.G1Elements, fs);
322-
}
323-
}
324324
}
325325
}

Dat/Objects/Vehicle/BodySprite.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public record BodySprite(
4646
public uint8_t NumFramesPerRotation { get; set; }
4747

4848
// unused in this tool, but we need to keep them so the object saves properly
49-
public image_id FlatImageId { get; set; }
50-
public image_id GentleImageId { get; set; }
51-
public image_id SlopedImageId { get; set; }
52-
public image_id SteepImageId { get; set; }
53-
public image_id UnkImageId1 { get; set; }
54-
public image_id UnkImageId2 { get; set; }
49+
[Browsable(false)] public image_id FlatImageId { get; set; }
50+
[Browsable(false)] public image_id GentleImageId { get; set; }
51+
[Browsable(false)] public image_id SlopedImageId { get; set; }
52+
[Browsable(false)] public image_id SteepImageId { get; set; }
53+
[Browsable(false)] public image_id UnkImageId1 { get; set; }
54+
[Browsable(false)] public image_id UnkImageId2 { get; set; }
5555

5656
//
5757

Dat/PaletteMap.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ public PaletteMap(Color[] _palette)
6363
public byte[] ConvertRgba32ImageToG1Data(Image<Rgba32> img, G1ElementFlags flags)
6464
{
6565
var pixels = img.Width * img.Height;
66-
var bytes = new byte[pixels];
66+
var isBgr = flags.HasFlag(G1ElementFlags.IsBgr24);
67+
var bytes = new byte[pixels * (isBgr ? 3 : 1)];
6768

6869
var index = 0;
6970
for (var y = 0; y < img.Height; ++y)
7071
{
7172
for (var x = 0; x < img.Width; ++x)
7273
{
73-
if (flags.HasFlag(G1ElementFlags.IsBgr24))
74+
if (isBgr)
7475
{
7576
var pixel = img[x, y];
7677
bytes[index++] = pixel.B;

Gui/Models/FileSystemItems.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public enum FileLocation
1212
Online,
1313
}
1414

15-
public abstract record FileSystemItemBase(string Filename, string Name, ObservableCollection<FileSystemItemBase>? SubNodes = null)
15+
public abstract record FileSystemItemBase(string Filename, string DisplayName, ObservableCollection<FileSystemItemBase>? SubNodes = null)
1616
{
1717
public string NameComputed
18-
=> $"{Name}{(SubNodes == null ? string.Empty : $" ({SubNodes.Count})")}"; // nested interpolated string...what have i become
18+
=> $"{DisplayName}{(SubNodes == null ? string.Empty : $" ({SubNodes.Count})")}"; // nested interpolated string...what have i become
1919
}
2020

21-
public record FileSystemItem(string Filename, string Name, bool IsVanilla, FileLocation FileLocation)
22-
: FileSystemItemBase(Filename, Name, null);
21+
public record FileSystemItem(string Filename, string DisplayName, bool IsVanilla, FileLocation FileLocation)
22+
: FileSystemItemBase(Filename, DisplayName, null);
2323

2424
//public record FileSystemDatGroup(string Path, DatFileType DatFileType, ObservableCollection<FileSystemItemBase> SubNodes)
2525
// : FileSystemItemBase(Path, DatFileType.ToString(), SubNodes);

Gui/Models/ObjectEditorModel.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public ObjectEditorModel()
6767
Logger = new Logger();
6868
LoggerObservableLogs = [];
6969
Logger.LogAdded += (sender, laea) => Dispatcher.UIThread.Post(() => LoggerObservableLogs.Insert(0, laea.Log));
70-
Logger.LogAdded += (sender, laea) => File.AppendAllLines(LoggingFile, [laea.Log.ToString()]);
70+
//Logger.LogAdded += (sender, laea) => File.AppendAllLines(LoggingFile, [laea.Log.ToString()]);
7171

7272
LoadSettings();
7373

@@ -171,7 +171,7 @@ public void SaveSettings()
171171
}
172172
}
173173

174-
public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoFile)
174+
public bool TryLoadObject(FileSystemItem filesystemItem, out UiDatLocoFile? uiLocoFile)
175175
{
176176
if (string.IsNullOrEmpty(filesystemItem.Filename))
177177
{
@@ -193,42 +193,42 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoF
193193

194194
if (!OnlineCache.TryGetValue(uniqueObjectId, out var locoObj))
195195
{
196-
Logger.Debug($"Didn't find object {filesystemItem.Name} with unique id {uniqueObjectId} in cache - downloading it from {WebClient.BaseAddress}");
196+
Logger.Debug($"Didn't find object {filesystemItem.DisplayName} with unique id {uniqueObjectId} in cache - downloading it from {WebClient.BaseAddress}");
197197
locoObj = Task.Run(async () => await Client.GetObjectAsync(WebClient, uniqueObjectId, true)).Result;
198198

199199
if (locoObj == null)
200200
{
201-
Logger.Error($"Unable to download object {filesystemItem.Name} with unique id {uniqueObjectId} from online - received no data");
201+
Logger.Error($"Unable to download object {filesystemItem.DisplayName} with unique id {uniqueObjectId} from online - received no data");
202202
return false;
203203
}
204204
else if (string.IsNullOrEmpty(locoObj.DatBytes))
205205
{
206-
Logger.Warning($"Unable to download object {filesystemItem.Name} with unique id {uniqueObjectId} from online - received no DAT object data. Will still show metadata");
206+
Logger.Warning($"Unable to download object {filesystemItem.DisplayName} with unique id {uniqueObjectId} from online - received no DAT object data. Will still show metadata");
207207
}
208208
else if (locoObj.IsVanilla)
209209
{
210-
Logger.Warning($"Unable to download object {filesystemItem.Name} with unique id {uniqueObjectId} from online - requested object is a vanilla object and it is illegal to distribute copyright material. Will still show metadata");
210+
Logger.Warning($"Unable to download object {filesystemItem.DisplayName} with unique id {uniqueObjectId} from online - requested object is a vanilla object and it is illegal to distribute copyright material. Will still show metadata");
211211
}
212212

213-
Logger.Info($"Downloaded object {filesystemItem.Name} with unique id {uniqueObjectId} and added it to the local cache");
214-
Logger.Debug($"{filesystemItem.Name} has authors=[{string.Join(", ", locoObj?.Authors?.Select(x => x.Name) ?? [])}], tags=[{string.Join(", ", locoObj?.Tags?.Select(x => x.Name) ?? [])}], modpacks=[{string.Join(", ", locoObj?.Modpacks?.Select(x => x.Name) ?? [])}], licence={locoObj?.Licence}");
213+
Logger.Info($"Downloaded object {filesystemItem.DisplayName} with unique id {uniqueObjectId} and added it to the local cache");
214+
Logger.Debug($"{filesystemItem.DisplayName} has authors=[{string.Join(", ", locoObj?.Authors?.Select(x => x.Name) ?? [])}], tags=[{string.Join(", ", locoObj?.Tags?.Select(x => x.Name) ?? [])}], modpacks=[{string.Join(", ", locoObj?.Modpacks?.Select(x => x.Name) ?? [])}], licence={locoObj?.Licence}");
215215
OnlineCache.Add(uniqueObjectId, locoObj!);
216216
}
217217
else
218218
{
219-
Logger.Debug($"Found object {filesystemItem.Name} with unique id {uniqueObjectId} in cache - reusing it");
219+
Logger.Debug($"Found object {filesystemItem.DisplayName} with unique id {uniqueObjectId} in cache - reusing it");
220220
}
221221

222222
if (locoObj != null)
223223
{
224224
if (locoObj.DatBytes?.Length > 0)
225225
{
226-
var obj = SawyerStreamReader.LoadFullObjectFromStream(Convert.FromBase64String(locoObj.DatBytes), Logger, $"{filesystemItem.Filename}-{filesystemItem.Name}", true);
226+
var obj = SawyerStreamReader.LoadFullObjectFromStream(Convert.FromBase64String(locoObj.DatBytes), Logger, $"{filesystemItem.Filename}-{filesystemItem.DisplayName}", true);
227227
fileInfo = obj.DatFileInfo;
228228
locoObject = obj.LocoObject;
229229
if (obj.LocoObject == null)
230230
{
231-
Logger.Warning($"Unable to load {filesystemItem.Name} from the received DAT object data");
231+
Logger.Warning($"Unable to load {filesystemItem.DisplayName} from the received DAT object data");
232232
}
233233
}
234234

@@ -278,14 +278,14 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoF
278278
return false;
279279
}
280280

281-
if (locoObject == null && fileInfo == null && metadata == null && images == null)
281+
if (fileInfo == null)
282282
{
283283
Logger.Error($"Unable to load {filesystemItem.Filename}");
284284
uiLocoFile = null;
285285
return false;
286286
}
287287

288-
uiLocoFile = new UiLocoFile() { DatFileInfo = fileInfo, LocoObject = locoObject, Metadata = metadata, Images = images };
288+
uiLocoFile = new UiDatLocoFile() { DatFileInfo = fileInfo, LocoObject = locoObject, Metadata = metadata, Images = images };
289289
return true;
290290
}
291291

Gui/Models/UiLocoFile.cs renamed to Gui/Models/UiDatLocoFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace AvaGui.Models
88
{
99
[TypeConverter(typeof(ExpandableObjectConverter))]
10-
public class UiLocoFile
10+
public class UiDatLocoFile
1111
{
1212
public required DatFileInfo DatFileInfo { get; set; }
1313
public ILocoObject? LocoObject { get; set; }

Gui/Typedefs.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
global using int16_t = System.Int16;
2+
global using object_id = System.Byte;
3+
global using Speed16 = System.Int16;
4+
global using uint16_t = System.UInt16;
25
global using uint32_t = System.UInt32;
6+
global using uint8_t = System.Byte;
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using ReactiveUI;
2-
31
namespace AvaGui.ViewModels
42
{
5-
public class CSSViewModel : ReactiveObject, ILocoFileViewModel;
3+
public class CSSViewModel : BaseLocoFileViewModel;
64
}

0 commit comments

Comments
 (0)