Convert strings in nif files to utf8

ini_importer_tests
Evil Eye 12 months ago
parent 5ae878c248
commit 27fa411f4f

@ -174,7 +174,7 @@ namespace
constexpr double expiryDelay = 0; constexpr double expiryDelay = 0;
Resource::ImageManager imageManager(&vfs, expiryDelay); Resource::ImageManager imageManager(&vfs, expiryDelay);
Resource::NifFileManager nifFileManager(&vfs); Resource::NifFileManager nifFileManager(&vfs, &encoder);
Resource::SceneManager sceneManager(&vfs, &imageManager, &nifFileManager, expiryDelay); Resource::SceneManager sceneManager(&vfs, &imageManager, &nifFileManager, expiryDelay);
Resource::BulletShapeManager bulletShapeManager(&vfs, &sceneManager, &nifFileManager, expiryDelay); Resource::BulletShapeManager bulletShapeManager(&vfs, &sceneManager, &nifFileManager, expiryDelay);

@ -221,7 +221,7 @@ namespace NavMeshTool
constexpr double expiryDelay = 0; constexpr double expiryDelay = 0;
Resource::ImageManager imageManager(&vfs, expiryDelay); Resource::ImageManager imageManager(&vfs, expiryDelay);
Resource::NifFileManager nifFileManager(&vfs); Resource::NifFileManager nifFileManager(&vfs, &encoder);
Resource::SceneManager sceneManager(&vfs, &imageManager, &nifFileManager, expiryDelay); Resource::SceneManager sceneManager(&vfs, &imageManager, &nifFileManager, expiryDelay);
Resource::BulletShapeManager bulletShapeManager(&vfs, &sceneManager, &nifFileManager, expiryDelay); Resource::BulletShapeManager bulletShapeManager(&vfs, &sceneManager, &nifFileManager, expiryDelay);
DetourNavigator::RecastGlobalAllocator::init(); DetourNavigator::RecastGlobalAllocator::init();

