Hooking to an existing IMC function - meldavy/ipf-documentation GitHub Wiki

In my previous write-up, I did a brief explanation of hook vs messages.

To add on to this, a message is "public api". Meaning that it's available for any listeners who want to listen to a message. It's like a free SNS topic anyone can subscribe to. However, messages can only get you so far - there's really not a message for everything. View the message dump.

To work around this limitation, we can use how lua and TOS uses global tables to store and call functions. Imagine that most IMC first party LUA code as functions that are stored in a key-value pair map.

global_map: {
    [FUNCTION_NAME_1] = functionRef_1,
    [FUNCTION_NAME_2] = functionRef_2,
    [FUNCTION_NAME_3] = functionRef_3,
}

All functions are stored with the function name as a key, and its value being a reference to the actual function. What we can do is replace the function reference to a reference to our own function, something like:

local originalFunctionReference = global_map[FUNCTION_NAME_1]
global_map[FUNCTION_NAME_1] = MyFunction

With such mechanism, we can hook into existing function calls, or even change them completely.