예제 #1
0
void KitsuneSwagConsole::LoadKSCLuaLib(){

	lua_sethook(L, luadiehook, LUA_MASKCOUNT, 10000);
	lua_register(L, "print", lua_putsmyprint);

	RegFunc( "Clear", lua_cls);
	RegFunc( "Exit", lua_die);
	RegFunc( "Close", lua_closesocket);
	RegFunc( "GetAll", lua_getall); 
	RegFunc( "GetInfo", lua_getinfo);
	RegFunc("Client", lua_connectclient);
	RegFunc("Server", lua_connectserver);
	RegFunc("SetVisible", lua_SetVisible);
	RegFunc("GetVisible", lua_GetVisible);
	RegFunc("Focus", lua_Focus);

	struct luaL_reg driver[] = {
		{ "Send", lua_send },
		{ "Close", lua_closesocket },
		{ "AsInt", lua_stoi },
		{ NULL, NULL },
	};

	struct luaL_reg ls_meta[] = {
		{ "__gc", ls__gc },
		{ "__tostring", ls__tostring },
		{ "__eq", ls__eq },
		{ "__lt", ls__lt },
		{ "__le", ls__le },
		//{ "__add", Foo_add },
		{ 0, 0 }
	};

	luaL_openlib(L, PROJECT_TABLENAME, driver, 0);

	luaL_newmetatable(L, PROJECT_TABLENAME);
	luaL_openlib(L, 0, ls_meta, 0);
	lua_pushliteral(L, "__index");
	lua_pushvalue(L, -3);
	lua_rawset(L, -3);
	lua_pushliteral(L, "__metatable");
	lua_pushvalue(L, -3);
	lua_rawset(L, -3);

	lua_setglobal(ASWN->L, PROJECT_TABLENAME);
}
예제 #2
0
void LuaEngine::Init(const char* main_script_file)
{
    Fini();
    int ret = 0;

    // TODO: 定时器个数采用宏配置
    heap_timer_ = new HeapTimer(1024);
    CHECK(heap_timer_ != NULL)
        << "timer heap init error!";

    master_state_ = luaL_newstate();
    CHECK(master_state_ != NULL)
        << "luaL_newstate error!";

    luaL_openlibs(master_state_);
    lua_tinker::init(master_state_);
    lua_tinker::init_s64(master_state_);
    lua_tinker::init_u64(master_state_);

    // 将script_main.lua所在文件夹路径加入require路径搜索
    char script_path[256] = {0};
    realpath(main_script_file, script_path);
    strncpy(script_path, dirname(script_path), 256);

    AddRequirePath(script_path);
    AddRequireCPath(script_path);

    // package.path 处理
    std::ostringstream oss_require;
    oss_require
        << "package.path = \"" << oss_require_path_.str() << "\"";

    ret = luaL_dostring(master_state_, oss_require.str().c_str());
    LOG(INFO) << "luaL_dostring [" << oss_require.str().c_str() << "]";
    CHECK(ret == 0)
        << "luaL_dostring [" << oss_require.str().c_str()
        << "] error!";

    oss_require.str("");
    oss_require_path_.str("");

    // package.cpath 处理
    oss_require
        << "package.cpath = \"" << oss_require_cpath_.str() << "\"";

    ret = luaL_dostring(master_state_, oss_require.str().c_str());
    LOG(INFO) << "luaL_dostring [" << oss_require.str().c_str() << "]";
    CHECK(ret == 0)
        << "luaL_dostring [" << oss_require.str().c_str()
        << "] error!";

    oss_require.str("");
    oss_require_cpath_.str("");

    // 设置LUA_SCRIPT_PATH
    SetGlobal("LUA_SCRIPT_PATH", script_path);

    // 注册log函数
    RegFunc("C_LOG_INFO", &LuaEngine::LogInfo);
    RegFunc("C_LOG_ERROR", &LuaEngine::LogError);
    // lua_tinker中日志函数
    RegFunc("_ALERT", &LuaEngine::LogInfo);

    ret = luaL_dofile(master_state_, main_script_file);
    if (ret != 0) {
        LOG(ERROR) << lua_tostring(master_state_, -1);
    }
    CHECK(ret == 0)
        << "luaL_dofile error!";

    // 第一次加载模块
    Reload();

    // 设置检查信号宏
    SetGlobal<int>("CHECK_SIG_OK", CHECK_SIG_OK);
    SetGlobal<int>("CHECK_SIG_TIMEOUT", CHECK_SIG_TIMEOUT);

    // 设置定时器
    SetHandler(this, &LuaEngine::OnTimer);
}
예제 #3
0
void CSJsUi::Register(CefRefPtr<CefV8Value> windowObject)
{
	CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(NULL, NULL);
	
    RegFunc(obj, "startMove");
    RegFunc(obj, "stopMove");
    RegFunc(obj, "startResize");
    RegFunc(obj, "stopResize");
    RegFunc(obj, "show");
    RegFunc(obj, "close");
    RegFunc(obj, "setSizeHints");
    RegFunc(obj, "setSize");
    RegFunc(obj, "move");
    RegFunc(obj, "screenSize");
    RegFunc(obj, "createWindow");
    RegFunc(obj, "menu");
    RegFunc(obj, "mainMenu");
    
	// bind window.ui object
	windowObject->SetValue("ui", obj, V8_PROPERTY_ATTRIBUTE_READONLY);    
}