diff --git a/code/components/citizen-server-impl/include/BuildInfo.h b/code/components/citizen-server-impl/include/BuildInfo.h new file mode 100644 index 0000000000..6c4ba87764 --- /dev/null +++ b/code/components/citizen-server-impl/include/BuildInfo.h @@ -0,0 +1,8 @@ +namespace fx +{ +class BuildInfo +{ +public: + static int GetBuildNumber(); +}; +} diff --git a/code/components/citizen-server-impl/src/BuildInfo.cpp b/code/components/citizen-server-impl/src/BuildInfo.cpp new file mode 100644 index 0000000000..1961e5fbf0 --- /dev/null +++ b/code/components/citizen-server-impl/src/BuildInfo.cpp @@ -0,0 +1,9 @@ +#include "StdInc.h" +#include "BuildInfo.h" +#include + +int fx::BuildInfo::GetBuildNumber() +{ + const char* lastPeriod = strchr(GIT_TAG, '.'); + return lastPeriod == nullptr ? 0 : strtol(lastPeriod + 1, nullptr, 10); +} diff --git a/code/components/citizen-server-impl/src/InfoHttpHandler.cpp b/code/components/citizen-server-impl/src/InfoHttpHandler.cpp index 5e59cd8ea7..9a429f7857 100644 --- a/code/components/citizen-server-impl/src/InfoHttpHandler.cpp +++ b/code/components/citizen-server-impl/src/InfoHttpHandler.cpp @@ -26,6 +26,8 @@ #include "ForceConsteval.h" +#include "BuildInfo.h" + using json = nlohmann::json; inline uint32_t SwapLong(uint32_t x) @@ -166,8 +168,7 @@ void InfoHttpHandlerComponentLocals::AttachToObject(fx::ServerInstanceBase* inst maxClientsVar = instance->AddVariable("sv_maxClients", ConVar_ServerInfo, 30); iconVar = instance->AddVariable("sv_icon", ConVar_Internal, ""); versionVar = instance->AddVariable("version", ConVar_Internal, "FXServer-" GIT_DESCRIPTION); - const char* lastPeriod = strrchr(GIT_TAG, '.'); - int versionBuildNo = lastPeriod == nullptr ? 0 : strtol(lastPeriod + 1, nullptr, 10); + int versionBuildNo = fx::BuildInfo::GetBuildNumber(); versionBuildNoVar = instance->AddVariable("buildNumber", ConVar_Internal, versionBuildNo); crashCmd = instance->AddCommand("_crash", []() { diff --git a/code/components/citizen-server-impl/src/ServerResources.cpp b/code/components/citizen-server-impl/src/ServerResources.cpp index 37b70a5450..bde1733061 100644 --- a/code/components/citizen-server-impl/src/ServerResources.cpp +++ b/code/components/citizen-server-impl/src/ServerResources.cpp @@ -37,6 +37,8 @@ #include +#include "BuildInfo.h" + #if defined(_DEBUG) && defined(_WIN32) #include #endif @@ -874,6 +876,10 @@ void fx::ServerEventComponent::TriggerClientEvent(const std::string_view& eventN static InitFunction initFunction2([]() { + fx::ScriptEngine::RegisterNativeHandler("GET_BUILD_NUMBER", [](fx::ScriptContext& context) + { + return fx::BuildInfo::GetBuildNumber(); + }); fx::ScriptEngine::RegisterNativeHandler("PRINT_STRUCTURED_TRACE", [](fx::ScriptContext& context) { std::string_view jsonData = context.CheckArgument(0); diff --git a/ext/native-decls/GetBuildNumber.md b/ext/native-decls/GetBuildNumber.md new file mode 100644 index 0000000000..9c7c016534 --- /dev/null +++ b/ext/native-decls/GetBuildNumber.md @@ -0,0 +1,38 @@ +--- +ns: CFX +apiset: server +--- +## GET_BUILD_NUMBER + +```c +int GET_BUILD_NUMBER(); +``` + +**Note:** This build number is not the same as the client `gamebuild`. This build refers to the artifact version of `FXServer` + +## Return value +Returns the build number of FXServer. + +## Examples +```lua +local serverVersion = GetBuildNumber() +if serverVersion < 10309 then + print("Your FXServer is outdated, please update.") +end +``` + +```js +const serverVersion = GetBuildNumber(); +if (serverVersion < 10309) { + console.log("Your FXServer is outdated, please update."); +} +``` + +```cs +using static CitizenFX.Core.Native.API; + +int serverVersion = GetBuildNumber(); +if (serverVersion < 10309) { + Debug.WriteLine("Your FXServer is outdated, please update."); +} +``` \ No newline at end of file