scripting: first check if we are still loaded before we try to remove ourselves

Signed-off-by: Ruben Smits <ruben.smits@intermodalics.eu>
scripting: disable events in StateMachine::deactivate() and add mutex protection to StateMachine::activate()

Signed-off-by: Johannes Meyer <johannes@intermodalics.eu>