Fix crashes caused by missing globals

develop
Quietust 2014-07-21 19:14:43 -05:00
parent 38cdb37433
commit 9435d8f4d9
3 changed files with 59 additions and 8 deletions

@ -394,12 +394,14 @@ static bool try_autocomplete(color_ostream &con, const std::string &first, std::
} }
string findScript(string path, string name) { string findScript(string path, string name) {
//first try the save folder if it exists if (df::global::world) {
string save = World::ReadWorldFolder(); //first try the save folder if it exists
if ( save != "" ) { string save = World::ReadWorldFolder();
string file = path + "/data/save/" + save + "/raw/scripts/" + name; if ( save != "" ) {
if (fileExists(file)) { string file = path + "/data/save/" + save + "/raw/scripts/" + name;
return file; if (fileExists(file)) {
return file;
}
} }
} }
string file = path + "/raw/scripts/" + name; 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) { 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 #ifdef _WIN32
static const std::string separator = "\\"; static const std::string separator = "\\";
#else #else

@ -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); //out.print("%s,%d: on load, frame_counter = %d\n", __FILE__, __LINE__, tick);
*/ */
//tickQueue.clear(); //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; nextItem = *df::global::item_next_id;
nextBuilding = *df::global::building_next_id; nextBuilding = *df::global::building_next_id;
@ -291,6 +301,9 @@ void DFHack::EventManager::manageEvents(color_ostream& out) {
if ( !gameLoaded ) { if ( !gameLoaded ) {
return; return;
} }
if (!df::global::world)
return;
CoreSuspender suspender; CoreSuspender suspender;
int32_t tick = df::global::world->frame_counter; 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) { static void manageTickEvent(color_ostream& out) {
if (!df::global::world)
return;
unordered_set<EventHandler> toRemove; unordered_set<EventHandler> toRemove;
int32_t tick = df::global::world->frame_counter; int32_t tick = df::global::world->frame_counter;
while ( !tickQueue.empty() ) { while ( !tickQueue.empty() ) {
@ -342,6 +357,10 @@ static void manageTickEvent(color_ostream& out) {
} }
static void manageJobInitiatedEvent(color_ostream& out) { static void manageJobInitiatedEvent(color_ostream& out) {
if (!df::global::world)
return;
if (!df::global::job_next_id)
return;
if ( lastJobId == -1 ) { if ( lastJobId == -1 ) {
lastJobId = *df::global::job_next_id - 1; lastJobId = *df::global::job_next_id - 1;
return; return;
@ -375,6 +394,8 @@ static int32_t getWorkerID(df::job* job) {
TODO: consider checking item creation / experience gain just in case TODO: consider checking item creation / experience gain just in case
*/ */
static void manageJobCompletedEvent(color_ostream& out) { static void manageJobCompletedEvent(color_ostream& out) {
if (!df::global::world)
return;
int32_t tick0 = eventLastTick[EventType::JOB_COMPLETED]; int32_t tick0 = eventLastTick[EventType::JOB_COMPLETED];
int32_t tick1 = df::global::world->frame_counter; int32_t tick1 = df::global::world->frame_counter;
@ -500,6 +521,8 @@ static void manageJobCompletedEvent(color_ostream& out) {
} }
static void manageUnitDeathEvent(color_ostream& out) { static void manageUnitDeathEvent(color_ostream& out) {
if (!df::global::world)
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::UNIT_DEATH].begin(), handlers[EventType::UNIT_DEATH].end()); multimap<Plugin*,EventHandler> copy(handlers[EventType::UNIT_DEATH].begin(), handlers[EventType::UNIT_DEATH].end());
for ( size_t a = 0; a < df::global::world->units.all.size(); a++ ) { for ( size_t a = 0; a < df::global::world->units.all.size(); a++ ) {
df::unit* unit = df::global::world->units.all[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) { 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 ) { if ( nextItem >= *df::global::item_next_id ) {
return; return;
} }
@ -552,6 +579,10 @@ static void manageItemCreationEvent(color_ostream& out) {
} }
static void manageBuildingEvent(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 * TODO: could be faster
* consider looking at jobs: building creation / destruction * consider looking at jobs: building creation / destruction
@ -591,6 +622,8 @@ static void manageBuildingEvent(color_ostream& out) {
} }
static void manageConstructionEvent(color_ostream& out) { static void manageConstructionEvent(color_ostream& out) {
if (!df::global::world)
return;
//unordered_set<df::construction*> constructionsNow(df::global::world->constructions.begin(), df::global::world->constructions.end()); //unordered_set<df::construction*> constructionsNow(df::global::world->constructions.begin(), df::global::world->constructions.end());
multimap<Plugin*,EventHandler> copy(handlers[EventType::CONSTRUCTION].begin(), handlers[EventType::CONSTRUCTION].end()); multimap<Plugin*,EventHandler> 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) { static void manageSyndromeEvent(color_ostream& out) {
if (!df::global::world)
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::SYNDROME].begin(), handlers[EventType::SYNDROME].end()); multimap<Plugin*,EventHandler> copy(handlers[EventType::SYNDROME].begin(), handlers[EventType::SYNDROME].end());
int32_t highestTime = -1; int32_t highestTime = -1;
for ( auto a = df::global::world->units.all.begin(); a != df::global::world->units.all.end(); a++ ) { 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) { static void manageInvasionEvent(color_ostream& out) {
if (!df::global::ui)
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::INVASION].begin(), handlers[EventType::INVASION].end()); multimap<Plugin*,EventHandler> copy(handlers[EventType::INVASION].begin(), handlers[EventType::INVASION].end());
if ( df::global::ui->invasions.next_id <= nextInvasion ) if ( df::global::ui->invasions.next_id <= nextInvasion )
@ -666,6 +703,8 @@ static void manageInvasionEvent(color_ostream& out) {
} }
static void manageEquipmentEvent(color_ostream& out) { static void manageEquipmentEvent(color_ostream& out) {
if (!df::global::world)
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::INVENTORY_CHANGE].begin(), handlers[EventType::INVENTORY_CHANGE].end()); multimap<Plugin*,EventHandler> copy(handlers[EventType::INVENTORY_CHANGE].begin(), handlers[EventType::INVENTORY_CHANGE].end());
unordered_map<int32_t, InventoryItem> itemIdToInventoryItem; unordered_map<int32_t, InventoryItem> itemIdToInventoryItem;
@ -747,6 +786,8 @@ static void manageEquipmentEvent(color_ostream& out) {
} }
static void updateReportToRelevantUnits() { static void updateReportToRelevantUnits() {
if (!df::global::world)
return;
if ( df::global::world->frame_counter <= reportToRelevantUnitsTime ) if ( df::global::world->frame_counter <= reportToRelevantUnitsTime )
return; return;
reportToRelevantUnitsTime = df::global::world->frame_counter; reportToRelevantUnitsTime = df::global::world->frame_counter;
@ -767,6 +808,8 @@ static void updateReportToRelevantUnits() {
} }
static void manageReportEvent(color_ostream& out) { static void manageReportEvent(color_ostream& out) {
if (!df::global::world)
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::REPORT].begin(), handlers[EventType::REPORT].end()); multimap<Plugin*,EventHandler> copy(handlers[EventType::REPORT].begin(), handlers[EventType::REPORT].end());
std::vector<df::report*>& reports = df::global::world->status.reports; std::vector<df::report*>& reports = df::global::world->status.reports;
size_t a = df::report::binsearch_index(reports, lastReport, false); 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) { static void manageUnitAttackEvent(color_ostream& out) {
if (!df::global::world)
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::UNIT_ATTACK].begin(), handlers[EventType::UNIT_ATTACK].end()); multimap<Plugin*,EventHandler> copy(handlers[EventType::UNIT_ATTACK].begin(), handlers[EventType::UNIT_ATTACK].end());
std::vector<df::report*>& reports = df::global::world->status.reports; std::vector<df::report*>& reports = df::global::world->status.reports;
size_t a = df::report::binsearch_index(reports, lastReportUnitAttack, false); 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) { static void manageInteractionEvent(color_ostream& out) {
if (!df::global::world)
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::INTERACTION].begin(), handlers[EventType::INTERACTION].end()); multimap<Plugin*,EventHandler> copy(handlers[EventType::INTERACTION].begin(), handlers[EventType::INTERACTION].end());
std::vector<df::report*>& reports = df::global::world->status.reports; std::vector<df::report*>& reports = df::global::world->status.reports;
size_t a = df::report::binsearch_index(reports, lastReportInteraction, false); size_t a = df::report::binsearch_index(reports, lastReportInteraction, false);

@ -488,7 +488,7 @@ local function is_valid_world(world)
if not ms.is_valid_vector(world.units.all, 4) 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.units.bad, 4)
or not ms.is_valid_vector(world.history.figures, 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 then
dfhack.printerr('Vector layout check failed.') dfhack.printerr('Vector layout check failed.')
return false return false