diff --git a/library/Core.cpp b/library/Core.cpp index 175610700..d2e2d347e 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -394,12 +394,14 @@ static bool try_autocomplete(color_ostream &con, const std::string &first, std:: } string findScript(string path, string name) { - //first try the save folder if it exists - string save = World::ReadWorldFolder(); - if ( save != "" ) { - string file = path + "/data/save/" + save + "/raw/scripts/" + name; - if (fileExists(file)) { - return file; + if (df::global::world) { + //first try the save folder if it exists + string save = World::ReadWorldFolder(); + if ( save != "" ) { + string file = path + "/data/save/" + save + "/raw/scripts/" + name; + if (fileExists(file)) { + return file; + } } } string file = path + "/raw/scripts/" + name; @@ -1407,7 +1409,9 @@ void Core::onUpdate(color_ostream &out) } static void handleLoadAndUnloadScripts(Core* core, color_ostream& out, state_change_event event) { - //TODO: use different separators for windows + if (!df::global::world) + return; + //TODO: use different separators for windows #ifdef _WIN32 static const std::string separator = "\\"; #else diff --git a/library/modules/EventManager.cpp b/library/modules/EventManager.cpp index 3dfe89822..86736d646 100644 --- a/library/modules/EventManager.cpp +++ b/library/modules/EventManager.cpp @@ -237,6 +237,16 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event //out.print("%s,%d: on load, frame_counter = %d\n", __FILE__, __LINE__, tick); */ //tickQueue.clear(); + if (!df::global::item_next_id) + return; + if (!df::global::building_next_id) + return; + if (!df::global::job_next_id) + return; + if (!df::global::ui) + return; + if (!df::global::world) + return; nextItem = *df::global::item_next_id; nextBuilding = *df::global::building_next_id; @@ -291,6 +301,9 @@ void DFHack::EventManager::manageEvents(color_ostream& out) { if ( !gameLoaded ) { return; } + if (!df::global::world) + return; + CoreSuspender suspender; int32_t tick = df::global::world->frame_counter; @@ -316,6 +329,8 @@ void DFHack::EventManager::manageEvents(color_ostream& out) { } static void manageTickEvent(color_ostream& out) { + if (!df::global::world) + return; unordered_set toRemove; int32_t tick = df::global::world->frame_counter; while ( !tickQueue.empty() ) { @@ -342,6 +357,10 @@ static void manageTickEvent(color_ostream& out) { } static void manageJobInitiatedEvent(color_ostream& out) { + if (!df::global::world) + return; + if (!df::global::job_next_id) + return; if ( lastJobId == -1 ) { lastJobId = *df::global::job_next_id - 1; return; @@ -375,6 +394,8 @@ static int32_t getWorkerID(df::job* job) { TODO: consider checking item creation / experience gain just in case */ static void manageJobCompletedEvent(color_ostream& out) { + if (!df::global::world) + return; int32_t tick0 = eventLastTick[EventType::JOB_COMPLETED]; int32_t tick1 = df::global::world->frame_counter; @@ -500,6 +521,8 @@ static void manageJobCompletedEvent(color_ostream& out) { } static void manageUnitDeathEvent(color_ostream& out) { + if (!df::global::world) + return; multimap copy(handlers[EventType::UNIT_DEATH].begin(), handlers[EventType::UNIT_DEATH].end()); for ( size_t a = 0; a < df::global::world->units.all.size(); a++ ) { df::unit* unit = df::global::world->units.all[a]; @@ -520,6 +543,10 @@ static void manageUnitDeathEvent(color_ostream& out) { } static void manageItemCreationEvent(color_ostream& out) { + if (!df::global::world) + return; + if (!df::global::item_next_id) + return; if ( nextItem >= *df::global::item_next_id ) { return; } @@ -552,6 +579,10 @@ static void manageItemCreationEvent(color_ostream& out) { } static void manageBuildingEvent(color_ostream& out) { + if (!df::global::world) + return; + if (!df::global::building_next_id) + return; /* * TODO: could be faster * consider looking at jobs: building creation / destruction @@ -591,6 +622,8 @@ static void manageBuildingEvent(color_ostream& out) { } static void manageConstructionEvent(color_ostream& out) { + if (!df::global::world) + return; //unordered_set constructionsNow(df::global::world->constructions.begin(), df::global::world->constructions.end()); multimap copy(handlers[EventType::CONSTRUCTION].begin(), handlers[EventType::CONSTRUCTION].end()); @@ -626,6 +659,8 @@ static void manageConstructionEvent(color_ostream& out) { } static void manageSyndromeEvent(color_ostream& out) { + if (!df::global::world) + return; multimap copy(handlers[EventType::SYNDROME].begin(), handlers[EventType::SYNDROME].end()); int32_t highestTime = -1; for ( auto a = df::global::world->units.all.begin(); a != df::global::world->units.all.end(); a++ ) { @@ -653,6 +688,8 @@ static void manageSyndromeEvent(color_ostream& out) { } static void manageInvasionEvent(color_ostream& out) { + if (!df::global::ui) + return; multimap copy(handlers[EventType::INVASION].begin(), handlers[EventType::INVASION].end()); if ( df::global::ui->invasions.next_id <= nextInvasion ) @@ -666,6 +703,8 @@ static void manageInvasionEvent(color_ostream& out) { } static void manageEquipmentEvent(color_ostream& out) { + if (!df::global::world) + return; multimap copy(handlers[EventType::INVENTORY_CHANGE].begin(), handlers[EventType::INVENTORY_CHANGE].end()); unordered_map itemIdToInventoryItem; @@ -747,6 +786,8 @@ static void manageEquipmentEvent(color_ostream& out) { } static void updateReportToRelevantUnits() { + if (!df::global::world) + return; if ( df::global::world->frame_counter <= reportToRelevantUnitsTime ) return; reportToRelevantUnitsTime = df::global::world->frame_counter; @@ -767,6 +808,8 @@ static void updateReportToRelevantUnits() { } static void manageReportEvent(color_ostream& out) { + if (!df::global::world) + return; multimap copy(handlers[EventType::REPORT].begin(), handlers[EventType::REPORT].end()); std::vector& reports = df::global::world->status.reports; size_t a = df::report::binsearch_index(reports, lastReport, false); @@ -795,6 +838,8 @@ static df::unit_wound* getWound(df::unit* attacker, df::unit* defender) { } static void manageUnitAttackEvent(color_ostream& out) { + if (!df::global::world) + return; multimap copy(handlers[EventType::UNIT_ATTACK].begin(), handlers[EventType::UNIT_ATTACK].end()); std::vector& reports = df::global::world->status.reports; size_t a = df::report::binsearch_index(reports, lastReportUnitAttack, false); @@ -929,6 +974,8 @@ static std::string getVerb(df::unit* unit, std::string reportStr) { } static void manageInteractionEvent(color_ostream& out) { + if (!df::global::world) + return; multimap copy(handlers[EventType::INTERACTION].begin(), handlers[EventType::INTERACTION].end()); std::vector& reports = df::global::world->status.reports; size_t a = df::report::binsearch_index(reports, lastReportInteraction, false); diff --git a/scripts/devel/find-offsets.lua b/scripts/devel/find-offsets.lua index 1ce7a4425..f33b0124a 100644 --- a/scripts/devel/find-offsets.lua +++ b/scripts/devel/find-offsets.lua @@ -488,7 +488,7 @@ local function is_valid_world(world) if not ms.is_valid_vector(world.units.all, 4) or not ms.is_valid_vector(world.units.bad, 4) or not ms.is_valid_vector(world.history.figures, 4) - or not ms.is_valid_vector(world.cur_savegame.map_features, 4) + or not ms.is_valid_vector(world.features.map_features, 4) then dfhack.printerr('Vector layout check failed.') return false