bool CharacterService::loadAllCharacters() { UNIQUE_LOCK(mMutex); fs::path loadPath("data/characters"); fs::directory_iterator endIt; if (!fs::exists(loadPath) || !fs::is_directory(loadPath)) return false; for (fs::directory_iterator dirIt(loadPath); dirIt != endIt; ++dirIt) { fs::path filePath = *dirIt; if (fs::is_regular_file(filePath)) { if (filePath.extension().string() == ".json") { try { Json::Value characterJson = RS->readJsonFile(filePath.string()); std::shared_ptr<Character> character = std::make_shared<Character>(); Json::deserialize(characterJson, character); if (mCharacters[character->id()]) { std::cerr << "Duplicated character id " << character->id() << std::endl; std::cerr << "Ignored"; continue; } mCharacters[character->id()] = character; } catch (const SerializationException &e) { std::cerr << "Loading " << filePath.string() << " failed" << std::endl; std::cerr << e.what(); } } } } return true; }
std::shared_ptr<Character> CharacterService::createCharacter(const std::string &characterId) { UNIQUE_LOCK(mMutex); std::shared_ptr<Character> character = std::make_shared<Character>(characterId); character->setSaveWithLevel(false); mCharacters[characterId] = character; return character; }
// 放入一个任务(可能有多线程操作,须加锁) int TaskQueue::push(void *task) { FUNCTION_TRACK(); // 函数轨迹跟综 UNIQUE_LOCK(m_PushLock); return m_queue.Push( task ); }
// 取出一个任务(有多线程操作,须加锁) void *TaskQueue::pop() { FUNCTION_TRACK(); // 函数轨迹跟综 UNIQUE_LOCK(m_PopLock); void *element = NULL; m_queue.Pop(element); return element; }
std::shared_ptr<Character> CharacterService::loadCharacter(const std::string &fileName) { UNIQUE_LOCK(mMutex); Json::Value characterJson = RS->readJsonFile("data/characters/" + fileName); std::shared_ptr<Character> character = std::make_shared<Character>(); try { Json::deserialize(characterJson, character); if (mCharacters[character->id()]) { std::cerr << "Duplicated character id " << character->id() << std::endl; return mCharacters[character->id()]; } mCharacters[character->id()] = character; return character; } catch (const SerializationException &e) { std::cerr << "Loading " << fileName << " failed" << std::endl; std::cerr << e.what(); } return nullptr; }
bool CharacterService::doesCharacterExits(const std::string &characterId) { UNIQUE_LOCK(mMutex); return mCharacters.find(characterId) != mCharacters.end(); }
std::shared_ptr<Character> CharacterService::character(const std::string &characterId) { UNIQUE_LOCK(mMutex); auto characterIt = mCharacters.find(characterId); if (characterIt != mCharacters.end()) return characterIt->second; return nullptr; }