Skip to content

Commit f62dc76

Browse files
committed
Merge branch 'newgeneratorapi'
2 parents 6897f7c + 047fa81 commit f62dc76

File tree

6 files changed

+105
-35
lines changed

6 files changed

+105
-35
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace IntelOrca.Biohazard.BioRand.Common.Extensions
4+
{
5+
internal static class DateTimeExtensions
6+
{
7+
public static long ToUnixTimeSeconds(this DateTime dt)
8+
{
9+
var offset = new DateTimeOffset(dt);
10+
return offset.ToUnixTimeSeconds();
11+
}
12+
13+
public static DateTime ToDateTime(this long unixTimeSeconds)
14+
{
15+
var offset = DateTimeOffset.FromUnixTimeSeconds(unixTimeSeconds);
16+
return offset.UtcDateTime;
17+
}
18+
}
19+
}

src/IntelOrca.Biohazard.BioRand.Common/IntelOrca.Biohazard.BioRand.Common.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<Nullable>enable</Nullable>
55
<LangVersion>12.0</LangVersion>
6-
<Version>1.2.9</Version>
6+
<Version>1.3.0</Version>
77
<Title>BioRand Common Library</Title>
88
<Authors>Ted John</Authors>
99
<Product>$(AssemblyName)</Product>

src/IntelOrca.Biohazard.BioRand.Common/RandomizerAgent.cs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
using System.Net.Http;
55
using System.Text;
66
using System.Text.Json;
7+
using System.Text.Json.Serialization;
78
using System.Threading;
89
using System.Threading.Tasks;
10+
using IntelOrca.Biohazard.BioRand.Common.Extensions;
911

1012
namespace IntelOrca.Biohazard.BioRand
1113
{
@@ -15,11 +17,9 @@ public class RandomizerAgent : IDisposable
1517
private const string StatusGenerating = "Generating";
1618
private const string StatusUploading = "Uploading";
1719

20+
private static readonly JsonSerializerOptions _options;
21+
1822
private readonly HttpClient _httpClient = new();
19-
private readonly JsonSerializerOptions _options = new JsonSerializerOptions()
20-
{
21-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
22-
};
2323
private readonly IRandomizerAgentHandler _handler;
2424
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);
2525
private DateTime _lastHeartbeatTime;
@@ -38,7 +38,14 @@ public class RandomizerAgent : IDisposable
3838
public TimeSpan PollTime { get; set; } = TimeSpan.FromSeconds(5);
3939
public TimeSpan RestartTime { get; set; } = TimeSpan.FromSeconds(5);
4040

41-
public bool UseMultiPartFormUpload { get; } = true;
41+
static RandomizerAgent()
42+
{
43+
_options = new JsonSerializerOptions()
44+
{
45+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
46+
};
47+
_options.Converters.Add(new UnixDateTimeConverter());
48+
}
4249

