Refactor processing running and merge apr2025 generator into
BaseRandomizer
This commit is contained in:
41
ALttPRandomizer/Service/ProcessService.cs
Normal file
41
ALttPRandomizer/Service/ProcessService.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
namespace ALttPRandomizer.Service {
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
public class ProcessService {
|
||||
public ProcessService(ILogger<ProcessService> logger) {
|
||||
this.Logger = logger;
|
||||
}
|
||||
|
||||
public ILogger<ProcessService> Logger { get; }
|
||||
|
||||
public Process StartProcess(string logPrefix, string workingDirectory, params string[] args) {
|
||||
var start = new ProcessStartInfo(args[0], args.Skip(1)) {
|
||||
WorkingDirectory = workingDirectory,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
|
||||
this.Logger.LogInformation("{prefix} - executing command: {command}", logPrefix, string.Join(" ", args.Select(arg => arg.Contains(' ') ? $"\"{arg}\"" : arg)));
|
||||
|
||||
var process = Process.Start(start) ?? throw new GenerationFailedException("{0} - Process failed to start.", logPrefix);
|
||||
process.EnableRaisingEvents = true;
|
||||
|
||||
process.OutputDataReceived += (_, args) => {
|
||||
if (args.Data != null) {
|
||||
Logger.LogInformation("{prefix} - STDOUT: {output}", logPrefix, args.Data);
|
||||
}
|
||||
};
|
||||
process.ErrorDataReceived += (_, args) => {
|
||||
if (args.Data != null) {
|
||||
Logger.LogInformation("{prefix} STDERR: {output}", logPrefix, args.Data);
|
||||
}
|
||||
};
|
||||
|
||||
process.BeginOutputReadLine();
|
||||
process.BeginErrorReadLine();
|
||||
return process;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,24 +2,18 @@
|
||||
using ALttPRandomizer.Azure;
|
||||
using ALttPRandomizer.Model;
|
||||
using ALttPRandomizer.Randomizers;
|
||||
using ALttPRandomizer.Settings;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class RandomizeService {
|
||||
public RandomizeService(
|
||||
IdGenerator idGenerator,
|
||||
IServiceProvider serviceProvider,
|
||||
BaseRandomizer baseRandomizer,
|
||||
AzureStorage azureStorage,
|
||||
ILogger<RandomizeService> logger) {
|
||||
this.IdGenerator = idGenerator;
|
||||
this.ServiceProvider = serviceProvider;
|
||||
this.BaseRandomizer = baseRandomizer;
|
||||
this.AzureStorage = azureStorage;
|
||||
this.Logger = logger;
|
||||
@@ -29,25 +23,15 @@
|
||||
|
||||
private IdGenerator IdGenerator { get; }
|
||||
private BaseRandomizer BaseRandomizer { get; }
|
||||
private IServiceProvider ServiceProvider { get; }
|
||||
private AzureStorage AzureStorage { get; }
|
||||
|
||||
public async Task<string> RandomizeSeed(SeedSettings settings, string? seedId = null) {
|
||||
var id = seedId ?? this.IdGenerator.GenerateId();
|
||||
this.Logger.LogInformation("Generating seed {seedId} with settings {@settings}", id, settings);
|
||||
|
||||
var fi = typeof(RandomizerInstance).GetField(settings.Randomizer.ToString(), BindingFlags.Static | BindingFlags.Public);
|
||||
this.BaseRandomizer.Validate(settings);
|
||||
|
||||
var randomizerKey = fi?.GetCustomAttribute<RandomizerNameAttribute>()?.Name;
|
||||
|
||||
if (randomizerKey == null) {
|
||||
throw new InvalidSettingsException("Invalid randomizer: {0}", settings.Randomizer);
|
||||
}
|
||||
|
||||
var randomizer = this.ServiceProvider.GetRequiredKeyedService<IRandomizer>(randomizerKey);
|
||||
randomizer.Validate(settings);
|
||||
|
||||
await randomizer.Randomize(id, settings, seedId == null);
|
||||
await this.BaseRandomizer.Randomize(id, settings, seedId == null);
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user