KaoSDlanor

fs_mt

May 3rd, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.38 KB | None | 0 0
  1. function string.split(str,chars,num)
  2.     num=tonumber(num)
  3.     local tStrings={}
  4.     local count=1
  5.     local lastfound=1
  6.     for at=1,str:len() do
  7.         if str:sub(at,at+chars:len()-1)==chars then
  8.             if num~=nil and count<num then
  9.                 count=count+1
  10.             elseif num~=nil then
  11.                 return str:sub(1,at-1),str:sub(at+chars:len())
  12.             else
  13.                 table.insert(tStrings,str:sub(lastfound,at-1))
  14.                 lastfound=at+chars:len()
  15.             end
  16.         end
  17.     end
  18.     if num==nil then
  19.         table.insert(tStrings,str:sub(lastfound))
  20.         return unpack(tStrings)
  21.     end
  22.     return false
  23. end
  24.  
  25. function check(sDir)
  26.     sDir=shell.resolve(sDir)
  27.     local tPath={string.split(sDir,"/")}
  28.     local at=""
  29.     local i=1
  30.     while i<=#tPath do
  31.         local redirected=false
  32.         local mtFile=fs.combine(at,"fsmetatable")
  33.         at=fs.combine(at,tPath[i])
  34.         if not fs.exists(at) and fs.exists(mtFile) then
  35.             local mt=setmetatable({},{__index=getfenv()})
  36.             setfenv(loadfile(mtFile),mt)()
  37.             if mt.__index then
  38.                 at=check(type(mt.__index)=="function" and mt.__index(at,tPath[i]) or type(mt.__index)=="string" and fs.combine(mt.__index,tPath[i]))
  39.                 redirected=true
  40.             end
  41.         end
  42.         if not redirected and not fs.exists(at) then
  43.             error("'"..at.."' does not exist so '"..sDir.."' cannot exist",2)
  44.         elseif not redirected and i<#tPath and not fs.isDir(at) then
  45.             error("'"..at.."' is not a directory so '"..sDir.."' cannot exist",2)
  46.         end
  47.         i=i+1
  48.     end
  49.     return at
  50. end
Advertisement
Add Comment
Please, Sign In to add comment