= 0x18 && $h['res'] <= strlen($bytes) && $h['code'] + 2 >= 0x18 && $h['code'] + 2 < $h['res'] && ($h['before'] === 0 || 10 + $h['before'] < $h['res']) && ($h['after'] === 0 || 6 + $h['after'] < $h['res']); } /** From the unpacked bytes (the VM's image). */ public static function fromBytes(string $name, string $bytes, int $pack = 0, int $check = 1, ?string $specs = null): self { $h = unpack('vid/vcode/Cu1/Cu2/Vafter/Vbefore/Vres/vw1/vvram/vw3', $bytes); $s = new self(); $s->name = $name; $s->id = $h['id']; $s->class1 = $h['u1']; $s->class2 = $h['u2']; $s->words1 = $h['w1']; $s->vram = $h['vram']; $s->words3 = $h['w3']; $s->pack = $pack; $s->check = $check; $s->specs = $specs; $s->main = $h['code'] + 2; $s->before = $h['before'] ? 10 + $h['before'] : 0; $s->after = $h['after'] ? 6 + $h['after'] : 0; $s->code = substr($bytes, 0x18, $h['res'] - 0x18); $s->resources = substr($bytes, $h['res']); return $s; } /** The unpacked bytes (what the VM loads). */ public function bytes(): string { $res = 0x18 + strlen($this->code); return pack('vvCCVVVvvv', $this->id, $this->main - 2, $this->class1, $this->class2, $this->after ? $this->after - 6 : 0, $this->before ? $this->before - 10 : 0, $res, $this->words1, $this->vram, $this->words3) . $this->code . $this->resources; } /** The .io file of the script, not packed (magic: the file size, packing byte 0). */ public function ioFile(): string { $body = $this->bytes(); $head = $this->check === 0 ? 6 + 16 : 6; return pack('Vv', $head + strlen($body), $this->check) . ($this->check === 0 ? str_pad((string) $this->specs, 16, "\0") : '') . $body; } }