Skip to content

Commit 98406a4

Browse files
committed
fix multiple bugs with web server, gui interaction
1 parent 68cb126 commit 98406a4

File tree

10 files changed

+75
-36
lines changed

10 files changed

+75
-36
lines changed

AvaGui/Models/ObjectEditorModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoF
227227
}
228228
else
229229
{
230-
var obj = SawyerStreamReader.LoadFullObjectFromFile(filesystemItem.Filename, logger: Logger);
230+
var filename = Path.Combine(Settings.ObjDataDirectory, filesystemItem.Filename);
231+
var obj = SawyerStreamReader.LoadFullObjectFromFile(filename, logger: Logger);
231232
if (obj != null)
232233
{
233234
fileInfo = obj.Value.DatFileInfo;
@@ -416,8 +417,8 @@ public async Task UploadDatToServer(ObjectIndexEntry dat)
416417
{
417418
Logger.Info($"Uploading {dat.Filename} to object repository");
418419
var lastModifiedTime = File.GetLastWriteTimeUtc(dat.Filename); // this is the "Modified" time as shown in Windows
419-
420-
await Client.UploadDatFileAsync(WebClient, dat.Filename, await File.ReadAllBytesAsync(dat.Filename), lastModifiedTime, Logger);
420+
var filename = Path.Combine(Settings.ObjDataDirectory, dat.Filename);
421+
await Client.UploadDatFileAsync(WebClient, dat.Filename, await File.ReadAllBytesAsync(filename), lastModifiedTime, Logger);
421422
}
422423
}
423424
}

DatabaseSeeder/Program.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ static void SeedDb(LocoDb db, bool deleteExisting)
3939
{
4040
Console.WriteLine("Clearing database");
4141
_ = db.Objects.ExecuteDelete();
42-
_ = db.Authors.ExecuteDelete();
4342
_ = db.Tags.ExecuteDelete();
4443
_ = db.Modpacks.ExecuteDelete();
44+
_ = db.Authors.ExecuteDelete();
4545
_ = db.Licences.ExecuteDelete();
4646
_ = db.SaveChanges(); // not necessary since ExecuteDelete auto-saves
4747
}
4848

49-
const string ObjDirectory = "Q:\\Games\\Locomotion\\Server";
49+
const string ObjDirectory = "Q:\\Games\\Locomotion\\Server\\Objects";
5050
var allDatFiles = SawyerStreamUtils.GetDatFilesInDirectory(ObjDirectory);
5151

5252
Console.WriteLine("Seeding");
@@ -122,13 +122,16 @@ static void SeedDb(LocoDb db, bool deleteExisting)
122122
var objectMetadata = JsonSerializer.Deserialize<IEnumerable<ObjectMetadata>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\objectMetadata.json"), jsonOptions);
123123
var objectMetadataDict = objectMetadata!.ToDictionary(x => (x.ObjectName, x.Checksum), x => x);
124124

