Files
Neutron/tools/binary/collision_response_probe.c
T

81 lines
4.6 KiB
C

/* Isolated original 47BD80 component probe. Run only through reference_capture
* against a generated copied installation. No game entry point or EXE import
* initialization is executed. Explicit guarded absolute operands are rebased in
* memory; only quick_magnitude's IAT entry is bound to the copied OMT DLL. */
#include <windows.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct { float x,y,z,w; } Vec;
typedef unsigned (__cdecl *Kernel)(Vec,Vec,Vec,Vec,Vec,Vec*,float,int);
static unsigned char *mapped;
#define AT(va) (mapped+(va)-0x400000)
#include "collision_response_relocations.h"
static void require(int ok,const char *message) {
if(!ok){fprintf(stderr,"FAIL %s\n",message);exit(2);}
}
static void map_copy(void) {
unsigned char *image=VirtualAlloc(NULL,0x299000,
MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
require(image!=NULL,"component map");mapped=image;
FILE *f=fopen("Neutron.exe","rb");require(f!=NULL,"copied EXE");
fseek(f,0,SEEK_END);long size=ftell(f);rewind(f);
require(size>64&&size<16000000,"bounded EXE");
unsigned char *data=malloc(size);require(data!=NULL,"file allocation");
require(fread(data,1,size,f)==(size_t)size,"read EXE");fclose(f);
require(*(uint16_t*)data==IMAGE_DOS_SIGNATURE,"DOS header");
uint32_t offset=*(uint32_t*)(data+0x3c);
require(offset<(uint32_t)size-sizeof(IMAGE_NT_HEADERS32),"NT header bounds");
IMAGE_NT_HEADERS32 *nt=(void*)(data+offset);
require(nt->Signature==IMAGE_NT_SIGNATURE&&nt->FileHeader.Machine==IMAGE_FILE_MACHINE_I386
&&nt->OptionalHeader.ImageBase==0x400000&&nt->OptionalHeader.SizeOfImage==0x299000,"PE32 layout");
IMAGE_SECTION_HEADER *sections=IMAGE_FIRST_SECTION(nt);
require((unsigned char*)(sections+nt->FileHeader.NumberOfSections)<=data+size,"section table bounds");
for(unsigned i=0;i<nt->FileHeader.NumberOfSections;i++) {
IMAGE_SECTION_HEADER *s=&sections[i];
require((uint64_t)s->PointerToRawData+s->SizeOfRawData<=(uint64_t)size
&&(uint64_t)s->VirtualAddress+s->SizeOfRawData<=nt->OptionalHeader.SizeOfImage,"section bounds");
memcpy(image+s->VirtualAddress,data+s->PointerToRawData,s->SizeOfRawData);
}
free(data);
for(unsigned i=0;i<sizeof(relocations)/sizeof(relocations[0]);i++) {
uint32_t *operand=(void*)AT(relocations[i].va);
require(*operand==relocations[i].target,"guarded relocation operand");
*operand=(uint32_t)AT(relocations[i].target);
}
HMODULE omt=LoadLibraryA("Z:\\work\\game\\OMT2.dll");require(omt!=NULL,"copied OMT");
FARPROC magnitude=GetProcAddress(omt,"?quick_magnitude@OMedia3DVector@@QBEMXZ");
require(magnitude!=NULL,"quick magnitude");
*(FARPROC*)AT(0x48d104)=magnitude;
*(unsigned char*)AT(0x695d44)=0; // Skip optional debug drawing.
}
static void sample(const char *name,Vec a,Vec b,Vec c,Vec n,Vec previous,Vec current,
float radius,int edge,int mode) {
*(float*)AT(0x695d5c)=-1;*(float*)AT(0x5cfbe0)=radius;*(int*)AT(0x695d74)=mode;
memset(AT(0x5cfbe8),0,16);
unsigned contact=((Kernel)AT(0x47bd80))(a,b,c,n,previous,&current,radius,edge)&0xff;
Vec hit=*(Vec*)AT(0x5cfbe8);
printf("CASE %s contact=%u root=%.9g,%.9g,%.9g hit=%.9g,%.9g,%.9g face=%.9g edge=%.9g\n",
name,contact,current.x,current.y,current.z,hit.x,hit.y,hit.z,*(float*)AT(0x695d5c),*(float*)AT(0x5cfbe0));
require(isfinite(current.x)&&isfinite(current.y)&&isfinite(current.z),"finite result");
}
int main(void) {
setbuf(stdout,NULL);map_copy();
Vec a={-100,-100,0,1},b={-100,100,0,1},c={100,100,0,1},n={0,0,1,0};
sample("wall_face",a,b,c,n,(Vec){0,20,20,1},(Vec){0,20,5,1},10,1,1);
sample("wall_pass",a,b,c,n,(Vec){0,20,20,1},(Vec){0,20,-20,1},10,1,1);
sample("wall_depart",a,b,c,n,(Vec){0,20,5,1},(Vec){0,20,20,1},10,1,1);
sample("wall_side_edge",a,b,c,n,(Vec){105,100,20,1},(Vec){105,100,5,1},10,1,1);
sample("wall_edge_disabled",a,b,c,n,(Vec){105,100,20,1},(Vec){105,100,5,1},10,0,1);
sample("wall_edge_below",a,b,c,n,(Vec){0,-110,20,1},(Vec){0,-110,5,1},10,1,0);
sample("wall_edge_mode1",a,b,c,n,(Vec){0,-110,20,1},(Vec){0,-110,5,1},10,1,1);
sample("upper_edge_mode0",a,b,c,n,(Vec){105,105,20,1},(Vec){105,105,5,1},10,1,0);
sample("upper_edge_mode1",a,b,c,n,(Vec){105,105,20,1},(Vec){105,105,5,1},10,1,1);
sample("ceiling",(Vec){-100,100,-100,1},(Vec){100,100,100,1},(Vec){100,100,-100,1},
(Vec){0,-1,0,0},(Vec){0,80,0,1},(Vec){0,95,0,1},10,1,1);
puts("COLLISION_COMPONENT_COMPLETE: isolated original kernel; synthetic geometry, no whole-game runtime");
return 0;
}