1 -- 编译一个代码文件
2 -- loadfile (lua_State *L, const char *filename);
3 -- 将一个文件加载为lua代码块,仅编译不执行,返回值为编译后的
4 -- 代码块函数和错误信息。
5 function COMPILE(file)
6 local fun, err = loadfile(file);
7 return fun;
8 end
9
10 -- 编译并运行一个代码文件
11 function LOAD_RUN(file)
12 return require(file);
13 end
14
15 -- 载入一个目录下的所有代码文件(list.lua指明了有哪些文件)
16 function LOAD_PATH(path)
17 -- 取得list.lua的地址
18 local listPath = path .. "/" .. "list";
19
20 -- 加载list,需要加载的所有文件名字
21 local fileNames = LOAD_RUN(listPath);
22
23 -- 进行加载
24 local file = {};
25 for i = 1, #fileNames do
26 local bin = path .. "/" .. fileNames[i];
27 file[fileNames[i]] = LOAD_RUN(bin);
28 end
29 return file;
30 end
31
32 -- 卸载目录下的所有文件
33 function UNLOAD_PATH(path)
34 local listPath = path .. "/" .. "list";
35 local fileNames = LOAD_RUN(listPath);
36 local file = {};
37 for i = 1, #fileNames do
38 -- 设置已经被加载的fileNames[i]为空
39 package.loaded[path .. "/" .. fileNames[i]] = nil;
40 end
41
42 -- 回收内存("collect"做一次完整的垃圾收集循环)
43 collectgarbage("collect");
44 end
45
46 -- 重新载入某个文件
47 function update(file)
48 local pos = string.find(file,"[^%/.]*$");
49 local module_name = string.sub(file, pos);
50 local mod = package.loaded[module_name];
51 if not mod then
52 mod = package.loaded[module_name.."M"];
53 end
54
55 -- 若该文件为模块,且存在 destruct 方法,则先调用析构方法,再重新加载
56 if type(mod) == "table" and type(mod.destruct) == "function" then
57 mod.destruct();
58 end
59
60 package.loaded[file] = false;
61 local ret = require(file);
62
63 mod = package.loaded[module_name];
64 if not mod then
65 mod = package.loaded[module_name.."M"];
66 end
67
68 -- 若该文件为模块,且存在 destruct 方法,则先调用析构方法,再重新加载
69 if type(mod) == "table" and type(mod.init) == "function" then
70 mod.init();
71 end
72
73 -- 回收垃圾
74 -- 无须开启 gc,否则会导致客户端启动速度变慢,见 SLIMEC-7396
75 --collectgarbage("collect");
76 return ret;
77 end
78
79 -- 使用代码更新指定文件
80 function updateByScript(file, script)
81 local pos = string.find(file,"[^%/.]*$");
82 local module_name = string.sub(file, pos);
83 local mod = package.loaded[module_name];
84 if not mod then
85 mod = package.loaded[module_name.."M"];
86 end
87
88 -- 若该文件为模块,且存在 destruct 方法,则先调用析构方法,再重新加载
89 if type(mod) == "table" and type(mod.destruct) == "function" then
90 mod.destruct();
91 end
92 package.loaded[file] = false;
93
94 -- 载入
95 local ret = loadstring(script)();
96
97 -- 若该文件为模块,且存在 destruct 方法,则先调用析构方法,再重新加载
98 mod = package.loaded[module_name];
99 if not mod then
100 mod = package.loaded[module_name.."M"];
101 end
102
103 if type(mod) == "table" and type(mod.init) == "function" then
104 mod.init();
105 end
106
107 -- 回收垃圾
108 --collectgarbage("collect");
109
110 return ret;
111 end
112
113 -- 重新载入一个目录下的所有代码文件(list.lua指明了有哪些文件)
114 function RRLOAD_PATH(path)
115 local listPath = path .. "/" .. "list";
116 local fileNames = LOAD_RUN(listPath);
117 local file = {};
118 for i = 1, #fileNames do
119 package.loaded[path .. "/" .. fileNames[i]] = nil;
120 local bin = update(path .. "/" .. fileNames[i]);
121 file[fileNames[i]] = bin;
122 end
123 return file;
124 end
complier