Token EBNFScanner::scanIdentifier() { TokPosition pos = createPosition(); char currchar = getCurrentCharacter(); String content = String(&currchar, 1); CharArray _equals; _equals << '-' << '_'; while (hasNextCharacter()) { nextCharacter(); // Ensure this character is in identifier. if(TokenScannerHelper::isAlphaNum(getCurrentCharacter()) || TokenScannerHelper::isEquals(getCurrentCharacter(), _equals) ) { content.append(getCurrentCharacter()); } else { backupCharacter(); break; } } return Token (TokIdentifier, content, pos); }
void MWState::StateManager::askLoadRecent() { if (MWBase::Environment::get().getWindowManager()->getMode() == MWGui::GM_MainMenu) return; if( !mAskLoadRecent ) { if(getCurrentCharacter()->begin() == getCurrentCharacter()->end() )//no saves { MWBase::Environment::get().getWindowManager()->pushGuiMode (MWGui::GM_MainMenu); } else { MWState::Slot lastSave = *getCurrentCharacter()->begin(); std::vector<std::string> buttons; buttons.push_back("#{sYes}"); buttons.push_back("#{sNo}"); std::string tag("%s"); std::string message = MWBase::Environment::get().getWindowManager()->getGameSettingString("sLoadLastSaveMsg", tag); size_t pos = message.find(tag); message.replace(pos, tag.length(), lastSave.mProfile.mDescription); MWBase::Environment::get().getWindowManager()->messageBox(message, buttons); mAskLoadRecent = true; } } }
// // e x t r a c t S t r i n g // bool DinoLineBuffer::extractString(const DinoLineBufferPosition &startPosition, const DinoLineBufferPosition &endPosition, char *targetString){ // StartPosition invalid, probably because the line of the startPosition // has already been overwritten, i.e. the string is too long if (!isValidPosition(startPosition)) { ogdf::strcpy(targetString, DinoLineBuffer::c_maxStringLength, "String too long!"); return false; } // EndPosition must be valid OGDF_ASSERT(isValidPosition(endPosition)) // Remember original currentPosition DinoLineBufferPosition originalCurrentPosition = getCurrentPosition(); // Begin at startPosition setCurrentPosition(startPosition); // Copy characters to tempString int targetStringIndex = 0; while (getCurrentPosition() != endPosition) { // Check if eof OGDF_ASSERT(getCurrentCharacter() != EOF) // Put character into targetString targetString[targetStringIndex] = getCurrentCharacter(); ++targetStringIndex; // String too long if (targetStringIndex >= DinoLineBuffer::c_maxStringLength - 1){ ogdf::strcpy(targetString, DinoLineBuffer::c_maxStringLength, "String too long!"); // Set back the original current position setCurrentPosition(originalCurrentPosition); return false; } // Move to next character moveToNextCharacter(); } // Copy characters to tempString // Set back the original current position setCurrentPosition(originalCurrentPosition); // Terminate string targetString[targetStringIndex] = '\0'; return true; } // extractString
// // s k i p W h i t e s p a c e // void DinoLineBuffer::skipWhitespace(){ if (getCurrentCharacter() == EOF){ return; } while ((isspace(getCurrentCharacter())) && (!(getCurrentCharacter() == EOF))) { moveToNextCharacter(); } } // skipWhitespace
void MWState::StateManager::quickLoad() { if (Character* mCurrentCharacter = getCurrentCharacter (false)) { if (mCurrentCharacter->begin() == mCurrentCharacter->end()) return; loadGame (mCurrentCharacter, mCurrentCharacter->begin()->mPath.string()); //Get newest save } }
Token EBNFScanner::scanLiteral() { TokPosition pos = createPosition(); char _start = getCurrentCharacter(); String content(&_start, 1); while (hasNextCharacter()) { nextCharacter(); content.append(getCurrentCharacter()); // Ensure begin and end of literal. if(TokenScannerHelper::isQuote(getCurrentCharacter()) && getCurrentCharacter() == _start) { break; } } return Token (TokLiteral, content, pos); }
Token EBNFScanner::scanComment() { char currchar = getCurrentCharacter(); TokPosition pos = createPosition(); String content = String(&currchar, 1); while (hasNextCharacter()) { nextCharacter(); content.append(getCurrentCharacter()); if(getCurrentCharacter() == '*' && peekCharacter() == ')') { nextCharacter(); content.append(getCurrentCharacter()); break; } checkNewLine(); } return Token (TokComment, content, pos); }
void MWState::StateManager::quickSave (std::string name) { if (!(mState==State_Running && MWBase::Environment::get().getWorld()->getGlobalInt ("chargenstate")==-1 // char gen && MWBase::Environment::get().getWindowManager()->isSavingAllowed())) { //You can not save your game right now MWBase::Environment::get().getWindowManager()->messageBox("#{sSaveGameDenied}"); return; } const Slot* slot = NULL; Character* mCurrentCharacter = getCurrentCharacter(true); //Get current character //Find quicksave slot for (Character::SlotIterator it = mCurrentCharacter->begin(); it != mCurrentCharacter->end(); ++it) { if (it->mProfile.mDescription == name) slot = &*it; } saveGame(name, slot); }
void MWState::StateManager::update (float duration) { mTimePlayed += duration; // Note: It would be nicer to trigger this from InputManager, i.e. the very beginning of the frame update. if (mAskLoadRecent) { int iButton = MWBase::Environment::get().getWindowManager()->readPressedButton(); if(iButton==0) { mAskLoadRecent = false; //Load last saved game for current character MWState::Character *curCharacter = getCurrentCharacter(); MWState::Slot lastSave = *curCharacter->begin(); loadGame(curCharacter, &lastSave); } else if(iButton==1) { mAskLoadRecent = false; MWBase::Environment::get().getWindowManager()->pushGuiMode (MWGui::GM_MainMenu); } } }
Token EBNFScanner::scanOperator() { TokPosition pos = createPosition(); char currchar = getCurrentCharacter(); String content = String(&currchar, 1); char peak = peekCharacter(); if(getCurrentCharacter() == ':' && peak == ':') { nextCharacter(); content.append(getCurrentCharacter()); nextCharacter(); if(getCurrentCharacter() != '=') { raiseError (String("Expecting '=' but got '") + getCurrentCharacter() + "' instead."); content.append(getCurrentCharacter()); return Token (Token::TypeInvalid, content, pos); } content.append(getCurrentCharacter()); } return Token (TokOperator, content, pos); }
void MWState::StateManager::quickLoad() { if (Character* mCurrentCharacter = getCurrentCharacter (false)) if (const MWState::Slot* slot = &*mCurrentCharacter->begin()) //Get newest save loadGame (mCurrentCharacter, slot); }
void MWState::StateManager::saveGame (const std::string& description, const Slot *slot) { try { ESM::SavedGame profile; MWBase::World& world = *MWBase::Environment::get().getWorld(); MWWorld::Ptr player = world.getPlayerPtr(); profile.mContentFiles = world.getContentFiles(); profile.mPlayerName = player.getClass().getName (player); profile.mPlayerLevel = player.getClass().getNpcStats (player).getLevel(); std::string classId = player.get<ESM::NPC>()->mBase->mClass; if (world.getStore().get<ESM::Class>().isDynamic(classId)) profile.mPlayerClassName = world.getStore().get<ESM::Class>().find(classId)->mName; else profile.mPlayerClassId = classId; profile.mPlayerCell = world.getCellName(); profile.mInGameTime.mGameHour = world.getTimeStamp().getHour(); profile.mInGameTime.mDay = world.getDay(); profile.mInGameTime.mMonth = world.getMonth(); profile.mInGameTime.mYear = world.getYear(); profile.mTimePlayed = mTimePlayed; profile.mDescription = description; int screenshotW = 259*2, screenshotH = 133*2; // *2 to get some nice antialiasing Ogre::Image screenshot; world.screenshot(screenshot, screenshotW, screenshotH); Ogre::DataStreamPtr encoded = screenshot.encode("jpg"); profile.mScreenshot.resize(encoded->size()); encoded->read(&profile.mScreenshot[0], encoded->size()); if (!slot) slot = getCurrentCharacter()->createSlot (profile); else slot = getCurrentCharacter()->updateSlot (slot, profile); boost::filesystem::ofstream stream (slot->mPath, std::ios::binary); ESM::ESMWriter writer; const std::vector<std::string>& current = MWBase::Environment::get().getWorld()->getContentFiles(); for (std::vector<std::string>::const_iterator iter (current.begin()); iter!=current.end(); ++iter) writer.addMaster (*iter, 0); // not using the size information anyway -> use value of 0 writer.setFormat (ESM::Header::CurrentFormat); int recordCount = 1 // saved game header +MWBase::Environment::get().getJournal()->countSavedGameRecords() +MWBase::Environment::get().getWorld()->countSavedGameRecords() +MWBase::Environment::get().getScriptManager()->getGlobalScripts().countSavedGameRecords() +MWBase::Environment::get().getDialogueManager()->countSavedGameRecords() +MWBase::Environment::get().getWindowManager()->countSavedGameRecords(); writer.setRecordCount (recordCount); writer.save (stream); Loading::Listener& listener = *MWBase::Environment::get().getWindowManager()->getLoadingScreen(); listener.setProgressRange(recordCount); listener.setLabel("#{sNotifyMessage4}"); Loading::ScopedLoad load(&listener); writer.startRecord (ESM::REC_SAVE); slot->mProfile.save (writer); writer.endRecord (ESM::REC_SAVE); listener.increaseProgress(); MWBase::Environment::get().getJournal()->write (writer, listener); MWBase::Environment::get().getDialogueManager()->write (writer, listener); MWBase::Environment::get().getWorld()->write (writer, listener); MWBase::Environment::get().getScriptManager()->getGlobalScripts().write (writer, listener); MWBase::Environment::get().getWindowManager()->write(writer, listener); // Ensure we have written the number of records that was estimated if (writer.getRecordCount() != recordCount+1) // 1 extra for TES3 record std::cerr << "Warning: number of written savegame records does not match. Estimated: " << recordCount+1 << ", written: " << writer.getRecordCount() << std::endl; writer.close(); if (stream.fail()) throw std::runtime_error("Write operation failed"); Settings::Manager::setString ("character", "Saves", slot->mPath.parent_path().filename().string()); } catch (const std::exception& e) { std::stringstream error; error << "Failed to save game: " << e.what(); std::cerr << error.str() << std::endl; std::vector<std::string> buttons; buttons.push_back("#{sOk}"); MWBase::Environment::get().getWindowManager()->messageBox(error.str(), buttons); // If no file was written, clean up the slot if (slot && !boost::filesystem::exists(slot->mPath)) getCurrentCharacter()->deleteSlot(slot); } }
TCPMessagePtr GameClient::processClient() { TCPMessagePtr msg(new TCPMessage); if (SDLNet_SocketReady(_socket)){ msg = TCPMessage::receiveFromSocket(_socket); std::cout << "Got message[" << static_cast<int>( msg->type ) << "]: "<< msg->data << "\n"; if ( msg->type == TCPMessage::MSG_LOGIN ){ size_t tmpPos = msg->data.find_first_of(';'); if (tmpPos == std::string::npos){ std::cout << "Invalid LOGIN message!!!\n"; return TCPMessagePtr(new TCPMessage(TCPMessage::MSG_DISCONNECT, "")); } // check login std::string username(msg->data.substr(0, tmpPos)); std::string password(msg->data.substr(tmpPos + 1)); msg->type = login( username, password ); } else if ( msg->type == TCPMessage::MSG_LIST_CHARS ) { std::string chars; sqlite3_stmt* query = Database::getInstance()->prepare( QUERY_CHARACTER_LIST_BY_ACCID ); sqlite3_bind_int( query, 1, _id ); while(sqlite3_step( query ) == SQLITE_ROW) { chars += reinterpret_cast<const char*>( sqlite3_column_text( query, 0 ) ); if (chars.empty() == false) chars += ';'; } TCPMessage charsMsg( TCPMessage::MSG_LIST_CHARS, chars ); charsMsg.sendToSocket(_socket); } else if ( msg->type == TCPMessage::MSG_SELECT_CHAR ) { selectChar(msg->data); } else if ( msg->type == TCPMessage::MSG_MOVE_REQ ) { const int* x = reinterpret_cast<const int*>(msg->data.c_str()); const int* y = reinterpret_cast<const int*>(msg->data.c_str() + INT_SIZE); std::cout << "move request: " << *x << " " << *y << "\n"; int coords[] = { *x, *y }; if ( _world->canMoveTo( *x, *y, *this, true ) == false ){ // reset to current coords coords[0] = getCurrentCharacter()->position().x; coords[1] = getCurrentCharacter()->position().y; // send correction message TCPMessagePtr msg = TCPMessage::createMessage( TCPMessage::MSG_MOVE_COORD, coords, 2 ); msg->sendToSocket(_socket); } else { // save the coords getCurrentCharacter()->position().x = coords[0]; getCurrentCharacter()->position().y = coords[1]; TCPMessagePtr playerMsg = TCPMessage::createMessage( TCPMessage::MSG_PLAYER_MOVE, coords, 2, getCurrentCharacter()->getName()); _world->broadcastMessage( playerMsg, this ); } } } return msg; }
void MWState::StateManager::saveGame (const std::string& description, const Slot *slot) { try { ESM::SavedGame profile; MWBase::World& world = *MWBase::Environment::get().getWorld(); MWWorld::Ptr player = world.getPlayerPtr(); profile.mContentFiles = world.getContentFiles(); profile.mPlayerName = player.get<ESM::NPC>()->mBase->mName; profile.mPlayerLevel = player.getClass().getNpcStats (player).getLevel(); std::string classId = player.get<ESM::NPC>()->mBase->mClass; if (world.getStore().get<ESM::Class>().isDynamic(classId)) profile.mPlayerClassName = world.getStore().get<ESM::Class>().find(classId)->mName; else profile.mPlayerClassId = classId; profile.mPlayerCell = world.getCellName(); profile.mInGameTime.mGameHour = world.getTimeStamp().getHour(); profile.mInGameTime.mDay = world.getDay(); profile.mInGameTime.mMonth = world.getMonth(); profile.mInGameTime.mYear = world.getYear(); profile.mTimePlayed = mTimePlayed; profile.mDescription = description; writeScreenshot(profile.mScreenshot); if (!slot) slot = getCurrentCharacter()->createSlot (profile); else slot = getCurrentCharacter()->updateSlot (slot, profile); // Write to a memory stream first. If there is an exception during the save process, we don't want to trash the // existing save file we are overwriting. std::stringstream stream; ESM::ESMWriter writer; const std::vector<std::string>& current = MWBase::Environment::get().getWorld()->getContentFiles(); for (std::vector<std::string>::const_iterator iter (current.begin()); iter!=current.end(); ++iter) writer.addMaster (*iter, 0); // not using the size information anyway -> use value of 0 writer.setFormat (ESM::SavedGame::sCurrentFormat); // all unused writer.setVersion(0); writer.setType(0); writer.setAuthor(""); writer.setDescription(""); int recordCount = 1 // saved game header +MWBase::Environment::get().getJournal()->countSavedGameRecords() +MWBase::Environment::get().getWorld()->countSavedGameRecords() +MWBase::Environment::get().getScriptManager()->getGlobalScripts().countSavedGameRecords() +MWBase::Environment::get().getDialogueManager()->countSavedGameRecords() +MWBase::Environment::get().getWindowManager()->countSavedGameRecords() +MWBase::Environment::get().getMechanicsManager()->countSavedGameRecords(); writer.setRecordCount (recordCount); writer.save (stream); Loading::Listener& listener = *MWBase::Environment::get().getWindowManager()->getLoadingScreen(); // Using only Cells for progress information, since they typically have the largest records by far listener.setProgressRange(MWBase::Environment::get().getWorld()->countSavedGameCells()); listener.setLabel("#{sNotifyMessage4}", true); Loading::ScopedLoad load(&listener); writer.startRecord (ESM::REC_SAVE); slot->mProfile.save (writer); writer.endRecord (ESM::REC_SAVE); MWBase::Environment::get().getJournal()->write (writer, listener); MWBase::Environment::get().getDialogueManager()->write (writer, listener); MWBase::Environment::get().getWorld()->write (writer, listener); MWBase::Environment::get().getScriptManager()->getGlobalScripts().write (writer, listener); MWBase::Environment::get().getWindowManager()->write(writer, listener); MWBase::Environment::get().getMechanicsManager()->write(writer, listener); // Ensure we have written the number of records that was estimated if (writer.getRecordCount() != recordCount+1) // 1 extra for TES3 record std::cerr << "Warning: number of written savegame records does not match. Estimated: " << recordCount+1 << ", written: " << writer.getRecordCount() << std::endl; writer.close(); if (stream.fail()) throw std::runtime_error("Write operation failed (memory stream)"); // All good, write to file boost::filesystem::ofstream filestream (slot->mPath, std::ios::binary); filestream << stream.rdbuf(); if (filestream.fail()) throw std::runtime_error("Write operation failed (file stream)"); Settings::Manager::setString ("character", "Saves", slot->mPath.parent_path().filename().string()); } catch (const std::exception& e) { std::stringstream error; error << "Failed to save game: " << e.what(); std::cerr << error.str() << std::endl; std::vector<std::string> buttons; buttons.push_back("#{sOk}"); MWBase::Environment::get().getWindowManager()->interactiveMessageBox(error.str(), buttons); // If no file was written, clean up the slot if (slot && !boost::filesystem::exists(slot->mPath)) getCurrentCharacter()->deleteSlot(slot); } }
// // m o v e T o N e x t C h a r a c t e r // char DinoLineBuffer::moveToNextCharacter(){ // Return if end of file is reached if (getCurrentCharacter() == EOF){ return EOF; } // Increment position m_currentPosition.incrementPosition(); // End of line is reached, there can be some consecutive lines // with only \0 in it; hence we use a while loop while (getCurrentCharacter() == '\0'){ // Current line is equal to most recently read line, // i.e. we have to read a new line from the file if (m_currentPosition.getLineNumber() == m_numberOfMostRecentlyReadLine){ // Increment line pointer (modulo c_maxNoOfLines - 1) if (m_numberOfMostRecentlyReadLine == (DinoLineBuffer::c_maxNoOfLines - 1)){ m_numberOfMostRecentlyReadLine = 0; } else { ++m_numberOfMostRecentlyReadLine; } // Increment update count ++(m_lineUpdateCountArray[m_numberOfMostRecentlyReadLine]); // Increment inputFileLineCounter ++m_inputFileLineCounter; // Set current position m_currentPosition.set(m_numberOfMostRecentlyReadLine, m_lineUpdateCountArray[m_numberOfMostRecentlyReadLine], 0); // End of file is reached if (m_pIs->eof()){ // Set eof marker setCurrentCharacter(EOF); } // Read next line and put it to the new position else{ m_pIs->getline(getCurrentCharacterPointer(), DinoLineBuffer::c_maxLineLength); } } // Current line is equal to most recently read line // Current line is NOT equal to most recently read line, i.e. // it is not necessary to read a new line from the file but to // set the currentPosition to the next line which is already in // the line buffer. else{ int newLine; // Increment current line pointer (modulo c_maxNoOfLines - 1) if (m_currentPosition.getLineNumber() == (DinoLineBuffer::c_maxNoOfLines - 1)){ newLine = 0; } else { newLine = m_currentPosition.getLineNumber() + 1; } // Set current position m_currentPosition.set(newLine, m_lineUpdateCountArray[newLine], 0); } // Current line is NOT equal to most recently read line } // End of line is reached return getCurrentCharacter(); } // moveToNextCharacter