Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(native/getbuild): new native to get the version of FXServer #2865

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.");
}
```
Loading