-
Notifications
You must be signed in to change notification settings - Fork 0
/
vjassImport.lua
176 lines (121 loc) · 2.93 KB
/
vjassImport.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require 'waterlua'
local params = {...}
local map = params[1]
local lookupPaths = params[2]
assert(map, 'no map')
map = io.toAbsPath(map)
if (lookupPaths ~= nil) then
lookupPaths = lookupPaths:split(';')
end
lookupPaths = lookupPaths or {}
for i = 1, #lookupPaths, 1 do
local path = lookupPaths[i]
path = path:gsub('/', '\\')
if not path:match('\\$') then
path = path..'\\'
end
lookupPaths[i] = path
end
local inputDir = io.local_dir()..[[Input\]]
local outputDir = io.local_dir()..[[Output\]]
removeDir(inputDir)
removeDir(outputDir)
assert(map, 'no map')
osLib.clearScreen()
require 'portLib'
flushDir(inputDir)
createDir(inputDir)
mpqExtract(map, [[war3map.j]], inputDir)
flushDir(outputDir)
createDir(outputDir)
local fIn = io.open(inputDir..[[war3map.j]], 'r')
local fOut = io.open(outputDir..[[war3map.j]], 'w+')
local function splitargs(line)
local res = {}
while (line:len() > 0) do
local pos, posEnd = line:find('[^%s]')
if (pos == nil) then
line = ""
else
line = line:sub(pos)
local arg = nil
if (line:sub(1, 1) == "\"") then
line = line:sub(2)
local pos, posEnd = line:find("\"")
if (pos == nil) then
pos = line:len() + 1
end
arg = line:sub(1, pos - 1)
if (posEnd == nil) then
line = ""
else
line = line:sub(posEnd + 1)
end
else
local pos, posEnd = line:find('%s')
if (pos == nil) then
pos = line:len() + 1
end
arg = line:sub(1, pos - 1)
if (posEnd == nil) then
line = ""
else
line = line:sub(posEnd + 1)
end
end
if (arg ~= nil) then
res[#res + 1] = arg
end
end
end
return res
end
local function searchLine(line)
local sear = '^%s*//! import%s+([%w%p_]*)'
local name = line:match(sear)
if (name ~= nil) then
--if ((name:sub(1, 1) == "\"") and (name:sub(name:len(), name:len()) == "\"")) then
-- name = name:sub(2, name:len() - 1)
--end
name = splitargs(name)[1]
local tryTable = {}
tryTable[#tryTable + 1] = name
if not io.isAbsPath(name) then
for i = 1, #lookupPaths, 1 do
tryTable[#tryTable + 1] = lookupPaths[i]..name
i = i + 1
end
end
local fImp = io.open(tryTable[1], 'r')
local i = 2
while ((fImp == nil) and (i <= #tryTable)) do
fImp = io.open(tryTable[i], 'r')
i = i + 1
end
if (fImp == nil) then
error('cannot open '..tostring(name)..' tried:\n'..table.concat(tryTable, '\n'))
end
if (fImp ~= nil) then
fOut:write('//import start: ', name, '\n')
for impLine in fImp:lines() do
--fOut:write(impLine, '\n')
searchLine(impLine)
end
fOut:write('//import end: ', name, '\n')
fImp:close()
else
fOut:write('//import not found: ', name, '\n')
error('import ', name, ' not found')
end
else
fOut:write(line, '\n')
end
end
for line in fIn:lines() do
searchLine(line)
end
fIn:close()
fOut:close()
local impPort = createMpqPort()
impPort:addImport(outputDir..[[war3map.j]], [[war3map.j]])
impPort:commit(map)