diff --git a/src/scripts.cc b/src/scripts.cc index 80c1de1..3ac7e4b 100644 --- a/src/scripts.cc +++ b/src/scripts.cc @@ -2337,37 +2337,30 @@ int _scr_remove_all() _queue_clear_type(EVENT_TYPE_SCRIPT, NULL); _scr_message_free(); - for (int scrType = 0; scrType < SCRIPT_TYPE_COUNT; scrType++) { - ScriptList* scriptList = &(gScriptLists[scrType]); + for (int scriptType = 0; scriptType < SCRIPT_TYPE_COUNT; scriptType++) { + 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; while (scriptListExtent != NULL) { - ScriptListExtent* next = NULL; - for (int scriptIndex = 0; scriptIndex < scriptListExtent->length;) { - Script* script = &(scriptListExtent->scripts[scriptIndex]); + int index = 0; + while (scriptListExtent != NULL && index < scriptListExtent->length) { + Script* script = &(scriptListExtent->scripts[index]); if ((script->flags & SCRIPT_FLAG_0x10) != 0) { - scriptIndex++; + index++; } else { - if (scriptIndex != 0 || scriptListExtent->length != 1) { + if (index == 0 && scriptListExtent->length == 1) { + scriptListExtent = scriptListExtent->next; scriptRemove(script->sid); } else { - next = scriptListExtent->next; 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; + } } }