]> git.localhorst.tv Git - alttp.git/blob - app/Console/Commands/GenerateAosSeed.php
faster implementation for generating AoSR seeds
[alttp.git] / app / Console / Commands / GenerateAosSeed.php
1 <?php
2
3 namespace App\Console\Commands;
4
5 use App\Beat\Encoder;
6 use App\Models\AosSeed;
7 use Illuminate\Console\Command;
8 use Illuminate\Support\Facades\Storage;
9 use Symfony\Component\Process\Process;
10
11 class GenerateAosSeed extends Command
12 {
13         /**
14          * The name and signature of the console command.
15          *
16          * @var string
17          */
18         protected $signature = 'aos:generate {id}';
19
20         /**
21          * The console command description.
22          *
23          * @var string
24          */
25         protected $description = 'Generate AoS seed';
26
27         /**
28          * Execute the console command.
29          *
30          * @return int
31          */
32         public function handle()
33         {
34                 $seed = AosSeed::findOrFail($this->argument('id'));
35                 $seed->status = 'generating';
36                 $seed->error_detail = null;
37                 $seed->save();
38
39                 $stage = 'initial';
40                 try {
41                         $temp_dir = sys_get_temp_dir();
42
43                         $params = array_merge(['seed' => $seed->seed], $seed->settings);
44                         $settings = http_build_query($params, '', '&');
45
46                         $romFile = $temp_dir.'/'.$seed->hash.'.gba';
47                         $spoilerFile = $temp_dir.'/'.$seed->hash.'.txt';
48
49                         $stage = 'randomizing';
50                         $proc = new Process([config('aos.cli'), config('aos.base_rom'), $romFile, $settings]);
51                         $proc->mustRun();
52
53                         $stage = 'calculating patch';
54                         $encoder = new Encoder(file_get_contents(config('aos.base_rom')));
55                         $patch = $encoder->createPatch(file_get_contents($romFile));
56                         Storage::disk('aos-seeds')->put($seed->hash.'.bps', $patch);
57                         unlink($romFile);
58
59                         $stage = 'saving spoiler';
60                         Storage::disk('aos-spoilers')->put($seed->hash.'.txt', $proc->getOutput());
61
62                         $stage = 'done';
63                         $seed->status = 'generated';
64                         $seed->save();
65                 } catch (\Throwable $e) {
66                         $seed->status = 'error';
67                         $seed->error_detail = [
68                                 'stage' => $stage,
69                                 'type' => get_class($e),
70                                 'message' => $e->getMessage(),
71                         ];
72                         $seed->save();
73                         return 1;
74                 }
75
76                 return 0;
77         }
78 }