Skip to content

Commit

Permalink
✨ VersionName doesn't accept Beta anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFouchy committed Feb 3, 2025
1 parent 71d1592 commit 6477cbe
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 101 deletions.
16 changes: 5 additions & 11 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,16 @@ void App::imgui_menus()

void App::launch(Project const& project)
{
if (project.version_name() < VersionName{"Beta 19"})
if (!project.version_name().has_value())
{
ImGuiNotify::send({
.type = ImGuiNotify::Type::Error,
.title = "Can't open project",
.custom_imgui_content = [&]() {
Cool::ImGuiExtras::markdown(fmt::format(
"Old versions cannot be used with the launcher.\n"
"You can download Coollab **{}** manually from [our release page](https://github.com/CoolLibs/Lab/releases)",
project.version_name().as_string()
));
},
.type = ImGuiNotify::Type::Error,
.title = "Can't open project",
.content = "Unknown version",
});
return;
}
version_manager().install_ifn_and_launch(project.version_name(), FileToOpen{project.file_path()});
version_manager().install_ifn_and_launch(*project.version_name(), FileToOpen{project.file_path()});
}

void App::launch(std::filesystem::path const& project_file_path)
Expand Down
11 changes: 4 additions & 7 deletions src/Project/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ auto Project::name() const -> std::string
return Cool::File::file_name_without_extension(_file_path).string();
}

auto Project::version_name() const -> VersionName const&
auto Project::version_name() const -> std::optional<VersionName> const&
{
return _version_name.get_value([&]() {
return _version_name.get_value([&]() -> std::optional<VersionName> {
auto file = std::ifstream{file_path()};
if (!file.is_open())
{
// TODO(Launcher) Handler error: std::cerr << "Error: " << strerror(errno) << std::endl;
return VersionName{"1.0.0"}; // TODO(Launcher) better default version
}
return std::nullopt;
auto version = ""s;
std::getline(file, version);
return VersionName{version};
return VersionName::from(version);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Project/Project.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Project {
auto file_path() const -> std::filesystem::path { return Cool::File::weakly_canonical(_file_path); }
auto name() const -> std::string;
auto id() const -> reg::AnyId const& { return _uuid; }
auto version_name() const -> VersionName const&;
auto version_name() const -> std::optional<VersionName> const&;
auto thumbnail_path() const -> std::filesystem::path { return info_folder_path() / "thumbnail.png"; }
auto time_of_last_change() const -> std::filesystem::file_time_type const&;
auto info_folder_path() const -> std::filesystem::path { return Path::projects_info_folder() / reg::to_string(id()); }
Expand All @@ -33,6 +33,6 @@ class Project {
private:
std::filesystem::path _file_path{};
reg::AnyId _uuid{};
mutable Cool::Cached<VersionName> _version_name{};
mutable Cool::Cached<std::optional<VersionName>> _version_name{};
mutable Cool::Cached<std::filesystem::file_time_type> _time_of_last_change{};
};
5 changes: 4 additions & 1 deletion src/Project/ProjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ void ProjectManager::imgui(std::function<void(Project const&)> const& launch_pro
ImGui::BeginGroup();
ImGui::TextUnformatted(project.file_path().string().c_str());
ImGui::PushFont(Cool::Font::italic());
ImGui::TextUnformatted(version_manager().label_with_installation_icon(project.version_name()).c_str());
if (project.version_name().has_value())
ImGui::TextUnformatted(version_manager().label_with_installation_icon(*project.version_name()).c_str());
else
ImGui::TextUnformatted("Unknown version");
ImGui::PopFont();
ImGui::EndGroup();
}))
Expand Down
6 changes: 3 additions & 3 deletions src/Version/Task_FetchListOfVersions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ void Task_FetchListOfVersions::execute()

try
{
auto const version_name = VersionName{version.at("name")};
if (!version_name.is_valid())
auto const version_name = VersionName::from(version.at("name"));
if (!version_name.has_value()) // This will ignore all the old Beta versions, which is what we want because they are not compatible with the launcher
continue;

for (auto const& asset : version.at("assets"))
{
auto const download_url = std::string{asset.at("browser_download_url")};
if (download_url.find(zip_name_for_current_os()) == std::string::npos)
continue;
version_manager().set_download_url(version_name, download_url);
version_manager().set_download_url(*version_name, download_url);
break;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Version/VersionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ static auto get_all_locally_installed_versions(std::vector<Version>& versions) -
return version.name.as_string() == name;
}))
{
versions.emplace_back(VersionName{name}, InstallationStatus::Installed);
auto const version_name = VersionName::from(name);
if (version_name.has_value())
versions.emplace_back(*version_name, InstallationStatus::Installed);
}
}
}
Expand Down
126 changes: 57 additions & 69 deletions src/Version/VersionName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#include <cassert>
#include <compare>


static auto is_number(char c) -> bool
{
return '0' <= c && c <= '9';
}

VersionName::VersionName(std::string name)
: _name{std::move(name)}
, _is_experimental{_name.find("Experimental(") != std::string::npos}
, _is_beta{_name.starts_with("Beta ")}
auto VersionName::from(std::string name) -> std::optional<VersionName>
{
if (name.empty())
return std::nullopt;

VersionName ver;
ver._name = std::move(name);
ver._is_experimental = ver._name.find("Experimental(") != std::string::npos;

auto acc = std::string{};
auto nb_dots = 0;
auto const register_current_version_part = [&]() {
Expand All @@ -21,53 +24,56 @@ VersionName::VersionName(std::string name)
int const nb = std::stoi(acc);
acc = "";
if (nb_dots == 0)
_major = nb;
ver._major = nb;
else if (nb_dots == 1)
_minor = nb;
ver._minor = nb;
else
{
assert(nb_dots == 2);
_patch = nb;
ver._patch = nb;
}
}
catch (...)
{
_is_valid = false;
return;
return std::nullopt;
}
};

for (size_t i = _is_beta ? 5 /*skip "Beta "*/ : 0; i <= _name.size(); ++i)
for (size_t i = 0; i <= ver._name.size(); ++i)
{
if (i == _name.size())
if (i == ver._name.size())
{
register_current_version_part();
break;
}
if (is_number(_name[i]))
acc += _name[i];
else if (_name[i] == '.')

if (is_number(ver._name[i]))
{
acc += ver._name[i];
}
else if (ver._name[i] == '.')
{
register_current_version_part();
nb_dots++;
if (nb_dots > 2)
break;
}
else
else if (ver._name[i] == ' ')
{
register_current_version_part();
break;
}
else
{
return std::nullopt;
}
}

return ver;
}

