97 lines
6.8 KiB
C
97 lines
6.8 KiB
C
/* Isolated copied EXE scalar slices. No game entrypoint, ray, body or renderer.
|
|
Absolute scalar operands are relocated into the private copied image. */
|
|
#include <windows.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#define TC __attribute__((thiscall))
|
|
static unsigned char*mapped;
|
|
#define AT(a) (mapped+(a)-0x400000)
|
|
#define F(a) (*(float*)AT(a))
|
|
static unsigned char object[0x1f00],hook[0x600],shape[16];
|
|
static uintptr_t shape_table[0x40/4];
|
|
static float scale_z;static int scale_calls;
|
|
static void require(int ok,const char*why){if(!ok){fprintf(stderr,"FAIL %s\n",why);exit(2);}}
|
|
static void TC scale(void*self,float x,float y,float z){require(self==shape&&x==1&&y==1,"shape scale arguments");scale_z=z;scale_calls++;}
|
|
static void map_source(void){
|
|
FILE*f=fopen("Neutron.exe","rb");require(f!=NULL,"generated EXE copy");fseek(f,0,SEEK_END);long size=ftell(f);rewind(f);
|
|
require(size==1019904,"EXE size");unsigned char*d=malloc(size);require(d!=NULL,"buffer");require(fread(d,1,size,f)==(size_t)size,"read");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");
|
|
unsigned char*m=VirtualAlloc(NULL,0x299000,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);require(m!=NULL,"private source map");mapped=m;
|
|
IMAGE_SECTION_HEADER*s=IMAGE_FIRST_SECTION(nt);require((unsigned char*)(s+nt->FileHeader.NumberOfSections)<=d+size,"sections");
|
|
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(m+s[i].VirtualAddress,d+s[i].PointerToRawData,s[i].SizeOfRawData);
|
|
}free(d);
|
|
shape_table[0x34/4]=(uintptr_t)scale;*(void**)shape=shape_table;*(void**)(hook+0x57c)=shape;*(void**)(object+0xb0c)=hook;
|
|
}
|
|
static void relocate(uint32_t start,uint32_t end){
|
|
const uint32_t operands[]={0x48d96c,0x48d914,0x4a3e5c,0x4f82b8,0x4f82b4,0x4a3e58,0x4f82c8,0x4a3e88,
|
|
0x4f82bc,0x4f82c0,0x4f82c4,0x4a3e80,0x4a2ad0,0x48d400,0x4a3e78,0x49fae4,0x4a3e74,0x4a3e70,0x4a3e6c};
|
|
for(uint32_t a=start;a+4<=end;a++){
|
|
uint32_t value;memcpy(&value,AT(a),4);
|
|
for(unsigned i=0;i<sizeof(operands)/sizeof(operands[0]);i++)if(value==operands[i]){
|
|
uint32_t relocated=(uintptr_t)AT(value);memcpy(AT(a),&relocated,4);a+=3;break;
|
|
}
|
|
}
|
|
}
|
|
static void emit_byte(unsigned char**p,unsigned char x){*(*p)++=x;}
|
|
static void word(unsigned char**p,uint32_t x){memcpy(*p,&x,4);*p+=4;}
|
|
static void jump(unsigned char*p,void*target){*p=0xe9;int32_t delta=(unsigned char*)target-p-5;memcpy(p+1,&delta,4);}
|
|
static void stack_copy(unsigned char**p,int src,int dest){
|
|
emit_byte(p,0x8b);emit_byte(p,0x48);emit_byte(p,src); /* ecx=[eax+src] */
|
|
emit_byte(p,0x89);emit_byte(p,0x8c);emit_byte(p,0x24);word(p,dest);
|
|
}
|
|
static void*wrapper(uint32_t entry,uint32_t exit1,uint32_t exit2,int kind){
|
|
unsigned char*code=VirtualAlloc(NULL,256,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);require(code!=NULL,"thunk");unsigned char*p=code;
|
|
emit_byte(&p,0x53);emit_byte(&p,0x55);emit_byte(&p,0x56);emit_byte(&p,0x57); /* save all nonvolatile registers */
|
|
emit_byte(&p,0x81);emit_byte(&p,0xec);word(&p,0xa0);
|
|
emit_byte(&p,0x8b);emit_byte(&p,0x84);emit_byte(&p,0x24);word(&p,0xb4); /* eax=args */
|
|
if(kind==0){stack_copy(&p,0,0x10);stack_copy(&p,4,0x14);stack_copy(&p,8,0x18);}
|
|
else if(kind==1)stack_copy(&p,0,0x54);
|
|
else {stack_copy(&p,0,0x98);if(kind==3){stack_copy(&p,4,0x0c);stack_copy(&p,8,0x14);}}
|
|
emit_byte(&p,0xbe);word(&p,(uintptr_t)object);emit_byte(&p,0xb8);word(&p,(uintptr_t)AT(0x4f8214));
|
|
jump(p,AT(entry));p+=5;unsigned char*epilogue=p;
|
|
emit_byte(&p,0x81);emit_byte(&p,0xc4);word(&p,0xa0);emit_byte(&p,0x5f);emit_byte(&p,0x5e);emit_byte(&p,0x5d);emit_byte(&p,0x5b);emit_byte(&p,0xc3);
|
|
jump(AT(exit1),epilogue);if(exit2)jump(AT(exit2),epilogue);return code;
|
|
}
|
|
typedef void(*run_fn)(float*);
|
|
static run_fn attachment,held,fast,damping;
|
|
static void attach_case(const char*name,float x,float y,float z,float ax,float ay,float az,float first){
|
|
float args[]={x,y,z};F(0x4f8214)=ax;F(0x4f8218)=ay;F(0x4f821c)=az;F(0x4f82b4)=first;F(0x4f82b8)=123;
|
|
scale_z=12345;scale_calls=0;attachment(args);
|
|
printf("CASE attach %s in=%.9g,%.9g,%.9g,%.9g,%.9g,%.9g,%.9g out=%.9g,%.9g,%.9g,%d\n",name,x,y,z,ax,ay,az,first,F(0x4f82b8),F(0x4f82b4),scale_z,scale_calls);
|
|
}
|
|
static void reel_case(const char*name,int is_fast,float length,float target,float dt){
|
|
float args[]={dt};F(0x4f82b8)=length;F(0x4f82c8)=target;(is_fast?fast:held)(args);
|
|
printf("CASE %s %s in=%.9g,%.9g,%.9g out=%.9g\n",is_fast?"fast":"held",name,length,target,dt,F(0x4f82b8));
|
|
}
|
|
static void swing_case(const char*name,float x,float z,float timer,float dt,float ox,float oz,float length){
|
|
float args[]={dt,ox,oz};F(0x4f82bc)=x;F(0x4f82c0)=z;F(0x4f82c4)=timer;F(0x4f82b8)=length;damping(args);
|
|
printf("CASE swing %s in=%.9g,%.9g,%.9g,%.9g,%.9g,%.9g,%.9g out=%.9g,%.9g,%.9g\n",name,x,z,timer,dt,ox,oz,length,F(0x4f82bc),F(0x4f82c0),F(0x4f82c4));
|
|
}
|
|
int main(void){
|
|
map_source();relocate(0x426be8,0x426c70);relocate(0x426dac,0x426dd5);relocate(0x429dc9,0x429df5);relocate(0x42a077,0x42a1a2);
|
|
attachment=wrapper(0x426be8,0x426c70,0,0);held=wrapper(0x426dac,0x426dd5,0x426dfd,1);
|
|
fast=wrapper(0x429dc9,0x429df5,0,2);damping=wrapper(0x42a077,0x42a1a2,0,3);
|
|
attach_case("first",0,0,0,0,300,400,0);attach_case("retained",0,0,0,0,600,800,400);
|
|
attach_case("fraction",1.25f,-30.75f,48.125f,634.625f,413.75f,-95.125f,0);
|
|
attach_case("short",0,0,0,0,50,0,0);attach_case("zero",0,0,0,0,0,0,0);
|
|
attach_case("negative",-130,315,-490,-530,-285,310,0);
|
|
attach_case("same_first",0,0,0,0,300,400,400);attach_case("negative_first",0,0,0,0,300,400,-100);
|
|
reel_case("above",0,251,0,.1f);reel_case("equal",0,250,0,.1f);reel_case("below",0,249,0,.1f);
|
|
reel_case("negative",0,-100,0,.1f);reel_case("long",0,300,0,.6f);reel_case("tick",0,950,0,1.f/60);
|
|
reel_case("above",1,251,250,.1f);reel_case("equal",1,250,250,.1f);reel_case("below",1,249,250,.1f);
|
|
reel_case("negative_target",1,50,-200,.1f);reel_case("long",1,300,0,.6f);reel_case("tick",1,950,0,1.f/60);
|
|
swing_case("free",10,20,0,.1f,100,-200,400);swing_case("equal",50,-50,0,.2f,0,0,400);
|
|
swing_case("short",100,-100,.2f,0,0,0,349);swing_case("long",100,-100,.2f,0,0,0,350);
|
|
swing_case("one_damping",50,50,0,.6f,0,0,400);swing_case("offset",60,-55,.19f,.02f,102,-73,349.9f);
|
|
swing_case("near_clamp",49.99999f,-49.99999f,.19f,.02f,-.0001f,.0001f,400);
|
|
swing_case("tick",4.125f,-2.75f,.19f,1.f/60,170.125f,-333.75f,350);
|
|
swing_case("below_time",8,9,.19f,.009f,90,-65,800);swing_case("negative_length",5,-6,.1f,.2f,-200,90,-5);
|
|
puts("GRAPPLER_SCALARS_COMPLETE");return 0;
|
|
}
|