@ -84,7 +84,7 @@ void readNIF(
try try
{ {
Nif::NIFFile file(fullPath); Nif::NIFFile file(fullPath);
Nif::Reader reader(file); Nif::Reader reader(file, nullptr);
if (vfs != nullptr) if (vfs != nullptr)
reader.parse(vfs->get(pathStr)); reader.parse(vfs->get(pathStr));
else else

@ -149,7 +149,7 @@ CSMWorld::Data::Data(ToUTF8::FromType encoding, const Files::PathContainer& data
mResourcesManager.setVFS(mVFS.get()); mResourcesManager.setVFS(mVFS.get());
constexpr double expiryDelay = 0; constexpr double expiryDelay = 0;
mResourceSystem = std::make_unique<Resource::ResourceSystem>(mVFS.get(), expiryDelay); mResourceSystem = std::make_unique<Resource::ResourceSystem>(mVFS.get(), expiryDelay, &mEncoder);
Shader::ShaderManager::DefineMap defines Shader::ShaderManager::DefineMap defines
= mResourceSystem->getSceneManager()->getShaderManager().getGlobalDefines(); = mResourceSystem->getSceneManager()->getShaderManager().getGlobalDefines();

@ -706,7 +706,8 @@ void OMW::Engine::prepareEngine()
VFS::registerArchives(mVFS.get(), mFileCollections, mArchives, true); VFS::registerArchives(mVFS.get(), mFileCollections, mArchives, true);
mResourceSystem = std::make_unique<Resource::ResourceSystem>(mVFS.get(), Settings::cells().mCacheExpiryDelay); mResourceSystem
= std::make_unique<Resource::ResourceSystem>(mVFS.get(), Settings::cells().mCacheExpiryDelay, mEncoder.get());
mResourceSystem->getSceneManager()->getShaderManager().setMaxTextureUnits(mGlMaxTextureImageUnits); mResourceSystem->getSceneManager()->getShaderManager().setMaxTextureUnits(mGlMaxTextureImageUnits);
mResourceSystem->getSceneManager()->setUnRefImageDataAfterApply( mResourceSystem->getSceneManager()->setUnRefImageDataAfterApply(
false); // keep to Off for now to allow better state sharing false); // keep to Off for now to allow better state sharing

@ -24,7 +24,7 @@
namespace Nif namespace Nif
{ {
Reader::Reader(NIFFile& file) Reader::Reader(NIFFile& file, const ToUTF8::Utf8Encoder* encoder)
: mVersion(file.mVersion) : mVersion(file.mVersion)
, mUserVersion(file.mUserVersion) , mUserVersion(file.mUserVersion)
, mBethVersion(file.mBethVersion) , mBethVersion(file.mBethVersion)
@ -33,6 +33,7 @@ namespace Nif
, mRecords(file.mRecords) , mRecords(file.mRecords)
, mRoots(file.mRoots) , mRoots(file.mRoots)
, mUseSkinning(file.mUseSkinning) , mUseSkinning(file.mUseSkinning)
, mEncoder(encoder)
{ {
} }
@ -519,7 +520,7 @@ namespace Nif
const std::array<std::uint64_t, 2> fileHash = Files::getHash(mFilename, *stream); const std::array<std::uint64_t, 2> fileHash = Files::getHash(mFilename, *stream);
mHash.append(reinterpret_cast<const char*>(fileHash.data()), fileHash.size() * sizeof(std::uint64_t)); mHash.append(reinterpret_cast<const char*>(fileHash.data()), fileHash.size() * sizeof(std::uint64_t));
NIFStream nif(*this, std::move(stream)); NIFStream nif(*this, std::move(stream), mEncoder);
// Check the header string // Check the header string
std::string head = nif.getVersionString(); std::string head = nif.getVersionString();

@ -11,6 +11,11 @@
#include "record.hpp" #include "record.hpp"
namespace ToUTF8
{
class Utf8Encoder;
}
namespace Nif namespace Nif
{ {
@ -112,6 +117,7 @@ namespace Nif
std::vector<std::string> mStrings; std::vector<std::string> mStrings;
bool& mUseSkinning; bool& mUseSkinning;
const ToUTF8::Utf8Encoder* mEncoder;
static std::atomic_bool sLoadUnsupportedFiles; static std::atomic_bool sLoadUnsupportedFiles;
static std::atomic_bool sWriteNifDebugLog; static std::atomic_bool sWriteNifDebugLog;
@ -122,7 +128,7 @@ namespace Nif
public: public:
/// Open a NIF stream. The name is used for error messages. /// Open a NIF stream. The name is used for error messages.
explicit Reader(NIFFile& file); Reader(NIFFile& file, const ToUTF8::Utf8Encoder* encoder);
/// Parse the file /// Parse the file
void parse(Files::IStreamPtr&& stream); void parse(Files::IStreamPtr&& stream);

@ -4,6 +4,8 @@
#include "niffile.hpp" #include "niffile.hpp"
#include "../to_utf8/to_utf8.hpp"
namespace namespace
{ {
@ -58,6 +60,8 @@ namespace Nif
size_t end = str.find('\0'); size_t end = str.find('\0');
if (end != std::string::npos) if (end != std::string::npos)
str.erase(end); str.erase(end);
if (mEncoder)
str = mEncoder->getStatelessEncoder().getUtf8(str, ToUTF8::BufferAllocationPolicy::UseGrowFactor, mBuffer);
return str; return str;
} }

@ -8,6 +8,7 @@
#include <istream> #include <istream>
#include <stdexcept> #include <stdexcept>
#include <stdint.h> #include <stdint.h>
#include <string>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
@ -22,6 +23,11 @@
#include "niftypes.hpp" #include "niftypes.hpp"
namespace ToUTF8
{
class Utf8Encoder;
}
namespace Nif namespace Nif
{ {
@ -67,11 +73,14 @@ namespace Nif
{ {
const Reader& mReader; const Reader& mReader;
Files::IStreamPtr mStream; Files::IStreamPtr mStream;
const ToUTF8::Utf8Encoder* mEncoder;
std::string mBuffer;
public: public:
explicit NIFStream(const Reader& reader, Files::IStreamPtr&& stream) explicit NIFStream(const Reader& reader, Files::IStreamPtr&& stream, const ToUTF8::Utf8Encoder* encoder)
: mReader(reader) : mReader(reader)
, mStream(std::move(stream)) , mStream(std::move(stream))
, mEncoder(encoder)
{ {
} }

@ -207,9 +207,11 @@ namespace Resource
namespace Resource namespace Resource
{ {
KeyframeManager::KeyframeManager(const VFS::Manager* vfs, SceneManager* sceneManager, double expiryDelay) KeyframeManager::KeyframeManager(
const VFS::Manager* vfs, SceneManager* sceneManager, double expiryDelay, const ToUTF8::Utf8Encoder* encoder)
: ResourceManager(vfs, expiryDelay) : ResourceManager(vfs, expiryDelay)
, mSceneManager(sceneManager) , mSceneManager(sceneManager)
, mEncoder(encoder)
{ {
} }
@ -226,7 +228,7 @@ namespace Resource
if (Misc::getFileExtension(normalized) == "kf") if (Misc::getFileExtension(normalized) == "kf")
{ {
auto file = std::make_shared<Nif::NIFFile>(normalized); auto file = std::make_shared<Nif::NIFFile>(normalized);
Nif::Reader reader(*file); Nif::Reader reader(*file, mEncoder);
reader.parse(mVFS->getNormalized(normalized)); reader.parse(mVFS->getNormalized(normalized));
NifOsg::Loader::loadKf(*file, *loaded.get()); NifOsg::Loader::loadKf(*file, *loaded.get());
} }

@ -9,6 +9,11 @@
#include "resourcemanager.hpp" #include "resourcemanager.hpp"
namespace ToUTF8
{
class Utf8Encoder;
}
namespace Resource namespace Resource
{ {
/// @brief extract animations from OSG formats to OpenMW's animation system /// @brief extract animations from OSG formats to OpenMW's animation system
@ -48,7 +53,8 @@ namespace Resource
class KeyframeManager : public ResourceManager class KeyframeManager : public ResourceManager
{ {
public: public:
explicit KeyframeManager(const VFS::Manager* vfs, SceneManager* sceneManager, double expiryDelay); explicit KeyframeManager(const VFS::Manager* vfs, SceneManager* sceneManager, double expiryDelay,
const ToUTF8::Utf8Encoder* encoder);
~KeyframeManager() = default; ~KeyframeManager() = default;
/// Retrieve a read-only keyframe resource by name (case-insensitive). /// Retrieve a read-only keyframe resource by name (case-insensitive).
@ -59,6 +65,7 @@ namespace Resource
private: private:
SceneManager* mSceneManager; SceneManager* mSceneManager;
const ToUTF8::Utf8Encoder* mEncoder;
}; };
} }

@ -24,21 +24,22 @@ namespace Resource
{ {
} }
NifFileHolder() {} NifFileHolder() = default;
META_Object(Resource, NifFileHolder) META_Object(Resource, NifFileHolder)
Nif::NIFFilePtr mNifFile; Nif::NIFFilePtr mNifFile;
}; };
NifFileManager::NifFileManager(const VFS::Manager* vfs) NifFileManager::NifFileManager(const VFS::Manager* vfs, const ToUTF8::Utf8Encoder* encoder)
// NIF files aren't needed any more once the converted objects are cached in SceneManager / BulletShapeManager, // NIF files aren't needed any more once the converted objects are cached in SceneManager / BulletShapeManager,
// so no point in using an expiry delay. // so no point in using an expiry delay.
: ResourceManager(vfs, 0) : ResourceManager(vfs, 0)
, mEncoder(encoder)
{ {
} }
NifFileManager::~NifFileManager() {} NifFileManager::~NifFileManager() = default;
Nif::NIFFilePtr NifFileManager::get(const std::string& name) Nif::NIFFilePtr NifFileManager::get(const std::string& name)
{ {
@ -48,7 +49,7 @@ namespace Resource
else else
{ {
auto file = std::make_shared<Nif::NIFFile>(name); auto file = std::make_shared<Nif::NIFFile>(name);
Nif::Reader reader(*file); Nif::Reader reader(*file, mEncoder);
reader.parse(mVFS->get(name)); reader.parse(mVFS->get(name));
obj = new NifFileHolder(file); obj = new NifFileHolder(file);
mCache->addEntryToObjectCache(name, obj); mCache->addEntryToObjectCache(name, obj);

@ -5,6 +5,11 @@
#include "resourcemanager.hpp" #include "resourcemanager.hpp"
namespace ToUTF8
{
class Utf8Encoder;
}
namespace Resource namespace Resource
{ {
@ -12,8 +17,10 @@ namespace Resource
/// @note May be used from any thread. /// @note May be used from any thread.
class NifFileManager : public ResourceManager class NifFileManager : public ResourceManager
{ {
const ToUTF8::Utf8Encoder* mEncoder;
public: public:
NifFileManager(const VFS::Manager* vfs); NifFileManager(const VFS::Manager* vfs, const ToUTF8::Utf8Encoder* encoder);
~NifFileManager(); ~NifFileManager();
/// Retrieve a NIF file from the cache, or load it from the VFS if not cached yet. /// Retrieve a NIF file from the cache, or load it from the VFS if not cached yet.

@ -10,13 +10,13 @@
namespace Resource namespace Resource
{ {
ResourceSystem::ResourceSystem(const VFS::Manager* vfs, double expiryDelay) ResourceSystem::ResourceSystem(const VFS::Manager* vfs, double expiryDelay, const ToUTF8::Utf8Encoder* encoder)
: mVFS(vfs) : mVFS(vfs)
{ {
mNifFileManager = std::make_unique<NifFileManager>(vfs); mNifFileManager = std::make_unique<NifFileManager>(vfs, encoder);
mImageManager = std::make_unique<ImageManager>(vfs, expiryDelay); mImageManager = std::make_unique<ImageManager>(vfs, expiryDelay);
mSceneManager = std::make_unique<SceneManager>(vfs, mImageManager.get(), mNifFileManager.get(), expiryDelay); mSceneManager = std::make_unique<SceneManager>(vfs, mImageManager.get(), mNifFileManager.get(), expiryDelay);
mKeyframeManager = std::make_unique<KeyframeManager>(vfs, mSceneManager.get(), expiryDelay); mKeyframeManager = std::make_unique<KeyframeManager>(vfs, mSceneManager.get(), expiryDelay, encoder);
addResourceManager(mNifFileManager.get()); addResourceManager(mNifFileManager.get());
addResourceManager(mKeyframeManager.get()); addResourceManager(mKeyframeManager.get());

@ -15,6 +15,11 @@ namespace osg
class State; class State;
} }
namespace ToUTF8
{
class Utf8Encoder;
}
namespace Resource namespace Resource
{ {
@ -30,7 +35,7 @@ namespace Resource
class ResourceSystem class ResourceSystem
{ {
public: public:
explicit ResourceSystem(const VFS::Manager* vfs, double expiryDelay); explicit ResourceSystem(const VFS::Manager* vfs, double expiryDelay, const ToUTF8::Utf8Encoder* encoder);
~ResourceSystem(); ~ResourceSystem();
SceneManager* getSceneManager(); SceneManager* getSceneManager();

Loading…
Cancel
Save