【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
把lua嵌入到c++中当读配置文件,呵呵,想到来os都混了一年多了,还没分享过代码,不是我小气,是一直都忙着整公司代码,商业代码不敢随便分享咯,最近下班空闲的时候迷上了duilib这个界面库,所以产生了个邪恶的想法
,本来duilib都支持xml定义界面,所以想到了xml来定义界面,c++实现必要的解析功能,然后把lua脚本嵌入进来写逻辑处理。东西还没整完,最近一直都很忙,但是手贱先贴点代码上来
调用lua函数
const char * ReadResources(char *name) { lua_State * L = luaL_newstate() ; //创建lua运行环境 int ret = 0 ; ret = luaL_loadfile(L,"winlt.lua") ; //加载lua脚本文件 ret = lua_pcall(L,0,0,0) ; lua_getglobal(L,"getResource"); //函数名 lua_pushlstring(L,name,strlen(name)); //运行函数并把结果压入栈 lua_call(L, 1, 1); const char * c=(const char *)lua_tostring(L,-1); lua_pop(L, 1); lua_close(L) ; //释放lua运行环境 return c; }
winlt.lua文件代码
entity={ ["ResourcePath"]="\\skin", ["ResourceZip"]="default.zip", ["title"]="彩票筛选器", ["xml"]="skin_zh.xml" }; function getResource(name) return entity[name]; endmain函数代码
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow) { CPaintManagerUI::SetInstance(hInstance); //CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath()); CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + ReadResources("ResourcePath")); CPaintManagerUI::SetResourceZip(ReadResources("ResourceZip")); HRESULT Hr = ::CoInitialize(NULL); if( FAILED(Hr) ) return 0; CFrameWindowWnd* pFrame = new CFrameWindowWnd(); if( pFrame == NULL ) return 0; pFrame->Create(NULL, ReadResources("title"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE); pFrame->CenterWindow(); ::ShowWindow(*pFrame,SW_SHOW); CPaintManagerUI::MessageLoop(); ::CoUninitialize(); return 0; }呵呵,这样以来,程序可以不经过编辑都能实现多皮肤多语音,多变逻辑处理了,至于lua处理逻辑部分,我还在完善中,呵呵