Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Definitions.ObjectModels;

public class LocoObjectMetadata(string internalName)
public class ObjectMetadata(string internalName)
{
public UniqueObjectId UniqueObjectId { get; init; }

Expand Down
41 changes: 41 additions & 0 deletions Definitions/Web/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public static async Task<IEnumerable<DtoObjectEntry>> GetObjectListAsync(HttpCli
id,
logger);

public static async Task<DtoObjectDescriptor?> UpdateObjectAsync(HttpClient client, UniqueObjectId id, DtoObjectDescriptor request, ILogger? logger = null)
=> await ClientHelpers.PutAsync<DtoObjectDescriptor, DtoObjectDescriptor>(
client,
ApiVersion,
RoutesV2.Objects,
id,
request,
logger);

public static async Task<byte[]?> GetObjectFileAsync(HttpClient client, UniqueObjectId id, ILogger? logger = null)
=> await ClientHelpers.SendRequestAsync(
client,
Expand Down Expand Up @@ -54,4 +63,36 @@ public static async Task<IEnumerable<DtoObjectEntry>> GetObjectListAsync(HttpCli
entry,
logger);
}

public static async Task<IEnumerable<DtoLicenceEntry>> GetLicencesAsync(HttpClient client, ILogger? logger = null)
=> await ClientHelpers.GetAsync<IEnumerable<DtoLicenceEntry>>(
client,
ApiVersion,
RoutesV2.Licences,
null,
logger) ?? [];

public static async Task<IEnumerable<DtoAuthorEntry>> GetAuthorsAsync(HttpClient client, ILogger? logger = null)
=> await ClientHelpers.GetAsync<IEnumerable<DtoAuthorEntry>>(
client,
ApiVersion,
RoutesV2.Authors,
null,
logger) ?? [];

public static async Task<IEnumerable<DtoTagEntry>> GetTagsAsync(HttpClient client, ILogger? logger = null)
=> await ClientHelpers.GetAsync<IEnumerable<DtoTagEntry>>(
client,
ApiVersion,
RoutesV2.Tags,
null,
logger) ?? [];

public static async Task<IEnumerable<DtoItemPackEntry>> GetObjectPacksAsync(HttpClient client, ILogger? logger = null)
=> await ClientHelpers.GetAsync<IEnumerable<DtoItemPackEntry>>(
client,
ApiVersion,
RoutesV2.ObjectPacks,
null,
logger) ?? [];
}
2 changes: 1 addition & 1 deletion Gui/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<DataTemplate DataType="vm:Graphics.ImageTableViewModel">
<vi:ImageTableView />
</DataTemplate>
<DataTemplate DataType="vm:LocoObjectMetadataViewModel">
<DataTemplate DataType="vm:ObjectMetadataViewModel">
<vi:MetadataView />
</DataTemplate>
<DataTemplate DataType="vm:ObjectEditorViewModel">
Expand Down
2 changes: 1 addition & 1 deletion Gui/Converters/EnumToMaterialIconConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class EnumToMaterialIconConverter : IValueConverter
static readonly Dictionary<string, string> MiscMappings = new()
{
{ nameof(ObjectIndexEntry), "ViewList" },
{ nameof(LocoObjectMetadata), "ViewListOutline" },
{ nameof(ObjectMetadata), "ViewListOutline" },
};

