Load object version into a tuple

This commit is contained in:
spacek531 2023-01-07 14:25:39 -08:00 committed by GitHub
parent a0b4f1b2d2
commit c82c4ca3a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 8 deletions

View file

@ -329,6 +329,54 @@ std::unique_ptr<IStream> ObjectAsset::GetStream() const
return {};
}
u8string VersionString(const ObjectVersion& version)
{
return std::to_string(std::get<0>(version)) + "." + std::to_string(std::get<1>(version)) + "."
+ std::to_string(std::get<2>(version));
}
ObjectVersion VersionTuple(std::string_view version)
{
if (version.empty())
{
return std::make_tuple(0, 0, 0);
}
auto nums = String::Split(version, ".");
uint16_t versions[VersionNumFields] = {};
if (nums.size() > VersionNumFields)
{
log_warning("%i fields found in version string '%s', expected X.Y.Z", nums.size(), version);
}
if (nums.size() == 0)
{
log_warning("No fields found in version string '%s', expected X.Y.Z", version);
return std::make_tuple(0, 0, 0);
}
try
{
size_t highestIndex = std::min(nums.size(), VersionNumFields);
for (size_t i = 0; i < highestIndex; i++)
{
auto value = stoi(nums.at(i));
constexpr auto maxValue = std::numeric_limits<uint16_t>().max();
if (value > maxValue)
{
log_warning(
"Version value too high in version string '%s', version value will be capped to %i.", version, maxValue);
value = maxValue;
}
versions[i] = value;
}
}
catch (const std::exception&)
{
log_warning("Malformed version string '%s', expected X.Y.Z", version);
}
return std::make_tuple(versions[0], versions[1], versions[2]);
}
#ifdef __WARN_SUGGEST_FINAL_METHODS__
# pragma GCC diagnostic pop
#endif

View file

@ -29,6 +29,10 @@ constexpr const ObjectEntryIndex OBJECT_ENTRY_INDEX_NULL = std::numeric_limits<O
struct ObjectRepositoryItem;
using ride_type_t = uint16_t;
constexpr const size_t VersionNumFields = 3;
using ObjectVersion = std::tuple<uint16_t, uint16_t, uint16_t>;
static_assert(std::tuple_size<ObjectVersion>{} == VersionNumFields);
// First 0xF of rct_object_entry->flags
enum class ObjectType : uint8_t
{
@ -192,7 +196,7 @@ struct ObjectEntryDescriptor
// JSON
ObjectType Type{};
std::string Identifier;
std::string Version;
ObjectVersion Version;
ObjectEntryDescriptor() = default;
explicit ObjectEntryDescriptor(const rct_object_entry& newEntry);
@ -250,7 +254,7 @@ class Object
{
private:
std::string _identifier;
std::string _version;
ObjectVersion _version;
ObjectEntryDescriptor _descriptor{};
StringTable _stringTable;
ImageTable _imageTable;
@ -364,11 +368,11 @@ public:
const std::vector<std::string>& GetAuthors() const;
void SetAuthors(std::vector<std::string>&& authors);
const std::string& GetVersion() const
const ObjectVersion& GetVersion() const
{
return _version;
}
void SetVersion(const std::string& version)
void SetVersion(const ObjectVersion& version)
{
_version = version;
}
@ -406,3 +410,6 @@ constexpr bool IsIntransientObjectType(ObjectType type)
{
return type == ObjectType::Audio;
}
u8string VersionString(const ObjectVersion& version);
ObjectVersion VersionTuple(std::string_view version);

View file

@ -521,7 +521,7 @@ namespace ObjectFactory
if (id == OpenRCT2::Audio::AudioObjectIdentifiers::Rct2cBase)
id = OpenRCT2::Audio::AudioObjectIdentifiers::Rct2Base;
auto version = Json::GetString(jRoot["version"]);
auto version = VersionTuple(Json::GetString(jRoot["version"]));
ObjectEntryDescriptor descriptor;
auto originalId = Json::GetString(jRoot["originalId"]);
if (originalId.length() == 8 + 1 + 8 + 1 + 8)

View file

@ -43,7 +43,7 @@ struct ObjectRepositoryItem
rct_object_entry ObjectEntry;
std::string Path;
std::string Name;
std::string Version;
ObjectVersion Version;
std::vector<std::string> Authors;
std::vector<ObjectSourceGame> Sources;
std::shared_ptr<Object> LoadedObject{};

View file

@ -346,7 +346,7 @@ namespace OpenRCT2
}
}
desc.Identifier = identifier;
desc.Version = cs.Read<std::string>();
desc.Version = VersionTuple(cs.Read<std::string>());
if (version <= 2)
{
@ -395,7 +395,7 @@ namespace OpenRCT2
{
cs.Write(DESCRIPTOR_JSON);
cs.Write(entry.Identifier);
cs.Write(entry.Version);
cs.Write(VersionString(entry.Version));
}
else
{