better geode version comparison & handling

This commit is contained in:
altalk23 2024-01-31 00:47:30 +03:00
parent c348fc8a55
commit d9c65b37ea
10 changed files with 136 additions and 26 deletions

View file

@ -35,7 +35,9 @@ namespace geode {
EnableFailed,
MissingDependency,
PresentIncompatibility,
UnzipFailed
UnzipFailed,
UnsupportedVersion,
UnsupportedGeodeVersion,
};
Type type;
std::variant<ghc::filesystem::path, ModMetadata, Mod*> cause;

View file

@ -161,6 +161,11 @@ namespace geode {
*/
[[nodiscard]] std::optional<std::string> getGameVersion() const;
/**
* Gets the target Geode version for the current platform.
*/
[[nodiscard]] VersionInfo getGeodeVersion() const;
/**
* Checks if mod can be installed on the current GD version.
* Returns Ok() if it can, Err otherwise.

View file

@ -228,6 +228,8 @@ namespace geode {
std::string toString() const;
friend GEODE_DLL std::string format_as(ComparableVersionInfo const& version);
};
bool GEODE_DLL semverCompare(VersionInfo const& current, VersionInfo const& target);
}
template <class V>

View file

@ -148,10 +148,8 @@ VersionInfo Loader::Impl::maxModVersion() {
};
}
bool Loader::Impl::isModVersionSupported(VersionInfo const& version) {
return
version >= this->minModVersion() &&
version <= this->maxModVersion();
bool Loader::Impl::isModVersionSupported(VersionInfo const& target) {
return semverCompare(this->getVersion(), target);
}
// Data saving
@ -421,6 +419,37 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
m_refreshingModCount -= 1;
};
{ // version checking
auto res = node->getMetadata().checkGameVersion();
if (!res) {
m_problems.push_back({
LoadProblem::Type::UnsupportedVersion,
node,
res.unwrapErr()
});
log::error("Unsupported game version: {}", res.unwrapErr());
m_refreshingModCount -= 1;
log::popNest();
return;
}
if (!this->isModVersionSupported(node->getMetadata().getGeodeVersion())) {
m_problems.push_back({
LoadProblem::Type::UnsupportedGeodeVersion,
node,
fmt::format(
"Geode version {} is not supported (current: {})",
node->getMetadata().getGeodeVersion().toString(),
this->getVersion().toString()
)
});
log::error("Unsupported Geode version: {}", node->getMetadata().getGeodeVersion());
m_refreshingModCount -= 1;
log::popNest();
return;
}
}
if (early) {
auto res = unzipFunction();
if (!res) {

View file

@ -357,8 +357,6 @@ bool Mod::Impl::getLaunchFlag(std::string_view const name) const {
// Loading, Toggling, Installing
Result<> Mod::Impl::loadBinary() {
// i dont know where to put this so ill just plop it here
GEODE_UNWRAP(m_metadata.checkGameVersion());
log::debug("Loading binary for mod {}", m_metadata.getID());
if (m_enabled)

View file

@ -67,7 +67,7 @@ Result<ModMetadata> ModMetadata::Impl::createFromSchemaV010(ModJson const& rawJs
JsonChecker checker(impl->m_rawJSON);
auto root = checker.root(checkerRoot).obj();
root.addKnownKey("geode");
root.needs("geode").into(impl->m_geodeVersion);
root.addKnownKey("gd");
// Check GD version
@ -194,23 +194,23 @@ Result<ModMetadata> ModMetadata::Impl::create(ModJson const& json) {
"specified, or its formatting is invalid (required: \"[v]X.X.X\")!"
);
}
if (schema < Loader::get()->minModVersion()) {
return Err(
"[mod.json] is built for an older version (" + schema.toString() +
") of Geode (current: " + Loader::get()->getVersion().toString() +
"). Please update the mod to the latest version, "
"and if the problem persists, contact the developer "
"to update it."
);
}
if (schema > Loader::get()->maxModVersion()) {
return Err(
"[mod.json] is built for a newer version (" + schema.toString() +
") of Geode (current: " + Loader::get()->getVersion().toString() +
"). You need to update Geode in order to use "
"this mod."
);
}
// if (schema < Loader::get()->minModVersion()) {
// return Err(
// "[mod.json] is built for an older version (" + schema.toString() +
// ") of Geode (current: " + Loader::get()->getVersion().toString() +
// "). Please update the mod to the latest version, "
// "and if the problem persists, contact the developer "
// "to update it."
// );
// }
// if (schema > Loader::get()->maxModVersion()) {
// return Err(
// "[mod.json] is built for a newer version (" + schema.toString() +
// ") of Geode (current: " + Loader::get()->getVersion().toString() +
// "). You need to update Geode in order to use "
// "this mod."
// );
// }
// Handle mod.json data based on target
if (schema < VersionInfo(0, 1, 0)) {
@ -407,6 +407,10 @@ std::optional<std::string> ModMetadata::getGameVersion() const {
return m_impl->m_gdVersion;
}
VersionInfo ModMetadata::getGeodeVersion() const {
return m_impl->m_geodeVersion;
}
Result<> ModMetadata::checkGameVersion() const {
if (!m_impl->m_gdVersion.empty()) {
auto const ver = m_impl->m_gdVersion;

View file

@ -17,6 +17,7 @@ namespace geode {
std::string m_name;
std::string m_developer;
std::string m_gdVersion;
VersionInfo m_geodeVersion;
std::optional<std::string> m_description;
std::optional<std::string> m_details;
std::optional<std::string> m_changelog;

View file

@ -95,6 +95,16 @@ bool ProblemsListCell::init(LoadProblem problem, ProblemsListPopup* list, CCSize
message = fmt::format("{} has failed unzipping", cause);
m_longMessage = problem.message;
break;
case LoadProblem::Type::UnsupportedVersion:
icon = "info-alert.png"_spr;
message = fmt::format("{} is incompatible with this version of GD", cause);
m_longMessage = problem.message;
break;
case LoadProblem::Type::UnsupportedGeodeVersion:
icon = "info-alert.png"_spr;
message = fmt::format("{} is incompatible with this version of Geode", cause);
m_longMessage = problem.message;
break;
}
m_problem = std::move(problem);

View file

@ -182,3 +182,62 @@ std::string ComparableVersionInfo::toString() const {
std::string geode::format_as(ComparableVersionInfo const& version) {
return version.toString();
}
bool geode::semverCompare(VersionInfo const& current, VersionInfo const& target) {
if (target.getMajor() != current.getMajor()) {
return false;
}
if (target.getMinor() > current.getMinor()) {
return false;
}
auto ct = current.getTag();
auto tt = target.getTag();
if (ct && tt) {
auto currentTag = ct.value();
auto targetTag = tt.value();
switch (targetTag.value) {
case VersionTag::Alpha:
if (currentTag.value > VersionTag::Alpha) {
return false;
}
if (currentTag.number && targetTag.number) {
return currentTag.number.value() == targetTag.number.value();
}
if (currentTag.number) {
return true;
}
if (targetTag.number) {
return false;
}
return true;
case VersionTag::Beta:
if (currentTag.number && targetTag.number) {
return currentTag.number.value() >= targetTag.number.value();
}
if (currentTag.number) {
return true;
}
if (targetTag.number) {
return false;
}
return true;
default:
return true;
}
}
else if (ct) {
auto currentTag = ct.value();
if (currentTag.value > VersionTag::Alpha) {
return true;
}
return false;
}
else if (tt) {
auto targetTag = tt.value();
if (targetTag.value > VersionTag::Alpha) {
return true;
}
return false;
}
return true;
}

View file

@ -1,6 +1,6 @@
{
"geode": "2.0.0",
"gd": "*",
"gd": "*",
"version": "1.0.0",
"id": "geode.testdep",
"name": "Geode Test Dependency",