Upload patchfiles to azure blobstore
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
*.sfc
|
*.sfc
|
||||||
|
appsettings.Development.json
|
||||||
|
|
||||||
## Ignore Visual Studio temporary files, build results, and
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
## files generated by popular Visual Studio add-ons.
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Azure.Identity" Version="1.13.2" />
|
||||||
|
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
|
||||||
<PackageReference Include="Microsoft.Azure.WebPubSub.AspNetCore" Version="1.4.0" />
|
<PackageReference Include="Microsoft.Azure.WebPubSub.AspNetCore" Version="1.4.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
|
||||||
<PackageReference Include="System.Text.Json" Version="9.0.2" />
|
<PackageReference Include="System.Text.Json" Version="9.0.2" />
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
namespace ALttPRandomizer.Options {
|
namespace ALttPRandomizer.Options {
|
||||||
|
using System;
|
||||||
|
|
||||||
public class ServiceOptions {
|
public class ServiceOptions {
|
||||||
public string PythonPath { get; set; } = null!;
|
public string PythonPath { get; set; } = null!;
|
||||||
public string RandomizerPath { get; set; } = null!;
|
public string RandomizerPath { get; set; } = null!;
|
||||||
|
public AzureSettings AzureSettings { get; set; } = new AzureSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AzureSettings {
|
||||||
|
public Uri BlobstoreEndpoint { get; set; } = null!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,13 @@
|
|||||||
{
|
{
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using ALttPRandomizer.Options;
|
using ALttPRandomizer.Options;
|
||||||
|
using Azure.Identity;
|
||||||
|
using Azure.Storage.Blobs;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
@@ -19,10 +23,19 @@
|
|||||||
|
|
||||||
builder.Services.Configure<ServiceOptions>(builder.Configuration.GetSection("ALttPRandomizer"));
|
builder.Services.Configure<ServiceOptions>(builder.Configuration.GetSection("ALttPRandomizer"));
|
||||||
|
|
||||||
|
builder.Services.AddLogging(lb => lb.AddConsole());
|
||||||
|
|
||||||
builder.Services.AddControllers().AddJsonOptions(x =>
|
builder.Services.AddControllers().AddJsonOptions(x =>
|
||||||
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
|
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
var provider = builder.Services.BuildServiceProvider();
|
||||||
|
var settings = provider.GetRequiredService<IOptionsMonitor<ServiceOptions>>().CurrentValue!;
|
||||||
|
|
||||||
|
var token = new DefaultAzureCredential();
|
||||||
|
var seedClient = new BlobContainerClient(settings.AzureSettings.BlobstoreEndpoint, token);
|
||||||
|
|
||||||
|
builder.Services.AddSingleton(seedClient);
|
||||||
builder.Services.AddScoped<Randomizer, Randomizer>();
|
builder.Services.AddScoped<Randomizer, Randomizer>();
|
||||||
builder.Services.AddScoped<IdGenerator, IdGenerator>();
|
builder.Services.AddScoped<IdGenerator, IdGenerator>();
|
||||||
|
|
||||||
|
|||||||
@@ -1,53 +1,104 @@
|
|||||||
namespace ALttPRandomizer {
|
namespace ALttPRandomizer {
|
||||||
using ALttPRandomizer.Options;
|
using ALttPRandomizer.Options;
|
||||||
|
using Azure.Storage.Blobs;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class Randomizer {
|
public class Randomizer {
|
||||||
public Randomizer(IdGenerator idGenerator, IOptionsMonitor<ServiceOptions> optionsMonitor) {
|
public Randomizer(
|
||||||
|
IdGenerator idGenerator,
|
||||||
|
BlobContainerClient seedClient,
|
||||||
|
IOptionsMonitor<ServiceOptions> optionsMonitor,
|
||||||
|
ILogger<Randomizer> logger) {
|
||||||
this.IdGenerator = idGenerator;
|
this.IdGenerator = idGenerator;
|
||||||
this.optionsMonitor = optionsMonitor;
|
this.SeedClient = seedClient;
|
||||||
|
this.OptionsMonitor = optionsMonitor;
|
||||||
|
this.Logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IOptionsMonitor<ServiceOptions> optionsMonitor;
|
private BlobContainerClient SeedClient { get; }
|
||||||
private IdGenerator IdGenerator;
|
private IOptionsMonitor<ServiceOptions> OptionsMonitor { get; }
|
||||||
private ServiceOptions Configuration => optionsMonitor.CurrentValue;
|
private IdGenerator IdGenerator { get; }
|
||||||
|
private ILogger<Randomizer> Logger { get; }
|
||||||
|
private ServiceOptions Configuration => this.OptionsMonitor.CurrentValue;
|
||||||
|
|
||||||
public string Randomize() {
|
public string Randomize() {
|
||||||
var start = new ProcessStartInfo() {
|
var start = new ProcessStartInfo() {
|
||||||
FileName = Configuration.PythonPath,
|
FileName = Configuration.PythonPath,
|
||||||
WorkingDirectory = Configuration.RandomizerPath,
|
WorkingDirectory = Configuration.RandomizerPath,
|
||||||
RedirectStandardOutput = true,
|
RedirectStandardOutput = true,
|
||||||
|
RedirectStandardError = true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var id = IdGenerator.GenerateId();
|
||||||
|
|
||||||
var args = start.ArgumentList;
|
var args = start.ArgumentList;
|
||||||
args.Add("DungeonRandomizer.py");
|
args.Add("DungeonRandomizer.py");
|
||||||
args.Add("--rom=../alttp.sfc");
|
args.Add("--rom=../alttp.sfc");
|
||||||
args.Add("--bps");
|
args.Add("--bps");
|
||||||
|
|
||||||
|
args.Add("--outputpath");
|
||||||
|
args.Add(Path.GetTempPath());
|
||||||
|
|
||||||
|
args.Add("--outputname");
|
||||||
|
args.Add(id);
|
||||||
|
|
||||||
args.Add("--quickswap");
|
args.Add("--quickswap");
|
||||||
|
|
||||||
var process = Process.Start(start) ?? throw new GenerationFailedException("Process failed to start.");
|
var process = Process.Start(start) ?? throw new GenerationFailedException("Process failed to start.");
|
||||||
process.EnableRaisingEvents = true;
|
process.EnableRaisingEvents = true;
|
||||||
|
|
||||||
var id = IdGenerator.GenerateId();
|
process.OutputDataReceived += (_, args) => this.Logger.LogInformation("Randomizer STDOUT: {output}", args.Data);
|
||||||
|
process.ErrorDataReceived += (_, args) => this.Logger.LogInformation("Randomizer STDERR: {output}", args.Data);
|
||||||
|
|
||||||
process.Exited += (sender, args) => {
|
process.BeginOutputReadLine();
|
||||||
|
process.BeginErrorReadLine();
|
||||||
|
|
||||||
|
process.Exited += async (sender, args) => {
|
||||||
var exitcode = process.ExitCode;
|
var exitcode = process.ExitCode;
|
||||||
process.Dispose();
|
process.Dispose();
|
||||||
|
|
||||||
if (exitcode != 0) {
|
if (exitcode != 0) {
|
||||||
this.GenerationFailed(id, exitcode);
|
this.GenerationFailed(id, exitcode);
|
||||||
} else {
|
} else {
|
||||||
this.GenerationSucceeded(id);
|
await this.GenerationSucceeded(id);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerationSucceeded(string id) {
|
private async Task GenerationSucceeded(string id) {
|
||||||
|
var rom = Path.Join(Path.GetTempPath(), string.Format("DR_{0}.sfc", id));
|
||||||
|
|
||||||
|
var bpsIn = Path.Join(Path.GetTempPath(), string.Format("DR_{0}.bps", id));
|
||||||
|
var bpsOut = string.Format("{0}/patch.bps", id);
|
||||||
|
|
||||||
|
var spoilerIn = Path.Join(Path.GetTempPath(), string.Format("DR_{0}_Spoiler.txt", id));
|
||||||
|
var spoilerOut = string.Format("{0}/spoiler.txt", id);
|
||||||
|
|
||||||
|
var uploadPatch = UploadFile(bpsOut, bpsIn);
|
||||||
|
var uploadSpoiler = UploadFile(spoilerOut, spoilerIn);
|
||||||
|
|
||||||
|
await Task.WhenAll(uploadPatch, uploadSpoiler);
|
||||||
|
|
||||||
|
File.Delete(rom);
|
||||||
|
|
||||||
|
this.Logger.LogDebug("Finished uploading seed id {id}", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task UploadFile(string name, string filepath) {
|
||||||
|
using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read)) {
|
||||||
|
this.Logger.LogDebug("Uploading file {filepath} -> {name}", filepath, name);
|
||||||
|
await this.SeedClient.UploadBlobAsync(name, stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Logger.LogDebug("Deleting file {filepath}", filepath);
|
||||||
|
File.Delete(filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerationFailed(string id, int exitcode) {
|
private void GenerationFailed(string id, int exitcode) {
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"ALttPRandomizer": {
|
|
||||||
"pythonPath": "C:\\Program Files (x86)\\Python38-32\\python",
|
|
||||||
"randomizerPath": "C:\\Users\\ardna\\git-projects\\ALttPRandomizer\\ALttPDoorRandomizer"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user