mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
snlua support multi-path
This commit is contained in:
2
config
2
config
@@ -7,6 +7,6 @@ address = "tcp://127.0.0.1:2525"
|
|||||||
master = "tcp://127.0.0.1:2012"
|
master = "tcp://127.0.0.1:2012"
|
||||||
start = "main"
|
start = "main"
|
||||||
standalone = true
|
standalone = true
|
||||||
luaservice = root.."service/?.lua"
|
luaservice = root.."service/?.lua;"..root.."service/?/init.lua"
|
||||||
cpath = root.."service/?.so"
|
cpath = root.."service/?.so"
|
||||||
protopath = root.."proto"
|
protopath = root.."proto"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
lua_State *
|
lua_State *
|
||||||
snlua_create(void) {
|
snlua_create(void) {
|
||||||
@@ -13,26 +14,67 @@ snlua_create(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_load(lua_State *L, char ** filename) {
|
_try_load(lua_State *L, const char * path, int pathlen, const char * name) {
|
||||||
const char * name = strsep(filename, " \r\n");
|
int namelen = strlen(name);
|
||||||
const char * path = skynet_command(NULL, "GETENV", "luaservice");
|
char tmp[pathlen + namelen];
|
||||||
int namesz = strlen(name);
|
|
||||||
int sz = strlen(path) + namesz;
|
|
||||||
char tmp[sz];
|
|
||||||
int i;
|
int i;
|
||||||
for (i=0;path[i]!='?' && path[i]!='\0';i++) {
|
for (i=0;i<pathlen;i++) {
|
||||||
|
if (path[i] == '?')
|
||||||
|
break;
|
||||||
tmp[i] = path[i];
|
tmp[i] = path[i];
|
||||||
}
|
}
|
||||||
memcpy(tmp+i,name,namesz);
|
|
||||||
if (path[i] == '?') {
|
if (path[i] == '?') {
|
||||||
strcpy(tmp+i+namesz,path+i+1);
|
memcpy(tmp+i,name,namelen);
|
||||||
|
memcpy(tmp+i+namelen,path+i+1,pathlen - i -1);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,"snlua : Invalid lua service path\n");
|
fprintf(stderr,"snlua : Invalid lua service path\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
tmp[namelen+pathlen-1] = '\0';
|
||||||
|
FILE *f = fopen(tmp,"rb");
|
||||||
|
if (f == NULL) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
fclose(f);
|
||||||
int r = luaL_loadfile(L,tmp);
|
int r = luaL_loadfile(L,tmp);
|
||||||
return r != LUA_OK;
|
if (r == LUA_OK) {
|
||||||
|
lua_getglobal(L,"package");
|
||||||
|
lua_getfield(L,-1,"path");
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
luaL_addlstring(&b, path, pathlen);
|
||||||
|
luaL_addchar(&b,';');
|
||||||
|
luaL_addvalue(&b);
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
lua_setfield(L,-2,"path");
|
||||||
|
lua_pop(L,1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_load(lua_State *L, char ** filename) {
|
||||||
|
const char * name = strsep(filename, " \r\n");
|
||||||
|
const char * path = skynet_command(NULL, "GETENV", "luaservice");
|
||||||
|
while (path[0]) {
|
||||||
|
int pathlen;
|
||||||
|
char * pathend = strchr(path,';');
|
||||||
|
if (pathend) {
|
||||||
|
pathlen = pathend - path;
|
||||||
|
} else {
|
||||||
|
pathlen = strlen(path);
|
||||||
|
}
|
||||||
|
int r = _try_load(L, path, pathlen, name);
|
||||||
|
if (r >=0) {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
path+=pathlen;
|
||||||
|
if (path[0]==';')
|
||||||
|
++path;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@@ -63,8 +105,12 @@ snlua_init(lua_State *L, struct skynet_context *ctx, const char * args) {
|
|||||||
|
|
||||||
const char * filename = parm;
|
const char * filename = parm;
|
||||||
int r = _load(L, &parm);
|
int r = _load(L, &parm);
|
||||||
if (r) {
|
if (r != 0) {
|
||||||
|
if (r<0) {
|
||||||
|
skynet_error(ctx, "lua parser [%s] load error", filename);
|
||||||
|
} else {
|
||||||
skynet_error(ctx, "lua parser [%s] error : %s", filename, lua_tostring(L,-1));
|
skynet_error(ctx, "lua parser [%s] error : %s", filename, lua_tostring(L,-1));
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int n=0;
|
int n=0;
|
||||||
|
|||||||
Reference in New Issue
Block a user