Posted 24 April 2013 - 01:50 PM
buck·et list [buhk-it list]
noun
- A list of things to accomplish before one's death.
Description:
[indent=1]Adds onUnload() functionality to APIs.
That means APIs get the ability to react to os.unloadAPI() and execute some code before they're being unloaded.
API BucketList also cleans up after itself, i.e. after the last API that has been loaded with it is unloaded, API BucketList will revert everything to how it was before.[/indent]
Download:
[indent=1]http://pastebin.com/wZaSER5P[/indent]
How to use it?
- Download API BucketList
- Open up your API and put the following code at the end of it:
- The argument in the first paranthesis needs to be the absolute path of API BucketList. (as a String)
- The first argument of the third paranthesis needs to be the name of your API file. (as a String)
- The second argument of the third paranthesis needs to be your custom unload function. (as a reference)
- /myAPIs/testAPI - The API you want to use BucketList with.
- /testProgram - A program that makes use of testAPI.
- /BucketList - The code from this thread.
function onUnload()
-- Code that you want to be executed when the API is being unloaded.
end
loadfile( "/apis/BucketList" )()( "yourAPIName", onUnload )
You can name onUnload() anything you like.
The important part is the loadfile call:
Spoiler
Let's say we have the following files:Spoiler
-- ---------------------------------------------------------
-- Your main API code.
-- ---------------------------------------------------------
-- Backup native print
nativePrint = _G.print
-- Custom print
function myPrint( ... )
write("Simon says: ")
nativePrint( ... )
end
-- Overwrite native print
_G.print = myPrint
-- ---------------------------------------------------------
-- The unload code for BucketList, added at the end.
-- ---------------------------------------------------------
-- Cleanup
local function cleanUp()
nativePrint("cleanUp() called")
_G.print = nativePrint
end
-- Enable BucketList for cleanUp()
loadfile("/BucketList")()("testAPI", cleanUp)
/testProgram:
Spoiler
os.loadAPI("/myAPIs/testAPI")
print("Hello!")
os.unloadAPI("testAPI")
print("Hello!")
Then the output of /testProgram would be:
Simon says: Hello!
cleanUp() called
Hello!
Which means that /myAPIs/testAPI successfully executed its cleanUp() function during os.unloadAPI("testAPI").