FileSystem.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. --[[
  2. MIT License
  3. Copyright (c) 2019 Mitchell Davis <coding.jackalope@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. --]]
  20. local FileSystem = {}
  21. function FileSystem.Separator()
  22. -- Lua/Love2D returns all paths with back slashes.
  23. return "/"
  24. end
  25. function FileSystem.GetDirectoryItems(Directory, Options)
  26. Options = Options == nil and {} or Options
  27. Options.Files = Options.Files == nil and true or Options.Files
  28. Options.Directories = Options.Directories == nil and true or Options.Directories
  29. Options.Filter = Options.Filter == nil and "*.*" or Options.Filter
  30. local Cmd = ""
  31. local OS = love.system.getOS()
  32. if string.sub(Directory, #Directory, #Directory) ~= FileSystem.Separator() then
  33. Directory = Directory .. FileSystem.Separator()
  34. end
  35. if OS == "Windows" then
  36. Directory = string.gsub(Directory, "/", "\\")
  37. if Options.Files and not Options.Directories then
  38. Cmd = 'DIR "' .. Directory .. Options.Filter .. '" /B /A:-D-H'
  39. elseif Options.Directories and not Options.Files then
  40. Cmd = 'DIR "' .. Directory .. '" /B /A:D-H'
  41. else
  42. Cmd = 'DIR "' .. Directory .. '" /B /A-H'
  43. end
  44. else
  45. if Options.Files and not Options.Directories then
  46. Cmd = 'find "' .. Directory .. '" \\( ! -regex ".*/\\..*" \\) -maxdepth 1 -type f \\( -iname \\' .. Options.Filter .. ' \\)'
  47. elseif Options.Directories and not Options.Files then
  48. Cmd = 'find "' .. Directory .. '" ! -path ' .. Directory .. ' \\( ! -regex ".*/\\..*" \\) -maxdepth 1 -type d'
  49. else
  50. Cmd = 'ls -1 ' .. Directory
  51. end
  52. end
  53. local Result = {}
  54. local Handle, Error = io.popen(Cmd)
  55. if Handle ~= nil then
  56. local I = 1
  57. for Item in Handle:lines() do
  58. if Item ~= "nil" then
  59. Result[I] = Item
  60. I = I + 1
  61. end
  62. end
  63. io.close(Handle)
  64. end
  65. return Result
  66. end
  67. function FileSystem.Exists(Path)
  68. local Handle = io.open(Path)
  69. if Handle ~= nil then
  70. io.close(Handle)
  71. return true
  72. else
  73. local OS = love.system.getOS()
  74. if OS == "Windows" then
  75. local OK, Error, Code = os.rename(Path, Path)
  76. if OK then
  77. return true
  78. else
  79. if Code == 13 then
  80. return true
  81. end
  82. end
  83. end
  84. end
  85. return false
  86. end
  87. function FileSystem.IsDirectory(Path)
  88. return FileSystem.Exists(Path .. FileSystem.Separator())
  89. end
  90. function FileSystem.Parent(Path)
  91. local Result = Path
  92. local Index = 1
  93. local I = Index
  94. repeat
  95. Index = I
  96. I = string.find(Path, FileSystem.Separator(), Index + 1, true)
  97. until I == nil
  98. if Index > 1 then
  99. Result = string.sub(Path, 1, Index - 1)
  100. end
  101. return Result
  102. end
  103. function FileSystem.GetBaseName(Path, RemoveExtension)
  104. local Result = string.match(Path, "^.+/(.+)$")
  105. if Result == nil then
  106. Result = Path
  107. end
  108. if RemoveExtension then
  109. Result = FileSystem.RemoveExtension(Result)
  110. end
  111. return Result
  112. end
  113. function FileSystem.GetDirectory(Path)
  114. local Result = string.match(Path, "(.+)/")
  115. if Result == nil then
  116. Result = Path
  117. end
  118. return Result
  119. end
  120. function FileSystem.GetRootDirectory(Path)
  121. local Result = Path
  122. local Index = string.find(Path, FileSystem.Separator(), 1, true)
  123. if Index ~= nil then
  124. Result = string.sub(Path, 1, Index - 1)
  125. end
  126. return Result
  127. end
  128. function FileSystem.GetSlabPath()
  129. local Path = love.filesystem.getSource()
  130. if not FileSystem.IsDirectory(Path) then
  131. Path = love.filesystem.getSourceBaseDirectory()
  132. end
  133. return Path .. "/Slab"
  134. end
  135. function FileSystem.RemoveExtension(Path)
  136. local Result = string.match(Path, "(.+)%.")
  137. if Result == nil then
  138. Result = Path
  139. end
  140. return Result
  141. end
  142. function FileSystem.ReadContents(Path, IsBinary)
  143. local Result = nil
  144. local Mode = IsBinary and "rb" or "r"
  145. local Handle, Error = io.open(Path, Mode)
  146. if Handle ~= nil then
  147. Result = Handle:read("*a")
  148. Handle:close()
  149. end
  150. return Result, Error
  151. end
  152. function FileSystem.SaveContents(Path, Contents)
  153. local Result = false
  154. local Handle, Error = io.open(Path, "w")
  155. if Handle ~= nil then
  156. Handle:write(Contents)
  157. Handle:close()
  158. Result = true
  159. end
  160. return Result, Error
  161. end
  162. return FileSystem