diff --git a/ALttPRandomizer/ALttPRandomizer.csproj b/ALttPRandomizer/ALttPRandomizer.csproj index 4324281..66ee819 100644 --- a/ALttPRandomizer/ALttPRandomizer.csproj +++ b/ALttPRandomizer/ALttPRandomizer.csproj @@ -14,4 +14,13 @@ + + + Always + + + Always + + + diff --git a/ALttPRandomizer/GenerateSeedController.cs b/ALttPRandomizer/GenerateSeedController.cs index 897f8f5..d10a519 100644 --- a/ALttPRandomizer/GenerateSeedController.cs +++ b/ALttPRandomizer/GenerateSeedController.cs @@ -3,10 +3,17 @@ using Microsoft.AspNetCore.Mvc; public class GenerateController : Controller { + public GenerateController(Randomizer randomizer) { + this.Randomizer = randomizer; + } + + private Randomizer Randomizer { get; } + [Route("/generate")] [HttpPost] public ActionResult Generate(SeedSettings settings) { - return Content("Hello world"); + var result = this.Randomizer.Randomize(); + return Content(result); } } } diff --git a/ALttPRandomizer/GenerationFailedException.cs b/ALttPRandomizer/GenerationFailedException.cs new file mode 100644 index 0000000..aab5281 --- /dev/null +++ b/ALttPRandomizer/GenerationFailedException.cs @@ -0,0 +1,7 @@ +namespace ALttPRandomizer { + using System; + + public class GenerationFailedException : Exception { + public GenerationFailedException(string message) : base(message) { } + } +} diff --git a/ALttPRandomizer/IdGenerator.cs b/ALttPRandomizer/IdGenerator.cs new file mode 100644 index 0000000..17f8d00 --- /dev/null +++ b/ALttPRandomizer/IdGenerator.cs @@ -0,0 +1,19 @@ +namespace ALttPRandomizer { + using System; + + public class IdGenerator { + private const string chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; + private const int length = 10; + private static Random random = new Random(); + + public string GenerateId() { + var str = new char[length]; + + for (int i = 0; i < length; i++) { + str[i] = chars[random.Next(chars.Length)]; + } + + return new string(str); + } + } +} diff --git a/ALttPRandomizer/Options/ServiceOptions.cs b/ALttPRandomizer/Options/ServiceOptions.cs new file mode 100644 index 0000000..32ff7e7 --- /dev/null +++ b/ALttPRandomizer/Options/ServiceOptions.cs @@ -0,0 +1,6 @@ +namespace ALttPRandomizer.Options { + public class ServiceOptions { + public string PythonPath { get; set; } = null!; + public string RandomizerPath { get; set; } = null!; + } +} diff --git a/ALttPRandomizer/Program.cs b/ALttPRandomizer/Program.cs index dd73718..1eeaae3 100644 --- a/ALttPRandomizer/Program.cs +++ b/ALttPRandomizer/Program.cs @@ -1,7 +1,9 @@ namespace ALttPRandomizer { using System.Text.Json.Serialization; + using ALttPRandomizer.Options; using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; internal class Program @@ -10,10 +12,20 @@ { var builder = WebApplication.CreateBuilder(args); + builder.Configuration + .AddJsonFile("appsettings.json") + .AddJsonFile("appsettings.Development.json") + .AddEnvironmentVariables(); + + builder.Services.Configure(builder.Configuration.GetSection("ALttPRandomizer")); + builder.Services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())); builder.Services.AddSwaggerGen(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + var app = builder.Build(); app.UseHttpsRedirection(); diff --git a/ALttPRandomizer/Randomizer.cs b/ALttPRandomizer/Randomizer.cs new file mode 100644 index 0000000..f09260a --- /dev/null +++ b/ALttPRandomizer/Randomizer.cs @@ -0,0 +1,56 @@ +namespace ALttPRandomizer { + using ALttPRandomizer.Options; + using Microsoft.Extensions.Options; + using System.Diagnostics; + + public class Randomizer { + public Randomizer(IdGenerator idGenerator, IOptionsMonitor optionsMonitor) { + this.IdGenerator = idGenerator; + this.optionsMonitor = optionsMonitor; + } + + private IOptionsMonitor optionsMonitor; + private IdGenerator IdGenerator; + private ServiceOptions Configuration => optionsMonitor.CurrentValue; + + public string Randomize() { + var start = new ProcessStartInfo() { + FileName = Configuration.PythonPath, + WorkingDirectory = Configuration.RandomizerPath, + RedirectStandardOutput = true, + }; + + var args = start.ArgumentList; + args.Add("DungeonRandomizer.py"); + args.Add("--rom=../alttp.sfc"); + args.Add("--bps"); + + args.Add("--quickswap"); + + var process = Process.Start(start) ?? throw new GenerationFailedException("Process failed to start."); + process.EnableRaisingEvents = true; + + var id = IdGenerator.GenerateId(); + + process.Exited += (sender, args) => { + var exitcode = process.ExitCode; + process.Dispose(); + + if (exitcode != 0) { + this.GenerationFailed(id, exitcode); + } else { + this.GenerationSucceeded(id); + } + }; + + return id; + } + + private void GenerationSucceeded(string id) { + + } + + private void GenerationFailed(string id, int exitcode) { + } + } +} diff --git a/ALttPRandomizer/appsettings.Development.json b/ALttPRandomizer/appsettings.Development.json new file mode 100644 index 0000000..f6af898 --- /dev/null +++ b/ALttPRandomizer/appsettings.Development.json @@ -0,0 +1,6 @@ +{ + "ALttPRandomizer": { + "pythonPath": "C:\\Program Files (x86)\\Python38-32\\python", + "randomizerPath": "C:\\Users\\ardna\\git-projects\\ALttPRandomizer\\ALttPDoorRandomizer" + } +} \ No newline at end of file diff --git a/ALttPRandomizer/appsettings.json b/ALttPRandomizer/appsettings.json new file mode 100644 index 0000000..6a4adb9 --- /dev/null +++ b/ALttPRandomizer/appsettings.json @@ -0,0 +1,4 @@ +{ + "ALttPRandomizer": { + } +} \ No newline at end of file