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

Feature: ReDM update notifer #128

Merged
merged 7 commits into from
Jun 23, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Many features have long been built and optimized to work directly in ReGameDLL_C
- [ReGameDLL_CS](https://github.com/s1lentq/ReGameDLL_CS) installed;
- Installed AMXModX ([`v1.9`](https://www.amxmodx.org/downloads-new.php) or [`v1.10`](https://www.amxmodx.org/downloads-new.php?branch=master));
- Installed [ReAPI](https://github.com/s1lentq/reapi) amxx module;
- Installed [AmxxEasyHttp](https://github.com/Next21Team/AmxxEasyHttp) module (optional);

## Installation
Check the wiki [Installation](https://redeathmatch.github.io/en/Getting-started/installation/)
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,4 @@ jobs:
with:
files: |
*.zip
*.amxx
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ RUN curl -sSL ${AMXModX_URL} | bsdtar -xf - addons/ \
RUN releaseLink="https://github.com/s1lentq/reapi/releases/download/5.24.0.300/reapi-bin-5.24.0.300.zip" \
&& curl -sSL ${releaseLink} | bsdtar -xf - --exclude='*.dll' --exclude='*.pdb' addons/

# # Install AmxxEasyHttp
RUN releaseLink="https://github.com/Next21Team/AmxxEasyHttp/releases/download/1.3.0/release_linux_v1.3.0.zip" \
&& curl -sSL ${releaseLink} | bsdtar -xf - --strip-components=1

COPY cstrike .

SHELL ["/bin/bash", "-c"]
Expand Down
8 changes: 8 additions & 0 deletions cstrike/addons/amxmodx/scripting/ReDeathmatch.sma
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <fakemeta_util>
#include <amxmisc>
#include <json>
#include <easy_http>

#include <reapi>
#include <redm>
Expand All @@ -19,6 +20,7 @@
#include "ReDeathmatch/ReDM_features.inc"
#include "ReDeathmatch/ReDM_round_modes.inc"
#include "ReDeathmatch/ReDM_api.inc"
#include "ReDeathmatch/ReDM_updater.inc"


static bool: g_prevState
Expand Down Expand Up @@ -65,6 +67,7 @@ public plugin_init() {
register_concmd("redm", "ConCmd_redm", ADMIN_MAP, "Get info.", .FlagManager = false)

ApiInit_Forwards()
Updater_Init()
}

public plugin_precache() {
Expand Down Expand Up @@ -111,6 +114,11 @@ public plugin_unpause() {
SetActive(true)
}

public plugin_natives() {
ApiInit_Natives()
Updater_Natives()
}

public client_putinserver(player) {
if (!IsActive())
return
Expand Down
2 changes: 1 addition & 1 deletion cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_api.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ static g_fwdGetConfigName = -1
static g_fwdGetConfigPrefixName = -1


public plugin_natives() {
public ApiInit_Natives() {
register_native("redm_is_active", "native_redm_is_active")
register_native("redm_set_active", "native_redm_set_active")
register_native("redm_get_config", "native_redm_get_config")
Expand Down
148 changes: 148 additions & 0 deletions cstrike/addons/amxmodx/scripting/ReDeathmatch/ReDM_updater.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
static const g_versionLink[] = "https://api.github.com/repos/"
+ "ReDeathmatch/ReDeathmatch_AMXX"
+ "/releases/latest"

static bool: redm_update_notify

Updater_Init() {
bind_pcvar_num(
create_cvar(
"redm_update_notify", "1",
.has_min = true, .min_val = 0.0,
.has_max = true, .max_val = 1.0,
.flags = _FCVAR_BOOLEAN,
.description = "Notify for new ReDM update.^n\
Don't use into ReDM configs!"
),
redm_update_notify
)

register_concmd(
"redm_update_check", "ConCmd_redm_update_check",
ADMIN_MAP,
"Checks ReDM update."
)

CheckUpdate()
}

public ConCmd_redm_update_check(const player) {
SetGlobalTransTarget(player)

if (!cmd_access(player, level, commandId, 1))
return PLUGIN_HANDLED

Request_NewVersion(g_versionLink)

return PLUGIN_HANDLED
}

Updater_Natives() {
set_module_filter("@ModuleFilter")
set_native_filter("@NativeFilter")
}

@ModuleFilter(const library[], LibType: type) {
return strcmp("easy_http", library) == 0 ? PLUGIN_HANDLED : PLUGIN_CONTINUE
}

@NativeFilter(const nativeName[], index, trap) {
if (strncmp(nativeName, "ezhttp_", 7) == 0)
return PLUGIN_HANDLED

if (strncmp(nativeName, "ezjson_", 7) == 0)
return PLUGIN_HANDLED

return PLUGIN_CONTINUE
}

static CheckUpdate() {
if (!redm_update_notify)
return

new bool: isManualBuild = (strfind(REDM_VERSION, "manual") != -1)
if (isManualBuild)
return

if (is_module_loaded("Amxx Easy Http") == -1) {
LogMessageEx(Warning, "The `AmxxEasyHttp` module is not loaded! The new version cannot be verified.")
LogMessageEx(Warning, "Please install AmxxEasyHttp: `https://github.com/Next21Team/AmxxEasyHttp` or disable update checks (`redm_update_notify 0`).")

return
}

Request_NewVersion(g_versionLink)
}

static Request_NewVersion(const link[]) {
ezhttp_get(link, "@RequestCallback_NewVersion")
}

@RequestCallback_NewVersion(EzHttpRequest: request_id) {
if (ezhttp_get_error_code(request_id) != EZH_OK) {
new error[64]
ezhttp_get_error_message(request_id, error, charsmax(error))

LogMessageEx(Warning, "RequestCallback_NewVersion(%i): Response error: %s", request_id, error)
return
}

new response[8192]
ezhttp_get_data(request_id, response, charsmax(response))

if (contain(response, "tag_name") == -1) {
LogMessageEx(Warning, "RequestCallback_NewVersion: Wrong response! (don't contain `tag_name`). res=`%s`", response)
return
}

new EzJSON: json = ezjson_parse(response)
if (json == EzInvalid_JSON) {
LogMessageEx(Warning, "RequestCallback_NewVersion: Can't parse response JSON!")
goto END
}

new tag_name[32]
ezjson_object_get_string(json, "tag_name", tag_name, charsmax(tag_name))

if (CmpVersions(REDM_VERSION, tag_name) >= 0)
goto END

new html_url[256]
ezjson_object_get_string(json, "html_url", html_url, charsmax(html_url))

NotifyUpdate(tag_name, html_url)

END:
ezjson_free(json)
}

static NotifyUpdate(const newVersion[], const URL[]) {
LogMessageEx(Info, "^n^t ReDeathmatch (%s) has update! New version `%s`.^n\
Download link: `%s`", REDM_VERSION, newVersion, URL
)
}

static stock CmpVersions(const a[], const b[]) {
new segmentsA[32][32]
new segmentsB[32][32]

new countA = explode_string(
a[!isdigit(a[0]) ? 1 : 0],
".",
segmentsA, sizeof segmentsA, charsmax(segmentsA[])
)

new countB = explode_string(
b[!isdigit(b[0]) ? 1 : 0],
".",
segmentsB, sizeof segmentsB, charsmax(segmentsB[])
)

for(new i, l = min(countA, countB); i < l; i++) {
new diff = strtol(segmentsA[i]) - strtol(segmentsB[i])
if (diff)
return diff
}

return countA - countB
}
Loading