Skip to content
Snippets Groups Projects
Commit 1b2653d2 authored by dg's avatar dg
Browse files

start a module downloader

git-svn-id: http://svn.net-core.org/repos/t-engine4@423 51575b47-30f0-44d4-a5cc-537603b46e54
parent 621078ec
No related branches found
No related tags found
No related merge requests found
require "engine.class"
require "engine.Dialog"
module(..., package.seeall, class.inherit(engine.Dialog))
function _M:init(title, url, on_finish)
self.url = url
self.received = 0
self.on_finish = on_finish
engine.Dialog.init(self, title or "Download", 400, 100)
self:keyCommands({
},{
})
end
function _M:drawDialog(s)
if self.th then
local t = self.linda:receive(0, "final")
-- print("done ?", t)
local len = self.linda:receive(0, "received")
print("got", len)
if len then self.received = self.received + len end
local v, err = self.th:join(0)
if err then error(err) end
if t then
print("done !", #t, table.concat(t))
self.changed = false
self.linda = nil
self.th = nil
end
end
s:drawString(self.font, "Received: "..self.received, 2, 2, 255, 255, 255)
end
function _M:startDownload()
local l = lanes.linda()
function list_handler(src)
require "socketed"
local http = require "socket.http"
local ltn12 = require "ltn12"
local t = {}
http.request{url = src, sink = function(chunk, err)
if chunk then
l:send(0, "received", #chunk)
table.insert(t, chunk)
end
return 1
end}
l:send("final", t)
end
self.th = lanes.gen("*", list_handler)(self.url)
self.linda = l
end
require "engine.class"
require "engine.Game"
require "engine.Module"
require "engine.KeyBind"
local Module = require "engine.Module"
local Savefile = require "engine.Savefile"
local Dialog = require "engine.Dialog"
local ButtonList = require "engine.ButtonList"
local DownloadDialog = require "engine.dialogs.DownloadDialog"
module(..., package.seeall, class.inherit(engine.Game))
......@@ -15,8 +16,8 @@ function _M:init()
end
function _M:run()
self.mod_list = engine.Module:listModules()
self.save_list = engine.Module:listSavefiles()
self.mod_list = Module:listModules()
self.save_list = Module:listSavefiles()
-- Setup display
self:selectStepMain()
......@@ -56,7 +57,7 @@ function _M:commandLineArgs(args)
end
if req_mod then
local mod = engine.Module:loadDefinition("/modules/"..req_mod)
local mod = Module:loadDefinition("/modules/"..req_mod)
if mod then
local M = mod.load()
_G.game = M.new()
......@@ -98,6 +99,12 @@ function _M:selectStepMain()
self:selectStepLoad()
end,
},
{
name = "Install a game module",
fct = function()
self:selectStepInstall()
end,
},
{
name = "Exit",
fct = function()
......@@ -183,9 +190,45 @@ function _M:selectStepLoad()
end
self:registerDialog(display_module)
self.step = ButtonList.new(list, 10, 10, self.w * 0.24, (5 + 35) * #self.mod_list, nil, 5)
self.step = ButtonList.new(list, 10, 10, self.w * 0.24, (5 + mod_font:lineSkip()) * #self.mod_list, nil, 5)
self.step:select(2)
self.step:setKeyHandling()
self.step:setMouseHandling()
self.step.key:addBind("EXIT", function() self:unregisterDialog(display_module) self:selectStepMain() end)
end
function _M:selectStepInstall()
local linda, th = Module:loadRemoteList()
local dllist = linda:receive("moduleslist")
th:join()
local display_module = Dialog.new("", self.w * 0.73, self.h, self.w * 0.26, 0, 255)
for i, mod in ipairs(dllist) do
mod.fct = function()
local d = DownloadDialog.new("Downloading: "..mod.long_name, mod.download, function(self)
print("Got ", #self.data, self.data)
end)
self:registerDialog(d)
d:startDownload()
end
mod.onSelect = function()
display_module.title = mod.long_name
display_module.changed = true
end
end
display_module.drawDialog = function(self, s)
local lines = dllist[game.step.selected].description:splitLines(self.w - 8, self.font)
local r, g, b
for i = 1, #lines do
r, g, b = s:drawColorString(self.font, lines[i], 0, i * self.font:lineSkip(), r, g, b)
end
end
self:registerDialog(display_module)
self.step = ButtonList.new(dllist, 10, 10, self.w * 0.24, (5 + 35) * #dllist, nil, 5)
self.step:setKeyHandling()
self.step:setMouseHandling()
self.step.key:addBind("EXIT", function() self:unregisterDialog(display_module) self:selectStepMain() end)
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment