diff --git a/src/object.c b/src/object.c index fa863c7c23..092047756a 100644 --- a/src/object.c +++ b/src/object.c @@ -25,119 +25,147 @@ #include "platform/platform.h" #include "util/sawyercoding.h" -int object_entry_compare(rct_object_entry *a, rct_object_entry *b); -int object_calculate_checksum(rct_object_entry *entry, char *data, int dataLength); -int object_paint(int type, int eax, int ebx, int ecx, int edx, int esi, int edi, int ebp); -rct_object_entry *object_get_next(rct_object_entry *entry); +int object_load_entry(const char *path, rct_object_entry *outEntry) +{ + FILE *file; + + file = fopen(path, "rb"); + if (file == NULL) + return 0; + + if (fread(outEntry, sizeof(rct_object_entry), 1, file) != 1) { + fclose(file); + return 0; + } + + fclose(file); + return 1; +} + +static int object_load_file(int groupIndex, const rct_object_entry *entry, int* chunkSize, const rct_object_entry *installedObject) +{ + int i, objectType; + rct_object_entry openedEntry; + char path[260]; + FILE *file; + + subsitute_path(path, RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char), (char*)installedObject + 16); + + // log_verbose("loading object, %s", path); + + file = fopen(path, "rb"); + if (file == NULL) + return 0; + + fread(&openedEntry, sizeof(rct_object_entry), 1, file); + if (!object_entry_compare(&openedEntry, entry)) { + fclose(file); + return 0; + } + + // Get chunk size + char *pos = (char*)installedObject + 16; + do { + pos++; + } while (*(pos - 1) != 0); + + // Read chunk + *chunkSize = *((uint32*)pos); + char *chunk; + + if (*chunkSize == 0xFFFFFFFF) { + chunk = rct2_malloc(0x600000); + *chunkSize = sawyercoding_read_chunk(file, chunk); + chunk = rct2_realloc(chunk, *chunkSize); + } + else { + chunk = rct2_malloc(*chunkSize); + *chunkSize = sawyercoding_read_chunk(file, chunk); + } + fclose(file); + + + + // Calculate and check checksum + if (object_calculate_checksum(&openedEntry, chunk, *chunkSize) != openedEntry.checksum) { + log_error("Object Load failed due to checksum failure."); + RCT2_GLOBAL(0x00F42BD9, uint8) = 2; + rct2_free(chunk); + return 0; + } + + if (object_paint(openedEntry.flags & 0x0F, 2, 0, openedEntry.flags & 0x0F, 0, (int)chunk, 0, 0)) { + log_error("Object Load failed due to paint failure."); + RCT2_GLOBAL(0x00F42BD9, uint8) = 3; + rct2_free(chunk); + return 0; + } + + int yyy = RCT2_GLOBAL(0x009ADAF0, uint32); + + if (yyy >= 0x4726E){ + log_error("Object Load failed due to yyy failure."); + RCT2_GLOBAL(0x00F42BD9, uint8) = 4; + rct2_free(chunk); + return 0; + } + //B84 is openedEntry + objectType = openedEntry.flags & 0x0F; + int esi = RCT2_ADDRESS(0x98D97C, uint32)[objectType * 2]; + if (groupIndex == -1) { + for (i = 0; ((sint32*)esi)[i] != -1; i++) { + if (i + 1 >= object_entry_group_counts[objectType]) { + log_error("Object Load failed due to ??? failure."); + RCT2_GLOBAL(0x00F42BD9, uint8) = 5; + rct2_free(chunk); + return 0; + } + } + groupIndex = i; + } + ((char**)esi)[groupIndex] = chunk; + + int* edx = (int*)(groupIndex * 20 + RCT2_ADDRESS(0x98D980, uint32)[objectType * 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(objectType, 0, groupIndex, objectType, 0, (int)chunk, 0, 0); + return 1; +} + /** * * rct2: 0x006A985D */ -int object_load(int groupIndex, rct_object_entry *entry, int* chunk_size) +int object_load(int groupIndex, rct_object_entry *entry, int* chunkSize) { + // Alow chunkSize to be null + int tempChunkSize; + if (chunkSize == NULL) + chunkSize = &tempChunkSize; + 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(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32))){ + if (RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32) == 0) { RCT2_GLOBAL(0xF42BD9, uint8) = 0; log_error("Object Load failed due to no items installed check."); return 1; } - for (int 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; - subsitute_path(path, RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char), objectPath); - rct_object_entry openedEntry; - FILE *file = fopen(path, "rb"); - if (file != NULL) { - fread(&openedEntry, sizeof(rct_object_entry), 1, file); - if (object_entry_compare(&openedEntry, entry)) { - - // Get chunk size - char *pos = (char*)installedObject + 16; - do { - pos++; - } while (*(pos - 1) != 0); - - // Read chunk - *chunk_size = *((uint32*)pos); - char *chunk; - - if (*chunk_size == 0xFFFFFFFF) { - chunk = rct2_malloc(0x600000); - *chunk_size = sawyercoding_read_chunk(file, chunk); - chunk = rct2_realloc(chunk, *chunk_size); - } - else { - chunk = rct2_malloc(*chunk_size); - *chunk_size = sawyercoding_read_chunk(file, chunk); - } - fclose(file); - - - - // Calculate and check 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); - return 0; - } - - if (object_paint(openedEntry.flags & 0x0F, 2, 0, openedEntry.flags & 0x0F, 0, (int)chunk, 0, 0)) { - log_error("Object Load failed due to paint failure."); - RCT2_GLOBAL(0x00F42BD9, uint8) = 3; - rct2_free(chunk); - return 0; - } - - int yyy = RCT2_GLOBAL(0x009ADAF0, uint32); - - if (yyy >= 0x4726E){ - log_error("Object Load failed due to yyy failure."); - RCT2_GLOBAL(0x00F42BD9, uint8) = 4; - rct2_free(chunk); - return 0; - } - //B84 is openedEntry - int ebp = openedEntry.flags & 0x0F; - int esi = RCT2_ADDRESS(0x98D97C, uint32)[ebp * 2]; - int ecx = groupIndex; - if (ecx == -1){ - 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; - rct2_free(chunk); - return 0; - } - } - } - ((char**)esi)[ecx] = chunk; - - 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(ebp, 0, ecx, ebp, 0, (int)chunk, 0, 0); - return 1; - } - fclose(file); - } - } - installedObject = object_get_next(installedObject); + rct_object_entry *installedObject = object_list_find(entry); + if (installedObject == NULL) { + log_error("object not installed"); + return 0; } - //6a991f - // Installed Object can not be found. - log_error("Object Load failed due to file not installed."); + + if (object_load_file(groupIndex, entry, chunkSize, installedObject)) + return 1; + return 0; - //return !(RCT2_CALLPROC_X(0x006A985D, 0, 0, groupIndex, 0, 0, 0, (int)entry) & 0x400); } /** rct2: 0x006a9f42 @@ -310,7 +338,7 @@ void object_unload(int groupIndex, rct_object_entry_extended *entry) RCT2_CALLPROC_X(0x006A9CAF, 0, groupIndex, 0, 0, 0, 0, (int)entry); } -int object_entry_compare(rct_object_entry *a, rct_object_entry *b) +int object_entry_compare(const rct_object_entry *a, const rct_object_entry *b) { if (a->flags & 0xF0) { if ((a->flags & 0x0F) != (b->flags & 0x0F)) @@ -333,10 +361,10 @@ int object_entry_compare(rct_object_entry *a, rct_object_entry *b) return 1; } -int object_calculate_checksum(rct_object_entry *entry, char *data, int dataLength) +int object_calculate_checksum(const rct_object_entry *entry, const char *data, int dataLength) { int i; - char *eee = (char*)entry; + const char *eee = (char*)entry; int checksum = 0xF369A75B; char *ccc = (char*)&checksum; diff --git a/src/object.h b/src/object.h index 30e8de7cd8..fc9bcb37a5 100644 --- a/src/object.h +++ b/src/object.h @@ -47,6 +47,7 @@ typedef struct { extern int object_entry_group_counts[]; extern int object_entry_group_encoding[]; +int object_load_entry(const char *path, rct_object_entry *outEntry); void object_list_load(); void set_load_objects_fail_reason(); int object_read_and_load_entries(FILE *file); @@ -58,9 +59,12 @@ void object_unload(int groupIndex, rct_object_entry_extended *entry); int object_get_scenario_text(rct_object_entry *entry); void object_free_scenario_text(); int object_get_length(rct_object_entry *entry); -rct_object_entry *object_get_next(rct_object_entry *entry); -int object_calculate_checksum(rct_object_entry *entry, char *data, int dataLength); +int object_entry_compare(const rct_object_entry *a, const rct_object_entry *b); +int object_calculate_checksum(const rct_object_entry *entry, const char *data, int dataLength); int object_paint(int type, int eax, int ebx, int ecx, int edx, int esi, int edi, int ebp); +rct_object_entry *object_get_next(rct_object_entry *entry); int sub_6A9F42(FILE *file, rct_object_entry* entry); +rct_object_entry *object_list_find(rct_object_entry *entry); + #endif diff --git a/src/object_list.c b/src/object_list.c index 1cdd285d58..2890e1b8da 100644 --- a/src/object_list.c +++ b/src/object_list.c @@ -18,10 +18,10 @@ * along with this program. If not, see . *****************************************************************************/ -#include #include "addresses.h" #include "localisation/localisation.h" #include "object.h" +#include "platform/platform.h" #include "platform/osinterface.h" #include "ride/track.h" #include "util/sawyercoding.h" @@ -81,6 +81,11 @@ struct { void **data; rct_object_entry_extended *entries; } object_entry_groups[ (void**)(0x009ACFA4 + (720 * 4)), (rct_object_entry_extended*)(0x00F3F03C + (720 * 20)) // scenario text }; +static int object_list_cache_load(int totalFiles, uint64 totalFileSize, int fileDateModifiedChecksum); +static int object_list_cache_save(int fileCount, uint64 totalFileSize, int fileDateModifiedChecksum, int currentItemOffset); + +void object_list_create_hash_table(); + static void get_plugin_path(char *path) { char *homePath = osinterface_get_orct2_homefolder(); @@ -104,15 +109,23 @@ static void object_list_examine() object = object_get_next(object); } + + // Create a search index + object_list_create_hash_table(); } -/* rct2:0x006DED68 */ -void reset_9E32F8(){ +/** + * + * rct2: 0x006DED68 + */ +void reset_9E32F8() +{ uint8* edi = RCT2_ADDRESS(0x009E32F8, uint8); memset(edi, 0xFF, 90); } -void sub_6A9FC0(){ +void sub_6A9FC0() +{ reset_9E32F8(); RCT2_GLOBAL(0x009ADAF0, uint32) = 0xF26E; @@ -126,14 +139,15 @@ void sub_6A9FC0(){ } } -int object_copy(uint8* destination_object, uint8* installed_object){ - uint8* original_dest = destination_object; +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{ + do { *destination_object++ = *installed_object++; } while (*(destination_object - 1)); @@ -141,7 +155,7 @@ int object_copy(uint8* destination_object, uint8* installed_object){ destination_object += 4; installed_object += 4; - do{ + do { *destination_object++ = *installed_object++; } while (*(destination_object - 1)); @@ -152,16 +166,16 @@ int object_copy(uint8* destination_object, uint8* installed_object){ 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); + 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); + 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; @@ -170,87 +184,59 @@ int object_copy(uint8* destination_object, uint8* installed_object){ return destination_object - original_dest; } +static int object_list_query_directory(int *outTotalFiles, uint64 *outTotalFileSize, int *outFileDateModifiedChecksum) +{ + int enumFileHandle, totalFiles, fileDateModifiedChecksum; + uint64 totalFileSize; + file_info enumFileInfo; + + totalFiles = 0; + totalFileSize = 0; + fileDateModifiedChecksum = 0; + + // Enumerate through each object in the directory + enumFileHandle = platform_enumerate_files_begin(RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char)); + if (enumFileHandle != INVALID_HANDLE) { + while (platform_enumerate_files_next(enumFileHandle, &enumFileInfo)) { + totalFiles++; + totalFileSize += enumFileInfo.size; + fileDateModifiedChecksum ^= + (uint32)(enumFileInfo.last_modified >> 32) ^ + (uint32)(enumFileInfo.last_modified & 0xFFFFFFFF); + fileDateModifiedChecksum = ror32(fileDateModifiedChecksum, 5); + } + platform_enumerate_files_end(enumFileHandle); + } + + *outTotalFiles = totalFiles; + *outTotalFileSize = totalFileSize; + *outFileDateModifiedChecksum = fileDateModifiedChecksum; +} + /** * * rct2: 0x006A8B40 */ void object_list_load() { - HANDLE hFindFile; - WIN32_FIND_DATAA findFileData; - int totalFiles = 0, totalFileSize = 0, fileDateModifiedChecksum = 0; + int enumFileHandle, totalFiles, fileDateModifiedChecksum; + uint64 totalFileSize; + file_info enumFileInfo; - char pluginPath[MAX_PATH]; - get_plugin_path(pluginPath); - - // Enumerate through each object in the directory - hFindFile = FindFirstFile(RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char), &findFileData); - if (hFindFile != INVALID_HANDLE_VALUE) { - do { - totalFiles++; - totalFileSize += findFileData.nFileSizeLow; - fileDateModifiedChecksum ^= - findFileData.ftLastWriteTime.dwLowDateTime ^ - findFileData.ftLastWriteTime.dwHighDateTime; - fileDateModifiedChecksum = ror32(fileDateModifiedChecksum, 5); - } while (FindNextFile(hFindFile, &findFileData)); - FindClose(hFindFile); - } + object_list_query_directory(&totalFiles, &totalFileSize, &fileDateModifiedChecksum); + // Would move this into cache load, but its used further on totalFiles = ror32(totalFiles, 24); totalFiles = (totalFiles & ~0xFF) | 1; totalFiles = rol32(totalFiles, 24); - // Read plugin header - rct_plugin_header pluginHeader; - - FILE *file = fopen(pluginPath, "rb"); - if (file != NULL) { - if (fread(&pluginHeader, sizeof(pluginHeader), 1, file) == 1) { - // Check if object repository has changed in anyway - if ( - totalFiles == pluginHeader.total_files && - totalFileSize == pluginHeader.total_file_size && - fileDateModifiedChecksum == pluginHeader.date_modified_checksum - ) { - // 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; - } - - // 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(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32) = pluginHeader.object_list_no_items; - - fclose(file); - sub_6A9FC0(); - object_list_examine(); - return; - } - } - } - fclose(file); - } + if (object_list_cache_load(totalFiles, totalFileSize, fileDateModifiedChecksum)) + return; // Reload object list - RCT2_GLOBAL(0x00F42B94, uint32) = totalFiles; - RCT2_GLOBAL(0x00F42B98, uint32) = totalFileSize; - RCT2_GLOBAL(0x00F42B9C, uint32) = fileDateModifiedChecksum; - //RCT2_CALLPROC_EBPSAFE(0x006A8D8F); - int eax = 3161; - if (RCT2_GLOBAL(0x9AA00D, uint8) != 0){ - eax = 3160; + if (RCT2_GLOBAL(0x9AA00D, uint8) != 0) 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(); @@ -267,242 +253,271 @@ void object_list_load() return; } - uint32 installed_buffer_size = 0x1000; + uint32 fileCount = 0; 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; - } + log_verbose("building cache of available objects..."); - for (uint8 first_time = 1; first_time || FindNextFile(hFindFile, &findFileData);){ - first_time = 0; + enumFileHandle = platform_enumerate_files_begin(RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char)); + if (enumFileHandle != INVALID_HANDLE) { + uint32 installed_buffer_size = 0x1000; - RCT2_GLOBAL(0x9ABD98, HANDLE) = hFindFile; - - file_count++; - // update progress bar. - eax = (file_count << 8) / ((RCT2_GLOBAL(0xF42B94, uint32) & 0xFFFFFF) + 1); + while (platform_enumerate_files_next(enumFileHandle, &enumFileInfo)) { + fileCount++; - - - 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]; + 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; } } - 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, ")"); + char path[MAX_PATH]; + subsitute_path(path, RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char), enumFileInfo.path); + + rct_object_entry* entry = RCT2_ADDRESS(0x00F42B74, rct_object_entry); + if (!object_load_entry(path, entry)) + continue; + + 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, enumFileInfo.path); 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); - } + // Chunk size is set to unknown + *((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; - 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); - } + RCT2_GLOBAL(0x9ADAF0, uint32) = 0xF26E; - *((uint32*)installed_entry_pointer) = RCT2_GLOBAL(0xF433DD, uint32); - installed_entry_pointer += 4; + RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)++; - int size_of_object = installed_entry_pointer - RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*) - current_item_offset; + // This is a variable used by object_load to decide if it should + // use object_paint on the entry. + RCT2_GLOBAL(0x009ADAFD, uint8) = 1; - object_unload(entry->flags & 0xF, (rct_object_entry_extended*)entry); + // Probably used by object paint. + RCT2_GLOBAL(0x009ADAF4, uint32) = 0xF42BDB; - // Return pointer to start of entry - installed_entry_pointer -= size_of_object; + int chunk_size; + if (!object_load(-1, entry, &chunk_size)){ + RCT2_GLOBAL(0x009ADAF4, sint32) = -1; + RCT2_GLOBAL(0x009ADAFD, uint8) = 0; + RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)--; + continue; + } - uint8* copied_entry = RCT2_ADDRESS(0x140E9AC, uint8); + int objectType = entry->flags & 0xF; - size_of_object = object_copy(copied_entry, installed_entry_pointer); + // See above note + RCT2_GLOBAL(0x009ADAF4, sint32) = -1; + RCT2_GLOBAL(0x009ADAFD, uint8) = 0; - RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)--; - copied_entry += sizeof(rct_object_entry); - // Skip filename - while (*copied_entry++); + if ((entry->flags & 0xF0) == 0x80) { + RCT2_GLOBAL(0x00F42B70, uint32)++; + if (RCT2_GLOBAL(0x00F42B70, uint32) > 772){ + RCT2_GLOBAL(0x00F42B70, uint32)--; + RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32)--; + continue; + } + } + *((sint32*)installed_entry_pointer) = chunk_size; + installed_entry_pointer += 4; - // Skip - copied_entry += 4; + uint8* chunk = RCT2_GLOBAL(RCT2_ADDRESS_CURR_OBJECT_CHUNK_POINTER, uint8*); // Loaded in object_load - installed_entry_pointer = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*); + // When made of two parts i.e Wooden Roller Coaster (Dream Woodie Cars); + if (objectType == 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]; + } + } - for (uint32 i = 0; i < RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32); ++i){ + 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(0x00F42BBC, uint32))); + strcat(installed_entry_pointer, ")"); + while (*installed_entry_pointer++); + } else{ + strcpy(installed_entry_pointer, language_get_string(RCT2_GLOBAL(0x00F42BBC, uint32))); + while (*installed_entry_pointer++); + } + *((uint32*)installed_entry_pointer) = RCT2_GLOBAL(0x009ADAF0, uint32) - 0xF26E; + installed_entry_pointer += 4; - uint8* temp_installed_entry = installed_entry_pointer; - temp_installed_entry += sizeof(rct_object_entry); + uint8* esi = RCT2_ADDRESS(0x00F42BDB, uint8); + uint8 num_unk_objects = *esi++; + *installed_entry_pointer++ = num_unk_objects; + if (num_unk_objects > 0) { + memcpy(installed_entry_pointer, esi, num_unk_objects * sizeof(rct_object_entry)); + installed_entry_pointer += num_unk_objects * sizeof(rct_object_entry); + } + uint8 no_theme_objects = *esi++; + *installed_entry_pointer++ = no_theme_objects ; + if (no_theme_objects > 0) { + memcpy(installed_entry_pointer, esi, no_theme_objects * sizeof(rct_object_entry)); + installed_entry_pointer += no_theme_objects * sizeof(rct_object_entry); + } + + *((uint32*)installed_entry_pointer) = RCT2_GLOBAL(0x00F433DD, 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(objectType, (rct_object_entry_extended*)entry); + + // Return pointer to start of entry + installed_entry_pointer -= size_of_object; + + uint8* copied_entry = RCT2_ADDRESS(0x0140E9AC, 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 (*temp_installed_entry++); + while (*copied_entry++); // Skip - temp_installed_entry += 4; + copied_entry += 4; - if (strcmp(temp_installed_entry, copied_entry) <= 0)break; + installed_entry_pointer = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*); - installed_entry_pointer = (uint8*)(object_get_next((rct_object_entry*)installed_entry_pointer)); + 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 numBytesToMove = 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 (numBytesToMove > 0) + memmove(move_location, curr_location, numBytesToMove); + + copied_entry = RCT2_ADDRESS(0x0140E9AC, 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)++; } - - // 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)++; + platform_enumerate_files_end(enumFileHandle); } - - 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); - - get_plugin_path(pluginPath); - FILE* obj_list_file = fopen(pluginPath,"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); - } + object_list_cache_save(fileCount, totalFileSize, fileDateModifiedChecksum, current_item_offset); + // ride_list_item ride_list; ride_list.entry_index = 0xFC; ride_list.type = 0xFC; - track_load_list(ride_list); object_list_examine(); } +static int object_list_cache_load(int totalFiles, uint64 totalFileSize, int fileDateModifiedChecksum) +{ + char path[MAX_PATH]; + FILE *file; + rct_plugin_header pluginHeader; + + log_verbose("loading object list cache (plugin.dat)"); + + get_plugin_path(path); + file = fopen(path, "rb"); + if (file == NULL) { + log_verbose("Unable to load %s", path); + return 0; + } + + if (fread(&pluginHeader, sizeof(rct_plugin_header), 1, file) == 1) { + // Check if object repository has changed in anyway + if ( + pluginHeader.total_files == totalFiles && + pluginHeader.total_file_size == totalFileSize && + pluginHeader.date_modified_checksum == fileDateModifiedChecksum + ) { + // 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; + } + + // 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(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32) = pluginHeader.object_list_no_items; + + fclose(file); + sub_6A9FC0(); + object_list_examine(); + return 1; + } + } + } + + fclose(file); + + log_error("loading object list cache failed"); + return 0; +} + +static int object_list_cache_save(int fileCount, uint64 totalFileSize, int fileDateModifiedChecksum, int currentItemOffset) +{ + char path[MAX_PATH]; + FILE *file; + rct_plugin_header pluginHeader; + + log_verbose("saving object list cache (plugin.dat)"); + + pluginHeader.total_files = fileCount | 0x01000000; + pluginHeader.total_file_size = (uint32)totalFileSize; + pluginHeader.date_modified_checksum = fileDateModifiedChecksum; + pluginHeader.object_list_size = currentItemOffset; + pluginHeader.object_list_no_items = RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32); + + get_plugin_path(path); + file = fopen(path,"wb"); + if (file == NULL) { + log_error("Failed to save %s", path); + return 0; + } + + fwrite(&pluginHeader, sizeof(rct_plugin_header), 1, file); + fwrite(RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*), pluginHeader.object_list_size, 1, file); + fclose(file); + return 1; +} + static int check_object_entry(rct_object_entry *entry) { uint32 *dwords = (uint32*)entry; @@ -552,7 +567,9 @@ int object_read_and_load_entries(FILE *file) int i, j; rct_object_entry *entries; - log_verbose("Entered object read and load entries"); + + log_verbose("loading required objects"); + // Read all the object entries entries = malloc(OBJECT_ENTRY_COUNT * sizeof(rct_object_entry)); sawyercoding_read_chunk(file, (uint8*)entries); @@ -570,14 +587,9 @@ 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], &chunk_size)) { - // Failed to load the object - //Destroy progress bar - log_error("failed to load entry:"); - log_error("%.8s", entries[i].name); + if (!object_load(entryGroupIndex, &entries[i], NULL)) { + log_error("failed to load entry: %.8s", entries[i].name); memcpy((char*)0x13CE952, &entries[i], sizeof(rct_object_entry)); free(entries); object_unload_all(); @@ -607,3 +619,67 @@ void object_unload_all() sub_6A9FC0(); } + + + +uint32 _installedObjectHashTableSize; +rct_object_entry ** _installedObjectHashTable = NULL; +uint32 _installedObjectHashTableCollisions; + +uint32 object_get_hash_code(rct_object_entry *object) +{ + uint32 hash = 5381; + uint8 *byte = (uint8*)object; + int i; + + for (i = 0; i < 8; i++) + hash = ((hash << 5) + hash) + object->name[i]; + + return hash; +} + +void object_list_create_hash_table() +{ + rct_object_entry *installedObject; + int numInstalledObjects = RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, sint32); + + if (_installedObjectHashTable != NULL) + free(_installedObjectHashTable); + + _installedObjectHashTableSize = max(8192, numInstalledObjects * 4); + _installedObjectHashTable = calloc(_installedObjectHashTableSize, sizeof(rct_object_entry*)); + _installedObjectHashTableCollisions = 0; + + installedObject = RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, rct_object_entry*); + for (int i = 0; i < numInstalledObjects; i++) { + uint32 hash = object_get_hash_code(installedObject); + uint32 index = hash % _installedObjectHashTableSize; + + // Find empty slot + while (_installedObjectHashTable[index] != NULL) { + _installedObjectHashTableCollisions++; + index++; + } + + // Set hash table slot + _installedObjectHashTable[index] = installedObject; + + // Next installde object + installedObject = object_get_next(installedObject); + } +} + +rct_object_entry *object_list_find(rct_object_entry *entry) +{ + uint32 hash = object_get_hash_code(entry); + uint32 index = hash % _installedObjectHashTableSize; + + while (_installedObjectHashTable[index] != NULL) { + if (object_entry_compare(entry, _installedObjectHashTable[index])) + return _installedObjectHashTable[index]; + + index++; + } + + return NULL; +} \ No newline at end of file diff --git a/src/platform/platform.h b/src/platform/platform.h index 0cd62e38db..2b2e8db336 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -21,10 +21,20 @@ #ifndef _PLATFORM_H_ #define _PLATFORM_H_ +#include "../common.h" + #ifndef MAX_PATH #define MAX_PATH 260 #endif +#define INVALID_HANDLE -1 + +typedef struct { + const char *path; + uint64 size; + uint64 last_modified; +} file_info; + // Platform specific definitions char platform_get_path_separator(); int platform_file_exists(const char *path); @@ -32,7 +42,7 @@ int platform_directory_exists(const char *path); int platform_ensure_directory_exists(const char *path); int platform_lock_single_instance(); int platform_enumerate_files_begin(const char *pattern); -int platform_enumerate_files_next(int handle, char **outFileName); +int platform_enumerate_files_next(int handle, file_info *outFileInfo); void platform_enumerate_files_end(int handle); void platform_hide_cursor(); void platform_show_cursor(); diff --git a/src/platform/windows.c b/src/platform/windows.c index 302f151e97..c258dbcc77 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -24,6 +24,7 @@ #include "../addresses.h" #include "../cmdline.h" #include "../openrct2.h" +#include "platform.h" // The name of the mutex used to prevent multiple instances of the game from running #define SINGLE_INSTANCE_MUTEX_NAME "RollerCoaster Tycoon 2_GSKMUTEX" @@ -144,10 +145,10 @@ int platform_enumerate_files_begin(const char *pattern) } } - return -1; + return INVALID_HANDLE; } -int platform_enumerate_files_next(int handle, char **outFileName) +int platform_enumerate_files_next(int handle, file_info *outFileInfo) { int result; enumerate_file_info *enumFileInfo; @@ -168,7 +169,9 @@ int platform_enumerate_files_next(int handle, char **outFileName) } if (result) { - *outFileName = enumFileInfo->data.cFileName; + outFileInfo->path = enumFileInfo->data.cFileName; + outFileInfo->size = ((uint64)enumFileInfo->data.nFileSizeHigh << 32ULL) | (uint64)enumFileInfo->data.nFileSizeLow; + outFileInfo->last_modified = ((uint64)enumFileInfo->data.ftLastWriteTime.dwHighDateTime << 32ULL) | (uint64)enumFileInfo->data.ftLastWriteTime.dwLowDateTime; return 1; } else { return 0; diff --git a/src/scenario_list.c b/src/scenario_list.c index 13cd812662..8b7a26cc5f 100644 --- a/src/scenario_list.c +++ b/src/scenario_list.c @@ -49,7 +49,7 @@ static rct_scenario_basic *get_scenario_by_filename(const char *filename) void scenario_load_list() { int i, enumFileHandle; - char *enumFileName; + file_info enumFileInfo; // Load scores scenario_scores_load(); @@ -60,9 +60,9 @@ void scenario_load_list() // Enumerate through each scenario in the directory enumFileHandle = platform_enumerate_files_begin(RCT2_ADDRESS(RCT2_ADDRESS_SCENARIOS_PATH, char)); - if (enumFileHandle >= 0) { - while (platform_enumerate_files_next(enumFileHandle, &enumFileName)) { - scenario_list_add(enumFileName); + if (enumFileHandle != INVALID_HANDLE) { + while (platform_enumerate_files_next(enumFileHandle, &enumFileInfo)) { + scenario_list_add(enumFileInfo.path); } platform_enumerate_files_end(enumFileHandle); }