-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.php
More file actions
301 lines (265 loc) · 7.45 KB
/
Copy pathinit.php
File metadata and controls
301 lines (265 loc) · 7.45 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
define('DS', DIRECTORY_SEPARATOR);
define('BIN_DIR', rtrim(dirname(dirname(__DIR__)), '/\\'));
if (DS == '/')
{
if(php_uname('s') == 'Linux'){
define('LUAJIT_BIN', BIN_DIR . '/linux/luajit');
define('LUAJIT64_BIN', BIN_DIR . '/linux/luajit64');
}else{
define('LUAJIT_BIN', BIN_DIR . '/mac/luajit');
define('LUAJIT64_BIN', BIN_DIR . '/mac/luajit64');
}
}
else
{
define('LUAJIT_BIN', BIN_DIR . '\\win32\\luajit.exe');
}
// helper functions
function fetchCommandLineArguments($arg, $options, $minNumArgs = 0)
{
if (!is_array($arg) || !is_array($options))
{
print("ERR: invalid command line arguments");
return false;
}
$config = array();
$newOptions = array();
for ($i = 0; $i < count($options); $i++)
{
$option = $options[$i];
$newOptions[$option[0]] = $option;
$config[$option[1]] = $option[3];
}
$options = $newOptions;
$i = 1;
while ($i < count($arg))
{
$a = $arg[$i];
if ($a{0} != '-')
{
printf("ERR: invalid argument %d: %s", $i, $a);
return false;
}
$a = substr($a, 1);
if (!isset($options[$a]))
{
printf("ERR: invalid argument %d: -%s", $i, $a);
return false;
}
$key = $options[$a][1];
$num = $options[$a][2];
$default = $options[$a][3];
if ($num == 0)
{
$config[$key] = true;
}
else
{
$values = array();
for ($n = 1; $n <= $num; $n++)
{
$values[] = $arg[$i + $n];
}
if (count($values) == 1)
{
$config[$key] = $values[0];
}
else
{
$config[$key] = $values;
}
}
$i = $i + $num + 1;
}
return $config;
}
function convertConfigValueToString($value)
{
if (is_null($value))
{
return null;
}
else if (is_array($value))
{
foreach ($value as $k => $v)
{
$value[$k] = convertConfigValueToString($v);
}
}
else if (is_string($value))
{
return '"' . $value . '"';
}
else
{
return (string)$value;
}
}
function dumpConfig($config, $options)
{
print("config:\n");
for ($i = 0; $i < count($options); $i++)
{
$key = $options[$i][1];
$value = convertConfigValueToString($config[$key]);
if ($value != null)
{
printf(" %s = %s\n", $key, $value);
}
}
print("\n");
}
function findFiles($dir, array & $files)
{
$dir = rtrim($dir, "/\\") . DS;
$dh = opendir($dir);
if ($dh == false) return;
while (($file = readdir($dh)) !== false)
{
if ($file == '.' || $file == '..' || $file == ".DS_Store" || $file == ".svn")
{
continue;
}
$path = $dir . $file;
if (is_dir($path))
{
findFiles($path, $files);
}
elseif (is_file($path))
{
$files[] = $path;
}
}
closedir($dh);
}
function getScriptFileBytecodes($bit, $path, $tmpfile)
{
if (!file_exists($path))
{
printf("ERR: cannot read Lua source file %s\n", $path);
return false;
}
if (file_exists($tmpfile))
{
if (!unlink($tmpfile))
{
printf("ERR: cannot remove tmp file %s\n", $tmpfile);
return false;
}
}
@mkdir(pathinfo($tmpfile, PATHINFO_DIRNAME), 0777, true);
$bin = null;
if ($bit == '32') {
$bin = LUAJIT_BIN;
} else {
$bin = LUAJIT64_BIN;
}
/* -g keep debug info */
$command = sprintf('%s -bg "%s" "%s"', $bin, $path, $tmpfile);
passthru($command);
if (!file_exists($tmpfile))
{
printf("ERR: cannot compile file %s\n", $path);
return false;
}
$contents = file_get_contents($tmpfile);
$contents = cutDebugInfo($contents);
return $contents;
}
/**
* 简化debug 信息中的路径
* @param $contents
* @return $contents
*/
function cutDebugInfo($contents){
//第4和第5个字节 存储debug信息中lua文件路径长度 + "@" 的长度 debug = “@” + path
$debugInfoLenBytes = stringToBytes(substr($contents,4,2));
$debugInfoLen = bytesToShort($debugInfoLenBytes,0,true);
$debugInfo = substr($contents,6,$debugInfoLen);
//printf("debugInfo:%d,%d,(%d) %s\n", $debugInfoLenBytes[0],$debugInfoLenBytes[1],$debugInfoLen,$debugInfo);
$contentsPrefixBytes = stringToBytes(substr($contents,0,5));
$contentsSuffix = substr($contents,5 + $debugInfoLen + 1);
$searchStr = "src".DIRECTORY_SEPARATOR;
//查找是否存在src/路径
$pos = strpos($debugInfo, "src".DIRECTORY_SEPARATOR);
//printf("search str \"%s\" result %d\n",$searchStr,$pos);
if($pos === false){
//不存在
}else{
//存在,则截断src/ 前面的路径字符存
$debugInfo = "@".substr($debugInfo,$pos);
$debugInfoLen = strlen($debugInfo);
$debugInfoLenBytes = shortToBytes(strlen($debugInfo),true);
//printf("debugInfo:%d,%d,(%d) %s\n", $debugInfoLenBytes[0],$debugInfoLenBytes[1],$debugInfoLen,$debugInfo);
//修改debug 信息长度
$contentsPrefixBytes[4] = $debugInfoLenBytes[0];
$contentsPrefixBytes[5] = $debugInfoLenBytes[1];
//拼接完整的数据
$contents = byteToString($contentsPrefixBytes).$debugInfo.$contentsSuffix;
}
return $contents;
}
/**
* 转换一个String字符串为byte数组
* @param $str 需要转换的字符串
* @return $bytes 目标byte数组
*/
function stringToBytes($string) {
$bytes = array();
for($i = 0; $i < strlen($string); $i++){
$bytes[] = ord($string[$i]);
}
return $bytes;
}
/**
* 将字节数组转化为String类型的数据
* @param $bytes 字节数组
* @return 一个String类型的数据
*/
function byteToString($bytes) {
$str = '';
foreach($bytes as $ch) {
$str .= chr($ch);
}
return $str;
}
/**
* 从字节数组中指定的位置读取一个Short类型的数据。
* @param $bytes 字节数组
* @param $position 指定的开始位置
* @param $isBigendian 字节数组是否大端
* @return $val 一个Short类型的数据
*/
function bytesToShort($bytes, $position,$isBigendian) {
$val = 0;
if($isBigendian){
$heigh = ($bytes[$position] & 0xff) << 8;
$low = $bytes[$position + 1] & 0xff;
}else{
$heigh = ($bytes[$position + 1] & 0xff) << 8;
$low = $bytes[$position] & 0xff;
}
$val = $heigh + $low;
//printf("bytesToShort %d + %d = %d\n",$heigh,$low,$val);
return $val;
}
/**
* 转换一个shor字符串为byte数组
* @param $val short 类型
* @return $byt 目标byte数组
*
*/
function shortToBytes($val,$isBigendian) {
$byt = array();
if($isBigendian){
$byt[0] = ($val >> 8 & 0xff);
$byt[1] = ($val & 0xff);
//printf("shortToBytes %d * 256 + %d = %d\n",$byt[0],$byt[1],$val);
}else{
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
//printf("shortToBytes %d + %d * 256 = %d\n",$byt[0],$byt[1],$val);
}
return $byt;
}