bool ScriptEngine::_initialize() { bool developmentMode = HAS_OPTION( m_serviceProvider, "dev" ); if( developmentMode == true ) { Py_ErrFormatFlag = 1; } else { Py_ErrFormatFlag = 0; } if( pybind::initialize( false, false, true ) == false ) { LOGGER_ERROR(m_serviceProvider)("ScriptEngine::initialize invalid initialize pybind" ); return false; } pybind::set_logger( (pybind::pybind_logger_t)s_pybind_logger, m_serviceProvider); # if defined(WIN32) && defined(_DEBUG) _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_WNDW ); _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_WNDW ); _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_WNDW ); # endif m_moduleMenge = this->initModule( "Menge" ); this->addGlobalModule( "Menge" , (ScriptObject *)m_moduleMenge ); uint32_t python_version = pybind::get_python_version(); this->addGlobalModule( "_PYTHON_VERSION" , (ScriptObject *)pybind::ptr( python_version ) ); pybind::set_currentmodule( m_moduleMenge ); pybind::interface_<ScriptLogger>( "ScriptLogger", true ) .def_native( "write", &ScriptLogger::py_write ) .def_property( "softspace", &ScriptLogger::getSoftspace, &ScriptLogger::setSoftspace ) ; m_loggerWarning = new ScriptLogger(m_serviceProvider); m_loggerWarning->setMessageLevel( LM_WARNING ); PyObject * py_logger = pybind::ptr( m_loggerWarning ); pybind::setStdOutHandle( py_logger ); pybind::decref( py_logger ); m_loggerError = new ScriptLogger(m_serviceProvider); m_loggerError->setMessageLevel( LM_ERROR ); PyObject * py_loggerError = pybind::ptr( m_loggerError ); pybind::setStdErrorHandle( py_loggerError ); pybind::decref( py_loggerError ); pybind::set_observer_bind_call( new My_observer_bind_call( m_serviceProvider ) ); pybind::interface_<ScriptModuleFinder>("ScriptModuleFinder", true) .def("find_module", &ScriptModuleFinder::find_module) .def("load_module", &ScriptModuleFinder::load_module) ; m_moduleFinder = new ScriptModuleFinder(); if( m_moduleFinder->initialize( m_serviceProvider ) == false ) { LOGGER_ERROR(m_serviceProvider)("ScriptEngine::initialize invalid initialize ScriptModuleFinder" ); return false; } PyObject * py_moduleFinder = pybind::ptr( m_moduleFinder ); m_moduleFinder->setEmbed( py_moduleFinder ); //pybind::decref( m_moduleMenge ); pybind::_set_module_finder( py_moduleFinder ); //bool gc_exist; //PyObject * gc = pybind::module_import( "gc", gc_exist ); //pybind::call_method( gc, "disable", "()" ); return true; }
void run(const char *code, int options) { /* Set up interpretation variables */ unsigned int dpointer = 0; /* dimensional pointer */ unsigned int dim = 1; /* Number of dimensions working with */ int *coord = malloc(sizeof(int) * dim); /* current coordinates */ int *loopqueue = malloc(sizeof(int)); /* loop list */ char *buffer = malloc(sizeof(char)); /* alloc now so it doesn't fail on `free` */ int loops = 0; /* Number of loops */ int offness = -1; /* Indicates offness (value is -1 or location of loop) */ /* World variables */ struct Cell *root = malloc(sizeof(struct Cell)); struct Cell *cell = root; /* Loop and temporary variables */ unsigned int i; unsigned int v; /* Performance enhancing variables */ int com_buffer = 0; /* Command buffer */ /* Set up options */ SET_OPTIONS_VARIABLE(options); /* Set initial coordinates */ coord[0] = 0; /* Set up binary tree */ root->right = NULL; root->left = NULL; root->value = 0; root->coord = malloc(sizeof(int) * dim); root->coord[0] = 0; root->dim = dim; /* Start interpretation */ for (i = 0; code[i] != '\0'; i++) { if (offness == -1 || code[i] == ']' || code[i] == '[') { switch (code[i]) { case '[': /* Speed boost for resets */ if (code[i + 1] == '-' && code[i + 2] == ']') { cell->value = 0; i += 2; break; } /* Increment size of loop queue */ loops++; /* Safely reallocate memory */ free(buffer); buffer = malloc(loops * sizeof(int)); if (buffer == NULL) { fprintf(stderr, "hdbf: runtime error: memory allocation error\n"); exit(EXIT_FAILURE); } memcpy(buffer, loopqueue, (loops - 1) * sizeof(int)); free(loopqueue); loopqueue = malloc(loops * sizeof(int)); memcpy(loopqueue, buffer, loops * sizeof(int)); /* Error if out of bounds */ if (loops <= 0) { fprintf(stderr, "hdbf: runtime error: loops below bounds\n"); exit(EXIT_FAILURE); } loopqueue[loops - 1] = i; /* Check if need to turn off */ if (offness == -1 && cell->value == 0) { offness = i; } break; case ']': /* Store last loopqueue value before deleting it */ v = loopqueue[loops - 1]; /* Delete last loopqueue value */ loops--; free(buffer); buffer = malloc(sizeof(int) * loops); memcpy(buffer, loopqueue, sizeof(int) * loops); free(loopqueue); loopqueue = malloc(sizeof(int) * loops); memcpy(loopqueue, buffer, sizeof(int) * loops); /* Turn on if necessary */ if (offness != -1) { if (offness == (signed) v) offness = -1; } else { /* Go back to matching `[` */ i = v - 1; } break; case '+': /* Increase value of cell */ com_buffer = 1; while (code[i + 1] == '+') { com_buffer++; i++; } cell->value += com_buffer; break; case '-': /* Decrease value of cell */ com_buffer = 1; while (code[i + 1] == '-') { com_buffer++; i++; } cell->value -= com_buffer; break; case '^': if (!HAS_OPTION(OPT_PUREBF)) { /* Increase dimensional pointer */ if ((++dpointer) >= dim) { dim++; free(buffer); buffer = malloc(sizeof(int) * dim); for (v = 0; v < dim; v++) { buffer[v] = coord[v]; } free(coord); coord = malloc(sizeof(int) * dim); for (v = 0; v < dim; v++) { coord[v] = buffer[v]; } coord[dim - 1] = 0; } } break; case 'V': /* Decrease dimentional pointer */ if (!HAS_OPTION(OPT_PUREBF)) { --dpointer; } break; case '>': com_buffer = 1; while (code[i + 1] == '>') { com_buffer++; i++; } /* Go in positive direction of current vector */ coord[dpointer] += com_buffer; cell = traverseWorld(root, coord, dim); break; case '<': com_buffer = 1; while (code[i + 1] == '<') { com_buffer++; i++; } /* Go in negative direction of current vector */ coord[dpointer] -= com_buffer; cell = traverseWorld(root, coord, dim); break; case '.': putchar(cell->value); break; case ',': cell->value = getchar(); break; case '?': /* Print out coordinates if debugging is on */ if (HAS_OPTION(OPT_DEBUG)) { printf("[("); for (v = 0; v < dim; v++) { printf("%d%c", coord[v], (v == dim - 1) ? ')' : ','); } printf("]\n"); } break; case '#': /* Print out cell if debugging is on */ if (HAS_OPTION(OPT_DEBUG)) { printf("{("); for (v = 0; v < (unsigned) (cell->dim); v++) { printf("%d%c", cell->coord[v], (v == (unsigned) (cell->dim - 1)) ? ')' : ','); } printf("=%d}\n", cell->value); } break; } } } /* Free stuff */ deleteWorld(root); free(root->coord); free(root); free(coord); free(loopqueue); free(buffer); }
bool AccountManager::loadAccounts() { bool noLoadAccount = HAS_OPTION( m_serviceProvider, "noaccounts" ); if( noLoadAccount == true ) { return true; } FilePath accountsPath = CONFIG_VALUE( m_serviceProvider, "Game", "AccountsPath", STRINGIZE_STRING_LOCAL( m_serviceProvider, "accounts.ini" ) ); bool accountsExist = FILE_SERVICE(m_serviceProvider) ->existFile( CONST_STRING(m_serviceProvider, user), accountsPath, nullptr ); if( accountsExist == false ) { LOGGER_WARNING(m_serviceProvider)( "AccountManager::loadAccounts not exist accounts '%s'" , accountsPath.c_str() ); return true; } InputStreamInterfacePtr file = FILE_SERVICE(m_serviceProvider) ->openInputFile( CONST_STRING(m_serviceProvider, user), accountsPath, false ); if( file == nullptr ) { LOGGER_ERROR(m_serviceProvider)("AccountManager::loadAccounts open accounts failed '%s'" , accountsPath.c_str() ); return false; } IniUtil::IniStore ini; if( IniUtil::loadIni( ini, file, m_serviceProvider ) == false ) { LOGGER_ERROR(m_serviceProvider)("AccountManager::loadAccounts parsing accounts failed '%s'" , accountsPath.c_str() ); return false; } file = nullptr; //unsigned int playerCount; //config.getSettingUInt( L"SETTINGS", L"AccountCount", playerCount ); if( IniUtil::getIniValue( ini, "SETTINGS", "AccountEnumerator", m_playerEnumerator, m_serviceProvider ) == false ) { LOGGER_ERROR(m_serviceProvider)("AccountManager::loadAccounts get AccountEnumerator failed '%s'" , accountsPath.c_str() ); return false; } if( IniUtil::getIniValue( ini, "SETTINGS", "DefaultAccountID", m_defaultAccountID, m_serviceProvider ) == false ) { LOGGER_INFO(m_serviceProvider)( "AccountManager::loadAccounts get DefaultAccountID failed '%s'" , accountsPath.c_str() ); } WString selectAccountID; if( IniUtil::getIniValue( ini, "SETTINGS", "SelectAccountID", selectAccountID, m_serviceProvider ) == false ) { LOGGER_INFO(m_serviceProvider)( "AccountManager::loadAccounts get SelectAccountID failed '%s'" , accountsPath.c_str() ); } TVectorWString values; if( IniUtil::getIniValue( ini, "ACCOUNTS", "Account", values, m_serviceProvider ) == false ) { LOGGER_INFO(m_serviceProvider)( "AccountManager::loadAccounts get ACCOUNTS failed '%s'" , accountsPath.c_str() ); } AccountInterfacePtr validAccount = nullptr; for( TVectorWString::const_iterator it = values.begin(), it_end = values.end(); it != it_end; ++it ) { const WString & name = *it; AccountInterfacePtr account = this->loadAccount_( name ); if( account == nullptr ) { LOGGER_ERROR(m_serviceProvider)("AccountManager::loadAccountsInfo invalid load account '%ls'" , name.c_str() ); continue; } validAccount = account; m_accounts.insert( std::make_pair( name, account ) ); } if( selectAccountID.empty() == false ) { LOGGER_INFO(m_serviceProvider)( "AccountManager::loadAccounts select account '%ls'" , selectAccountID.c_str() ); if( this->selectAccount( selectAccountID ) == false ) { LOGGER_ERROR(m_serviceProvider)("AccountManager::loadAccounts invalid set select account '%ls'" , selectAccountID.c_str() ); return false; } } else if( m_defaultAccountID.empty() == false ) { LOGGER_INFO(m_serviceProvider)( "AccountManager::loadAccounts set default account '%ls'" , m_defaultAccountID.c_str() ); if( this->selectAccount( m_defaultAccountID ) == false ) { LOGGER_ERROR(m_serviceProvider)("AccountManager::loadAccounts invalid set default account '%ls'" , m_defaultAccountID.c_str() ); return false; } } else if( validAccount != nullptr ) { const WString & accountID = validAccount->getName(); LOGGER_WARNING(m_serviceProvider)( "AccountManager::loadAccounts set valid account '%ls'" , accountID.c_str() ); if( this->selectAccount( accountID ) == false ) { LOGGER_ERROR(m_serviceProvider)("AccountManager::loadAccounts invalid set valid account '%ls'" , accountID.c_str() ); return false; } } else { LOGGER_INFO(m_serviceProvider)("AccountManager::loadAccounts invalid set any accounts" ); } return true; }