static readonly Dictionary<ObjectType, string> ObjectMapping = new()
Expand Down
2 changes: 1 addition & 1 deletion Gui/Models/LocoUIObjectModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Gui.Models;
public class LocoUIObjectModel
{
public LocoObject? LocoObject { get; set; }
public LocoObjectMetadata? Metadata { get; set; }
public ObjectMetadata? Metadata { get; set; }
public DatHeaderInfo? DatInfo { get; set; }
}
8 changes: 4 additions & 4 deletions Gui/Models/ObjectEditorModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ bool TryLoadOnlineFile(FileSystemItem filesystemItem, out LocoUIObjectModel? loc

DatHeaderInfo? fileInfo = null;
LocoObject? locoObject = null;
LocoObjectMetadata? metadata = null;
ObjectMetadata? metadata = null;
//List<Image<Rgba32>> images = [];

if (filesystemItem.Id == null)
Expand Down Expand Up @@ -304,7 +304,7 @@ bool TryLoadOnlineFile(FileSystemItem filesystemItem, out LocoUIObjectModel? loc
fileInfo = new DatHeaderInfo(fakeS5Header, ObjectHeader.NullHeader);
}

metadata = new LocoObjectMetadata(cachedLocoObjDto.Name)
metadata = new ObjectMetadata(cachedLocoObjDto.Name)
{
UniqueObjectId = cachedLocoObjDto.Id,
Description = cachedLocoObjDto.Description,
Expand Down Expand Up @@ -342,7 +342,7 @@ bool TryLoadLocalFile(FileSystemItem filesystemItem, out LocoUIObjectModel? loco

DatHeaderInfo? fileInfo = null;
LocoObject? locoObject = null;
LocoObjectMetadata? metadata = null;
ObjectMetadata? metadata = null;

var filename = File.Exists(filesystemItem.FileName)
? filesystemItem.FileName
Expand All @@ -351,7 +351,7 @@ bool TryLoadLocalFile(FileSystemItem filesystemItem, out LocoUIObjectModel? loco
var obj = SawyerStreamReader.LoadFullObject(filename, logger: Logger);
fileInfo = obj.DatFileInfo;
locoObject = obj.LocoObject;
metadata = new LocoObjectMetadata("<unknown>")
metadata = new ObjectMetadata("<unknown>")
{
CreatedDate = filesystemItem.CreatedDate?.ToDateTimeOffset(),
ModifiedDate = filesystemItem.ModifiedDate?.ToDateTimeOffset(),
Expand Down
15 changes: 15 additions & 0 deletions Gui/ObjectServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public async Task<IEnumerable<DtoObjectEntry>> GetObjectListAsync()
public async Task<DtoObjectDescriptor?> GetObjectAsync(UniqueObjectId id)
=> await Client.GetObjectAsync(WebClient, id, Logger);

public async Task<DtoObjectDescriptor?> UpdateObjectAsync(UniqueObjectId id, DtoObjectDescriptor request)
=> await Client.UpdateObjectAsync(WebClient, id, request, Logger);

public async Task<byte[]?> GetObjectFileAsync(UniqueObjectId id)
=> await Client.GetObjectFileAsync(WebClient, id, Logger);

Expand All @@ -72,4 +75,16 @@ public async Task<IEnumerable<DtoObjectEntry>> GetObjectListAsync()

public async Task<DtoObjectMissingEntry?> AddMissingObjectAsync(DtoObjectMissingUpload entry)
=> await Client.AddMissingObjectAsync(WebClient, entry, Logger);

public async Task<IEnumerable<DtoLicenceEntry>> GetLicencesAsync()
=> await Client.GetLicencesAsync(WebClient, Logger);

public async Task<IEnumerable<DtoAuthorEntry>> GetAuthorsAsync()
=> await Client.GetAuthorsAsync(WebClient, Logger);

public async Task<IEnumerable<DtoTagEntry>> GetTagsAsync()
=> await Client.GetTagsAsync(WebClient, Logger);

public async Task<IEnumerable<DtoItemPackEntry>> GetObjectPacksAsync()
=> await Client.GetObjectPacksAsync(WebClient, Logger);
}
2 changes: 1 addition & 1 deletion Gui/ViewModels/FolderTreeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public DesignerFolderTreeViewModel()
var availableFilterCategories = new List<FilterTypeViewModel>
{
new() { Type = typeof(ObjectIndexEntry), DisplayName = "Index data", IconName = nameof(ObjectIndexEntry) },
new() { Type = typeof (LocoObjectMetadata), DisplayName = "Metadata", IconName = nameof(LocoObjectMetadata) }
new() { Type = typeof (ObjectMetadata), DisplayName = "Metadata", IconName = nameof(ObjectMetadata) }
};

//Filters.Add(new FilterViewModel(availableFilterCategories, RemoveFilter));
Expand Down
10 changes: 0 additions & 10 deletions Gui/ViewModels/LocoObjectMetadataViewModel.cs

This file was deleted.

81 changes: 79 additions & 2 deletions Gui/ViewModels/LocoTypes/ObjectEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Dat.Converters;
using Dat.Data;
using Dat.FileParsing;
using Definitions.DTO;
using Definitions.ObjectModels;
using Definitions.ObjectModels.Objects.Common;
using Definitions.ObjectModels.Objects.Sound;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class ObjectEditorViewModel : BaseFileViewModel
public LocoUIObjectModel? CurrentObject { get; private set; }

[Reactive]
public LocoObjectMetadataViewModel? MetadataViewModel { get; set; }
public ObjectMetadataViewModel? MetadataViewModel { get; set; }

[Reactive]
public ObjectModelHeaderViewModel? ObjectModelHeaderViewModel { get; set; }
Expand Down Expand Up @@ -308,7 +309,7 @@ public override void Load()

if (CurrentObject?.Metadata != null)
{
MetadataViewModel = new LocoObjectMetadataViewModel(CurrentObject.Metadata);
MetadataViewModel = new ObjectMetadataViewModel(CurrentObject.Metadata, Model.ObjectServiceClient, logger);
}
else
{
Expand Down Expand Up @@ -350,6 +351,82 @@ public override void Save()
? CurrentFile.FileName
: Path.Combine(Model.Settings.DownloadFolder, Path.ChangeExtension($"{CurrentFile.DisplayName}-{CurrentFile.Id}", ".dat"));
SaveCore(savePath, new SaveParameters(SaveType.DAT, ObjectDatHeaderViewModel?.DatEncoding));

// Upload metadata to server when in online mode
if (CurrentFile.FileLocation == FileLocation.Online && CurrentFile.Id.HasValue && MetadataViewModel != null)
{
_ = UploadMetadataAsync(CurrentFile.Id.Value).ContinueWith(t =>
{
// Observe any exceptions to prevent unobserved task exceptions
if (t.Exception != null)
{
logger.Error($"Unhandled exception in metadata upload", t.Exception);
}
}, TaskContinuationOptions.OnlyOnFaulted);
}
}

async Task UploadMetadataAsync(UniqueObjectId objectId)
{
if (MetadataViewModel?.Metadata == null)
{
logger.Warning("Cannot upload metadata - metadata is null");
return;
}

if (CurrentObject?.DatInfo == null)
{
logger.Warning("Cannot upload metadata - DatInfo is null");
return;
}

try
{
logger.Info($"Uploading metadata for object {objectId}");

// Create DTO from current metadata
var dtoRequest = new DtoObjectDescriptor(
Id: objectId,
Name: MetadataViewModel.Metadata.InternalName,
DisplayName: CurrentFile.DisplayName,
DatChecksum: CurrentObject.DatInfo.S5Header.Checksum,
Description: MetadataViewModel.Metadata.Description,
ObjectSource: CurrentObject.DatInfo.S5Header.ObjectSource.Convert(
CurrentObject.DatInfo.S5Header.Name,
CurrentObject.DatInfo.S5Header.Checksum),
ObjectType: CurrentObject.DatInfo.S5Header.ObjectType.Convert(),
VehicleType: null,
Availability: MetadataViewModel.Metadata.Availability,
CreatedDate: MetadataViewModel.Metadata.CreatedDate.HasValue
? DateOnly.FromDateTime(MetadataViewModel.Metadata.CreatedDate.Value.UtcDateTime)
: null,
ModifiedDate: MetadataViewModel.Metadata.ModifiedDate.HasValue
? DateOnly.FromDateTime(MetadataViewModel.Metadata.ModifiedDate.Value.UtcDateTime)
: null,
UploadedDate: DateOnly.FromDateTime(MetadataViewModel.Metadata.UploadedDate.UtcDateTime),
Licence: MetadataViewModel.Metadata.Licence,
Authors: MetadataViewModel.Metadata.Authors,
Tags: MetadataViewModel.Metadata.Tags,
ObjectPacks: MetadataViewModel.Metadata.ObjectPacks,
DatObjects: MetadataViewModel.Metadata.DatObjects,
StringTable: new DtoStringTableDescriptor(new Dictionary<string, Dictionary<LanguageId, string>>(), objectId)
);

var result = await Model.ObjectServiceClient.UpdateObjectAsync(objectId, dtoRequest);

if (result != null)
{
logger.Info($"Successfully uploaded metadata for object {objectId}");
}
else
{
logger.Error($"Failed to upload metadata for object {objectId}");
}
}
catch (Exception ex)
{
logger.Error($"Error uploading metadata for object {objectId}", ex);
}
}

public override string? SaveAs(SaveParameters saveParameters)
Expand Down
Loading