Fix some scripts not being properly removed

Closes #224
This commit is contained in:
Alexander Batalov 2023-01-21 13:15:18 +03:00
parent 47f5be8340
commit ba49abcea6
1 changed files with 11 additions and 18 deletions

View File

@ -2337,37 +2337,30 @@ int _scr_remove_all()
_queue_clear_type(EVENT_TYPE_SCRIPT, NULL); _queue_clear_type(EVENT_TYPE_SCRIPT, NULL);
_scr_message_free(); _scr_message_free();
for (int scrType = 0; scrType < SCRIPT_TYPE_COUNT; scrType++) { for (int scriptType = 0; scriptType < SCRIPT_TYPE_COUNT; scriptType++) {
ScriptList* scriptList = &(gScriptLists[scrType]); ScriptList* scriptList = &(gScriptLists[scriptType]);
// TODO: Super odd way to remove scripts. The problem is that [scrRemove]
// does relocate scripts between extents, so current extent may become
// empty. In addition there is a 0x10 flag on the script that is not
// removed. Find a way to refactor this.
ScriptListExtent* scriptListExtent = scriptList->head; ScriptListExtent* scriptListExtent = scriptList->head;
while (scriptListExtent != NULL) { while (scriptListExtent != NULL) {
ScriptListExtent* next = NULL; int index = 0;
for (int scriptIndex = 0; scriptIndex < scriptListExtent->length;) { while (scriptListExtent != NULL && index < scriptListExtent->length) {
Script* script = &(scriptListExtent->scripts[scriptIndex]); Script* script = &(scriptListExtent->scripts[index]);
if ((script->flags & SCRIPT_FLAG_0x10) != 0) { if ((script->flags & SCRIPT_FLAG_0x10) != 0) {
scriptIndex++; index++;
} else { } else {
if (scriptIndex != 0 || scriptListExtent->length != 1) { if (index == 0 && scriptListExtent->length == 1) {
scriptListExtent = scriptListExtent->next;
scriptRemove(script->sid); scriptRemove(script->sid);
} else { } else {
next = scriptListExtent->next;
scriptRemove(script->sid); scriptRemove(script->sid);
// CE: Current extent is freed in |scriptRemove|. Break
// to prevent next iteration which needs to dereference
// extent to obtain it's length.
break;
} }
} }
} }
scriptListExtent = next; if (scriptListExtent != NULL) {
scriptListExtent = scriptListExtent->next;
}
} }
} }