Simplify material file pointer acrobatics

pull/3235/head
Alexei Kotov 8 months ago
parent 023c0f7a46
commit 78eda530ac

@ -8,7 +8,7 @@
#include <utility>
#include <vector>
#include <components/bgsm/reader.hpp>
#include <components/bgsm/file.hpp>
#include <components/files/configurationmanager.hpp>
#include <components/files/constrainedfilestream.hpp>
#include <components/files/conversion.hpp>
@ -88,11 +88,10 @@ void readFile(
}
else
{
Bgsm::Reader reader;
if (vfs != nullptr)
reader.parse(vfs->get(pathStr));
Bgsm::parse(vfs->get(pathStr));
else
reader.parse(Files::openConstrainedFileStream(fullPath));
Bgsm::parse(Files::openConstrainedFileStream(fullPath));
}
}
catch (std::exception& e)

@ -108,7 +108,7 @@ add_component_dir (settings
)
add_component_dir (bgsm
reader stream file
stream file
)
add_component_dir (bsa

@ -1,9 +1,37 @@
#include "file.hpp"
#include <array>
#include <stdexcept>
#include "stream.hpp"
namespace Bgsm
{
MaterialFilePtr parse(Files::IStreamPtr&& inputStream)
{
std::shared_ptr<Bgsm::MaterialFile> file;
BGSMStream stream(std::move(inputStream));
std::array<char, 4> signature;
stream.readArray(signature);
std::string shaderType(signature.data(), 4);
if (shaderType == "BGEM")
{
file = std::make_shared<BGEMFile>();
file->mShaderType = Bgsm::ShaderType::Effect;
}
else if (shaderType == "BGSM")
{
file = std::make_shared<BGSMFile>();
file->mShaderType = Bgsm::ShaderType::Lighting;
}
else
throw std::runtime_error("Invalid material file");
file->read(stream);
return file;
}
void MaterialFile::read(BGSMStream& stream)
{
stream.read(mVersion);

@ -10,6 +10,8 @@
#include <osg/Vec3f>
#include <osg/Vec4f>
#include <components/files/istreamptr.hpp>
namespace Bgsm
{
class BGSMStream;
@ -160,5 +162,6 @@ namespace Bgsm
};
using MaterialFilePtr = std::shared_ptr<const Bgsm::MaterialFile>;
MaterialFilePtr parse(Files::IStreamPtr&& stream);
}
#endif

@ -1,33 +0,0 @@
#include "reader.hpp"
#include <array>
#include <stdexcept>
#include <string>
#include "stream.hpp"
namespace Bgsm
{
void Reader::parse(Files::IStreamPtr&& inputStream)
{
BGSMStream stream(std::move(inputStream));
std::array<char, 4> signature;
stream.readArray(signature);
std::string shaderType(signature.data(), 4);
if (shaderType == "BGEM")
{
mFile = std::make_unique<BGEMFile>();
mFile->mShaderType = Bgsm::ShaderType::Effect;
}
else if (shaderType == "BGSM")
{
mFile = std::make_unique<BGSMFile>();
mFile->mShaderType = Bgsm::ShaderType::Lighting;
}
else
throw std::runtime_error("Invalid material file");
mFile->read(stream);
}
}

@ -1,22 +0,0 @@
#ifndef OPENMW_COMPONENTS_BGSM_READER_HPP
#define OPENMW_COMPONENTS_BGSM_READER_HPP
#include <memory>
#include <components/files/istreamptr.hpp>
#include "file.hpp"
namespace Bgsm
{
class Reader
{
std::unique_ptr<MaterialFile> mFile;
public:
void parse(Files::IStreamPtr&& stream);
std::unique_ptr<MaterialFile> getFile() { return std::move(mFile); }
};
}
#endif

@ -2,7 +2,6 @@
#include <osg/Object>
#include <components/bgsm/reader.hpp>
#include <components/vfs/manager.hpp>
#include "objectcache.hpp"
@ -41,9 +40,7 @@ namespace Resource
return static_cast<BgsmFileHolder*>(obj.get())->mBgsmFile;
else
{
Bgsm::Reader reader;
reader.parse(mVFS->get(name));
Bgsm::MaterialFilePtr file = reader.getFile();
Bgsm::MaterialFilePtr file = Bgsm::parse(mVFS->get(name));
obj = new BgsmFileHolder(file);
mCache->addEntryToObjectCache(name.value(), obj);
return file;

Loading…
Cancel
Save