202 lines
11 KiB
Python
202 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""Guarded Rocketship ownership/attachment evidence and bounded projections.
|
|
|
|
Reads originals without executing them. Projections describe loader selection,
|
|
engine transitions and the normal impact tail; they are not live-game probes.
|
|
"""
|
|
from __future__ import annotations
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import math
|
|
from pathlib import Path
|
|
import struct
|
|
|
|
from checkpoint_script_state import EXE_SHA256, find_case, read_va
|
|
from pe_inspect import PE
|
|
|
|
ROCKET_ID = 0x33524f43
|
|
BODIES = (
|
|
('owned_setup',0x422420,0x4224ee,'97380f20076b8ff8851f74727fc17d3ba41b2c2b7e56c16bd410baf031fe3898'),
|
|
('ship_ctor',0x43d840,0x43da40,'33d27053c2070f0c278bf3e14808d16c4479893d1ccb16c1ac8e199dfd9e53ba'),
|
|
('postload',0x43db10,0x43dcf4,'af2007e942ed715ce8dd03bb725a7ab10bb536444a88f227c25f83527d18c15b'),
|
|
('ship_update',0x43df70,0x43e7db,'707073786f6b19d6f77624ba4804376685652cfbd6dc3443143ca533e266de09'),
|
|
('engine',0x43ea80,0x43eb31,'c154bb368050f16079288ee806be45879b219d2de7446672d7b1eb60d572ea09'),
|
|
('class_id',0x462220,0x46222e,'8ae2a2a865683cb6583081b67d18ed787f53e6eeb3266a24bd6b9ed46df0ad80'),
|
|
('reuse',0x46e6a0,0x46e70c,'bd2f9447ddcfbce9c1a58382d027a7450ea898ebd616d81a581a3ba5c9d5df8f'),
|
|
('mount',0x42944d,0x429686,'2e8e15c52bf02a06afe2a56ccf8b31551368aefcde046c444ddb01a3ef978c70'),
|
|
('cleanup',0x4284fb,0x42871d,'0474c58185373ce9e7a546ddaf47790bbb5193ecaa4fc9223bbecef9eead2c21'),
|
|
('rider_damage',0x42a870,0x42a8c7,'59bfbe871fba2dea6bdbfca97db6fa8296db51b8b561582cda69f68ca7021efd'),
|
|
('jimmy_drive_alias',0x422b04,0x422b18,'12f09afaef07ba842d5b0897ac8bba5223a4b59a9e58d7b4bc38f55c61955d89'),
|
|
('dog_rock_alias',0x41ce2c,0x41ce40,'94fe59a673561374f0f3b3ec1b6dd1eb8dabbdc45e926c0463e670a616df56af'),
|
|
('engine_input',0x41ab12,0x41ac0e,'ec4cf4afb58528ca0baf325869ad78d9d6753ab13efdd5a95b2b815bf5f5bbc8'),
|
|
('control_store',0x475a40,0x475a4c,'d397cb6847c4ff58e32aac989afc348612b5285a4f03538ea6c016a4fdddda3c'),
|
|
('dynamic_inhibit',0x471940,0x47194d,'fa5b69a6074a26a0b8dd535d288645d2875039673639282571e029a60479cd37'),
|
|
)
|
|
|
|
|
|
def f32(value):
|
|
return struct.unpack('<f',struct.pack('<f',value))[0]
|
|
|
|
|
|
def collect(original: Path | str):
|
|
pe=PE(find_case(Path(original),'Neutron.exe'))
|
|
if hashlib.sha256(pe.data).hexdigest()!=EXE_SHA256:
|
|
raise ValueError('Unexpected original EXE identity')
|
|
bodies=[]
|
|
for name,start,end,expected in BODIES:
|
|
digest=hashlib.sha256(read_va(pe,start,end-start)).hexdigest()
|
|
if digest!=expected: raise ValueError(f'Changed lifecycle span {name}')
|
|
bodies.append(dict(name=name,va=hex(start),end_va=hex(end),
|
|
file_offset=hex(pe.offset(start-pe.base)),sha256=digest))
|
|
tables=((0x4b25ac,0x40c,0x43db10),(0x4b25ac,0x3c4,0x43df70),
|
|
(0x4b25ac,0x1e4,0x471940),(0x4b2a4c,0x128,0x43ea80),
|
|
(0x4a3bdc,0x1a0,0x42a870),(0x4a3bdc,0x1e0,0x425120))
|
|
for table,slot,target in tables:
|
|
if struct.unpack('<I',read_va(pe,table+slot,4))[0]!=target:
|
|
raise ValueError('Changed lifecycle virtual target')
|
|
for va,text in [(0x4ef274,b'HIDRIVE'),(0x4ef27c,b'jimdrive.ase'),
|
|
(0x4ee9a8,b'HIROCK'),(0x4ee9b0,b'godrocket.ASE'),(0x4f0990,b'ROCK')]:
|
|
if read_va(pe,va,len(text)+1)!=text+b'\0':raise ValueError('Changed animation binding')
|
|
for va,fmt,value in [(0x495324,'f',20.),(0x492a58,'d',1.),(0x48e5d0,'d',.5)]:
|
|
if struct.unpack('<'+fmt,read_va(pe,va,struct.calcsize(fmt)))[0]!=value:
|
|
raise ValueError('Changed impact scale constant')
|
|
return dict(sha256=EXE_SHA256,bodies=bodies,virtuals=[dict(table=hex(t),slot=hex(s),target=hex(v)) for t,s,v in tables],
|
|
owned_pointer='Jimmy complete+AB0',rider_pointer='rocket complete+740/interface+680',
|
|
reuse_id=ROCKET_ID,reuse_flag='interface+9',retained_flag='interface+11/+139=0',
|
|
rider_local=[0,0,0],companion_local=[0,10,-25],fire_local=[0,0,0],
|
|
scope='Guarded static lifecycle and bounded projections; no whole-game or original lifecycle execution')
|
|
|
|
|
|
def resolve_reused(entries):
|
|
"""46E6A0 predicate, then separate46E6E0 first-ID lookup, in registry order.
|
|
|
|
Entries are (identity, ObjectID, reusable). The lookup does not repeat the
|
|
reusable check. Normal Jimmy construction supplies the first/eligible ship.
|
|
"""
|
|
entries=list(entries)
|
|
if not any(object_id==ROCKET_ID and reusable for _,object_id,reusable in entries):return None
|
|
return next(identity for identity,object_id,_ in entries if object_id==ROCKET_ID)
|
|
|
|
|
|
def engine_transition(previous,handle_present,requested):
|
|
if previous not in (0,1,2) or requested not in (0,1,2):raise ValueError('Bounded engine states are0/1/2')
|
|
if requested in (1,2) and previous==requested:
|
|
return dict(stop=False,start=None,mode=requested)
|
|
return dict(stop=bool(handle_present),start={0:None,1:195,2:196}[requested],mode=requested)
|
|
|
|
|
|
def engine_input(up,down,speed):
|
|
if not math.isfinite(speed):raise ValueError('Nonfinite scalar')
|
|
return 2 if up else 0 if down or speed<=0 else 1
|
|
|
|
|
|
def impact_tail(active,timer,duration,impulse_flag,seconds,drive_burst=False):
|
|
if not all(math.isfinite(v) for v in (timer,duration,seconds)) or timer<0 or duration<0 or not 0<=seconds<1:
|
|
raise ValueError('Unsupported impact input')
|
|
impulses=[]
|
|
scale=None
|
|
drive=False
|
|
if active:
|
|
if impulse_flag:impulses.append(-700.)
|
|
#Source fst stores E88 but retains the unrounded x87 sum for comparison
|
|
#and the sine calculation. Double is a projection, not x87 emulation.
|
|
source_time=float(f32(timer))+float(f32(seconds))
|
|
timer=f32(source_time)
|
|
if source_time>duration:
|
|
active=impulse_flag=False
|
|
scale=(1.,1.,1.,1.)
|
|
drive=True
|
|
else:
|
|
pulse=f32((math.sin(source_time*20.)+1.)*.5)
|
|
scale=(1.,pulse,pulse,1.)
|
|
if drive_burst:impulses.append(-750.)
|
|
return dict(active=active,timer=timer,impulse_flag=impulse_flag,scale=scale,
|
|
select_drive=drive,queued_y=impulses)
|
|
|
|
|
|
def impact_probe_source(original):
|
|
collect(original)
|
|
pe=PE(find_case(Path(original),'Neutron.exe'))
|
|
code=read_va(pe,0x43e6e8,0x43e7db-0x43e6e8)
|
|
pairs=[]
|
|
for target in (0x495324,0x492a58,0x48e5d0):
|
|
encoded=struct.pack('<I',target)
|
|
for index in range(len(code)-3):
|
|
if code[index:index+4]==encoded:pairs.append((0x43e6e8+index,target))
|
|
return IMPACT_PROBE.replace('/* RELOCATIONS */',','.join('{0x%x,0x%x}'%p for p in pairs))
|
|
|
|
|
|
IMPACT_PROBE=r'''/* Generated isolated copied43E6E8..43E7DB impact tail. */
|
|
#include <windows.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#define TC __attribute__((thiscall))
|
|
#define AT(a) (mapped+(a)-0x400000)
|
|
#define F(g,o) (*(float*)((g)+(o)))
|
|
static unsigned char*mapped,*thunk,object[0x1000],rider[0x200],render[0x100];
|
|
static uintptr_t table[0x400/4],rider_table[0x200/4];
|
|
static int selected,impulse_count;static float impulses[4];
|
|
static void require(int ok,const char*what){if(!ok){fprintf(stderr,"FAIL %s\n",what);exit(2);}}
|
|
static void TC missing(void*self){(void)self;require(0,"unexpected virtual");}
|
|
static void TC select_drive(void*self,const char*name,int restart){(void)self;(void)name;(void)restart;selected++;}
|
|
static void TC impulse(void*self,float x,float y,float z){(void)self;require(x==0&&z==0&&impulse_count<4,"impulse shape");impulses[impulse_count++]=y;}
|
|
static void map_copy(void){
|
|
mapped=VirtualAlloc(NULL,0x299000,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);require(mapped!=NULL,"map source");
|
|
FILE*f=fopen("Neutron.exe","rb");require(f!=NULL,"copied EXE");fseek(f,0,SEEK_END);long size=ftell(f);rewind(f);require(size==1019904,"source size");
|
|
unsigned char*d=malloc(size);require(d!=NULL,"source buffer");require(fread(d,1,size,f)==(size_t)size,"read source");fclose(f);
|
|
require(*(uint16_t*)d==IMAGE_DOS_SIGNATURE,"DOS");uint32_t off=*(uint32_t*)(d+0x3c);require(off<(uint32_t)size-sizeof(IMAGE_NT_HEADERS32),"NT bounds");
|
|
IMAGE_NT_HEADERS32*nt=(void*)(d+off);require(nt->Signature==IMAGE_NT_SIGNATURE&&nt->FileHeader.Machine==IMAGE_FILE_MACHINE_I386&&nt->OptionalHeader.ImageBase==0x400000&&nt->OptionalHeader.SizeOfImage==0x299000,"PE identity");
|
|
IMAGE_SECTION_HEADER*s=IMAGE_FIRST_SECTION(nt);require((unsigned char*)(s+nt->FileHeader.NumberOfSections)<=d+size,"section table");
|
|
for(unsigned i=0;i<nt->FileHeader.NumberOfSections;i++){require((uint64_t)s[i].PointerToRawData+s[i].SizeOfRawData<=(uint64_t)size&&(uint64_t)s[i].VirtualAddress+s[i].SizeOfRawData<=0x299000,"section bounds");memcpy(mapped+s[i].VirtualAddress,d+s[i].PointerToRawData,s[i].SizeOfRawData);}free(d);
|
|
static const struct{uint32_t va,target;} patches[]={/* RELOCATIONS */};
|
|
for(unsigned i=0;i<sizeof(patches)/sizeof(patches[0]);i++){uint32_t*p=(void*)AT(patches[i].va);require(*p==patches[i].target,"operand guard");*p=(uint32_t)AT(patches[i].target);}
|
|
for(unsigned i=0;i<sizeof(table)/sizeof(table[0]);i++)table[i]=(uintptr_t)missing;
|
|
for(unsigned i=0;i<sizeof(rider_table)/sizeof(rider_table[0]);i++)rider_table[i]=(uintptr_t)missing;
|
|
table[0x280/4]=(uintptr_t)impulse;rider_table[0xe0/4]=(uintptr_t)select_drive;
|
|
const unsigned char code[]={0x83,0xec,0x38,0x53,0x55,0x56,0x57,0x8b,0xf1,0x33,0xdb,0xbf,1,0,0,0,0xe9,0,0,0,0};
|
|
thunk=VirtualAlloc(NULL,sizeof(code),MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);require(thunk!=NULL,"frame thunk");memcpy(thunk,code,sizeof(code));*(int32_t*)(thunk+17)=(int32_t)(AT(0x43e6e8)-(thunk+21));
|
|
}
|
|
static void run(const char*name,int active,float timer,float duration,int strong,float dt,int burst){
|
|
memset(object,0,sizeof(object));memset(render,0,sizeof(render));memset(rider,0,sizeof(rider));unsigned char*g=object+0xc0;
|
|
*(uintptr_t**)g=table;*(uintptr_t**)rider=rider_table;*(void**)(g+0x680)=rider;*(void**)(g+0x500)=render;
|
|
F(g,0xe88)=timer;F(g,0xec4)=duration;g[0xe84]=active;g[0xed0]=strong;g[0xec0]=burst;
|
|
for(int i=0;i<4;i++)F(render,0x4c+i*4)=1.25f;
|
|
selected=impulse_count=0;memset(impulses,0,sizeof(impulses));((void(TC*)(void*,float))thunk)(g,dt);
|
|
printf("CASE %s active=%u timer=%.9g strong=%u scale=%.9g,%.9g,%.9g,%.9g drive=%d count=%d impulses=%.9g,%.9g\n",name,g[0xe84],F(g,0xe88),g[0xed0],F(render,0x4c),F(render,0x50),F(render,0x54),F(render,0x58),selected,impulse_count,impulses[0],impulses[1]);
|
|
}
|
|
int main(void){map_copy();
|
|
run("inactive",0,.75f,1,0,.1f,0);
|
|
run("ordinary",1,0,1,0,1.f/60,0);
|
|
run("equal",1,.5f,1,0,.5f,0);
|
|
run("expired",1,.9f,1,0,.2f,0);
|
|
run("strong",1,.1f,1,1,.1f,0);
|
|
run("expired_both",1,1,1,1,.1f,1);
|
|
run("inactive_burst",0,.75f,1,0,.1f,1);
|
|
run("rounded_expired",1,.9999999403953552f,1,0,.0000001f,0);
|
|
puts("ROCKET_IMPACT_COMPONENT_COMPLETE:");return 0;}
|
|
'''
|
|
|
|
|
|
def main():
|
|
cli=argparse.ArgumentParser(description=__doc__)
|
|
cli.add_argument('original',type=Path)
|
|
cli.add_argument('--output',type=Path)
|
|
cli.add_argument('--probe-source',type=Path)
|
|
args=cli.parse_args();report=collect(args.original)
|
|
if args.probe_source:
|
|
target=args.probe_source.resolve()
|
|
if target.is_relative_to(args.original.resolve()):cli.error('Probe must be outside originals')
|
|
target.parent.mkdir(parents=True,exist_ok=True);target.write_text(impact_probe_source(args.original))
|
|
text=json.dumps(report,indent=2,allow_nan=False)+'\n'
|
|
if args.output:
|
|
target=args.output.resolve();original=args.original.resolve()
|
|
if target.is_relative_to(original):cli.error('Output must be outside originals')
|
|
target.parent.mkdir(parents=True,exist_ok=True);target.write_text(text)
|
|
else:print(text,end='')
|
|
|
|
|
|
if __name__=='__main__':main()
|