Skip to content

Commit fd19f26

Browse files
committed
support zip game
1 parent 4babad2 commit fd19f26

File tree

4 files changed

+95
-14
lines changed

4 files changed

+95
-14
lines changed

src/lualib/icon.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local datalist = require "soluna.datalist"
33
local file = require "soluna.file"
44
local mattext = require "soluna.material.text"
55

6-
global error, tostring
6+
global error, tostring, print
77

88
local icon = {}
99

@@ -16,7 +16,7 @@ function icon.bundle(filename)
1616
for i = 1, n do
1717
local icon = b[i]
1818
names[icon.name] = i - 1
19-
local src = file.load(path .. "/" .. icon.image) or error "Open icon fail : " .. tostring(icon.name)
19+
local src = file.load(path .. icon.image) or error ("Open icon fail : " .. tostring(icon.name))
2020
local img = sdf.load(src)
2121
icons[i] = img
2222
end

src/lualib/main.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ global load, require, assert, select, error, tostring, print
66
local init_func_temp = [=[
77
local name, service_path = ...
88
local embedsource = require "soluna.embedsource"
9+
local file = require "soluna.file"
910
package.path = [[${lua_path}]]
1011
package.cpath = [[${lua_cpath}]]
12+
local zipfile = [[${zipfile}]]
13+
if zipfile ~= "" then
14+
local zip = require "soluna.zip"
15+
zip.zipfile = zip.open(zipfile, "r")
16+
end
1117
_G.print_r = load(embedsource.runtime.print_r(), "@src/lualib/print_r.lua")()
1218
local packageloader = load(embedsource.runtime.packageloader(), "@src/lualib/packageloader.lua")
1319
packageloader()
@@ -35,11 +41,11 @@ local init_func_temp = [=[
3541
if embedcode then
3642
return load(embedcode(),"=("..name..")")
3743
end
38-
local filename, err = package.searchpath(name, service_path or "${service_path}")
44+
local filename, err = file.searchpath(name, service_path or "${service_path}")
3945
if not filename then
4046
return nil, err
4147
end
42-
return loadfile(filename)
48+
return load(file.load(filename), "@"..filename)
4349
]=]
4450

4551
local function start(config)
@@ -57,6 +63,7 @@ local function start(config)
5763
lua_path = package.path,
5864
lua_cpath = package.cpath,
5965
service_path = config.service_path or "",
66+
zipfile = config.args.zipfile or "",
6067
}),
6168
}
6269

@@ -180,6 +187,15 @@ end
180187

181188
function api.init(desc)
182189
-- todo : settings
190+
local zipfile = args[1] or args.zipfile or "main.zip"
191+
local zip = require "soluna.zip"
192+
local f = zip.open(zipfile, "r")
193+
if f then
194+
zip.zipfile = f
195+
args.zipfile = zipfile
196+
else
197+
args.zipfile = nil
198+
end
183199
local embedsource = require "soluna.embedsource"
184200
local packageloader = load(embedsource.runtime.packageloader(), "@src/lualib/packageloader.lua")
185201
packageloader()

src/lualib/packageloader.lua

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
local file = require "soluna.file"
2+
local zip = require "soluna.zip"
3+
local lfs = require "soluna.lfs"
24

35
local package = package
46
local string = string
@@ -9,8 +11,79 @@ global load, print
911
local dir_sep, temp_sep, temp_marker = package.config:match "(.)\n(.)\n(.)"
1012
local temp_pat = "[^"..temp_sep.."]+"
1113

14+
local zipfile = zip.zipfile
15+
local file_load = file.load
16+
local file_exist = file.exist
17+
18+
if zipfile then
19+
function file_load(fullname)
20+
local name = fullname:match "%./(.*)" or fullname
21+
return zipfile:readfile(name)
22+
end
23+
function file_exist(fullname)
24+
local name = fullname:match "%./(.*)" or fullname
25+
return zipfile:exist(name)
26+
end
27+
file.local_load = file.load
28+
file.local_exist = file.exist
29+
file.load = file_load
30+
file.exist = file_exist
31+
local list
32+
function file.dir(root)
33+
list = list or zipfile:list()
34+
root = root:gsub("[^/]$", "%0/")
35+
local iter = 1
36+
local n = #list
37+
local root_n = #root
38+
local last
39+
return function()
40+
while iter <= n do
41+
local t = list[iter]
42+
iter = iter + 1
43+
if t:sub(1, root_n) == root then
44+
local sname = t:sub(root_n+1):match "[^/]+"
45+
if sname ~= last then
46+
last = sname
47+
return sname
48+
end
49+
end
50+
end
51+
end
52+
end
53+
function file.attributes(fullname)
54+
list = list or zipfile:list()
55+
local pathname = fullname .. "/"
56+
local pathn = #pathname
57+
for i = 1, #list do
58+
local t = list[i]
59+
if fullname == t then
60+
return "file"
61+
elseif t:sub(1, pathn) == pathname then
62+
return "directory"
63+
end
64+
end
65+
end
66+
function file.searchpath(name, path)
67+
local cname = name:gsub("%.", "/")
68+
for temp in path:gmatch(temp_pat) do
69+
local fullname = temp:gsub(temp_marker, cname)
70+
if dir_sep ~= '/' then
71+
fullname = fullname:gsub(dir_sep, "/")
72+
end
73+
if file_exist(fullname) then
74+
return fullname
75+
end
76+
end
77+
end
78+
else
79+
file.dir = lfs.dir
80+
file.attributes = lfs.attributes
81+
file.local_load = file.load
82+
file.local_exist = file.exist
83+
end
84+
1285
local function fileload(name, fullname)
13-
local s, err = file.load(fullname)
86+
local s, err = file_load(fullname)
1487
local f = load(s, "@"..fullname)
1588
return f(name, fullname)
1689
end
@@ -22,7 +95,7 @@ local function search_file(name)
2295
if dir_sep ~= '/' then
2396
fullname = fullname:gsub(dir_sep, "/")
2497
end
25-
if file.exist(fullname) then
98+
if file_exist(fullname) then
2699
return fileload, fullname
27100
end
28101
end

src/luazip.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ lcompress(lua_State *L) {
4242
// 2...n : (1<<n)x size
4343
buf[0] = idx;
4444
lua_pushexternalstring(L, buf, len+1, external_free, NULL);
45-
// lua_pushlstring(L, buf, len + 1);
46-
// free(buf);
4745
return 1;
4846
}
4947

@@ -59,8 +57,6 @@ luncompress(lua_State *L) {
5957
int r = uncompress(buf, &dsz, (void *)(src + 1), sz - 1);
6058
if (r == Z_OK) {
6159
lua_pushexternalstring(L, buf, dsz, external_free, NULL);
62-
// lua_pushlstring(L, buf, dsz);
63-
// free(buf);
6460
return 1;
6561
}
6662
free(buf);
@@ -444,8 +440,6 @@ zipread_readfile(lua_State *L) {
444440
}
445441

446442
lua_pushexternalstring(L, buf, bytes, external_free, NULL);
447-
// lua_pushlstring(L, buf, bytes);
448-
// free(buf);
449443
close_file(L, zf);
450444
return 1;
451445
}
@@ -525,8 +519,6 @@ zipread_read(lua_State *L) {
525519
luaL_error(L, "Error: read file");
526520
}
527521
lua_pushexternalstring(L, buf, bytes, external_free, NULL);
528-
// lua_pushlstring(L, (const char *)buf, bytes);
529-
// free(buf);
530522
return 1;
531523
}
532524

0 commit comments

Comments
 (0)