125+
var gameReleaseDate = new DateTimeOffset(2004, 09, 07, 0, 0, 0, TimeSpan.Zero);
126+
125127
foreach (var objIndex in index.Objects.DistinctBy(x => (x.ObjectName, x.Checksum)))
126128
{
127129
var metadataKey = (objIndex.ObjectName, objIndex.Checksum);
128130
if (!objectMetadataDict.TryGetValue(metadataKey, out var meta))
129131
{ }
130132

131-
var creationTime = File.GetLastWriteTimeUtc(objIndex.Filename); // this is the "Modified" time as shown in Windows
133+
var fullPath = Path.Combine(ObjDirectory, objIndex.Filename);
134+
var creationTime = objIndex.IsVanilla ? gameReleaseDate : File.GetLastWriteTimeUtc(fullPath); // this is the "Modified" time as shown in Windows
132135

133136
var authors = meta?.Authors == null ? null : db.Authors.Where(x => meta.Authors.Contains(x.Name)).ToList();
134137
var tags = meta?.Tags == null ? null : db.Tags.Where(x => meta.Tags.Contains(x.Name)).ToList();
@@ -138,7 +141,7 @@ static void SeedDb(LocoDb db, bool deleteExisting)
138141
var tblLocoObject = new TblLocoObject()
139142
{
140143
Name = $"{objIndex.ObjectName}_{objIndex.Checksum}", // same as server upload name
141-
PathOnDisk = Path.Combine(ObjDirectory, objIndex.Filename),
144+
PathOnDisk = objIndex.Filename.Replace('\\', '/'), // make the path linux-compatible
142145
OriginalName = objIndex.ObjectName,
143146
OriginalChecksum = objIndex.Checksum,
144147
IsVanilla = objIndex.IsVanilla,

Definitions/Migrations/20240829075742_InitialCreate.Designer.cs renamed to Definitions/Migrations/20240829143539_InitialCreate.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

Definitions/Web/Client.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public static async Task UploadDatFileAsync(HttpClient client, string filename,
5757
{
5858
try
5959
{
60-
logger.Debug($"Posting {filename} to {client.BaseAddress}{Routes.UploadDat}");
60+
var route = $"{client.BaseAddress}{Routes.UploadDat}".Replace("//", "'/");
61+
logger.Debug($"Posting {filename} to {route}");
6162
var request = new DtoUploadDat(Convert.ToBase64String(datFileBytes), creationDate);
6263
var response = await client.PostAsJsonAsync(Routes.UploadDat, request);
6364

ObjectService/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
var builder = WebApplication.CreateBuilder(args);
1010

11+
builder.Logging.ClearProviders();
12+
builder.Logging.AddConsole();
13+
1114
var connectionString = builder.Configuration.GetConnectionString("SQLiteConnection");
1215

1316
// Add services to the container.
@@ -51,9 +54,6 @@
5154
builder.Services.AddSingleton<Server>();
5255
var serviceSettings = builder.Services.Configure<ObjectServiceSettings>(builder.Configuration.GetSection("ObjectService"));
5356

54-
builder.Logging.ClearProviders();
55-
builder.Logging.AddConsole();
56-
5757
var app = builder.Build();
5858
app.UseHttpLogging();
5959
app.UseRateLimiter();

ObjectService/Server.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.Mvc;
12
using Microsoft.EntityFrameworkCore;
23
using Microsoft.Extensions.Options;
34
using OpenLoco.Dat.Data;
@@ -37,7 +38,7 @@ await db.Objects
3738
x.VehicleType)).ToListAsync());
3839

3940
// eg: https://localhost:7230/objects/getdat?objectName=114&checksum=123$returnObjBytes=false
40-
public async Task<IResult> GetDat(string objectName, uint checksum, bool? returnObjBytes, LocoDb db)
41+
public async Task<IResult> GetDat(string objectName, uint checksum, bool? returnObjBytes, LocoDb db, [FromServices] ILogger<Server> logger)
4142
{
4243
var eObj = await db.Objects
4344
.Where(x => x.OriginalName == objectName && x.OriginalChecksum == checksum)
@@ -47,11 +48,11 @@ public async Task<IResult> GetDat(string objectName, uint checksum, bool? return
4748

4849
return eObj == null || eObj.Object == null
4950
? Results.NotFound()
50-
: Results.Ok(await PrepareLocoObject(eObj, settings.ObjectRootFolder, returnObjBytes ?? false));
51+
: Results.Ok(await PrepareLocoObject(eObj, settings.ObjectRootFolder, returnObjBytes ?? false, logger));
5152
}
5253

5354
// eg: https://localhost:7230/objects/getobject?uniqueObjectId=246263256&returnObjBytes=false
54-
public async Task<IResult> GetObject(int uniqueObjectId, bool? returnObjBytes, LocoDb db)
55+
public async Task<IResult> GetObject(int uniqueObjectId, bool? returnObjBytes, LocoDb db, [FromServices] ILogger<Server> logger)
5556
{
5657
Console.WriteLine($"Object [{uniqueObjectId}] requested");
5758
var eObj = await db.Objects
@@ -62,7 +63,7 @@ public async Task<IResult> GetObject(int uniqueObjectId, bool? returnObjBytes, L
6263

6364
return eObj == null || eObj.Object == null
6465
? Results.NotFound()
65-
: Results.Ok(await PrepareLocoObject(eObj, settings.ObjectRootFolder, returnObjBytes ?? false));
66+
: Results.Ok(await PrepareLocoObject(eObj, settings.ObjectRootFolder, returnObjBytes ?? false, logger));
6667
}
6768

6869
// eg: https://localhost:7230/objects/originaldatfile?objectName=114&checksum=123
@@ -104,11 +105,12 @@ public async Task<IResult> GetObjectFile(int uniqueObjectId, LocoDb db)
104105
: Results.NotFound();
105106
}
106107

107-
internal static async Task<DtoLocoObject> PrepareLocoObject(ExpandedTblLocoObject eObj, string objectRootFolder, bool returnObjBytes)
108+
internal static async Task<DtoLocoObject> PrepareLocoObject(ExpandedTblLocoObject eObj, string objectRootFolder, bool returnObjBytes, ILogger<Server> logger)
108109
{
109110
var obj = eObj!.Object;
110111

111-
var pathOnDisk = Path.Combine(objectRootFolder, obj.PathOnDisk);
112+
var pathOnDisk = Path.Combine(objectRootFolder, obj.PathOnDisk.Replace('\\', '/')); // handle windows paths by replacing path separator
113+
logger.LogInformation("Loading file from {0}", pathOnDisk);
112114
var bytes = returnObjBytes && !obj.IsVanilla && File.Exists(pathOnDisk) ? Convert.ToBase64String(await File.ReadAllBytesAsync(pathOnDisk)) : null;
113115

114116
return new DtoLocoObject(
@@ -181,17 +183,17 @@ public async Task<IResult> UploadDat(DtoUploadDat request, LocoDb db)
181183
return Results.Accepted($"Object already exists in the database. OriginalName={s5Header.Name} OriginalChecksum={s5Header.Checksum} UploadDate={existingObject!.UploadDate}");
182184
}
183185

184-
const string SettingsPath = "Q:\\Games\\Locomotion\\Server\\CustomObjects";
185186
const string UploadFolder = "UploadedObjects";
186187
var uuid = Guid.NewGuid();
187-
var saveFileName = Path.Combine(SettingsPath, UploadFolder, $"{uuid}.dat");
188+
var saveFileName = Path.Combine(settings.ObjectRootFolder, UploadFolder, $"{uuid}.dat");
188189
File.WriteAllBytes(saveFileName, datFileBytes);
189190

190191
Console.WriteLine($"File accepted OriginalName={s5Header.Name} OriginalChecksum={s5Header.Checksum} PathOnDisk={saveFileName}");
191192

192193
var locoTbl = new TblLocoObject()
193194
{
194195
Name = $"{s5Header.Name}_{s5Header.Checksum}", // same as DB seeder name
196+
PathOnDisk = Path.Combine(UploadFolder, $"{uuid}.dat"),
195197
OriginalName = s5Header.Name,
196198
OriginalChecksum = s5Header.Checksum,
197199
IsVanilla = false, // not possible to upload vanilla objects

deploy.sh renamed to build.sh

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# example usage:
4-
# ./deploy.sh 1.2.3
4+
# ./build.sh 1.2.3
55

66
# 1. Get the version from the first command-line argument
77
if [ -z "$1" ]; then
@@ -12,27 +12,15 @@ version="$1"
1212

1313
echo "Building version $version"
1414

15-
# 2. Write the version to version.txt. This is purely to generate a commit on master for new version
16-
echo "$version" > AvaGui/version.txt
17-
18-
# 3. Make a release commit
19-
git add AvaGui/version.txt
20-
git commit -m "prepare $version"
21-
git push
22-
23-
## 4. Make a tag
24-
git tag -a "$version" -m "tag $version"
25-
git push --tags
26-
27-
## 5. Build the project for different platforms
15+
## 2. Build the project for different platforms
2816
echo "Building"
2917
dotnet publish AvaGui/AvaGui.csproj -c Release -p:PublishSingleFile=true -p:Version=$version --self-contained --runtime win-x64
3018
dotnet publish AvaGui/AvaGui.csproj -c Release -p:PublishSingleFile=true -p:Version=$version --self-contained --runtime linux-x64
3119
dotnet publish AvaGui/AvaGui.csproj -c Release -p:PublishSingleFile=true -p:Version=$version --self-contained --runtime osx-x64
3220

33-
dotnet publish ObjectService/ObjectService.csproj -c Release -p:PublishSingleFile=true -p:Version=$version --self-contained --runtime linux-x64
21+
# dotnet publish ObjectService/ObjectService.csproj -c Release -p:PublishSingleFile=true -p:Version=$version --self-contained --runtime linux-x64
3422

35-
# 6. Create the ZIP and tar archives
23+
# 3. Create the ZIP and tar archives
3624
echo "Zipping"
3725

3826
pushd "AvaGui/bin/Release/net8.0/"

build_object_service.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# example usage:
4+
# ./build.sh 1.2.3
5+
6+
# 1. Get the version from the first command-line argument
7+
if [ -z "$1" ]; then
8+
echo "Error: Please provide the version as the first argument."
9+
exit 1
10+
fi
11+
version="$1"
12+
13+
echo "Building version $version"
14+
15+
dotnet publish ObjectService/ObjectService.csproj -c Release -p:PublishSingleFile=true -p:Version=$version --self-contained --runtime linux-x64
16+
17+
echo "Build and packaging complete!"

tag.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# example usage:
4+
# ./tag.sh 1.2.3
5+
6+
# 1. Get the version from the first command-line argument
7+
if [ -z "$1" ]; then
8+
echo "Error: Please provide the version as the first argument."
9+
exit 1
10+
fi
11+
version="$1"
12+
13+
echo "Building version $version"
14+
15+
# 2. Write the version to version.txt. This is purely to generate a commit on master for new version
16+
echo "$version" > AvaGui/version.txt
17+
18+
# 3. Make a release commit
19+
git add AvaGui/version.txt
20+
git commit -m "prepare $version"
21+
git push
22+
23+
## 4. Make a tag
24+
git tag -a "$version" -m "tag $version"
25+
git push --tags
26+
27+
echo "Tagging complete!"

0 commit comments

Comments
 (0)