From faf1f08b7faf7b64b17bee18623092e4e65d54e7 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 2 Jul 2016 21:52:17 +0100 Subject: [PATCH] fix loading object via console --- src/interface/console.c | 138 ++++++++++++-------------- src/object.h | 2 +- src/object/ObjectRepository.cpp | 29 +++++- src/object/ObjectRepository.h | 2 + src/object/RideObject.cpp | 1 + src/windows/editor_object_selection.c | 3 +- 6 files changed, 95 insertions(+), 80 deletions(-) diff --git a/src/interface/console.c b/src/interface/console.c index 80ae721210..1e4dc37ffa 100644 --- a/src/interface/console.c +++ b/src/interface/console.c @@ -30,6 +30,7 @@ #include "../input.h" #include "../network/twitch.h" #include "../object.h" +#include "../object/ObjectRepository.h" #include "../world/banner.h" #include "../world/climate.h" #include "../world/scenery.h" @@ -868,84 +869,69 @@ static int cc_twitch(const utf8 **argv, int argc) static int cc_load_object(const utf8 **argv, int argc) { if (argc > 0) { - utf8 path[MAX_PATH]; - - substitute_path(path, gRCT2AddressObjectDataPath, argv[0]); - strcat(path, ".DAT\0"); - - rct_object_entry entry; - if (object_load_entry(path, &entry)) { - uint8 type = entry.flags & 0xF; - uint8 index; - - if (check_object_entry(&entry)) { - if (!find_object_in_entry_group(&entry, &type, &index)){ - - int entryGroupIndex = 0; - for (; entryGroupIndex < object_entry_group_counts[type]; entryGroupIndex++){ - if (object_entry_groups[type].chunks[entryGroupIndex] == (uint8*)-1){ - break; - } - } - - if (entryGroupIndex >= object_entry_group_counts[type]) { - console_writeline_error("Too many objects of that type."); - } - else { - // Load the obect - if (!object_load_chunk(entryGroupIndex, &entry, NULL)) { - console_writeline_error("Could not load object file."); - } - else { - reset_loaded_objects(); - if (type == OBJECT_TYPE_RIDE) { - // Automatically research the ride so it's supported by the game. - rct_ride_entry *rideEntry; - int rideType; - - rideEntry = get_ride_entry(entryGroupIndex); - - for (int j = 0; j < 3; j++) { - rideType = rideEntry->ride_type[j]; - if (rideType != 255) - research_insert(true, 0x10000 | (rideType << 8) | entryGroupIndex, rideEntry->category[0]); - } - - gSilentResearch = true; - sub_684AC3(); - gSilentResearch = false; - } - else if (type == OBJECT_TYPE_SCENERY_SETS) { - rct_scenery_set_entry *scenerySetEntry; - - scenerySetEntry = get_scenery_group_entry(entryGroupIndex); - - research_insert(true, entryGroupIndex, RESEARCH_CATEGORY_SCENERYSET); - - gSilentResearch = true; - sub_684AC3(); - gSilentResearch = false; - } - scenery_set_default_placement_configuration(); - window_new_ride_init_vars(); - - RCT2_GLOBAL(RCT2_ADDRESS_WINDOW_UPDATE_TICKS, uint16) = 0; - gfx_invalidate_screen(); - console_writeline("Object file loaded."); - } - } - } - else { - console_writeline_error("Object is already in scenario."); - } - } - else { - console_writeline_error("The object file was invalid."); - } + char name[9] = { 0 }; + memset(name, ' ', 8); + int i = 0; + for (const char * ch = argv[0]; *ch != '\0' && i < 8; ch++) { + name[i++] = *ch; } - else { - console_writeline_error("Could not find the object file."); + + const ObjectRepositoryItem * ori = object_repository_find_object_by_name(name); + if (ori == NULL) { + console_writeline_error("Could not find the object."); + return 1; } + + const rct_object_entry * entry = &ori->ObjectEntry; + void * loadedObject = object_repository_find_loaded_object(entry); + if (loadedObject != NULL) { + console_writeline_error("Object is already in scenario."); + return 1; + } + + int groupIndex; + if (!object_load_chunk(-1, entry, &groupIndex)) { + console_writeline_error("Unable to load object."); + return 1; + } + + reset_loaded_objects(); + + uint8 objectType = entry->flags & 0x0F; + if (objectType == OBJECT_TYPE_RIDE) { + // Automatically research the ride so it's supported by the game. + rct_ride_entry *rideEntry; + int rideType; + + rideEntry = get_ride_entry(groupIndex); + + for (int j = 0; j < 3; j++) { + rideType = rideEntry->ride_type[j]; + if (rideType != 255) + research_insert(true, 0x10000 | (rideType << 8) | groupIndex, rideEntry->category[0]); + } + + gSilentResearch = true; + sub_684AC3(); + gSilentResearch = false; + } + else if (objectType == OBJECT_TYPE_SCENERY_SETS) { + rct_scenery_set_entry *scenerySetEntry; + + scenerySetEntry = get_scenery_group_entry(groupIndex); + + research_insert(true, groupIndex, RESEARCH_CATEGORY_SCENERYSET); + + gSilentResearch = true; + sub_684AC3(); + gSilentResearch = false; + } + scenery_set_default_placement_configuration(); + window_new_ride_init_vars(); + + RCT2_GLOBAL(RCT2_ADDRESS_WINDOW_UPDATE_TICKS, uint16) = 0; + gfx_invalidate_screen(); + console_writeline("Object file loaded."); } return 0; diff --git a/src/object.h b/src/object.h index be5ee43f44..abf9b63a30 100644 --- a/src/object.h +++ b/src/object.h @@ -119,7 +119,7 @@ bool object_saved_packed(SDL_RWops* rw, const rct_object_entry * entry); void object_unload_all(); int check_object_entry(rct_object_entry *entry); -int object_load_chunk(int groupIndex, const rct_object_entry *entry, int* chunk_size); +bool object_load_chunk(int groupIndex, const rct_object_entry * entry, int * outGroupIndex); int object_entry_compare(const rct_object_entry *a, const rct_object_entry *b); int object_calculate_checksum(const rct_object_entry *entry, const uint8 *data, int dataLength); void reset_loaded_objects(); diff --git a/src/object/ObjectRepository.cpp b/src/object/ObjectRepository.cpp index 16577c8a61..cd6f2383cf 100644 --- a/src/object/ObjectRepository.cpp +++ b/src/object/ObjectRepository.cpp @@ -595,7 +595,7 @@ extern "C" IObjectRepository * objRepo = GetObjectRepository(); } - int object_load_chunk(int groupIndex, const rct_object_entry * entry, int * chunkSize) + bool object_load_chunk(int groupIndex, const rct_object_entry * entry, int * outGroupIndex) { IObjectRepository * objRepo = GetObjectRepository(); Object * object = objRepo->LoadObject(entry); @@ -623,6 +623,10 @@ extern "C" } } chunkList[groupIndex] = object->GetLegacyData(); + if (outGroupIndex != nullptr) + { + *outGroupIndex = groupIndex; + } rct_object_entry_extended * extendedEntry = &object_entry_groups[objectType].entries[groupIndex]; Memory::Copy(extendedEntry, object->GetObjectEntry(), sizeof(rct_object_entry)); @@ -658,6 +662,23 @@ extern "C" return (void *)object; } + void * object_repository_find_loaded_object(const rct_object_entry * objectEntry) + { + for (size_t i = 0; i < 721; i++) + { + Object * object = _loadedObjects[i]; + if (object != nullptr) + { + const rct_object_entry * entry = object->GetObjectEntry(); + if (memcmp(objectEntry->name, entry->name, 8) == 0) + { + return (void *)object; + } + } + } + return nullptr; + } + void * object_repository_get_loaded_object(uint8 objectType, uint8 entryIndex) { int index = GetObjectEntryIndex(objectType, entryIndex); @@ -820,6 +841,12 @@ extern "C" return objectRepository->FindObject(entry); } + const ObjectRepositoryItem * object_repository_find_object_by_name(const char * name) + { + IObjectRepository * objectRepository = GetObjectRepository(); + return objectRepository->FindObject(name); + } + void object_delete(void * object) { if (object != nullptr) diff --git a/src/object/ObjectRepository.h b/src/object/ObjectRepository.h index 34e311c31c..7df740b2a6 100644 --- a/src/object/ObjectRepository.h +++ b/src/object/ObjectRepository.h @@ -76,7 +76,9 @@ IObjectRepository * GetObjectRepository(); size_t object_repository_get_items_count(); const ObjectRepositoryItem * object_repository_get_items(); const ObjectRepositoryItem * object_repository_find_object_by_entry(const rct_object_entry * entry); +const ObjectRepositoryItem * object_repository_find_object_by_name(const char * name); void * object_repository_load_object(const rct_object_entry * objectEntry); +void * object_repository_find_loaded_object(const rct_object_entry * objectEntry); void * object_repository_get_loaded_object(uint8 objectType, uint8 entryIndex); void object_repository_unload(size_t itemIndex); diff --git a/src/object/RideObject.cpp b/src/object/RideObject.cpp index f09304c610..01b9f75ce3 100644 --- a/src/object/RideObject.cpp +++ b/src/object/RideObject.cpp @@ -101,6 +101,7 @@ void RideObject::Load() _legacyType.name = language_allocate_object_string(GetName()); _legacyType.description = language_allocate_object_string(GetDescription()); _legacyType.images_offset = gfx_object_allocate_images(GetImageTable()->GetImages(), GetImageTable()->GetCount()); + _legacyType.vehicle_preset_list = &_presetColours; int cur_vehicle_images_offset = _legacyType.images_offset + 3; for (int i = 0; i < 4; i++) diff --git a/src/windows/editor_object_selection.c b/src/windows/editor_object_selection.c index d92021219d..54e1d9649c 100644 --- a/src/windows/editor_object_selection.c +++ b/src/windows/editor_object_selection.c @@ -1708,8 +1708,7 @@ static void editor_load_selected_objects() if (_objectSelectionFlags[i] & OBJECT_SELECTION_FLAG_SELECTED) { uint8 entry_index, entry_type; if (!find_object_in_entry_group(&items[i].ObjectEntry, &entry_type, &entry_index)) { - int chunk_size; - if (!object_load_chunk(-1, &items[i].ObjectEntry, &chunk_size)) { + if (!object_load_chunk(-1, &items[i].ObjectEntry, NULL)) { log_error("Failed to load entry %.8s", items->Name); }