terminal_commands.lua 7.1 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. else
  40. _auto_switchgame()
  41. end
  42. end
  43. function CMD.new()
  44. require("editor.sprite"):load()
  45. require("editor.code"):load()
  46. require("editor").lastCart = nil
  47. require("editor").lastSprpng = nil
  48. api.TileMap = api.MapObj()
  49. tout("CLEARED MEMORY",7)
  50. end
  51. function CMD.reload()
  52. api.EditorSheet = api.SpriteSheet(api.Image("/editorsheet.png"),24,12)
  53. api.loadDefaultCursors()
  54. tout("RELOADED EDITORSHEET",7)
  55. end
  56. function CMD.reboot()
  57. pcall(CMD.save,"save","/_backup")
  58. _REBOOT = true
  59. tout("REBOOTING...",7)
  60. end
  61. function CMD.save(command,name)
  62. local name = name
  63. if name then name = (name:sub(0,1) == "/" and name..".lk12" or term.rootDir..name..".lk12") else name = require("editor").lastCart end
  64. if not name then tout("PLEASE PROVIDE A NAME TO SAVE",9) return end
  65. local sm = require("editor.sprite"):export()
  66. local cd = require("editor.code"):export()
  67. local tm = require("editor.tile"):export()
  68. local saveCode = "local code = [["..cd.."]]\n\n"
  69. saveCode = saveCode .. "local spritemap = '"..sm.."'\n\n"
  70. saveCode = saveCode .. "local tilemap = '"..tm.."'\n\n"
  71. saveCode = saveCode .. "return {code=code,spritemap=spritemap,tilemap=tilemap}"
  72. local ok, err = api.fs.write(name,saveCode)
  73. if ok then
  74. tout("SAVED TO "..name,12)
  75. else
  76. tout("ERR: "..err,9)
  77. end
  78. require("editor").lastCart = name
  79. end
  80. function CMD.load(command,name)
  81. if not name then tout("PLEASE PROVIDE A NAME TO LOAD",9) return end
  82. local name = name:sub(0,1) == "/" and name..".lk12" or term.rootDir..name..".lk12"
  83. if not api.fs.exists(name) then tout(name.." DOES NOT EXISTS !",9) return end
  84. local code,err = api.fs.read(name)
  85. if not code then tout("ERR: "..(err or "UNKNOWN"),9) return end
  86. local chunk, err = loadstring(code)
  87. if not chunk then tout("ERR: "..err,9) return end
  88. setfenv(chunk,{})
  89. local ok, data = pcall(chunk)
  90. if not ok then tout("ERR: "..data,9) return end
  91. api.SpriteMap = api.SpriteSheet(api.ImageData(data.spritemap):image(),24,12)
  92. if data.tilemap then
  93. local MapData = api.ImageData(data.tilemap)
  94. api.TileMap = api.MapObj()
  95. api.TileMap:import(MapData)
  96. else
  97. api.TileMap = api.MapObj()
  98. end
  99. require("editor").lastsprpng = nil
  100. require("editor.code"):load(data.code)
  101. tout("LOADED "..name,12)
  102. require("editor").lastCart = name
  103. end
  104. function CMD.export(command,path)
  105. local es = (path == "editorsheet") or false
  106. local path = path or require("editor").lastSprpng
  107. if not path then tout("PLEASE PROVIDE A PATH TO EXPORT TO",9) return end
  108. require("editor.sprite"):export(es and path or "data/"..path)
  109. tout("EXPORTED TO /"..path..".PNG",12)
  110. end
  111. function CMD.import(command,path)
  112. local es = (path == "editorsheet") or false
  113. if not path then tout("PLEASE PROVIDE A PATH TO IMPORT FROM",9) return end
  114. if not love.filesystem.exists(es and path..".png" or "data/"..path..".png") then tout("/"..path..".png DOES NOT EXISTS !") return end
  115. require("editor.sprite"):load(es and path or "data/"..path)
  116. tout("IMPORTED /"..path..".PNG",12)
  117. require("editor").lastSprpng = path
  118. end
  119. function CMD.cd(command,path)
  120. if not path then tout("PLEASE PROVIDE A PATH FIRST",9) return end
  121. local curpath = path:sub(0,1) == "/" and path.."/" or term.rootDir..path.."/"
  122. if path == ".." then curpath = "/" end
  123. if not api.fs.exists(curpath) then tout(curpath.." DOES NOT EXISTS !",9) return end
  124. if not api.fs.isDir(curpath) then tout(path.." IS NOT A DIRECTORY !",9) return end
  125. term.rootDir = curpath
  126. end
  127. function CMD.mkdir(command,path)
  128. if not path then tout("PLEASE PROVIDE A PATH FIRST",9) return end
  129. local curpath = path:sub(0,1) == "/" and path or term.rootDir..path
  130. if api.fs.exists(curpath) then tout(path.." ALREADY EXISTS !",9) return end
  131. local ok, err = api.fs.mkDir(curpath)
  132. if ok then
  133. tout("MAKED DIRECTORY SUCCESSFULLY",12)
  134. else
  135. tout("ERR: "..err,9)
  136. end
  137. end
  138. function CMD.dir(command,path)
  139. local path = path or ""
  140. local curpath = path:sub(0,1) == "/" and path.."/" or term.rootDir..path.."/"
  141. local files = api.fs.dirItems(curpath)
  142. local dirstring = ""
  143. local filestring = ""
  144. for k,v in ipairs(files) do
  145. if api.fs.isDir(curpath..v) then
  146. dirstring = dirstring.." "..v
  147. elseif curpath..v ~= "//_backup.lk12" then
  148. filestring = filestring.." "..v
  149. end
  150. end
  151. if dirstring ~= "" then tout(dirstring,12) end
  152. if filestring ~= "" then tout(filestring,8) end
  153. end
  154. CMD.ls = CMD.dir
  155. function CMD.folder()
  156. love.system.openURL("file://"..love.filesystem.getSaveDirectory().."/data"..term.rootDir)
  157. end
  158. function CMD.shutdown()
  159. pcall(CMD.save,"save","/_backup")
  160. love.event.quit()
  161. end
  162. local function delDir(path)
  163. local files = api.fs.dirItems(path)
  164. for k, file in ipairs(files) do
  165. if api.fs.isFile(path..file) then
  166. local ok, err = api.fs.del(path..file)
  167. if not ok then return ok, err end
  168. else
  169. local ok, err = delDir(path..file.."/")
  170. if not ok then return ok, err end
  171. end
  172. end
  173. local ok, err = api.fs.del(path)
  174. if not ok then return ok, err end
  175. return true
  176. end
  177. function CMD.del(command,path)
  178. if not path then tout("PLEASE PROVIDE A PATH FIRST",9) return end
  179. local curpath = term.rootDir..path
  180. if not api.fs.exists(curpath) then tout(curpath.." DOES NOT EXISTS !",9) return end
  181. local ok, err
  182. if api.fs.isFile(curpath) then
  183. ok, err = api.fs.del(curpath)
  184. else
  185. ok, err = delDir(curpath.."/")
  186. end
  187. if ok then
  188. tout("DELETED "..path.." SUCCESSFULLY",12)
  189. else
  190. tout("ERR["..curpath.."]: "..(err or "UNKNOWN"),9)
  191. end
  192. if not love.filesystem.exists("/data/") then love.filesystem.createDirectory("/data/") end
  193. if not love.filesystem.exists("/data/demos/") then
  194. love.filesystem.createDirectory("/data/demos/")
  195. for k, demo in ipairs(love.filesystem.getDirectoryItems("/demos/")) do
  196. api.fs.write("/demos/"..demo,love.filesystem.read("/demos/"..demo))
  197. end
  198. end
  199. end
  200. return CMD