Skip to content

Implement Con_Printf() hook#222

Merged
wopox1337 merged 16 commits intorehlds:masterfrom
francoromaniello:Con_Printf
Oct 25, 2021
Merged

Implement Con_Printf() hook#222
wopox1337 merged 16 commits intorehlds:masterfrom
francoromaniello:Con_Printf

Conversation

@francoromaniello
Copy link
Contributor

Related to #219

@wopox1337
Copy link
Member

Why do you need this hook?

@RauliTop
Copy link
Contributor

Why do you need this hook?

It's commented here: #219

@wopox1337 wopox1337 changed the title Implement Con_Printf hook Implement Con_Printf() hook Oct 23, 2021
@wopox1337 wopox1337 self-requested a review October 23, 2021 10:43
@wopox1337 wopox1337 added the Type: 🚀 enhancement Improvement or addition of a new feature. label Oct 23, 2021
@wopox1337
Copy link
Member

@francoromaniello update PR please

@wopox1337
Copy link
Member

wopox1337 commented Oct 23, 2021

Thanks. What's about pointers style?

@wopox1337
Copy link
Member

test passed

Server Median FPS: `875.80` (min=163.92, max=924.80)
#include <amxmodx>
#include <reapi>

static HookChain: hook

public plugin_init() {
	DisableHookChain(
		(hook = RegisterHookChain(RH_Con_Printf, "RH_Con_Printf_Pre"))
	)

	set_task(0.1, "test", .flags = "b")
}

static Msg[ 100 ];


public test() {
	EnableHookChain(hook)
	server_cmd("stats"); server_exec()
	DisableHookChain(hook)

	static Array: arrayFPS
	if(!arrayFPS)
		arrayFPS = ArrayCreate()

	static tokens[ 14 ][ 32 ]

	for ( new i; i < sizeof tokens; i++ ) {
		trim( Msg );
		strtok( Msg, tokens[ i ], charsmax(tokens[]), Msg, charsmax( Msg ), ' ' )
	}

	static
		Float: currentFPS,
		Float: minFPS = cellmax,
		Float: maxFPS = cellmin

	currentFPS = str_to_float(tokens[12])

	ArrayPushCell(arrayFPS, currentFPS)

	if (currentFPS > maxFPS)
		maxFPS = currentFPS

	if (currentFPS < minFPS && currentFPS > 0.0)
		minFPS = currentFPS

	new median = ArrayGetMedian(arrayFPS)

	server_print("Server Median FPS: `%.2f` (min=%.2f, max=%.2f)",
		median, minFPS, maxFPS)
}

public RH_Con_Printf_Pre( const message[] ) {
	copy( Msg, charsmax( Msg ), message )

	return HC_SUPERCEDE
}

stock Float: ArrayGetMedian( const Array: array ) {
	SortADTArray(array, Sort_Ascending, Sort_Float)
	new n = ( ArraySize(array) + 1 ) / 2 - 1

	return Float: ArrayGetCell(array, n)
}

@wopox1337 wopox1337 merged commit dd1fdae into rehlds:master Oct 25, 2021
@francoromaniello francoromaniello deleted the Con_Printf branch October 25, 2021 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: 🚀 enhancement Improvement or addition of a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants