Added object_load_list. Fixes various object loading related bugs

This commit is contained in:
Duncan Frost 2014-11-30 21:16:36 +00:00
parent d00001fed2
commit 8354304299
4 changed files with 365 additions and 27 deletions

View File

@ -236,6 +236,10 @@
#define RCT2_ADDRESS_CONSTRUCT_PATH_FROM_Z 0x00F3EF8E
#define RCT2_ADDRESS_CONSTRUCT_PATH_DIRECTION 0x00F3EF90
#define RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS 0x00F42B6C
#define RCT2_ADDRESS_CURR_OBJECT_CHUNK_POINTER 0x00F42BC0
#define RCT2_ADDRESS_VOLUME_ADJUST_ZOOM 0x00F438AC
#define RCT2_ADDRESS_STAFF_HIGHLIGHTED_INDEX 0x00F43908

View File

@ -33,19 +33,19 @@ rct_object_entry *object_get_next(rct_object_entry *entry);
*
* rct2: 0x006A985D
*/
int object_load(int groupIndex, rct_object_entry *entry)
int object_load(int groupIndex, rct_object_entry *entry, int* chunk_size)
{
RCT2_GLOBAL(0xF42B64, uint32) = groupIndex;
//part of 6a9866
rct_object_entry *installedObject = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, rct_object_entry*);
if (!(RCT2_GLOBAL(0xF42B6C, uint32))){
if (!(RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32))){
RCT2_GLOBAL(0xF42BD9, uint8) = 0;
log_error("Object Load failed due to 0xF42B6C check.");
log_error("Object Load failed due to no items installed check.");
return 1;
}
for (int i = 0; i < RCT2_GLOBAL(0x00F42B6C, sint32); i++) {
for (int i = 0; i < RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, sint32); i++) {
if (object_entry_compare(installedObject, entry)){
char path[260];
@ -64,22 +64,24 @@ int object_load(int groupIndex, rct_object_entry *entry)
} while (*(pos - 1) != 0);
// Read chunk
int chunkSize = *((uint32*)pos);
*chunk_size = *((uint32*)pos);
char *chunk;
if (chunkSize == 0xFFFFFFFF) {
if (*chunk_size == 0xFFFFFFFF) {
chunk = rct2_malloc(0x600000);
chunkSize = sawyercoding_read_chunk(file, chunk);
chunk = rct2_realloc(chunk, chunkSize);
*chunk_size = sawyercoding_read_chunk(file, chunk);
chunk = rct2_realloc(chunk, *chunk_size);
}
else {
chunk = rct2_malloc(chunkSize);
chunkSize = sawyercoding_read_chunk(file, chunk);
chunk = rct2_malloc(*chunk_size);
*chunk_size = sawyercoding_read_chunk(file, chunk);
}
fclose(file);
// Calculate and check checksum
if (object_calculate_checksum(&openedEntry, chunk, chunkSize) != openedEntry.checksum) {
if (object_calculate_checksum(&openedEntry, chunk, *chunk_size) != openedEntry.checksum) {
log_error("Object Load failed due to checksum failure.");
RCT2_GLOBAL(0x00F42BD9, uint8) = 2;
rct2_free(chunk);
@ -106,7 +108,7 @@ int object_load(int groupIndex, rct_object_entry *entry)
int esi = RCT2_ADDRESS(0x98D97C, uint32)[ebp * 2];
int ecx = groupIndex;
if (ecx == -1){
for (int ecx = 0; ((sint32*)esi)[ecx] != -1; ecx++){
for (ecx = 0; ((sint32*)esi)[ecx] != -1; ecx++){
if ((ecx + 1) >= object_entry_group_counts[ebp]){
log_error("Object Load failed due to ??? failure.");
RCT2_GLOBAL(0x00F42BD9, uint8) = 5;
@ -119,8 +121,11 @@ int object_load(int groupIndex, rct_object_entry *entry)
int* edx = (int*)( ecx * 20 + RCT2_ADDRESS(0x98D980, uint32)[ebp * 2]);
memcpy(edx, (int*)&openedEntry, 20);
RCT2_GLOBAL(RCT2_ADDRESS_CURR_OBJECT_CHUNK_POINTER, char*) = chunk;
if (RCT2_GLOBAL(0x9ADAFD, uint8) == 0)return 1;
object_paint(ecx, 0, ecx, ebp, 0, (int)chunk, 0, 0);
object_paint(ebp, 0, ecx, ebp, 0, (int)chunk, 0, 0);
return 1;
}
fclose(file);
@ -227,8 +232,8 @@ int object_load_packed(FILE *file)
//esi
rct_object_entry *installedObject = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, rct_object_entry*);
if (RCT2_GLOBAL(0xF42B6C, uint32)){
for (uint32 i = 0; i < RCT2_GLOBAL(0xF42B6C, uint32); ++i){
if (RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)){
for (uint32 i = 0; i < RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32); ++i){
if (object_entry_compare(entry, installedObject)){
object_unload_all();
return 0;
@ -374,9 +379,9 @@ int object_scenario_load_custom_text(char* chunk){
int object_paint(int type, int eax, int ebx, int ecx, int edx, int esi, int edi, int ebp)
{
if (type == 10){
if (eax == 0) return object_scenario_load_custom_text((char*)esi);
}
//if (type == 10){
// if (eax == 0) return object_scenario_load_custom_text((char*)esi);
//}
return RCT2_CALLPROC_X(RCT2_ADDRESS(0x0098D9D4, uint32)[type], eax, ebx, ecx, edx, esi, edi, ebp) & 0x100;
}
@ -390,7 +395,7 @@ int object_get_scenario_text(rct_object_entry *entry)
int i;
rct_object_entry *installedObject = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, rct_object_entry*);
for (i = 0; i < RCT2_GLOBAL(0x00F42B6C, sint32); i++) {
for (i = 0; i < RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, sint32); i++) {
if (object_entry_compare(installedObject, entry)) {
char path[260];
char *objectPath = (char*)installedObject + 16;
@ -476,7 +481,7 @@ int object_get_length(rct_object_entry *entry)
rct_object_entry *object_get_next(rct_object_entry *entry)
{
uint8 *pos = (char*)entry;
uint8 *pos = (uint8*)entry;
// Skip sizeof(rct_object_entry)
pos += 16;

View File

@ -53,7 +53,7 @@ int object_read_and_load_entries(FILE *file);
int object_load_packed(FILE *file);
void object_unload_all();
int object_load(int groupIndex, rct_object_entry *entry);
int object_load(int groupIndex, rct_object_entry *entry, int* chunk_size);
void object_unload(int groupIndex, rct_object_entry_extended *entry);
int object_get_scenario_text(rct_object_entry *entry);
void object_free_scenario_text();

View File

@ -23,6 +23,7 @@
#include "object.h"
#include "util/sawyercoding.h"
#include "localisation/localisation.h"
#include "ride/track.h"
#define OBJECT_ENTRY_GROUP_COUNT 11
#define OBJECT_ENTRY_COUNT 721
@ -32,7 +33,7 @@ typedef struct {
uint32 total_file_size;
uint32 date_modified_checksum;
uint32 object_list_size;
uint32 var_10;
uint32 object_list_no_items;
} rct_plugin_header;
// 98DA00
@ -89,7 +90,7 @@ static void object_list_examine()
rct_object_entry *object;
object = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, rct_object_entry*);
for (i = 0; i < RCT2_GLOBAL(0x00F42B6C, sint32); i++) {
for (i = 0; i < RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, sint32); i++) {
if (object->flags & 0xF0)
RCT2_GLOBAL(0x00F42BDA, uint8) |= 1;
@ -97,6 +98,70 @@ static void object_list_examine()
}
}
/* rct2:0x006DED68 */
void reset_9E32F8(){
uint8* edi = RCT2_ADDRESS(0x009E32F8, uint8);
memset(edi, 0xFF, 90);
}
void sub_6A9FC0(){
reset_9E32F8();
RCT2_GLOBAL(0x009ADAF0, uint32) = 0xF26E;
for (int type = 0; type < 11; ++type){
for (int j = 0; j < object_entry_group_counts[type]; j++){
rct_object_entry* entry = (rct_object_entry*)object_entry_groups[type].data[j];
if (entry != (rct_object_entry*)0xFFFFFFFF)
object_paint(type, 0, 0, 0, 0, (int)entry, 0, 0);
}
}
}
int object_copy(uint8* destination_object, uint8* installed_object){
uint8* original_dest = destination_object;
memcpy(destination_object, installed_object, sizeof(rct_object_entry));
destination_object += sizeof(rct_object_entry);
installed_object += sizeof(rct_object_entry);
do{
*destination_object++ = *installed_object++;
} while (*(destination_object - 1));
*((sint32*)destination_object) = *((sint32*)installed_object);
destination_object += 4;
installed_object += 4;
do{
*destination_object++ = *installed_object++;
} while (*(destination_object - 1));
*((sint32*)destination_object) = *((sint32*)installed_object);
destination_object += 4;
installed_object += 4;
uint8 no_obj_unk = *installed_object++;
*destination_object++ = no_obj_unk;
memcpy(destination_object, installed_object, no_obj_unk*sizeof(rct_object_entry));
destination_object += no_obj_unk*sizeof(rct_object_entry);
installed_object += no_obj_unk*sizeof(rct_object_entry);
uint8 no_obj_theme = *installed_object++;
*destination_object++ = no_obj_theme;
memcpy(destination_object, installed_object, no_obj_theme*sizeof(rct_object_entry));
destination_object += no_obj_theme*sizeof(rct_object_entry);
installed_object += no_obj_theme*sizeof(rct_object_entry);
*((sint32*)destination_object) = *((sint32*)installed_object);
destination_object += 4;
installed_object += 4;
return destination_object - original_dest;
}
/**
*
* rct2: 0x006A8B40
@ -145,10 +210,10 @@ void object_list_load()
// Read installed object list
RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*) = rct2_malloc(pluginHeader.object_list_size);
if (fread(RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*), pluginHeader.object_list_size, 1, file) == 1) {
RCT2_GLOBAL(0x00F42B6C, uint32) = pluginHeader.var_10;
RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32) = pluginHeader.object_list_no_items;
fclose(file);
RCT2_CALLPROC_EBPSAFE(0x006A9FC0);
sub_6A9FC0();
object_list_examine();
return;
}
@ -161,7 +226,267 @@ void object_list_load()
RCT2_GLOBAL(0x00F42B94, uint32) = totalFiles;
RCT2_GLOBAL(0x00F42B98, uint32) = totalFileSize;
RCT2_GLOBAL(0x00F42B9C, uint32) = fileDateModifiedChecksum;
RCT2_CALLPROC_EBPSAFE(0x006A8D8F);
//RCT2_CALLPROC_EBPSAFE(0x006A8D8F);
int eax = 3161;
if (RCT2_GLOBAL(0x9AA00D, uint8) != 0){
eax = 3160;
RCT2_GLOBAL(0x9AA00D, uint8) = 0;
}
// File count removed and replaced by variable
// RCT2_GLOBAL(0xF42BA8, uint32) = 0;
uint32 file_count = 0;
// Progress bar related.
RCT2_GLOBAL(0xF42BD8, uint8) = 0;
sub_6A9FC0();
// Dispose installed object list
if (RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, sint32) != -1) {
rct2_free(RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*));
RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, sint32) = -1;
}
RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32) = 0;
RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*) = rct2_malloc(4096);
if (RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, int) == -1){
RCT2_CALLPROC_X(0x006E3838, 0x343, 0xC5A, 0, 0, 0, 0, 0);
return;
}
uint32 installed_buffer_size = 0x1000;
uint32 current_item_offset = 0;
hFindFile = FindFirstFile(RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char), &findFileData);
if (hFindFile == INVALID_HANDLE_VALUE){
//6a92ea This hasn't been implemented but there isn't much point.
// It would make a empty object file if no files found.
return;
}
for (uint8 first_time = 1; first_time || FindNextFile(hFindFile, &findFileData);){
first_time = 0;
RCT2_GLOBAL(0x9ABD98, HANDLE) = hFindFile;
file_count++;
// update progress bar.
eax = (file_count << 8) / ((RCT2_GLOBAL(0xF42B94, uint32) & 0xFFFFFF) + 1);
if ((eax & 0xFF) != RCT2_GLOBAL(0xF42BD8, uint8)){
RCT2_GLOBAL(0xF42BD8, uint8) = eax & 0xFF;
// update progress bar
}
if ((installed_buffer_size - current_item_offset) <= 2842){
installed_buffer_size += 0x1000;
RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*) = rct2_realloc(RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*), installed_buffer_size);
if (RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, int) == -1){
RCT2_CALLPROC_X(0x006E3838, 0x343, 0xC5A, 0, 0, 0, 0, 0);
return;
}
}
char path[260];
subsitute_path(path, RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char), findFileData.cFileName);
FILE *obj_file = fopen(path, "rb");
if (obj_file == NULL){
continue;
}
rct_object_entry* entry = RCT2_ADDRESS(0xF42B74, rct_object_entry);
if (fread(entry, sizeof(rct_object_entry), 1, obj_file) != 1){
fclose(obj_file);
continue;
}
fclose(obj_file);
RCT2_GLOBAL(0xF42BC4, uint32) = current_item_offset;
uint8* installed_entry_pointer = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*) + current_item_offset;
memcpy(installed_entry_pointer, entry, sizeof(rct_object_entry));
installed_entry_pointer += sizeof(rct_object_entry);
strcpy(installed_entry_pointer, findFileData.cFileName);
while (*installed_entry_pointer++);
*((sint32*)installed_entry_pointer) = -1;
*(installed_entry_pointer + 4) = 0;
*((sint32*)(installed_entry_pointer + 5)) = 0;
*((uint16*)(installed_entry_pointer + 9)) = 0;
*((uint32*)(installed_entry_pointer + 11)) = 0;
RCT2_GLOBAL(0x9ADAF0, uint32) = 0xF26E;
RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)++;
// This is a variable used by object_load to decide if it should
// use object_paint on the entry.
RCT2_GLOBAL(0x9ADAFD, uint8) = 1;
// Probably used by object paint.
RCT2_GLOBAL(0x9ADAF4, uint32) = 0xF42BDB;
int chunk_size;
if (!object_load(-1, entry, &chunk_size)){
RCT2_GLOBAL(0x9ADAF4, sint32) = -1;
RCT2_GLOBAL(0x9ADAFD, uint8) = 0;
RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)--;
continue;
}
// See above note
RCT2_GLOBAL(0x9ADAF4, sint32) = -1;
RCT2_GLOBAL(0x9ADAFD, uint8) = 0;
if ((entry->flags & 0xF0) == 0x80){
RCT2_GLOBAL(0xF42B70, uint32)++;
if (RCT2_GLOBAL(0xF42B70, uint32) > 772){
RCT2_GLOBAL(0xF42B70, uint32)--;
RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)--;
continue;
}
}
*((sint32*)installed_entry_pointer) = chunk_size;
installed_entry_pointer += 4;
uint8* chunk = RCT2_GLOBAL(RCT2_ADDRESS_CURR_OBJECT_CHUNK_POINTER, uint8*); // Loaded in object_load
// When made of two parts i.e Wooden Roller Coaster (Dream Woodie Cars);
if ((entry->flags & 0xF) == 0 && !(*((uint32*)(chunk + 8)) & 0x1000)){
rct_string_id obj_string = chunk[12];
if (obj_string == 0xFF){
obj_string = chunk[13];
if (obj_string == 0xFF){
obj_string = chunk[14];
}
}
obj_string += 2;
format_string(installed_entry_pointer, obj_string, 0);
strcat(installed_entry_pointer, "\t (");
strcat(installed_entry_pointer, language_get_string(RCT2_GLOBAL(0xF42BBC, uint32)));
strcat(installed_entry_pointer, ")");
while (*installed_entry_pointer++);
}
else{
strcpy(installed_entry_pointer, language_get_string(RCT2_GLOBAL(0xF42BBC, uint32)));
while (*installed_entry_pointer++);
}
*((uint32*)installed_entry_pointer) = RCT2_GLOBAL(0x9ADAF0, uint32) - 0xF26E;
installed_entry_pointer += 4;
uint8* esi = RCT2_ADDRESS(0xF42BDB, uint8);
int cl = *esi++;
*installed_entry_pointer++ = cl;
if (cl){
memcpy(installed_entry_pointer, esi, cl*sizeof(rct_object_entry));
installed_entry_pointer += cl*sizeof(rct_object_entry);
}
cl = *esi++;
*installed_entry_pointer++ = cl;
if (cl){
memcpy(installed_entry_pointer, esi, cl*sizeof(rct_object_entry));
installed_entry_pointer += cl*sizeof(rct_object_entry);
}
*((uint32*)installed_entry_pointer) = RCT2_GLOBAL(0xF433DD, uint32);
installed_entry_pointer += 4;
int size_of_object = installed_entry_pointer - RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*) - current_item_offset;
object_unload(entry->flags & 0xF, (rct_object_entry_extended*)entry);
// Return pointer to start of entry
installed_entry_pointer -= size_of_object;
uint8* copied_entry = RCT2_ADDRESS(0x140E9AC, uint8);
size_of_object = object_copy(copied_entry, installed_entry_pointer);
RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)--;
copied_entry += sizeof(rct_object_entry);
// Skip filename
while (*copied_entry++);
// Skip
copied_entry += 4;
installed_entry_pointer = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*);
for (uint32 i = 0; i < RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32); ++i){
uint8* temp_installed_entry = installed_entry_pointer;
temp_installed_entry += sizeof(rct_object_entry);
// Skip filename
while (*temp_installed_entry++);
// Skip
temp_installed_entry += 4;
if (strcmp(temp_installed_entry, copied_entry) <= 0)break;
installed_entry_pointer = (uint8*)(object_get_next((rct_object_entry*)installed_entry_pointer));
}
// Difference to new location
int no_bytes_to_move = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*) + current_item_offset - installed_entry_pointer;
uint8* curr_location = installed_entry_pointer;
uint8* move_location = installed_entry_pointer + size_of_object;
if (no_bytes_to_move){
memmove(move_location, curr_location, no_bytes_to_move);
}
copied_entry = RCT2_ADDRESS(0x140E9AC, uint8);
memcpy(installed_entry_pointer, copied_entry, size_of_object);
current_item_offset += size_of_object;
RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)++;
}
FindClose(hFindFile);
sub_6A9FC0();
// Size of list Not used any more.
// RCT2_GLOBAL(0xF42BA0, uint32) = current_item_offset;
// RCT2_GLOBAL(0xF42BA4, uint32) = RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32);
FILE* obj_list_file = fopen(get_file_path(PATH_ID_PLUGIN),"wb");
if (obj_list_file){
totalFiles = file_count;
totalFiles = ror32(totalFiles, 24);
totalFiles |= 1;
totalFiles = rol32(totalFiles, 24);
pluginHeader.total_files = totalFiles;
pluginHeader.date_modified_checksum = fileDateModifiedChecksum;
pluginHeader.total_file_size = totalFileSize;
pluginHeader.object_list_size = current_item_offset;
pluginHeader.object_list_no_items = RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32);
RCT2_GLOBAL(0xF42B94, uint32) = totalFiles;
fwrite(&pluginHeader, sizeof(rct_plugin_header), 1, obj_list_file);
fwrite(RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*), pluginHeader.object_list_size, 1, obj_list_file);
fclose(obj_list_file);
}
ride_list_item ride_list;
ride_list.entry_index = 0xFC;
ride_list.type = 0xFC;
track_load_list(ride_list);
object_list_examine();
}
static int check_object_entry(rct_object_entry *entry)
@ -231,8 +556,10 @@ int object_read_and_load_entries(FILE *file)
entryGroupIndex -= object_entry_group_counts[j];
}
// Not used but required for function call
int chunk_size;
// Load the obect
if (!object_load(entryGroupIndex, &entries[i])) {
if (!object_load(entryGroupIndex, &entries[i], &chunk_size)) {
// Failed to load the object
//Destroy progress bar
log_error("failed to load entry:");
@ -263,4 +590,6 @@ void object_unload_all()
for (j = 0; j < object_entry_group_counts[i]; j++)
if (object_entry_groups[i].data[j] != (void**)0xFFFFFFFF)
object_unload(j, &object_entry_groups[i].entries[j]);
sub_6A9FC0();
}