Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- fsPlugins created by airminer96
- ------------
- -- Fields --
- ------------
- -- Debug option
- local debug = false
- -- The functions to be replaced from the fs API
- local keys = {"list","exists","isDir","isReadOnly","getDrive","getSize","getFreeSpace","makeDir","delete","open"}
- -- The original values from the fs API
- local fsRaw = {}
- -- The original value of os.unloadAPI
- local unloadRaw
- -- The list of the registered handlers
- local handlers = {}
- ----------------
- -- Public API --
- ----------------
- --[[
- Register a handler
- parameters: handler
- A handler is a function, which is passed the following paramters:
- path: the path of the file being resolved
- handle: the table, from which the fs API methods should be grabbed from
- action: the name of the fs API method being used
- A handler must return the following values:
- path: the overridden path of the file being resolved
- handle: the overridden table, from which the fs API methods should be grabbed from
- ]]
- function registerHandler(handler)
- if type(handler) == "function" then
- table.insert(handlers,handler)
- else
- error("Registered handler is not a function",2)
- end
- end
- --[[
- Removes a handler
- parameters: handler
- ]]
- function removeHandler(handler)
- for k,v in ipairs(handlers) do
- if v == handler then
- table.remove(handlers,k)
- end
- end
- end
- ---------------
- -- Internals --
- ---------------
- -- Checks if the response from a handler is valid
- local function responseValid(path, handle)
- if (type(path) == "string" or type(path) == "nil") and (type(handle) == "table" or type(handle) == "nil") then
- for k,v in ipairs(keys) do
- if type(handle[v]) ~= "function" then
- return false
- end
- end
- return true
- end
- return false
- end
- -- Calls a handler, and removes it if it's response isn't valid
- local function callHandler(handler, path, handle, action)
- p,h = handler(path, handle, action)
- if responseValid(p,h) then
- return p,h
- else
- removeHandler(handler)
- return path,handle
- end
- end
- -- Resolves a path
- local function resolve(path, action)
- -- Quickfix for infinite looping
- loop = 0
- p = path
- h = fsRaw
- trace = {}
- activeHandlers = {unpack(handlers)}
- while #activeHandlers > 0 and loop < 100 do
- loop = loop + 1
- p1,h1 = callHandler(activeHandlers[1],p,h,action)
- if (p ~= p1) or (h ~= h1) then
- activeHandlers = {unpack(handlers)}
- p = p1
- h = h1
- else
- table.remove(activeHandlers,1)
- end
- end
- if debug then
- print(path.." --> "..p)
- end
- return p,h
- end
- -- Creates a wrapper function for the fs API function with the name of <action>
- local function funcFactory(action)
- return function(path, ...)
- p,h = resolve(path, action)
- return h[action](p, ...)
- end
- end
- -- The hook which overrides os.unloadAPI
- local function unload(name)
- if name == "fsPlugins" and fsPlugins then
- -- Restore fs API
- for k,v in pairs(fsRaw) do
- fs[k] = v
- end
- end
- if (name == "fsPlugins" or fsPlugins == nil) and os.unloadAPI == unload then
- -- Restore os.unloadAPI
- os.unloadAPI = unloadRaw
- end
- unloadRaw(name)
- end
- -- os.loadAPI sequence
- if shell == nil then
- if fsPlugins then
- -- Unload API if already loaded
- os.unloadAPI("fsPlugins")
- end
- -- Override os.unloadAPI
- unloadRaw = os.unloadAPI
- os.unloadAPI = unload
- -- Override fs API
- for k,v in ipairs(keys) do
- fsRaw[v] = fs[v]
- fs[v] = funcFactory(v)
- end
- -- Shell program sequence
- else
- tArgs = {...}
- if tArgs[1] == "load" then
- -- Load API
- os.loadAPI(shell.getRunningProgram())
- elseif tArgs[1] == "unload" then
- -- Unload API
- os.unloadAPI("fsPlugins")
- else
- -- Print Usages
- print("Usages:\nfsPlugins help\nfsPlugins api\nfsPlugins load\nfsPlugins unload")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment