From fd3cd887b6d76b542773f9eeee161a0ff169f114 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 2 Sep 2023 16:40:18 +0300 Subject: [PATCH] Add target_load --- src/mapper/mp_targt.cc | 56 +++++++++++++++++++++++++++++++++++++++++- src/mapper/mp_targt.h | 3 ++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/mapper/mp_targt.cc b/src/mapper/mp_targt.cc index 6d2a75a..6d4ba60 100644 --- a/src/mapper/mp_targt.cc +++ b/src/mapper/mp_targt.cc @@ -139,6 +139,60 @@ int target_header_load() return 0; } +// 0x49B6BC +int target_load(int pid, TargetSubNode** subnode_ptr) +{ + char path[COMPAT_MAX_PATH]; + size_t len; + char* extension; + FILE* stream; + TargetSubNode* subnode; + + target_make_path(path, pid); + + len = strlen(path); + path[len] = '\\'; + _proto_list_str(pid, path + len + 1); + + extension = strchr(path + len + 1, '.'); + if (extension != NULL) { + strcpy(extension + 1, "tgt"); + } else { + strcat(path, ".tgt"); + } + + stream = fopen(path, "rb"); + if (stream == NULL) { + *subnode_ptr = NULL; + return -1; + } + + if (target_find_free_subnode(&subnode) == -1) { + *subnode_ptr = NULL; + // FIXME: Leaks `stream`. + return -1; + } + + fread(subnode, sizeof(TargetSubNode), 1, stream); + + *subnode_ptr = subnode; + + while (subnode->next != NULL) { + subnode->next = (TargetSubNode*)internal_malloc(sizeof(TargetSubNode)); + if (subnode->next == NULL) { + // FIXME: Leaks `stream`. + return -1; + } + + subnode = subnode->next; + fread(subnode, sizeof(TargetSubNode), 1, stream); + } + + fclose(stream); + + return 0; +} + // 0x49B9C0 int target_find_free_subnode(TargetSubNode** subnode_ptr) { @@ -151,7 +205,7 @@ int target_find_free_subnode(TargetSubNode** subnode_ptr) *subnode_ptr = &(node->subnode); node->subnode.field_0 = -1; - node->subnode.field_28 = 0; + node->subnode.next = NULL; node->next = targetlist.tail; targetlist.tail = node; diff --git a/src/mapper/mp_targt.h b/src/mapper/mp_targt.h index 2769cc4..d85b20f 100644 --- a/src/mapper/mp_targt.h +++ b/src/mapper/mp_targt.h @@ -14,7 +14,7 @@ typedef struct TargetSubNode { int field_1C; int field_20; int field_24; - int field_28; + struct TargetSubNode* next; int field_2C; int field_30; int field_34; @@ -53,6 +53,7 @@ int target_init(); int target_exit(); int target_header_save(); int target_header_load(); +int target_load(int pid, TargetSubNode** subnode_ptr); int target_find_free_subnode(TargetSubNode** subnode_ptr); int pick_rot(); int target_pick_global_var(int* value_ptr);