Skip to content

Commit

Permalink
make update message adjustable by provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lionkor committed Dec 29, 2023
1 parent 002223a commit d07c4aa
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ set(PRJ_HEADERS
include/TScopedTimer.h
include/TServer.h
include/VehicleData.h
include/Env.h
)
# add all source files (.cpp) to this, except the one with main()
set(PRJ_SOURCES
Expand All @@ -70,6 +71,7 @@ set(PRJ_SOURCES
src/TScopedTimer.cpp
src/TServer.cpp
src/VehicleData.cpp
src/Env.cpp
)

find_package(Lua REQUIRED)
Expand Down
16 changes: 16 additions & 0 deletions include/Env.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <optional>
#include <string>
namespace Env {

enum class Key {
// provider settings
PROVIDER_UPDATE_MESSAGE,
};

std::optional<std::string> Get(Key key);

std::string_view ToString(Key key);

}
9 changes: 7 additions & 2 deletions src/Common.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "Common.h"

#include "Env.h"
#include "TConsole.h"
#include <array>
#include <charconv>
#include <fmt/core.h>
#include <iostream>
#include <map>
#include <regex>
Expand Down Expand Up @@ -201,8 +203,11 @@ void Application::CheckForUpdates() {
auto MyVersion = ServerVersion();
auto RemoteVersion = Version(VersionStrToInts(Response));
if (IsOutdated(MyVersion, RemoteVersion)) {
std::string RealVersionString = RemoteVersion.AsString();
beammp_warn(std::string(ANSI_YELLOW_BOLD) + "NEW VERSION IS OUT! Please update to the new version (v" + RealVersionString + ") of the BeamMP-Server! Download it here: https://beammp.com/! For a guide on how to update, visit: https://wiki.beammp.com/en/home/server-maintenance#updating-the-server" + std::string(ANSI_RESET));
std::string RealVersionString = std::string("v") + RemoteVersion.AsString();
const std::string DefaultUpdateMsg = "NEW VERSION IS OUT! Please update to the new version ({}) of the BeamMP-Server! Download it here: https://beammp.com/! For a guide on how to update, visit: https://wiki.beammp.com/en/home/server-maintenance#updating-the-server";
auto UpdateMsg = Env::Get(Env::Key::PROVIDER_UPDATE_MESSAGE).value_or(DefaultUpdateMsg);
UpdateMsg = fmt::vformat(std::string_view(UpdateMsg), fmt::make_format_args(RealVersionString));
beammp_warnf("{}{}{}", ANSI_YELLOW_BOLD, UpdateMsg, ANSI_RESET);
} else {
if (FirstTime) {
beammp_info("Server up-to-date!");
Expand Down
20 changes: 20 additions & 0 deletions src/Env.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Env.h"
#include <optional>

std::optional<std::string> Env::Get(Env::Key key) {
auto StrKey = ToString(key);
auto Value = std::getenv(StrKey.data());
if (!Value || std::string_view(Value).empty()) {
return std::nullopt;
}
return Value;
}

std::string_view Env::ToString(Env::Key key) {
switch (key) {

Check warning on line 14 in src/Env.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22-04-build

switch missing default case [-Wswitch-default]

Check warning on line 14 in src/Env.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22-04-build

switch missing default case [-Wswitch-default]

Check warning on line 14 in src/Env.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22-04-build

switch missing default case [-Wswitch-default]

Check warning on line 14 in src/Env.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22-04-build

switch missing default case [-Wswitch-default]
case Key::PROVIDER_UPDATE_MESSAGE:
return "BEAMMP_PROVIDER_UPDATE_MESSAGE";
break;
}
return "";
}

0 comments on commit d07c4aa

Please sign in to comment.