mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-06 00:55:50 +00:00
3affe9913f
Use LRU cache for ESMReaders. When cache capacity is reached close least recently used ESMReader. Remember the file name if a reader was open. Once the reader requested again open the file if there is stored name for it. Put released ESMReader to the back of the free items list. Close ESMReader's from the front of the free items list. Cached item can be used only by one client at the same time. If the same item is requested twice exception is thrown. This should never happen in practice. If this happens need to fix the client logic. It's allowed to go over the capacity limit when requesting different readers. Ideally this should never happen but there will be system error anyway signalizing about too many open files. Need to fix client logic in this case. All places that were using a vector of ESMReaders now using the cache. Cache is local for each use case and there is no need for a thread safety.
137 lines
5.1 KiB
C++
137 lines
5.1 KiB
C++
#include <components/esm3/loadacti.hpp>
|
|
#include <components/esm3/loadcell.hpp>
|
|
#include <components/esm3/loadcont.hpp>
|
|
#include <components/esm3/loaddoor.hpp>
|
|
#include <components/esm3/loadgmst.hpp>
|
|
#include <components/esm3/loadland.hpp>
|
|
#include <components/esm3/loadstat.hpp>
|
|
#include <components/esmloader/esmdata.hpp>
|
|
#include <components/esmloader/load.hpp>
|
|
#include <components/files/collections.hpp>
|
|
#include <components/files/multidircollection.hpp>
|
|
#include <components/to_utf8/to_utf8.hpp>
|
|
#include <components/esm3/readerscache.hpp>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#ifndef OPENMW_DATA_DIR
|
|
#error "OPENMW_DATA_DIR is not defined"
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
using namespace testing;
|
|
using namespace EsmLoader;
|
|
|
|
struct EsmLoaderTest : Test
|
|
{
|
|
const Files::PathContainer mDataDirs {{std::string(OPENMW_DATA_DIR)}};
|
|
const Files::Collections mFileCollections {mDataDirs, true};
|
|
const std::vector<std::string> mContentFiles {{"template.omwgame"}};
|
|
};
|
|
|
|
TEST_F(EsmLoaderTest, loadEsmDataShouldSupportOmwgame)
|
|
{
|
|
Query query;
|
|
query.mLoadActivators = true;
|
|
query.mLoadCells = true;
|
|
query.mLoadContainers = true;
|
|
query.mLoadDoors = true;
|
|
query.mLoadGameSettings = true;
|
|
query.mLoadLands = true;
|
|
query.mLoadStatics = true;
|
|
ESM::ReadersCache readers;
|
|
ToUTF8::Utf8Encoder* const encoder = nullptr;
|
|
const EsmData esmData = loadEsmData(query, mContentFiles, mFileCollections, readers, encoder);
|
|
EXPECT_EQ(esmData.mActivators.size(), 0);
|
|
EXPECT_EQ(esmData.mCells.size(), 1);
|
|
EXPECT_EQ(esmData.mContainers.size(), 0);
|
|
EXPECT_EQ(esmData.mDoors.size(), 0);
|
|
EXPECT_EQ(esmData.mGameSettings.size(), 1521);
|
|
EXPECT_EQ(esmData.mLands.size(), 1);
|
|
EXPECT_EQ(esmData.mStatics.size(), 2);
|
|
}
|
|
|
|
TEST_F(EsmLoaderTest, shouldIgnoreCellsWhenQueryLoadCellsIsFalse)
|
|
{
|
|
Query query;
|
|
query.mLoadActivators = true;
|
|
query.mLoadCells = false;
|
|
query.mLoadContainers = true;
|
|
query.mLoadDoors = true;
|
|
query.mLoadGameSettings = true;
|
|
query.mLoadLands = true;
|
|
query.mLoadStatics = true;
|
|
ESM::ReadersCache readers;
|
|
ToUTF8::Utf8Encoder* const encoder = nullptr;
|
|
const EsmData esmData = loadEsmData(query, mContentFiles, mFileCollections, readers, encoder);
|
|
EXPECT_EQ(esmData.mActivators.size(), 0);
|
|
EXPECT_EQ(esmData.mCells.size(), 0);
|
|
EXPECT_EQ(esmData.mContainers.size(), 0);
|
|
EXPECT_EQ(esmData.mDoors.size(), 0);
|
|
EXPECT_EQ(esmData.mGameSettings.size(), 1521);
|
|
EXPECT_EQ(esmData.mLands.size(), 1);
|
|
EXPECT_EQ(esmData.mStatics.size(), 2);
|
|
}
|
|
|
|
TEST_F(EsmLoaderTest, shouldIgnoreCellsGameSettingsWhenQueryLoadGameSettingsIsFalse)
|
|
{
|
|
Query query;
|
|
query.mLoadActivators = true;
|
|
query.mLoadCells = true;
|
|
query.mLoadContainers = true;
|
|
query.mLoadDoors = true;
|
|
query.mLoadGameSettings = false;
|
|
query.mLoadLands = true;
|
|
query.mLoadStatics = true;
|
|
ESM::ReadersCache readers;
|
|
ToUTF8::Utf8Encoder* const encoder = nullptr;
|
|
const EsmData esmData = loadEsmData(query, mContentFiles, mFileCollections, readers, encoder);
|
|
EXPECT_EQ(esmData.mActivators.size(), 0);
|
|
EXPECT_EQ(esmData.mCells.size(), 1);
|
|
EXPECT_EQ(esmData.mContainers.size(), 0);
|
|
EXPECT_EQ(esmData.mDoors.size(), 0);
|
|
EXPECT_EQ(esmData.mGameSettings.size(), 0);
|
|
EXPECT_EQ(esmData.mLands.size(), 1);
|
|
EXPECT_EQ(esmData.mStatics.size(), 2);
|
|
}
|
|
|
|
TEST_F(EsmLoaderTest, shouldIgnoreAllWithDefaultQuery)
|
|
{
|
|
const Query query;
|
|
ESM::ReadersCache readers;
|
|
ToUTF8::Utf8Encoder* const encoder = nullptr;
|
|
const EsmData esmData = loadEsmData(query, mContentFiles, mFileCollections, readers, encoder);
|
|
EXPECT_EQ(esmData.mActivators.size(), 0);
|
|
EXPECT_EQ(esmData.mCells.size(), 0);
|
|
EXPECT_EQ(esmData.mContainers.size(), 0);
|
|
EXPECT_EQ(esmData.mDoors.size(), 0);
|
|
EXPECT_EQ(esmData.mGameSettings.size(), 0);
|
|
EXPECT_EQ(esmData.mLands.size(), 0);
|
|
EXPECT_EQ(esmData.mStatics.size(), 0);
|
|
}
|
|
|
|
TEST_F(EsmLoaderTest, loadEsmDataShouldSkipUnsupportedFormats)
|
|
{
|
|
Query query;
|
|
query.mLoadActivators = true;
|
|
query.mLoadCells = true;
|
|
query.mLoadContainers = true;
|
|
query.mLoadDoors = true;
|
|
query.mLoadGameSettings = true;
|
|
query.mLoadLands = true;
|
|
query.mLoadStatics = true;
|
|
const std::vector<std::string> contentFiles {{"script.omwscripts"}};
|
|
ESM::ReadersCache readers;
|
|
ToUTF8::Utf8Encoder* const encoder = nullptr;
|
|
const EsmData esmData = loadEsmData(query, contentFiles, mFileCollections, readers, encoder);
|
|
EXPECT_EQ(esmData.mActivators.size(), 0);
|
|
EXPECT_EQ(esmData.mCells.size(), 0);
|
|
EXPECT_EQ(esmData.mContainers.size(), 0);
|
|
EXPECT_EQ(esmData.mDoors.size(), 0);
|
|
EXPECT_EQ(esmData.mGameSettings.size(), 0);
|
|
EXPECT_EQ(esmData.mLands.size(), 0);
|
|
EXPECT_EQ(esmData.mStatics.size(), 0);
|
|
}
|
|
}
|