mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-16 18:59:57 +00:00
Merge branch 'esm4_gmst' into 'master'
Support loading ESM4 GMST records See merge request OpenMW/openmw!2892
This commit is contained in:
commit
aad9ce53db
6 changed files with 137 additions and 1 deletions
|
@ -72,6 +72,17 @@ namespace EsmTool
|
|||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct WriteData
|
||||
{
|
||||
const T& mValue;
|
||||
|
||||
explicit WriteData(const T& value)
|
||||
: mValue(value)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
std::ostream& operator<<(std::ostream& stream, const WriteArray<T>& write)
|
||||
{
|
||||
|
@ -80,6 +91,18 @@ namespace EsmTool
|
|||
return stream;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::ostream& operator<<(std::ostream& stream, const WriteData<T>& /*write*/)
|
||||
{
|
||||
return stream << " ?";
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& stream, const WriteData<ESM4::GameSetting::Data>& write)
|
||||
{
|
||||
std::visit([&](const auto& v) { stream << v; }, write.mValue);
|
||||
return stream;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void readTypedRecord(const Params& params, ESM4::Reader& reader)
|
||||
{
|
||||
|
@ -114,6 +137,8 @@ namespace EsmTool
|
|||
std::cout << "\n Type: " << value.mType;
|
||||
if constexpr (ESM4::hasValue<T>)
|
||||
std::cout << "\n Value: " << value.mValue;
|
||||
if constexpr (ESM4::hasData<T>)
|
||||
std::cout << "\n Data: " << WriteData(value.mData);
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
|
@ -253,7 +278,8 @@ namespace EsmTool
|
|||
readTypedRecord<ESM4::GlobalVariable>(params, reader);
|
||||
return true;
|
||||
case ESM4::REC_GMST:
|
||||
break;
|
||||
readTypedRecord<ESM4::GameSetting>(params, reader);
|
||||
return true;
|
||||
case ESM4::REC_GRAS:
|
||||
readTypedRecord<ESM4::Grass>(params, reader);
|
||||
return true;
|
||||
|
|
|
@ -153,6 +153,7 @@ add_component_dir (esm4
|
|||
loadflst
|
||||
loadfurn
|
||||
loadglob
|
||||
loadgmst
|
||||
loadgras
|
||||
loadgrup
|
||||
loadhair
|
||||
|
|
68
components/esm4/loadgmst.cpp
Normal file
68
components/esm4/loadgmst.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "loadgmst.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "reader.hpp"
|
||||
|
||||
namespace ESM4
|
||||
{
|
||||
namespace
|
||||
{
|
||||
GameSetting::Data readData(FormId formId, std::string_view editorId, Reader& reader)
|
||||
{
|
||||
if (editorId.empty())
|
||||
throw std::runtime_error(
|
||||
"Unknown ESM4 GMST (" + std::to_string(formId) + ") data type: editor id is empty");
|
||||
const char type = editorId[0];
|
||||
switch (type)
|
||||
{
|
||||
case 'i':
|
||||
{
|
||||
std::int32_t value = 0;
|
||||
reader.get(value);
|
||||
return value;
|
||||
}
|
||||
case 'f':
|
||||
{
|
||||
float value = 0;
|
||||
reader.get(value);
|
||||
return value;
|
||||
}
|
||||
case 's':
|
||||
{
|
||||
std::string value;
|
||||
reader.getZString(value);
|
||||
return value;
|
||||
}
|
||||
default:
|
||||
throw std::runtime_error(
|
||||
"Unsupported ESM4 GMST (" + std::to_string(formId) + ") data type: " + std::string(editorId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameSetting::load(Reader& reader)
|
||||
{
|
||||
mFormId = reader.hdr().record.id;
|
||||
reader.adjustFormId(mFormId);
|
||||
mFlags = reader.hdr().record.flags;
|
||||
|
||||
while (reader.getSubRecordHeader())
|
||||
{
|
||||
const ESM4::SubRecordHeader& subHdr = reader.subRecordHeader();
|
||||
switch (subHdr.typeId)
|
||||
{
|
||||
case ESM4::SUB_EDID:
|
||||
reader.getZString(mEditorId);
|
||||
break;
|
||||
case ESM4::SUB_DATA:
|
||||
mData = readData(mFormId, mEditorId, reader);
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error("Unknown ESM4 GMST (" + std::to_string(mFormId) + ") subrecord "
|
||||
+ ESM::printName(subHdr.typeId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
27
components/esm4/loadgmst.hpp
Normal file
27
components/esm4/loadgmst.hpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
#ifndef OPENMW_COMPONENTS_ESM4_LOADGMST_H
|
||||
#define OPENMW_COMPONENTS_ESM4_LOADGMST_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "formid.hpp"
|
||||
|
||||
namespace ESM4
|
||||
{
|
||||
class Reader;
|
||||
|
||||
struct GameSetting
|
||||
{
|
||||
using Data = std::variant<float, std::int32_t, std::string>;
|
||||
|
||||
FormId mFormId; // from the header
|
||||
std::uint32_t mFlags; // from the header, see enum type RecordFlag for details
|
||||
std::string mEditorId;
|
||||
Data mData;
|
||||
|
||||
void load(Reader& reader);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // OPENMW_COMPONENTS_ESM4_LOADGMST_H
|
|
@ -28,6 +28,7 @@
|
|||
#include <components/esm4/loadflst.hpp>
|
||||
#include <components/esm4/loadfurn.hpp>
|
||||
#include <components/esm4/loadglob.hpp>
|
||||
#include <components/esm4/loadgmst.hpp>
|
||||
#include <components/esm4/loadgras.hpp>
|
||||
#include <components/esm4/loadgrup.hpp>
|
||||
#include <components/esm4/loadhair.hpp>
|
||||
|
|
|
@ -121,6 +121,19 @@ namespace ESM4
|
|||
|
||||
template <class T>
|
||||
inline constexpr bool hasValue = HasValue<T>::value;
|
||||
|
||||
template <class T, class = std::void_t<>>
|
||||
struct HasData : std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct HasData<T, std::void_t<decltype(T::mData)>> : std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline constexpr bool hasData = HasData<T>::value;
|
||||
}
|
||||
|
||||
#endif // OPENMW_COMPONENTS_ESM4_TYPETRAITS
|
||||
|
|
Loading…
Reference in a new issue