This repository has been archived by the owner on Sep 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
createupdates.lua
executable file
·151 lines (128 loc) · 3.8 KB
/
createupdates.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env lua
--[[
This tool generates update archives and a matching resources.xml file
based on the world data repository.
It expects 'git' and 'adler32' to be available in the path.
Configuration happens through the following environment variables:
WORLD_DATA_REPOSITORY (example: /home/user/world/.git)
CLIENT_UPDATES_DIR (example: /home/user/public_html/updates)
--]]
local function checkenv(varname)
local value = os.getenv(varname)
if not value then
print(varname .. ' not set')
os.exit(1)
end
return value
end
local WORLD_DATA_REPOSITORY = checkenv('WORLD_DATA_REPOSITORY')
local CLIENT_UPDATES_DIR = checkenv('CLIENT_UPDATES_DIR')
local function trim(s)
s = string.gsub(s, '^%s+', '') -- strip preceding whitespace
s = string.gsub(s, '%s+$', '') -- strip trailing whitespace
return s
end
local function capture(command)
local f = assert(io.popen(command, 'r'))
local s = assert(f:read('*a'))
f:close()
return trim(s)
end
local function execute(command)
local result = assert(os.execute(command))
if result ~= 0 then
print("Error executing:")
print(" " .. command)
os.exit(1)
end
end
local function git(subcommand)
return 'git --git-dir=' .. WORLD_DATA_REPOSITORY .. ' ' .. subcommand
end
local function adler32(file)
return string.sub(capture('adler32 ' .. file), -8)
end
local function last_revision(paths)
local output = capture(git('log -1 --oneline -- ' .. paths))
return assert(string.match(output, '(%w+) '))
end
local function exists(filename)
local file = io.open(filename, "r")
if file then
io.close(file)
return true
end
return false
end
local packages = {
{
name = "definitions",
paths = {
"attributes.xml",
"effects.xml",
"emotes.xml",
"equip.xml",
"hair.xml",
"items.xml",
"maps.xml",
"monsters.xml",
"npcs.xml",
"paths.xml",
"permissions.xml",
"skills.xml",
"specials.xml",
"status-effects.xml",
"units.xml",
},
},
{ name = "music", type = "music", required = "no", paths = { "music" }, },
{ name = "sound", paths = { "sfx" }, },
{ name = "maps", paths = { "maps" }, },
{
name = "graphics",
paths = {
"automapping",
"icons",
"items",
"minimaps",
"particles",
"sprites",
"tiles",
},
},
}
local resources_lines = {
'<?xml version="1.0"?>',
'<updates>',
}
for i=1,#packages do
local package = packages[i]
local paths = table.concat(package.paths, ' ')
local revision = last_revision(paths)
local filename = package.name .. "-" .. revision .. ".zip"
local fullname = CLIENT_UPDATES_DIR .. '/' .. filename
if exists(fullname) then
print("Skipping " .. filename .. " (already exists)")
else
print("Creating " .. filename)
execute(git('archive HEAD --output=' .. fullname .. ' ' .. paths))
end
local type = package.type or "data"
local hash = adler32(fullname)
local line = ' <update type="' .. type .. '"'
if package.required == "no" then
line = line .. ' required="no"'
end
line = line .. ' file="' .. filename .. '"'
line = line .. ' hash="' .. hash .. '" '
if package.description then
line = line .. ' description="' .. package.description .. '"'
end
line = line .. '/>'
table.insert(resources_lines, line)
end
table.insert(resources_lines, '</updates>')
print("Writing resources.xml")
local file = io.open(CLIENT_UPDATES_DIR .. "/resources.xml", "w")
file:write(table.concat(resources_lines, '\n') .. '\n')
file:close()