4350
public RandomizerAgent(string baseUri, string apiKey, int gameId, IRandomizerAgentHandler handler)
4451
{
@@ -303,26 +310,25 @@ private async Task GenerateRandomizer(QueueResponseItem q)
303310
_handler.LogInfo($"Uploading rando {q.Id}...");
304311
try
305312
{
306-
if (UseMultiPartFormUpload)
313+
foreach (var asset in output.Assets)
307314
{
308-
await PostFormAsync<object>("generator/end-form", new Dictionary<string, object>
315+
await PostFormAsync<object>("generator/asset", new Dictionary<string, object>
309316
{
310317
["id"] = Id,
311318
["randoId"] = q.Id,
312-
["pakOutput"] = output.PakOutput,
313-
["fluffyOutput"] = output.FluffyOutput
319+
["key"] = asset.Key,
320+
["title"] = asset.Title,
321+
["description"] = asset.Description,
322+
["data"] = asset.Data,
323+
["data.filename"] = asset.FileName
314324
});
315325
}
316-
else
326+
await PostAsync<object>("generator/end", new
317327
{
318-
await PostAsync<object>("generator/end", new
319-
{
320-
Id,
321-
RandoId = q.Id,
322-
output.PakOutput,
323-
output.FluffyOutput
324-
});
325-
}
328+
Id,
329+
RandoId = q.Id,
330+
output.Instructions
331+
});
326332
_handler.LogInfo($"Uploaded rando {q.Id}");
327333
}
328334
catch (Exception ex)
@@ -368,7 +374,7 @@ private async Task<T> PostFormAsync<T>(string path, Dictionary<string, object> f
368374
{
369375
if (kvp.Value is byte[] b)
370376
{
371-
form.Add(new ByteArrayContent(b), kvp.Key, kvp.Key);
377+
form.Add(new ByteArrayContent(b), kvp.Key, (string)formData[$"{kvp.Key}.filename"]);
372378
}
373379
else
374380
{
@@ -402,6 +408,7 @@ public class QueueResponseItem
402408
public int Id { get; set; }
403409
public int GameId { get; set; }
404410
public DateTime Created { get; set; }
411+
public string[] UserTags { get; set; } = [];
405412
public int UserId { get; set; }
406413
public int Seed { get; set; }
407414
public int ConfigId { get; set; }
@@ -419,5 +426,18 @@ public class QueueResponseItem
419426
private class HttpException(HttpStatusCode code) : Exception($"{code} returned from http request")
420427
{
421428
}
429+
430+
private class UnixDateTimeConverter : JsonConverter<DateTime>
431+
{
432+
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
433+
{
434+
return reader.GetInt64().ToDateTime();
435+
}
436+
437+
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
438+
{
439+
writer.WriteNumberValue(value.ToUnixTimeSeconds());
440+
}
441+
}
422442
}
423443
}
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
using System.Collections.Generic;
2+
using System.Collections.Immutable;
23

34
namespace IntelOrca.Biohazard.BioRand
45
{
5-
public class RandomizerOutput
6+
public class RandomizerOutput(
7+
ImmutableArray<RandomizerOutputAsset> assets,
8+
string instructions,
9+
Dictionary<string, string> logs)
610
{
7-
public byte[] PakOutput { get; }
8-
public byte[] FluffyOutput { get; }
9-
public Dictionary<string, string> Logs { get; }
10-
11-
public RandomizerOutput(byte[] pakOutput, byte[] fluffyOutput, Dictionary<string, string> logs)
12-
{
13-
PakOutput = pakOutput;
14-
FluffyOutput = fluffyOutput;
15-
Logs = logs;
16-
}
11+
public ImmutableArray<RandomizerOutputAsset> Assets => assets;
12+
public string Instructions => instructions;
13+
public Dictionary<string, string> Logs => logs;
1714
}
1815
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace IntelOrca.Biohazard.BioRand
2+
{
3+
public sealed class RandomizerOutputAsset(
4+
string key,
5+
string title,
6+
string description,
7+
string fileName,
8+
byte[] data)
9+
{
10+
public string Key => key;
11+
public string Title => title;
12+
public string Description => description;
13+
public string FileName => fileName;
14+
public byte[] Data => data;
15+
}
16+
}

test/IntelOrca.Biohazard.BioRand.Tests/TestAgent.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Threading.Tasks;
43
using Xunit;
54

@@ -26,9 +25,28 @@ private class Handler : IRandomizer, IRandomizerAgentHandler
2625
public RandomizerOutput Randomize(RandomizerInput input)
2726
{
2827
return new RandomizerOutput(
29-
new byte[16],
30-
new byte[16],
31-
new Dictionary<string, string>());
28+
[
29+
new RandomizerOutputAsset(
30+
"1-patch",
31+
"Patch",
32+
"Simply drop this file into your RE 4 install folder.",
33+
"biorand-re4r-58252-mod.zip",
34+
new byte[16]),
35+
new RandomizerOutputAsset(
36+
"2-fluffy",
37+
"Fluffy Mod",
38+
"Drop this zip file into Fluffy Mod Manager's mod folder and enable it.",
39+
"biorand-re4r-58252.zip",
40+
new byte[16])
41+
],
42+
"""
43+
<p class="mt-3">What should I do if my game crashes?</p>
44+
<ol class="ml-8 list-decimal text-gray-300">
45+
<li>Reload from last checkpoint and try again.</li>
46+
<li>Alter the enemy sliders slightly or reduce the number temporarily. This will reshuffle the enemies. Reload from last checkpoint and try again.</li> <li>As a last resort, change your seed, and reload from last checkpoint.</li>
47+
</ol>
48+
""",
49+
[]);
3250
}
3351

3452
public Task<bool> CanGenerateAsync(RandomizerAgent.QueueResponseItem queueItem) => Task.FromResult(true);

0 commit comments

Comments
 (0)