3 namespace App\Console\Commands;
6 use App\Models\AlttpSeed;
7 use Illuminate\Console\Command;
8 use Illuminate\Support\Facades\Http;
9 use Illuminate\Support\Facades\Storage;
10 use Symfony\Component\Process\Process;
12 class GenerateAlttpSeed extends Command
15 * The name and signature of the console command.
19 protected $signature = 'alttp:generate {id}';
22 * The console command description.
26 protected $description = 'Generate ALttP seed';
29 * Execute the console command.
33 public function handle()
35 $seed = AlttpSeed::findOrFail($this->argument('id'));
36 $seed->status = 'generating';
37 $seed->error_detail = null;
40 $temp_dir = sys_get_temp_dir();
41 $rom_file = $temp_dir.'/DR_'.$seed->hash.'.sfc';
42 $settings_file = $temp_dir.'/'.$seed->hash.'.json';
43 $spoiler_file = $temp_dir.'/DR_'.$seed->hash.'_Spoiler.txt';
49 $settings = array_merge([
50 'calc_playthrough' => true,
52 'create_spoiler' => true,
54 'enemizercli' => base_path().'/vendor/z3/enemizer_linux/EnemizerCLI.Core',
57 'outputpath' => $temp_dir,
58 'outputname' => $seed->hash,
59 'race' => $seed->race,
60 'rom' => config('alttp.base_rom'),
61 'seed' => $seed->seed,
63 file_put_contents($settings_file, json_encode($settings));
66 $proc = new Process([config('alttp.doors_cli'), '--settingsfile', $settings_file]);
67 $proc->setWorkingDirectory(dirname(config('alttp.doors_cli')));
70 $stage = 'calculating patch';
71 $encoder = new Encoder(file_get_contents(config('alttp.base_rom')));
72 $patch = $encoder->createPatch(file_get_contents($rom_file));
73 $spoiler = file_get_contents($spoiler_file);
74 Storage::disk('alttp-seeds')->put($seed->hash.'.bps', $patch);
75 Storage::disk('alttp-spoilers')->put($seed->hash.'.txt', $spoiler);
78 $seed->status = 'generated';
80 } catch (\Throwable $e) {
81 $this->error($e->getMessage());
82 $seed->status = 'error';
83 $seed->error_detail = [
85 'type' => get_class($e),
86 'message' => $e->getMessage(),
92 if (file_exists($rom_file)) unlink($rom_file);
93 if (file_exists($settings_file)) unlink($settings_file);
94 if (file_exists($spoiler_file)) unlink($spoiler_file);