terminal_commands.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. local term = require("terminal")
  2. local function tout(...) term:tout(...) end
  3. local CMD = {}
  4. function CMD.help()
  5. tout("COMMANDS <REQUIRED> [OPTIONAL]",13)
  6. --tout()
  7. tout("HELP: SHOW THIS HELP",7)
  8. tout("NEW: CLEARS THE MEMORY",7)
  9. tout("RELOAD: RELOADS THE EDITORSHEET",7)
  10. tout("RUN [args]: RUNS THE CURRENT GAME",7)
  11. tout("SAVE <NAME>: SAVES THE CURRENT GAME",7)
  12. tout("LOAD <NAME>: LOADS A GAME",7)
  13. tout("IMPORT <PATH>: IMPORTS A SPRITESHEET",7)
  14. tout("EXPORT <PATH>: EXPORTS THE SPRITESHEET",7)
  15. tout("CLS: CLEARS THE SCREEN",7)
  16. tout("VER: SHOWS LIKO12'S TITLE",7)
  17. --tout()
  18. tout("PRESS ESC TO OPEN THE EDITOR/EXIT THE GAME",9)
  19. tout("READ LIKO-12.TXT TO GET STARTED !",12)
  20. end
  21. function CMD.cls()
  22. for i=1, term.linesLimit do tout() end term:setLine(1)
  23. end
  24. function CMD.ver() tout() tout("-[[liko12]]-") tout(_LK12VER,_LK12VERC) end
  25. function CMD.run(command,...)
  26. local sm = require("editor.sprite"):export()
  27. local cd = require("editor.code"):export()
  28. local rt = require("runtime")
  29. local sprsheet = api.SpriteSheet(api.ImageData(sm):image(),24,12)
  30. local tilemap = api.MapObj(api.TileMap.w,api.TileMap.h):import(api.ImageData(require("editor.tile"):export()))
  31. local ok, err = rt:loadGame(cd,sprsheet,tilemap,function(err)
  32. _auto_exitgame()
  33. tout("ERR: "..err,9)
  34. tout(term.rootDir.."> ",8,true)
  35. end,...)
  36. if not ok then
  37. _auto_exitgame()
  38. tout("ERR: "..err,9)
  39. tout(term.rootDir.."> ",8,true)
  40. else
  41. _auto_switchgame()
  42. end
  43. end
  44. function CMD.new()
  45. require("editor.sprite"):load()
  46. require("editor.code"):load()
  47. require("editor").lastCart = nil
  48. require("editor").lastSprpng = nil
  49. api.TileMap = api.MapObj()
  50. tout("CLEARED MEMORY",7)
  51. end
  52. function CMD.reload()
  53. api.EditorSheet = api.SpriteSheet(api.Image("/editorsheet.png"),24,12)
  54. api.loadDefaultCursors()
  55. tout("RELOADED EDITORSHEET",7)
  56. end
  57. function CMD.reboot()
  58. pcall(CMD.save,"save","/_backup")
  59. _REBOOT = true
  60. tout("REBOOTING...",7)
  61. end
  62. function CMD.save(command,name)
  63. local name = name
  64. if name then name = (name:sub(0,1) == "/" and name..".lk12" or term.rootDir..name..".lk12") else name = require("editor").lastCart end
  65. if not name then tout("PLEASE PROVIDE A NAME TO SAVE",9) return end
  66. local sm = require("editor.sprite"):export()
  67. local cd = require("editor.code"):export()
  68. local tm = require("editor.tile"):export()
  69. local saveCode = "local code = [["..cd.."]]\n\n"
  70. saveCode = saveCode .. "local spritemap = '"..sm.."'\n\n"
  71. saveCode = saveCode .. "local tilemap = '"..tm.."'\n\n"
  72. saveCode = saveCode .. "return {code=code,spritemap=spritemap,tilemap=tilemap}"
  73. local ok, err = api.fs.write(name,saveCode)
  74. if ok then
  75. tout("SAVED TO "..name,12)
  76. else
  77. tout("ERR: "..err,9)
  78. end
  79. require("editor").lastCart = name
  80. end
  81. function CMD.load(command,name)
  82. if not name then tout("PLEASE PROVIDE A NAME TO LOAD",9) return end
  83. local name = name:sub(0,1) == "/" and name..".lk12" or term.rootDir..name..".lk12"
  84. if not api.fs.exists(name) then tout(name.." DOES NOT EXISTS !",9) return end
  85. local code,err = api.fs.read(name)
  86. if not code then tout("ERR: "..(err or "UNKNOWN"),9) return end
  87. local chunk, err = loadstring(code)
  88. if not chunk then tout("ERR: "..err,9) return end
  89. setfenv(chunk,{})
  90. local ok, data = pcall(chunk)
  91. if not ok then tout("ERR: "..data,9) return end
  92. api.SpriteMap = api.SpriteSheet(api.ImageData(data.spritemap):image(),24,12)
  93. if data.tilemap then
  94. local MapData = api.ImageData(data.tilemap)
  95. api.TileMap = api.MapObj()
  96. api.TileMap:import(MapData)
  97. else
  98. api.TileMap = api.MapObj()
  99. end
  100. require("editor").lastsprpng = nil
  101. require("editor.code"):load(data.code)
  102. tout("LOADED "..name,12)
  103. require("editor").lastCart = name
  104. end
  105. function CMD.export(command,path)
  106. local es = (path == "editorsheet") or false
  107. local path = path or require("editor").lastSprpng
  108. if not path then tout("PLEASE PROVIDE A PATH TO EXPORT TO",9) return end
  109. require("editor.sprite"):export(es and path or "data/"..path)
  110. tout("EXPORTED TO /"..path..".PNG",12)
  111. end
  112. function CMD.import(command,path)
  113. local es = (path == "editorsheet") or false
  114. if not path then tout("PLEASE PROVIDE A PATH TO IMPORT FROM",9) return end
  115. if not love.filesystem.exists(es and path..".png" or "data/"..path..".png") then tout("/"..path..".png DOES NOT EXISTS !") return end
  116. require("editor.sprite"):load(es and path or "data/"..path)
  117. tout("IMPORTED /"..path..".PNG",12)
  118. require("editor").lastSprpng = path
  119. end
  120. function CMD.cd(command,path)
  121. if not path then tout("PLEASE PROVIDE A PATH FIRST",9) return end
  122. local curpath = path:sub(0,1) == "/" and path.."/" or term.rootDir..path.."/"
  123. if path == ".." then curpath = "/" end
  124. if not api.fs.exists(curpath) then tout(curpath.." DOES NOT EXISTS !",9) return end
  125. if not api.fs.isDir(curpath) then tout(path.." IS NOT A DIRECTORY !",9) return end
  126. term.rootDir = curpath
  127. end
  128. function CMD.mkdir(command,path)
  129. if not path then tout("PLEASE PROVIDE A PATH FIRST",9) return end
  130. local curpath = path:sub(0,1) == "/" and path or term.rootDir..path
  131. if api.fs.exists(curpath) then tout(path.." ALREADY EXISTS !",9) return end
  132. local ok, err = api.fs.mkDir(curpath)
  133. if ok then
  134. tout("MAKED DIRECTORY SUCCESSFULLY",12)
  135. else
  136. tout("ERR: "..err,9)
  137. end
  138. end
  139. function CMD.dir(command,path)
  140. local path = path or ""
  141. local curpath = path:sub(0,1) == "/" and path.."/" or term.rootDir..path.."/"
  142. local files = api.fs.dirItems(curpath)
  143. local dirstring = ""
  144. local filestring = ""
  145. for k,v in ipairs(files) do
  146. if api.fs.isDir(curpath..v) then
  147. dirstring = dirstring.." "..v
  148. elseif curpath..v ~= "//_backup.lk12" then
  149. filestring = filestring.." "..v
  150. end
  151. end
  152. if dirstring ~= "" then tout(dirstring,12) end
  153. if filestring ~= "" then tout(filestring,8) end
  154. end
  155. CMD.ls = CMD.dir
  156. function CMD.folder()
  157. love.system.openURL("file://"..love.filesystem.getSaveDirectory().."/data"..term.rootDir)
  158. end
  159. function CMD.shutdown()
  160. pcall(CMD.save,"save","/_backup")
  161. love.event.quit()
  162. end
  163. local function delDir(path)
  164. local files = api.fs.dirItems(path)
  165. for k, file in ipairs(files) do
  166. if api.fs.isFile(path..file) then
  167. local ok, err = api.fs.del(path..file)
  168. if not ok then return ok, err end
  169. else
  170. local ok, err = delDir(path..file.."/")
  171. if not ok then return ok, err end
  172. end
  173. end
  174. local ok, err = api.fs.del(path)
  175. if not ok then return ok, err end
  176. return true
  177. end
  178. function CMD.del(command,path)
  179. if not path then tout("PLEASE PROVIDE A PATH FIRST",9) return end
  180. local curpath = term.rootDir..path
  181. if not api.fs.exists(curpath) then tout(curpath.." DOES NOT EXISTS !",9) return end
  182. local ok, err
  183. if api.fs.isFile(curpath) then
  184. ok, err = api.fs.del(curpath)
  185. else
  186. ok, err = delDir(curpath.."/")
  187. end
  188. if ok then
  189. tout("DELETED "..path.." SUCCESSFULLY",12)
  190. else
  191. tout("ERR["..curpath.."]: "..(err or "UNKNOWN"),9)
  192. end
  193. if not love.filesystem.exists("/data/") then love.filesystem.createDirectory("/data/") end
  194. if not love.filesystem.exists("/data/demos/") then
  195. love.filesystem.createDirectory("/data/demos/")
  196. for k, demo in ipairs(love.filesystem.getDirectoryItems("/demos/")) do
  197. api.fs.write("/demos/"..demo,love.filesystem.read("/demos/"..demo))
  198. end
  199. end
  200. end
  201. return CMD