auto operator<=>(VersionName const& a, VersionName const& b) -> std::strong_ordering
{
if (a._is_beta && !b._is_beta)
return std::strong_ordering::less;
if (!a._is_beta && b._is_beta)
return std::strong_ordering::greater;

if (a._major < b._major)
return std::strong_ordering::less;
if (b._major < a._major)
Expand Down Expand Up @@ -104,66 +110,48 @@ TEST_CASE("Parsing Coollab Version from string")
{
SUBCASE("")
{
auto const version = VersionName{"Beta 18"};
CHECK(version.is_beta() == true);
CHECK(version.is_experimental() == false);
CHECK(version.major() == 18);
CHECK(version.minor() == 0);
CHECK(version.patch() == 0);
}
SUBCASE("")
{
auto const version = VersionName{"Beta 21.1"};
CHECK(version.is_beta() == true);
CHECK(version.is_experimental() == false);
CHECK(version.major() == 21);
CHECK(version.minor() == 1);
CHECK(version.patch() == 0);
}
SUBCASE("")
{
auto const version = VersionName{"Beta 5.71.3"};
CHECK(version.is_beta() == true);
CHECK(version.is_experimental() == false);
CHECK(version.major() == 5);
CHECK(version.minor() == 71);
CHECK(version.patch() == 3);
auto const version = VersionName::from("3.7.2 Launcher");
CHECK(version.has_value());
CHECK(version->is_experimental() == false);
CHECK(version->major() == 3);
CHECK(version->minor() == 7);
CHECK(version->patch() == 2);
}
SUBCASE("")
{
auto const version = VersionName{"18"};
CHECK(version.is_beta() == false);
CHECK(version.is_experimental() == false);
CHECK(version.major() == 18);
CHECK(version.minor() == 0);
CHECK(version.patch() == 0);
auto const version = VersionName::from("18");
CHECK(version.has_value());
CHECK(version->is_experimental() == false);
CHECK(version->major() == 18);
CHECK(version->minor() == 0);
CHECK(version->patch() == 0);
}
SUBCASE("")
{
auto const version = VersionName{"21.1"};
CHECK(version.is_beta() == false);
CHECK(version.is_experimental() == false);
CHECK(version.major() == 21);
CHECK(version.minor() == 1);
CHECK(version.patch() == 0);
auto const version = VersionName::from("21.1");
CHECK(version.has_value());
CHECK(version->is_experimental() == false);
CHECK(version->major() == 21);
CHECK(version->minor() == 1);
CHECK(version->patch() == 0);
}
SUBCASE("")
{
auto const version = VersionName{"5.71.3"};
CHECK(version.is_beta() == false);
CHECK(version.is_experimental() == false);
CHECK(version.major() == 5);
CHECK(version.minor() == 71);
CHECK(version.patch() == 3);
auto const version = VersionName::from("5.71.3");
CHECK(version.has_value());
CHECK(version->is_experimental() == false);
CHECK(version->major() == 5);
CHECK(version->minor() == 71);
CHECK(version->patch() == 3);
}
SUBCASE("")
{
auto const version = VersionName{"5.71.3 Experimental(LED)"};
CHECK(version.is_beta() == false);
CHECK(version.is_experimental() == true);
CHECK(version.major() == 5);
CHECK(version.minor() == 71);
CHECK(version.patch() == 3);
auto const version = VersionName::from("5.71.3 Experimental(LED)");
CHECK(version.has_value());
CHECK(version->is_experimental() == true);
CHECK(version->major() == 5);
CHECK(version->minor() == 71);
CHECK(version->patch() == 3);
}
}
#endif
8 changes: 1 addition & 7 deletions src/Version/VersionName.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@

class VersionName {
public:
explicit VersionName(std::string name);
static auto from(std::string name) -> std::optional<VersionName>;

auto as_string() const -> std::string const& { return _name; }

auto major() const -> int { return _major; };
auto minor() const -> int { return _minor; };
auto patch() const -> int { return _patch; };
auto is_experimental() const -> bool { return _is_experimental; }
auto is_beta() const -> bool { return _is_beta; }

auto is_valid() const -> bool { return _is_valid; };

friend auto operator<=>(VersionName const&, VersionName const&) -> std::strong_ordering;
friend auto operator==(VersionName const&, VersionName const&) -> bool;
Expand All @@ -25,7 +22,4 @@ class VersionName {
int _minor{0};
int _patch{0};
bool _is_experimental{false};
bool _is_beta{false};

bool _is_valid{true};
};

0 comments on commit 6477cbe

Please sign in to comment.