Skip to content

Commit

Permalink
feat(native/getbuild): new native to get the version of FXServer
Browse files Browse the repository at this point in the history
  • Loading branch information
spacevx committed Oct 15, 2024
1 parent 20656b6 commit 71dcbd3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
8 changes: 8 additions & 0 deletions code/components/citizen-server-impl/include/BuildInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace fx
{
class BuildInfo
{
public:
static int GetBuildNumber();
};
}
9 changes: 9 additions & 0 deletions code/components/citizen-server-impl/src/BuildInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "StdInc.h"
#include "BuildInfo.h"
#include <cfx_version.h>

int fx::BuildInfo::GetBuildNumber()
{
const char* lastPeriod = strchr(GIT_TAG, '.');
return lastPeriod == nullptr ? 0 : strtol(lastPeriod + 1, nullptr, 10);
}
5 changes: 3 additions & 2 deletions code/components/citizen-server-impl/src/InfoHttpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "ForceConsteval.h"

#include "BuildInfo.h"

using json = nlohmann::json;

inline uint32_t SwapLong(uint32_t x)
Expand Down Expand Up @@ -166,8 +168,7 @@ void InfoHttpHandlerComponentLocals::AttachToObject(fx::ServerInstanceBase* inst
maxClientsVar = instance->AddVariable<int>("sv_maxClients", ConVar_ServerInfo, 30);
iconVar = instance->AddVariable<std::string>("sv_icon", ConVar_Internal, "");
versionVar = instance->AddVariable<std::string>("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<int>("buildNumber", ConVar_Internal, versionBuildNo);
crashCmd = instance->AddCommand("_crash", []()
{
Expand Down
6 changes: 6 additions & 0 deletions code/components/citizen-server-impl/src/ServerResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

#include <boost/algorithm/string.hpp>

#include "BuildInfo.h"

#if defined(_DEBUG) && defined(_WIN32)
#include <shellapi.h>
#endif
Expand Down Expand Up @@ -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<const char*>(0);
Expand Down
38 changes: 38 additions & 0 deletions ext/native-decls/GetBuildNumber.md
Original file line number Diff line number Diff line change
@@ -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.");
}
```

0 comments on commit 71dcbd3

Please sign in to comment.