Skip to content

Commit

Permalink
feat: add feature version detection (#180)
Browse files Browse the repository at this point in the history
Co-authored-by: Andersw88 <anders.wikstrom.88@gmail.com>
  • Loading branch information
FlayaN and Andersw88 authored Feb 21, 2024
1 parent deeb1a7 commit 0d7c6f6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
36 changes: 36 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,42 @@ if(MSVC_VERSION GREATER_EQUAL 1936 AND MSVC_IDE) # 17.6+
]==] @ONLY)
endif()

# #######################################################################################################################
# # Feature version detection
# #######################################################################################################################

file(GLOB_RECURSE FEATURE_CONFIG_FILES
LIST_DIRECTORIES false
CONFIGURE_DEPENDS
"features/*/Shaders/Features/*.ini"
)

foreach(FEATURE_PATH ${FEATURE_CONFIG_FILES})
get_filename_component(FEATURE ${FEATURE_PATH} NAME_WE)
file(READ "${FEATURE_PATH}" CONFIG_VALUE)
string(REGEX MATCH "Version = ([0-9]*)-([0-9]*)-([0-9]*)" _ ${CONFIG_VALUE})
set(ver_major ${CMAKE_MATCH_1})
set(ver_minor ${CMAKE_MATCH_2})
set(ver_patch ${CMAKE_MATCH_3})
list(APPEND FEATURE_VERSIONS \t\t{\ \"${FEATURE}\"sv,\ {${ver_major},${ver_minor},${ver_patch}}\ })
endforeach()

set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${FEATURE_CONFIG_FILES}")

string (REPLACE ";" ",\n" FEATURE_VERSIONS "${FEATURE_VERSIONS}")

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/FeatureVersions.h.in
${CMAKE_CURRENT_BINARY_DIR}/cmake/FeatureVersions.h
@ONLY
)

target_sources(
"${PROJECT_NAME}"
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/cmake/FeatureVersions.h
)

# #######################################################################################################################
# # Automatic deployment
# #######################################################################################################################
Expand Down
11 changes: 11 additions & 0 deletions cmake/FeatureVersions.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace FeatureVersions
{
using namespace std::literals::string_view_literals;

static const std::map<std::string_view, REL::Version> FEATURE_MINIMAL_VERSIONS
{
@FEATURE_VERSIONS@
};
}
30 changes: 27 additions & 3 deletions src/Feature.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Feature.h"

#include "FeatureVersions.h"
#include "Features/CloudShadows.h"
#include "Features/DistantTreeLighting.h"
#include "Features/DynamicCubemaps.h"
Expand All @@ -26,12 +27,35 @@ void Feature::Load(json&)
ini.SetUnicode();
ini.LoadFile(ini_path.c_str());
if (auto value = ini.GetValue("Info", "Version")) {
loaded = true;
REL::Version featureVersion(std::regex_replace(value, std::regex("-"), "."));

auto& minimalFeatureVersion = FeatureVersions::FEATURE_MINIMAL_VERSIONS.at(GetShortName());

bool oldFeature = featureVersion.compare(minimalFeatureVersion) == std::strong_ordering::less;
bool majorVersionMismatch = minimalFeatureVersion.major() < featureVersion.major();

if (!oldFeature && !majorVersionMismatch) {
loaded = true;
logger::info("{} {} successfully loaded", ini_filename, value);
} else {
loaded = false;

std::string minimalVersionString = minimalFeatureVersion.string();
minimalVersionString = minimalVersionString.substr(0, minimalVersionString.size() - 2);

if (majorVersionMismatch) {
failedLoadedMessage = std::format("{} {} requires a newer version of community shaders, the feature version should be {}", GetShortName(), value, minimalVersionString);
} else {
failedLoadedMessage = std::format("{} {} is an old feature version, required: {}", GetShortName(), value, minimalVersionString);
}
logger::warn("{}", failedLoadedMessage);
}

version = value;
logger::info("{} {} successfully loaded", ini_filename, value);
} else {
loaded = false;
logger::warn("{} missing version info; not successfully loaded", ini_filename);
failedLoadedMessage = std::format("{} missing version info; not successfully loaded", ini_filename);
logger::warn("{}", failedLoadedMessage);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct Feature
{
bool loaded = false;
std::string version;
std::string failedLoadedMessage;

virtual std::string GetName() = 0;
virtual std::string GetShortName() = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ void Menu::DrawSettings()
selectedFeature = i;
ImGui::SameLine();
ImGui::TextDisabled(fmt::format("({})", featureList[i]->version).c_str());
} else if (!featureList[i]->version.empty()) {
ImGui::TextDisabled(fmt::format("{} ({})", featureList[i]->GetName(), featureList[i]->version).c_str());
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text(featureList[i]->failedLoadedMessage.c_str());
}
}
ImGui::EndListBox();
}
Expand Down

0 comments on commit 0d7c6f6

Please sign in to comment.