Somepotato

WolfFS

Oct 2nd, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. wolf.MsgN("Loading filesystem....")
  2. wolffs={
  3.     mounted={}
  4. }
  5. local FS={}
  6. function FS:FileExists(dir)
  7.     return false
  8. end
  9. function FS:FileOpen(dir)
  10.     return false
  11. end
  12. function FS:FileRead(dir)
  13.     return ""
  14. end
  15. function FS:FileWrite(dir, stringcontent)
  16.     return false
  17. end
  18. function FS:IsFile(path)
  19.     return false
  20. end
  21. function FS:IsDir(path)
  22.     return false
  23. end
  24. function FS:MakeFile(path)
  25.     return false
  26. end
  27. function FS:MakeDir(path)
  28.     return false
  29. end
  30. function FS:TraverseDir(path)
  31.     return {}
  32. end
  33. function wolffs.Init()
  34.     dofile("fs/"..wolf.game..".lua")
  35.     wolffs.Main=wolffs.MountDir("data/wolfengine")
  36.     PrintTable(wolffs.TraverseDir("awol"))
  37. end
  38. function wolffs.MountDir(dir,root)
  39.     local mt=setmetatable({basedir=dir,root=root},{__index=wolffs.Base})
  40.     table.insert(wolffs.mounted,mt)
  41.     return mt
  42. end
  43. function wolffs.Mount(fs,root)
  44.     table.insert(wolffs.mounted,setmetatable({root=root},{__index=fs}))
  45. end
  46. function wolffs.NewFS()
  47.     return wolf.CopyTable(FS)
  48. end
  49. local function foreachmounted(callback)
  50.     for k,v in pairs(wolffs.mounted) do
  51.         local ret=callback(v,v.root~=nil and v.root or "")
  52.         if ret then return ret end
  53.     end
  54.     return false
  55. end
  56. local function isInDir(path1, path2)
  57.     if(path2=="") then return true end--its at the root
  58.     local subdirs1=path1:split("/")
  59.     local subdirs2=path2:split("/")
  60.     --get rid of last one as its a filename
  61.     table.remove(subdirs1,#subdirs1)
  62.     --2nd one is a root dir, not a filename
  63.     for k,v in pairs(subdirs1) do
  64.         if(subdirs2[k]:lower()~=v:lower()) then return false end
  65.     end
  66.     return true
  67. end
  68. --print(isInDir("data/test/aids.txt","data/test"))
  69. -------------
  70. function wolffs.FileExists(dir)
  71.     return foreachmounted(function(fs,root)
  72.         if(not isInDir(dir, root)) then return end
  73.        
  74.         if(fs:FileExists(dir:sub(#root+1))) then
  75.             return true
  76.         end
  77.     end)
  78. end
  79. function wolffs.FileRead(dir)
  80.     return foreachmounted(function(fs,root)
  81.         if(not isInDir(dir, root)) then return end
  82.        
  83.         if(fs:FileExists(dir:sub(#root+1))) then
  84.             return fs:FileRead(dir:sub(#root+1))
  85.         end
  86.     end)
  87. end
  88. function wolffs.FileWrite(dir,data)
  89.     return wolffs.Main:FileWrite(dir,data)
  90. end
  91. function wolffs.TraverseDir(dir)
  92.     local total={}
  93.     foreachmounted(function(fs,root)
  94.         if(not isInDir(dir, root)) then return end
  95.        
  96.         if(fs:IsDir(dir:sub(#root+1))) then
  97.             local traversed=fs:TraverseDir(dir:sub(#root+1))
  98.             for k,v in pairs(traversed) do
  99.                 local exit
  100.                 for k2,v2 in pairs(total) do if(v2==v) then exit=true break end end
  101.                 if not exit then
  102.                     table.insert(total,v)
  103.                 end
  104.             end
  105.         end
  106.     end)
  107.     return total
  108.     --return wolffs.Main:FileWrite(dir,data)
  109. end
  110.  
  111.  
  112. -------------
  113. wolffs.Init()
Advertisement
Add Comment
Please, Sign In to add comment