lovefs.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. --[[------------------------------------
  2. LoveFS v0.9
  3. Pure Lua FileSystem Access
  4. Under the MIT license.
  5. copyright(c) 2016 Caldas Lopes aka linux-man
  6. --]]------------------------------------
  7. local lovefs_dir = 'lovefs'
  8. local cp_table = {'737', '775', '850', '852', '855', '866', '8859-1', '8859-2', '8859-4', '8859-5', '8859-7', '8859-15', '8859-16', 'KOI8-R', 'KOI8-U'}
  9. local function strformat(str)
  10. str = str:gsub('"', '')
  11. str = string.format('%q',str)
  12. while str:find('\\\\') do str = str:gsub('\\\\', '\\') end
  13. while str:find('//') do str = str:gsub('//', '/') end
  14. return str
  15. end
  16. local function split(str, sep)
  17. local sep, fields = sep or ":", {}
  18. local pattern = string.format("([^%s]+)", sep)
  19. str:gsub(pattern, function(c) fields[#fields+1] = c end)
  20. return fields
  21. end
  22. local function split_lines(str)
  23. local t = {}
  24. for line in str:gmatch("[^\r\n]+") do table.insert(t, line) end
  25. return t
  26. end
  27. local function join_tables(t1, t2)
  28. local tb = {}
  29. for _,v in ipairs(t1) do table.insert(tb, v) end
  30. for _,v in ipairs(t2) do table.insert(tb, v) end
  31. return tb
  32. end
  33. local function normalize_utf8(str, always)
  34. always = always or false
  35. local str2 = str
  36. local utfcodes = '194,195,196,197,198,200,203,206,207,208,209,210,226'
  37. if always or not pcall(function() love.graphics.print(str, 0, 0) end) then
  38. str2 = ''
  39. for n = 1, #str do
  40. if str:byte(n) < 128 then
  41. str2 = str2..str:sub(n, n)
  42. else
  43. if utfcodes:find(tostring(str:byte(n))) then str2 = str2..'?' end
  44. end
  45. end
  46. end
  47. return str2
  48. end
  49. local function normalize_cp(str)
  50. local str2 = ''
  51. for n = 1, #str do
  52. if str:byte(n) < 128 then str2 = str2..str:sub(n, n)
  53. else str2 = str2..'?' end
  54. end
  55. return str2
  56. end
  57. filesystem = {}
  58. filesystem.__index = filesystem
  59. function filesystem:loadCp(codepage)
  60. self.tb_utf8, self.tb_cp = nil, nil
  61. if not codepage then
  62. if self.os == 'Windows' then
  63. _, lang = self:run('chcp')
  64. lang = lang:gsub('\n', '')
  65. else
  66. _, lang = self:run('echo $LANG')
  67. lang = lang:gsub('\n', '')
  68. end
  69. codepage = lang
  70. for _, c in ipairs(cp_table) do
  71. if lang:find(c) then codepage = c end
  72. end
  73. end
  74. self.cp = codepage
  75. if not love.filesystem.isFile(lovefs_dir..'/codepages/'..codepage) then
  76. if self.current then self:cd(self.current) end
  77. return false
  78. end
  79. local count = 128
  80. self.tb_utf8, self.tb_cp = {}, {}
  81. for line in love.filesystem.lines(lovefs_dir..'/codepages/'..codepage) do
  82. if line ~= '' then
  83. self.tb_utf8[count] = split(line, '\\')
  84. self.tb_cp[line] = count
  85. end
  86. count = count + 1
  87. end
  88. if self.current then self:cd(self.current) end
  89. return true
  90. end
  91. function filesystem:toUtf8(str)
  92. if not self.tb_utf8 then return normalize_utf8(str) end
  93. local str2 = ''
  94. for n = 1, #str do
  95. if str:byte(n) < 128 then str2 = str2..str:sub(n, n)
  96. else
  97. if self.tb_utf8[str:byte(n)] then
  98. for _, n in ipairs(self.tb_utf8[str:byte(n)]) do
  99. str2 = str2..string.char(n)
  100. end
  101. else str2 = str2..str:sub(n, n)
  102. end
  103. end
  104. end
  105. return normalize_utf8(str2)
  106. end
  107. function filesystem:path8p3(dir, all)
  108. dir = dir or self.current
  109. if not (dir:sub(2,2) == ':') then dir = strformat(self.current..'\\'..dir):sub(2, -2) end
  110. dir = dir:gsub('/', '\\')
  111. local tb_dir = split(dir, '\\')
  112. local dir8p3 = tb_dir[1]
  113. table.remove(tb_dir, 1)
  114. while #tb_dir > 1 do
  115. local r, c = self:run('dir /X /AD '..strformat(dir8p3..'\\'))
  116. if not r then return self.current end
  117. local dir_result = split_lines(c)
  118. local name8p3 = ''
  119. for _, line in ipairs(dir_result) do
  120. if line:find('<DIR>') and line:sub(50):find(tb_dir[1]:gsub("[%-]", "%%%0")) then
  121. if line:sub(37, 45):gsub(' ', '') ~= '' then
  122. name8p3 = line:sub(37, 45):gsub(' ', '')
  123. else
  124. name8p3 = line:sub(50)
  125. end
  126. end
  127. end
  128. if name8p3 ~= '' then dir8p3 = dir8p3..'\\'..name8p3
  129. else return self.current end
  130. table.remove(tb_dir, 1)
  131. end
  132. local name8p3 = tb_dir[1]
  133. if all then
  134. local r, c = self:run('dir /X /A-D '..strformat(dir8p3..'\\'))
  135. if not r then return self.current end
  136. local dir_result = split_lines(c)
  137. for _, line in ipairs(dir_result) do
  138. if line:sub(50):find(tb_dir[1]) then
  139. if line:sub(37, 45):gsub(' ', '') ~= '' then
  140. name8p3 = line:sub(37, 45):gsub(' ', '')
  141. else
  142. name8p3 = line:sub(50)
  143. end
  144. end
  145. end
  146. end
  147. return dir8p3..'\\'..name8p3
  148. end
  149. function filesystem:toCp(str)
  150. if not self.tb_cp then
  151. if self.os == 'Windows' then return normalize_utf8(str, true)
  152. else return str end
  153. end
  154. local str2 = ''
  155. for n = 1, #str do
  156. if str:byte(n) < 128 then str2 = str2..str:sub(n, n)
  157. else
  158. if n < #str-1 then
  159. if self.tb_cp['\\'..str:byte(n)..'\\'..str:byte(n+1)..'\\'..str:byte(n+2)] then
  160. str2 = str2..string.char(self.tb_cp['\\'..str:byte(n)..'\\'..str:byte(n+1)..'\\'..str:byte(n+2)])
  161. n = n + 2
  162. elseif self.tb_cp['\\'..str:byte(n)..'\\'..str:byte(n+1)] then
  163. str2 = str2..string.char(self.tb_cp['\\'..str:byte(n)..'\\'..str:byte(n+1)])
  164. n = n + 1
  165. end
  166. elseif n < #str then
  167. if self.tb_cp['\\'..str:byte(n)..'\\'..str:byte(n+1)] then
  168. str2 = str2..string.char(self.tb_cp['\\'..str:byte(n)..'\\'..str:byte(n+1)])
  169. n = n + 1
  170. end
  171. end
  172. end
  173. end
  174. if self.os == 'Windows' then str2 = normalize_cp(str2) end
  175. return str2
  176. end
  177. function filesystem:run(command)
  178. if love._os == 'Windows' then
  179. console = self.tempdir..os.tmpname()
  180. else
  181. console = os.tmpname()
  182. end
  183. r = os.execute(self:toCp(command)..' > '.. console)
  184. c = ''
  185. for line in io.lines(console) do
  186. line = self:toUtf8(line)
  187. if c == '' then c = line
  188. else c = c..'\n'..line end
  189. end
  190. os.remove(console)
  191. return r, c
  192. end
  193. function filesystem:ls(param, dir)
  194. dir = dir or self.current
  195. param = param or ''
  196. if self.os == 'Windows' then
  197. local r, c = self:run('cd /D '..strformat(dir)..' & dir /B /A-S-H '..param)
  198. str = c
  199. else
  200. local r, c = self:run('cd '..strformat(dir)..' ; ls -1 '..param)
  201. str = c:gsub('/', '')
  202. end
  203. return split_lines(str), str
  204. end
  205. function filesystem:lsDirs(param, dir)
  206. dir = dir or self.current
  207. param = param or ''
  208. if self.os == 'Windows' then return self:ls('/A-S-HD /ON '..param, dir)
  209. else return self:ls('-p '..param..' | grep /\\$', dir)
  210. end
  211. end
  212. function filesystem:lsFiles(param, dir)
  213. dir = dir or self.current
  214. param = param or ''
  215. if self.os == 'Windows' then return self:ls('/A-S-H-D /ON '..param, dir)
  216. else return self:ls('-p '..param..' | grep -v /\\$', dir)
  217. end
  218. end
  219. function filesystem:exists(name, dir)
  220. dir = dir or self.current
  221. if self.os == 'Windows' then r, c = self:run('cd /D '..strformat(dir)..' & dir '..strformat(name))
  222. else r, c = self:run('cd '..strformat(dir)..' ; ls '..strformat(name)) end
  223. return r == 0
  224. end
  225. function filesystem:isDirectory(name, dir)
  226. dir = dir or self.current
  227. if self.os == 'Windows' then r, c = self:run('cd /D '..strformat(dir)..' & cd '..strformat(name))
  228. else r, c = self:run('cd '..strformat(dir)..' ; cd '..strformat(name)) end
  229. return r == 0
  230. end
  231. function filesystem:isFile(name, dir)
  232. return self:exists(name, dir) and not self:isDirectory(name, dir)
  233. end
  234. function filesystem:dir(param, dir)
  235. return self:ls(param, dir)
  236. end
  237. function filesystem:cd(dir)
  238. dir = dir or self.current
  239. if self.os == 'Windows' then
  240. if not (dir:sub(2,2) == ':') then dir = self.current..'\\'..dir end
  241. dir = strformat(dir)
  242. r, c = self:run('cd /D '..strformat(dir))
  243. if r == 0 then
  244. r, c = self:run('cd /D '..strformat(dir)..' & cd')
  245. self.current = c:gsub('\n', '')
  246. self.dirs = self:lsDirs()
  247. self.files = self:lsFiles(self.param)
  248. self.all = join_tables(self.dirs, self.files)
  249. return true
  250. end
  251. else
  252. if not (dir:sub(1,1) == '/' or dir:sub(1,1) == '~') then dir = self.current..'/'..dir end
  253. dir = strformat(dir)
  254. r, c = self:run('cd '..dir)
  255. if r == 0 then
  256. r, c = self:run('cd '..dir..' ; pwd')
  257. self.current = c:gsub('//', '/'):gsub('\n', '')
  258. self.dirs = self:lsDirs()
  259. self.files = self:lsFiles(self.param)
  260. self.all = join_tables(self.dirs, self.files)
  261. return true
  262. end
  263. end
  264. return false
  265. end
  266. function filesystem:up()
  267. return self:cd('..')
  268. end
  269. function filesystem:setParam(param)
  270. if param then self.param = param end
  271. self:cd(self.current)
  272. end
  273. function filesystem:lsDrives()
  274. if self.os == 'Windows' then
  275. local str = io.popen('wmic logicaldisk get caption'):read('*all')
  276. if not str:find('C:') then str = 'default\nA:\nB:\nC:\nD:\nE:\nF:\nG:\nH:\nI:\nJ:\n' end
  277. tb = split_lines(self:toUtf8(str):gsub(' ', ''))
  278. table.remove(tb,1)
  279. elseif self.os == 'Linux' then
  280. tb = self:lsDirs('', '/media')
  281. for n, d in ipairs(tb) do tb[n] = '/media/'..tb[n] end
  282. table.insert(tb, 1, '/')
  283. else
  284. tb = self:lsDirs('', '/Volumes')
  285. for n, d in ipairs(tb) do tb[n] = '/Volumes/'..tb[n] end
  286. table.insert(tb, 1, '/')
  287. end
  288. return tb
  289. end
  290. function filesystem:copy(source, dest)
  291. love.filesystem.createDirectory('lovefs_temp')
  292. if self.os == 'Windows' then
  293. source = strformat(self:path8p3(self:toCp(source), true)):sub(2, -2)
  294. dest = strformat(self:path8p3(self:toCp(dest))):sub(2, -2)
  295. else
  296. source = strformat(self:toCp(source)):sub(2, -2)
  297. dest = strformat(self:toCp(dest)):sub(2, -2)
  298. end
  299. local inp = assert(io.open(source, "rb"))
  300. local out = assert(io.open(dest, "wb"))
  301. local data = inp:read("*all")
  302. out:write(data)
  303. assert(out:close())
  304. end
  305. function filesystem:loadImage(source)
  306. source = source or self.selectedFile
  307. self.selectedFile = nil
  308. love.filesystem.createDirectory('lovefs_temp')
  309. self:copy(source, love.filesystem.getSaveDirectory()..'/lovefs_temp/temp.file')
  310. return love.graphics.newImage('lovefs_temp/temp.file')
  311. end
  312. function filesystem:loadSource(source)
  313. source = source or self.selectedFile
  314. self.selectedFile = nil
  315. love.filesystem.createDirectory('lovefs_temp')
  316. self:copy(source, love.filesystem.getSaveDirectory()..'/lovefs_temp/temp.'..source:sub(-3))
  317. return love.audio.newSource('lovefs_temp/temp.'..source:sub(-3))
  318. end
  319. function filesystem:loadFont(size, source)
  320. source = source or self.selectedFile
  321. self.selectedFile = nil
  322. love.filesystem.createDirectory('lovefs_temp')
  323. self:copy(source, love.filesystem.getSaveDirectory()..'/lovefs_temp/temp.file')
  324. return love.graphics.newFont('lovefs_temp/temp.file', size)
  325. end
  326. function filesystem:saveImage(img, dest)
  327. if not pcall(function() love.graphics.newCanvas(img:getWidth(), img:getHeight()) end) then return false end
  328. dest = dest or self.selectedFile
  329. self.selectedFile = nil
  330. love.filesystem.createDirectory('lovefs_temp')
  331. love.graphics.setColor(255, 255, 255)
  332. local canvas = love.graphics.newCanvas(img:getWidth(), img:getHeight())
  333. love.graphics.setCanvas(canvas)
  334. love.graphics.draw(img, 0, 0)
  335. local id = canvas:getImageData()
  336. local tb = split(dest, '.')
  337. id:encode('lovefs_temp/temp.'..tb[#tb])
  338. self:copy(love.filesystem.getSaveDirectory()..'/lovefs_temp/temp.'..tb[#tb], dest)
  339. return true
  340. end
  341. function lovefs(dir)
  342. local temp = {}
  343. setmetatable(temp, filesystem)
  344. temp.os = love.system.getOS()
  345. temp.param = ''
  346. temp.selectedFile = nil
  347. if temp.os == 'Windows' then
  348. temp.tempdir = io.popen('echo %TEMP%'):read('*all'):gsub('\n', '')
  349. else
  350. temp.tempdir = ''
  351. end
  352. temp.home = love.filesystem.getUserDirectory()
  353. temp.current = temp.home
  354. dir = dir or temp.home
  355. temp:loadCp()
  356. if not temp:cd(dir) then
  357. if not temp:cd(temp.home) then
  358. if temp.os == 'Windows' then temp:cd('c:\\')
  359. else temp:cd('/') end
  360. end
  361. end
  362. temp.drives = temp:lsDrives()
  363. return temp
  364. end