update style

This commit is contained in:
rr- 2021-02-08 11:50:39 +01:00
parent 1eb83cd242
commit 685dde4d4b
No known key found for this signature in database
GPG key ID: CC65E6FD28CAE42A
6 changed files with 108 additions and 45 deletions

View file

@ -84,14 +84,20 @@ int LoadRooms(FILE *fp) {
return 0;
}
RoomInfo = (ROOM_INFO *)game_malloc(sizeof(ROOM_INFO) * RoomCount, GBUF_RoomInfos);
RoomInfo = (ROOM_INFO *)game_malloc(
sizeof(ROOM_INFO) * RoomCount, GBUF_RoomInfos
);
if (!RoomInfo) {
strcpy(StringToShow, "LoadRoom(): Could not allocate memory for rooms");
return 0;
}
int i = 0;
for (ROOM_INFO *current_room_info = RoomInfo; i < RoomCount; ++i, ++current_room_info) {
for (
ROOM_INFO *current_room_info = RoomInfo;
i < RoomCount;
++i, ++current_room_info
) {
// Room position
_fread(&current_room_info->x, sizeof(__int32), 1, fp);
current_room_info->y = 0;
@ -103,7 +109,9 @@ int LoadRooms(FILE *fp) {
// Room mesh
_fread(&dwCount, sizeof(__int32), 1, fp);
current_room_info->data = (__int16 *)game_malloc(sizeof(__int16) * dwCount, GBUF_RoomMesh);
current_room_info->data = (__int16 *)game_malloc(
sizeof(__int16) * dwCount, GBUF_RoomMesh
);
_fread(current_room_info->data, sizeof(__int16), dwCount, fp);
// Doors
@ -111,7 +119,9 @@ int LoadRooms(FILE *fp) {
if (!wCount) {
current_room_info->doors = NULL;
} else {
current_room_info->doors = (DOOR_INFOS *)game_malloc(sizeof(__int16) + sizeof(DOOR_INFO) * wCount, GBUF_RoomDoor);
current_room_info->doors = (DOOR_INFOS *)game_malloc(
sizeof(__int16) + sizeof(DOOR_INFO) * wCount, GBUF_RoomDoor
);
current_room_info->doors->count = wCount;
_fread(&current_room_info->doors->door, sizeof(DOOR_INFO), wCount, fp);
}
@ -120,7 +130,9 @@ int LoadRooms(FILE *fp) {
_fread(&current_room_info->xSize, sizeof(__int16), 1, fp);
_fread(&current_room_info->ySize, sizeof(__int16), 1, fp);
dwCount = current_room_info->ySize * current_room_info->xSize;
current_room_info->floor = (FLOOR_INFO *)game_malloc(sizeof(FLOOR_INFO) * dwCount, GBUF_RoomFloor);
current_room_info->floor = (FLOOR_INFO *)game_malloc(
sizeof(FLOOR_INFO) * dwCount, GBUF_RoomFloor
);
_fread(current_room_info->floor, sizeof(FLOOR_INFO), dwCount, fp);
// Room lights
@ -129,8 +141,16 @@ int LoadRooms(FILE *fp) {
if (!current_room_info->numLights) {
current_room_info->light = NULL;
} else {
current_room_info->light = (LIGHT_INFO *)game_malloc(sizeof(LIGHT_INFO) * current_room_info->numLights, GBUF_RoomLights);
_fread(current_room_info->light, sizeof(LIGHT_INFO), current_room_info->numLights, fp);
current_room_info->light = (LIGHT_INFO *)game_malloc(
sizeof(LIGHT_INFO) * current_room_info->numLights,
GBUF_RoomLights
);
_fread(
current_room_info->light,
sizeof(LIGHT_INFO),
current_room_info->numLights,
fp
);
}
// Static mesh infos
@ -138,8 +158,16 @@ int LoadRooms(FILE *fp) {
if (!current_room_info->numMeshes) {
current_room_info->mesh = NULL;
} else {
current_room_info->mesh = (MESH_INFO *)game_malloc(sizeof(MESH_INFO) * current_room_info->numMeshes, GBUF_RoomStaticMeshInfos);
_fread(current_room_info->mesh, sizeof(MESH_INFO), current_room_info->numMeshes, fp);
current_room_info->mesh = (MESH_INFO *)game_malloc(
sizeof(MESH_INFO) * current_room_info->numMeshes,
GBUF_RoomStaticMeshInfos
);
_fread(
current_room_info->mesh,
sizeof(MESH_INFO),
current_room_info->numMeshes,
fp
);
}
// Flipped (alternative) room
@ -219,7 +247,14 @@ void __cdecl LevelStats(int levelID) {
--secretsTotal;
}
while (secretsTotal);
sprintf(buf, "%s %d %s %d", "SECRETS", secretsTaken, "OF", SecretCounts[levelID]);
sprintf(
buf,
"%s %d %s %d",
"SECRETS",
secretsTaken,
"OF",
SecretCounts[levelID]
);
txt = T_Print(0, 40, 0, buf);
T_CentreH(txt, 1);
T_CentreV(txt, 1);
@ -345,7 +380,10 @@ int __cdecl LoadItems(FILE *handle)
Items = game_malloc(17408, 18);
if (!Items) {
strcpy(StringToShow, "LoadItems(): Unable to allocate memory for 'items'");
strcpy(
StringToShow,
"LoadItems(): Unable to allocate memory for 'items'"
);
return 0;
}
@ -365,17 +403,23 @@ int __cdecl LoadItems(FILE *handle)
int objectID = currentItem->objectID;
if (objectID < 0 || objectID >= ID_NUMBER_OBJECTS) {
sprintf(StringToShow, "LoadItems(): Bad Object number (%d) on Item %d", objectID, i);
sprintf(
StringToShow,
"LoadItems(): Bad Object number (%d) on Item %d",
objectID,
i
);
S_ExitSystem(StringToShow);
}
if (TR1MConfig.disable_medpacks) {
if (objectID == ID_LARGE_MEDIPACK_ITEM || objectID == ID_SMALL_MEDIPACK_ITEM) {
currentItem->pos.x = -1;
currentItem->pos.y = -1;
currentItem->pos.z = -1;
currentItem->roomNumber = 0;
}
if (TR1MConfig.disable_medpacks && (
objectID == ID_LARGE_MEDIPACK_ITEM ||
objectID == ID_SMALL_MEDIPACK_ITEM
)) {
currentItem->pos.x = -1;
currentItem->pos.y = -1;
currentItem->pos.z = -1;
currentItem->roomNumber = 0;
}
InitialiseItem(i);

View file

@ -1,7 +1,12 @@
#include <string.h>
#include "json_utils.h"
json_value *get_json_field(json_value *root, json_type fieldType, const char *name, int *pIndex) {
json_value *tr1m_json_get_field(
json_value *root,
json_type field_type,
const char *name,
int *pIndex
) {
if (root == NULL || root->type != json_object) {
return NULL;
}
@ -9,14 +14,11 @@ json_value *get_json_field(json_value *root, json_type fieldType, const char *na
unsigned int len = name ? strlen(name) : 0;
unsigned int i = pIndex ? *pIndex : 0;
for (; i < root->u.object.length; ++i) {
if (root->u.object.values[i].value->type == fieldType) {
if (
!name
|| (
len == root->u.object.values[i].name_length
&& !strncmp(root->u.object.values[i].name, name, len)
)
) {
if (root->u.object.values[i].value->type == field_type) {
if (!name || (
len == root->u.object.values[i].name_length
&& !strncmp(root->u.object.values[i].name, name, len)
)) {
result = root->u.object.values[i].value;
break;
}
@ -28,7 +30,7 @@ json_value *get_json_field(json_value *root, json_type fieldType, const char *na
return result;
}
int get_json_boolean_field_value(json_value *root, const char *name) {
json_value *field = get_json_field(root, json_boolean, name, NULL);
int tr1m_json_get_boolean_value(json_value *root, const char *name) {
json_value *field = tr1m_json_get_field(root, json_boolean, name, NULL);
return field ? field->u.boolean : 0;
}

View file

@ -3,7 +3,13 @@
#include "json-parser/json.h"
json_value *get_json_field(json_value *root, json_type fieldType, const char *name, int *pIndex);
int get_json_boolean_field_value(json_value *root, const char *name);
json_value *tr1m_json_get_field(
json_value *root,
json_type field_type,
const char *name,
int *pIndex
);
int tr1m_json_get_boolean_value(json_value *root, const char *name);
#endif

View file

@ -9,7 +9,7 @@
HINSTANCE hInstance = NULL;
static void Inject() {
static void tr1m_inject() {
INJECT(0x0042A2C0, DB_Log);
INJECT(0x0041C020, FindCdDrive);
INJECT(0x0041BFC0, GetFullPath);
@ -28,7 +28,7 @@ static void Inject() {
//INJECT(0x00430450, S_DrawAirBar);
}
static int ReadConfig() {
static int tr1m_read_config() {
FILE *fp = fopen("TR1Main.json", "rb");
if (!fp) {
return 0;
@ -39,26 +39,37 @@ static int ReadConfig() {
fseek(fp, 0, SEEK_SET);
char *cfg_data = malloc(cfg_size);
if (!cfg_data) {
fclose(fp);
return 0;
}
fread(cfg_data, 1, cfg_size, fp);
fclose(fp);
json_value *json = json_parse((const json_char*)cfg_data, cfg_size);
TR1MConfig.keep_health_between_levels = get_json_boolean_field_value(json, "keep_health_between_levels");
TR1MConfig.disable_medpacks = get_json_boolean_field_value(json, "disable_medpacks");
TR1MConfig.keep_health_between_levels = tr1m_json_get_boolean_value(
json, "keep_health_between_levels"
);
TR1MConfig.disable_medpacks = tr1m_json_get_boolean_value(
json, "disable_medpacks"
);
json_value_free(json);
free(cfg_data);
return 1;
}
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
BOOL APIENTRY DllMain(
HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved
) {
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
freopen("./TR1Main.log", "w", stdout);
ReadConfig();
tr1m_read_config();
TRACE("Attached");
hInstance = hinstDLL;
Inject();
tr1m_inject();
break;
case DLL_PROCESS_DETACH:

View file

@ -4,7 +4,7 @@
#include <stdio.h>
#include "util.h"
void InjectFunc(void *from, void *to) {
void tr1m_inject_func(void *from, void *to) {
DWORD tmp;
TRACE("Patching %p to %p", from, to);
VirtualProtect(from, sizeof(JMP), PAGE_EXECUTE_READWRITE, &tmp);
@ -14,12 +14,12 @@ void InjectFunc(void *from, void *to) {
buf.offset = (DWORD)(to) - ((DWORD)(from) + sizeof(JMP));
WriteProcessMemory(hCurrentProcess, from, &buf, sizeof(JMP), &tmp);
CloseHandle(hCurrentProcess);
// arsunt style
// arsunt style - doesn't work because of GLRage calling VirtualProtect
//((JMP*)(from))->opCode = 0xE9;
//((JMP*)(from))->offset = (DWORD)(to) - ((DWORD)(from) + sizeof(JMP));
}
void PrintStackTrace() {
void tr1m_print_stack_trace() {
const size_t MaxNameLen = 255;
BOOL result;
HANDLE process;

View file

@ -25,11 +25,11 @@ typedef struct {
#define VAR_I_(address, type, value) (*(type*)(address))
#define ARRAY_(address, type, length) (*(type(*)length)(address))
void InjectFunc(void *from, void *to);
void PrintStackTrace();
void tr1m_inject_func(void *from, void *to);
void tr1m_print_stack_trace();
#define INJECT(from, to) { \
InjectFunc((void*)from, (void*)to); \
tr1m_inject_func((void*)from, (void*)to); \
}
#endif