Support loading ESM4 GMST records

depth-refraction
elsid 2 years ago
parent a9c7354338
commit 87ac85223a
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -72,6 +72,17 @@ namespace EsmTool
} }
}; };
template <class T>
struct WriteData
{
const T& mValue;
explicit WriteData(const T& value)
: mValue(value)
{
}
};
template <class T> template <class T>
std::ostream& operator<<(std::ostream& stream, const WriteArray<T>& write) std::ostream& operator<<(std::ostream& stream, const WriteArray<T>& write)
{ {
@ -80,6 +91,18 @@ namespace EsmTool
return stream; 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> template <class T>
void readTypedRecord(const Params& params, ESM4::Reader& reader) void readTypedRecord(const Params& params, ESM4::Reader& reader)
{ {
@ -114,6 +137,8 @@ namespace EsmTool
std::cout << "\n Type: " << value.mType; std::cout << "\n Type: " << value.mType;
if constexpr (ESM4::hasValue<T>) if constexpr (ESM4::hasValue<T>)
std::cout << "\n Value: " << value.mValue; std::cout << "\n Value: " << value.mValue;
if constexpr (ESM4::hasData<T>)
std::cout << "\n Data: " << WriteData(value.mData);
std::cout << '\n'; std::cout << '\n';
} }
@ -253,7 +278,8 @@ namespace EsmTool
readTypedRecord<ESM4::GlobalVariable>(params, reader); readTypedRecord<ESM4::GlobalVariable>(params, reader);
return true; return true;
case ESM4::REC_GMST: case ESM4::REC_GMST:
break; readTypedRecord<ESM4::GameSetting>(params, reader);
return true;
case ESM4::REC_GRAS: case ESM4::REC_GRAS:
readTypedRecord<ESM4::Grass>(params, reader); readTypedRecord<ESM4::Grass>(params, reader);
return true; return true;

@ -151,6 +151,7 @@ add_component_dir (esm4
loadflst loadflst
loadfurn loadfurn
loadglob loadglob
loadgmst
loadgras loadgras
loadgrup loadgrup
loadhair loadhair

@ -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));
}
}
}
}

@ -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/loadflst.hpp>
#include <components/esm4/loadfurn.hpp> #include <components/esm4/loadfurn.hpp>
#include <components/esm4/loadglob.hpp> #include <components/esm4/loadglob.hpp>
#include <components/esm4/loadgmst.hpp>
#include <components/esm4/loadgras.hpp> #include <components/esm4/loadgras.hpp>
#include <components/esm4/loadgrup.hpp> #include <components/esm4/loadgrup.hpp>
#include <components/esm4/loadhair.hpp> #include <components/esm4/loadhair.hpp>

@ -121,6 +121,19 @@ namespace ESM4
template <class T> template <class T>
inline constexpr bool hasValue = HasValue<T>::value; 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 #endif // OPENMW_COMPONENTS_ESM4_TYPETRAITS

Loading…
Cancel
Save