//read config from file int NDInstanceBase::ReadConfig(const char *configname) { int ret; ndxml *xmlroot ; ndxml_root xmlfile; ret = ndxml_load(config_file, &xmlfile) ; if(0!=ret) { nd_logfatal("load xml from file\n") ; return -1; } xmlroot = ndxml_getnode(&xmlfile,"root") ; if(!xmlroot) { ndxml_destroy(&xmlfile); nd_logfatal("xml read error : root-node can not found \n") ; return -1; } if(-1==read_config(xmlroot, configname, &m_config) ) { ndxml_destroy(&xmlfile); nd_logfatal("xml config error %s node \n", configname) ; return -1; } ndxml_destroy(&xmlfile); return 0; }
int read_remote_proxy(char *file, struct nd_proxy_info *proxy_info ) { int ret; ndxml *xml_node, *xml_read ; ndxml_root xmlroot; nd_assert(file) ; ret = ndxml_load(file, &xmlroot) ; if(0!=ret) { return -1; } xml_node = ndxml_getnode(&xmlroot,"server_config") ; if(!xml_node) { ndxml_destroy(&xmlroot); return -1; } //read proxy xml_read = ndxml_refsub(xml_node, "proxy") ; if(xml_read) { ndxml *xmltmp = ndxml_refsub(xml_read, "proxy_type") ; if (!xmltmp) goto CFG_EXIT ; proxy_info->proxy_type = ndxml_getval_int(xmltmp); if(0==proxy_info->proxy_type )goto CFG_EXIT ; xmltmp = ndxml_refsub(xml_read, "proxy_host") ; if (!xmltmp) goto CFG_EXIT ; ndxml_getval_buf(xmltmp, proxy_info->proxy_host, sizeof(proxy_info->proxy_host)) ; xmltmp = ndxml_refsub(xml_read, "proxy_port") ; if (!xmltmp) goto CFG_EXIT ; proxy_info->proxy_port = ndxml_getval_int(xmltmp) ; xmltmp = ndxml_refsub(xml_read, "proxy_user") ; if (xmltmp) ndxml_getval_buf(xmltmp, proxy_info->user, sizeof(proxy_info->user)) ; xmltmp = ndxml_refsub(xml_read, "proxy_password") ; if (xmltmp) ndxml_getval_buf(xmltmp, proxy_info->password, sizeof(proxy_info->password)) ; } CFG_EXIT: ndxml_destroy(&xmlroot); return 0; }
int runGm(int argc, char *argv[]) { QApplication a(argc, argv); //use utf8 ndstr_set_code(APO_QT_SRC_TEXT_ENCODE); initWorkingPath(); ConnectDialog dlg; _LOAD_XML(editorSetting, CONFIG_FILE_PATH, "utf8", 0); _LOAD_XML(xmlSend, "../cfg/gm_msg.xml", "utf8", 0); //LogicEngineRoot::setSettingFile(CONFIG_FILE_PATH); if (!LogicCompiler::get_Instant()->setConfigFile(CONFIG_FILE_PATH, ND_ENCODE_UTF8)) { QMessageBox::critical(NULL, "Error", "open config file error"); exit(1); } dlg.m_editor_setting = LogicCompiler::get_Instant()->getConfig(); dlg.m_gmCfg = &xmlSend; dlg.LoadClientScript("../data/client_script.bin","../data/cehua_data.dat"); dlg.exec(); ndxml_save_encode(&xmlSend, "../cfg/gm_msg.xml", E_SRC_CODE_UTF_8, E_SRC_CODE_UTF_8); ndxml_destroy(&xmlSend); //WriteLog("...\n connect server end!"); dlg.show(); return a.exec(); }
void startDialog::on_Connect_clicked() { //ui->LogText->clear(); dialogCloseHelper _helperClose(this) ; WriteLog("begin connect to server...."); std::string filename = getPathFromConfig("gm_send_msg"); std::string client_script = getPathFromConfig("connect_script"); std::string package_file = getPathFromConfig("game_data_package_file"); _LOAD_XML(xmlSend, filename.c_str(), "utf8", 0); ConnectDialog dlg(NULL) ; dlg.m_editor_setting = apoEditorSetting::getInstant()->getConfig() ; dlg.m_gmCfg = &xmlSend; if (!dlg.LoadClientScript(client_script.c_str(), package_file.c_str())) { QMessageBox::warning(this, "Error", "load message data type error!", QMessageBox::Ok); } dlg.exec(); ndxml *msgRoot = ndxml_getnode(&xmlSend, "send_msg_list"); if (msgRoot){ ndxml_delnode(msgRoot, "history"); } ndxml_save_encode(&xmlSend, filename.c_str(), E_SRC_CODE_UTF_8, E_SRC_CODE_UTF_8); ndxml_destroy(&xmlSend) ; WriteLog("...\n connect server end!"); }
bool startDialog::compile() { std::string script_root = getPathFromConfig("script_root"); /* if (!nd_absolute_filename(script_root.c_str(), tmpbuf, sizeof(tmpbuf))) { nd_logerror("can not found file %s\n", script_root.c_str()); return false; } char tmpbuf[ND_FILE_PATH_SIZE]; std::string absPath = tmpbuf; absPath = nd_getpath(absPath.c_str(), tmpbuf, sizeof(tmpbuf)); */ ndxml_root xmlEntry; ndxml_initroot(&xmlEntry); if (-1 == ndxml_load_ex(script_root.c_str(), &xmlEntry, apoEditorSetting::getInstant()->m_encodeName.c_str())) { return false; } ndxml_root *xml = ndxml_getnode(&xmlEntry, "script_file_manager"); if (!xml){ return false; } char projPath[ND_FILE_PATH_SIZE]; nd_getpath(script_root.c_str(), projPath, sizeof(projPath)); WorkingPathSwitchHelper __pathHelper(projPath); bool ret = true; int num = ndxml_num(xml); for (int i = 0; i < num; i++) { ndxml *node = ndxml_getnodei(xml, i); if (!node) continue; std::string scriptPath = ndxml_getval(node); if (scriptPath.size() > 0) { scriptPath += "/"; } scriptPath += ndxml_getattr_val(node, "main_file"); if (!compileScript(scriptPath.c_str(),__pathHelper.workingPath())) { ret = false; break; } } ndxml_destroy(&xmlEntry); return true; }
int read_dbconfig(const char *fileName, const char *dbCfgname ,struct nd_db_config *db_cfg) { //ND_TRACE_FUNC() ; int ret = -1; ndxml *xmlroot, *xmlsub, *xmlnode ; ndxml_root xmlfile; #define GET_VAL(name) \ xmlnode = ndxml_refsub(xmlsub, #name) ; \ if (!xmlnode){ \ ret = -1; \ goto READ_EXIT ; \ } \ strncpy(db_cfg->db_##name, ndxml_getval(xmlnode), sizeof(db_cfg->db_##name) ) if ( 0 != ndxml_load(fileName , &xmlfile ) ){ nd_logfatal("read file %s error %s\n", fileName, nd_last_error() ) ; return -1; } xmlroot = ndxml_getnode( &xmlfile, "root" ) ; if ( !xmlroot ) { goto READ_EXIT; } //read connect config xmlsub = ndxml_refsub( xmlroot, dbCfgname ) ; if ( !xmlsub ) { goto READ_EXIT; } GET_VAL( host ) ; GET_VAL( database ) ; GET_VAL( user ) ; GET_VAL( password) ; ret = 0 ; READ_EXIT: ndxml_destroy( &xmlfile ); return ret ; }
CNewfileDlg::~CNewfileDlg() { ndxml_destroy(&m_xmlRoot); }
int set_xml_value(char *file, char *node_name_list, char *attr_name, char*attr_val, char *node_val) { int ret; ndxml_root xmlfile; ndxml *xnode = 0 ; char *p = node_name_list; char name[128] ; ret = ndxml_load(file, &xmlfile) ; if(0!=ret) { fprintf(stderr,"load xml from %s\n", file) ; return -1; } p = (char*)ndstr_nstr_end(p, name, '.', sizeof(name)) ; if (IS_NUMERALS(name[0])) { xnode = ndxml_getnodei(&xmlfile, ndstr_atoi_hex(name)); } else { xnode = ndxml_getnode(&xmlfile, name) ; } if (!xnode) { fprintf(stderr,"read xml-node %s error \n", name) ; } bool success = true ; while (p && *p) { if (*p == '.') { ++p ; if (!*p) { break ; } } p = (char*) ndstr_nstr_end(p, name, '.', sizeof(name)) ; if (IS_NUMERALS(name[0])) { xnode = ndxml_refsubi(xnode, ndstr_atoi_hex(name)); } else { xnode = ndxml_refsub(xnode, name) ; } if (!xnode) { fprintf(stderr,"read xml-node %s error \n", name) ; success = false ; break ; } } if (success) { success = false; if (attr_name && attr_name[0] && attr_val && attr_val[0]) { ndxml_setattrval(xnode,attr_name, attr_val ) ; success = true ; } if (node_val && node_val[0]) { ndxml_setval(xnode, node_val) ; success = true ; } if (success) { ndxml_save(&xmlfile,file) ; } } ndxml_destroy(&xmlfile); return success ? 0: -1 ; }
int main(int argc, char *argv[]) { int i ; const char *encode = "gbk" ; fprintf(stderr, "current path =%s\n", nd_getcwd()); std::string _input_dir ; std::string _output_dir = "."; //get config file for (i=1; i<argc; i++){ if(0 == strcmp(argv[i],"-d" ) && i< argc-1) { _input_dir = argv[++i] ; } else if (0 == strcmp(argv[i], "-o") && i < argc - 1) { _output_dir = argv[++i]; } else if (0==strcmp("-encode", argv[i])) { encode = argv[++i] ; } } if (_input_dir.length() <= 0 ) { fprintf(stderr, "usage: -d input-config-direct\n") ; exit(-1) ; } //int ret ; ndxml_root xmlMarco , xmlDatatype , xmlMessage ; LOAD_XML_FROM_FILE(&xmlMarco, _input_dir ,"/marco.xml",encode) ; LOAD_XML_FROM_FILE(&xmlMessage, _input_dir ,"/message.xml",encode) ; LOAD_XML_FROM_FILE(&xmlDatatype, _input_dir, "/datatype.xml", encode); #define RESET_OUT_PATH(_name) do { \ std::string myPath = _output_dir + _name ; \ nd_rmdir(myPath.c_str()); \ if(-1==nd_mkdir(myPath.c_str()) ) { nd_logerror("create direct %s error\n", myPath.c_str());} \ }while(0) RESET_OUT_PATH("/cpp"); RESET_OUT_PATH("/cpp_ue"); std::string outFile; outFile = _output_dir + "/cpp_ue/auto_dataType"; if (-1 == build_dataTypeForUE4(&xmlDatatype, outFile.c_str())) { fprintf(stderr, "export datatype error \n"); exit(1); } outFile = _output_dir + "/cpp/auto_dataType"; if(-1==build_dataType(&xmlDatatype, outFile.c_str(),false ) ) { fprintf(stderr, "export datatype error \n") ; exit(1) ; } outFile = _output_dir + "/cpp/auto_dataTypeDb"; if(-1==build_dataType(&xmlDatatype, outFile.c_str(),true ) ) { fprintf(stderr, "export datatype error \n") ; exit(1) ; } outFile = _output_dir + "/cpp/_msgid_export.h"; if(build_messageID(&xmlMessage, outFile.c_str())){ fprintf(stderr, "export MessageID error \n") ; exit(1) ; } outFile = _output_dir + "/cpp/auto_macroDefine.h"; if(build_marco(&xmlMarco, outFile.c_str())) { fprintf(stderr, "export Macro error \n") ; exit(1) ; } ndxml_destroy(&xmlMarco); ndxml_destroy(&xmlDatatype); ndxml_destroy(&xmlMessage); /* //export cs //load for utf8 RESET_OUT_PATH("/csharp"); RESET_OUT_PATH("/luaOut"); ndstr_set_code(E_SRC_CODE_UTF_8); LOAD_XML_FROM_FILE(&xmlMarco, _input_dir, "/marco.xml", "utf8"); LOAD_XML_FROM_FILE(&xmlMessage, _input_dir, "/message.xml", "utf8"); LOAD_XML_FROM_FILE(&xmlDatatype, _input_dir, "/datatype.xml", "utf8"); //outFile = _output_dir + "/csharp/ProtocolMessage.cs"; if(build_CSharp(&xmlMessage,&xmlMarco , &xmlDatatype, _output_dir.c_str()) ) { fprintf(stderr, "export CSharp error \n") ; exit(1) ; } outFile = _output_dir + "/luaOut/NetDataStruct.lua"; if ( build_luaDataStruct(&xmlDatatype, outFile.c_str())) { fprintf(stderr, "export lua error \n"); exit(1); } outFile = _output_dir + "/luaOut/NetMessageIDDefine.lua"; if (build_luaMessageID(&xmlMessage, outFile.c_str())) { fprintf(stderr, "export lua mesage id error \n"); exit(1); } ndxml_destroy(&xmlMarco); ndxml_destroy(&xmlDatatype); ndxml_destroy(&xmlMessage) ; */ exit(0) ; }
bool startDialog::compileScript(const char *scriptFile, const char *editorWorkingPath) { ndxml_root xmlScript; ndxml_initroot(&xmlScript); if (-1 == ndxml_load_ex(scriptFile, &xmlScript, apoEditorSetting::getInstant()->m_encodeName.c_str())) { return false; } const char*inFile = scriptFile; const char *outFile = getScriptSetting(&xmlScript, "out_file"); if (!outFile){ nd_logerror("compile %s error !!!\nMaybe file is destroyed\n", scriptFile); return false; } //get out file absolute path std::string curPath = nd_getcwd(); char outFilePath[ND_FILE_PATH_SIZE]; if (!nd_getpath(scriptFile, outFilePath, sizeof(outFilePath))) { nd_logerror("get out file path error 1\n"); return false; } if (-1 == nd_chdir(outFilePath)) { nd_logerror("get script file-path error 2\n"); return false; } if (!nd_absolute_filename(outFile, outFilePath, sizeof(outFilePath))) { nd_logerror("get out file path error 3\n"); nd_chdir(curPath.c_str()); return false; } nd_chdir(curPath.c_str()); outFile = outFilePath; // ---end get out file absolute path int outEncode = getScriptExpEncodeType(&xmlScript); bool withDebug = getScriptExpDebugInfo(&xmlScript); //std::string outPath = outFile; ndxml_destroy(&xmlScript); //outFile = outPath.c_str(); int orderType = ND_L_ENDIAN; const char *orderName = apoEditorSetting::getInstant()->getValueFromSetting("bin_data_byte_order"); if (orderName) { orderType = atoi(orderName); } LogicCompiler &lgcompile= *LogicCompiler::get_Instant(); if (!lgcompile.compileXml(inFile, outFile, outEncode, withDebug, orderType)) { const char *pFunc = lgcompile.m_cur_function.c_str(); const char *pStep = lgcompile.m_cur_step.c_str(); char func_name[256]; char step_name[256]; if (outEncode == E_SRC_CODE_GBK) { pFunc = nd_gbk_to_utf8(pFunc, func_name, sizeof(func_name)); pStep = nd_gbk_to_utf8(pStep, step_name, sizeof(step_name)); } nd_logerror("compile error file %s, function %s, step %s , stepindex %d\n", lgcompile.m_cur_file.c_str(), pFunc, pStep, lgcompile.m_cur_node_index); return false; } nd_logmsg("!!!!!!!!!!COMPILE %s success !!!!!!!!!!!\n begining run script...\n", scriptFile); ClientMsgHandler::ApoConnectScriptOwner apoOwner; LogicEngineRoot *scriptRoot = LogicEngineRoot::get_Instant(); nd_assert(scriptRoot); scriptRoot->setOutPutEncode(E_SRC_CODE_UTF_8); scriptRoot->setPrint(ND_LOG_WRAPPER_PRINT(startDialog), NULL); scriptRoot->getGlobalParser().setSimulate(true); //scriptRoot->getGlobalParser().setOwner(&apoOwner); if (0 != scriptRoot->LoadScript(outFile, &scriptRoot->getGlobalParser())){ WriteLog("load script error n"); LogicEngineRoot::destroy_Instant(); return false; } WriteLog("start run script...\n"); scriptRoot->setDftScriptModule(scriptRoot->getMainModuleName()); if (0 != scriptRoot->test()){ WriteLog("run script error\n"); const char *curErrNode = scriptRoot->getGlobalParser().getLastErrorNode(); if (curErrNode && *curErrNode) { showScriptError(scriptFile, curErrNode,editorWorkingPath); } LogicEngineRoot::destroy_Instant(); return false; } LogicEngineRoot::destroy_Instant(); nd_logmsg("!!!!!!!!!!!!!!!!!!!SCRIPT %s SUCCESS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",scriptFile); return true; }