Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua Love2D - How can I make it download a file?

Title. It's only allowed to save to a certain directory, but is there a way of making an executable made with it update itself? I've got the code to check if it's out of date (HttpGet), but not sure how to intall the newer update.

Main reason is people are complaining about having to repeatedly redownload my RPG. Would it be easier to package it with a C# auto-Updater they can run?

like image 892
Ashley Davies Avatar asked Dec 27 '22 23:12

Ashley Davies


1 Answers

You can not make the .love file "update itself". That is not possible, unless you use the operative system's package manager or something similar (apt-get in Ubuntu, the app store on mac, and whatever microsoft uses, if it has it).

If you don't want to do that, then the second best way to make that work would be making your love2d executable a "shell"; an "empty game" that simply downloads stuff from the internet, and later on it executes some function that initializes everything, including the proper game.

As jpjacobs says, you can download stuff from the web using LuaSocket (which is already included in LÖVE 0.7). For example, this is how you download a png (I've copied the code from here):

if not love.filesystem.exists("expo.png") then
  local http = require("socket.http")
  local b, c, h = http.request("http://love2d.org/w/images/e/e3/Resource-AnalExplosion.png")
  love.filesystem.write("expo.png", b)
end

There's also a way to uncompress data using the GNU unzip algorithm using pure lua. It's implemented by the /AdvTiledLoader/external/gunzip.lua file in Kadoba's Advanced TileLoader.

So I guess you can make a game that:

  1. Starts by reading a file called version.lua, and comparing it to a file in your server (http://www.your-server.com/latest-version-number). That file simply contains a number, like "48".
  2. If no file and server could not be contacted, then error "could not download game".
  3. If no file, or current version < latest version from the server, download zip file from server (http://www.your-server.com/latest.zip)
  4. If latest.zip downloaded successfully, erase everything inside the /latest directory and uncompress latest.zip on the new latest. Update version.lua with the new version (return 48)
  5. Detect when you are working offline - If could not download latest.zip or version, but there's already a version.lua and latest folder, give just a warning, not an error.
  6. Require the file that contains the real game; probably something like require 'latest.main'

Notes:

  • I'm not familiar with luasocket. It is possible that there's a way to get the 'last updated' date from http://www.your-server.com/latest.zip, so you can simply get rid of the latest-version-number stuff.
  • I haven't used gunzip.lua myself. I don't know how it handles multiple files, or directories.
like image 104
kikito Avatar answered Jan 06 '23 08:01

kikito