Skip to content

Commit

Permalink
More misc cleanup in preperation for more plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rtldg committed Jan 28, 2025
1 parent f9235bf commit aa82825
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 15 deletions.
3 changes: 3 additions & 0 deletions extshared/src/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ bool MyExtension::is_plugin_compatible(IPluginContext* ctx)
{
static std::string pubvarname = std::string(GetExtensionName()) + "_compat_version";

if (this->compat_version == -1)
return true;

cell_t* pubvar = get_pubvar(ctx->GetRuntime(), pubvarname.c_str());
if (!pubvar || *pubvar != this->compat_version) [[unlikely]]
{
Expand Down
4 changes: 2 additions & 2 deletions srcwrjson/src/natives_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ static cell_t N_SRCWRJSON_SetZss(IPluginContext* ctx, const cell_t* params)
GET_HANDLE(params[1], object, g_JSONType);
int flags = params[2];
char* key;
(void)ctx->LocalToString(params[3], &key);
(void)ctx->LocalToStringNULL(params[3], &key);
void* other;
GET_HANDLE(params[4], other, g_JSONType);
char* key2;
(void)ctx->LocalToString(params[5], &key2);
(void)ctx->LocalToStringNULL(params[5], &key2);
MAYBE_FORMAT(5, key2);
return rust_SRCWRJSON_SetZss(object, flags, key, other, key2, fmtlen);
}
Expand Down
18 changes: 10 additions & 8 deletions srcwrtimer/addons/sourcemod/scripting/include/srcwr/core.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#define MAPNAMEBUFSZ 129 // TODO: Engine uses char[128] for the mapname stem?
#define CSS_MAXPLAYERS 65 // thanks tf2 for adding 100 player support for some reason
#define MAX_NETWORKED_ENTS 2048

//////////////////////////////////////////////////////////////////////////
// macros
Expand Down Expand Up @@ -64,6 +65,7 @@ SRCWRJSON gJ_Timer[MAXPLAYERS+1];
// [0] is special and for the server. If you clobber it then you'll break everything.
SRCWRJSON gJ_Player[MAXPLAYERS+1];
// Settings and shit for clients...
// Do not manually SET values because the `SRCWR_OnEditPlayerSettings()` forward will not trigger for plugins.
SRCWRJSON gJ_Settings[MAXPLAYERS+1];

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -120,12 +122,6 @@ stock void SRCWR_SetDefaultPlayerSettings(const char[] settings_name, const char
SetFailState("Failed to set default player settings (/%s). Check for trailing commas.\n\n%s\n", settings_name, settings_str);
}

// TODO: Remove? Have srcwr.sp auto fill settings for each client-index OnClientDisconnect_Post/OnClientConnect?
stock void SRCWR_LoadDefaultPlayerSettings(int client, const char[] settings_name)
{
gJ_Settings[client].SetZss(0, settings_name, gJ_Settings[0], "/player_defaults/%s", settings_name);
}

//////////////////////////////////////////////////////////////////////////
// stocks - SRCWR related
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -255,6 +251,10 @@ stock float NormalizeAngleNoNaN(float angle)
// natives - misc
//////////////////////////////////////////////////////////////////////////

// If you add custom settings from a third-party plugin then srcwr.smx will need to load the translations.
// Otherwise shit won't display translated strings for keys.
native void SRCWR_LoadTranslations(const char[] file);
// If you add custom settings from a third-party plugin then you'll need to call this.
native void SRCWR_RecreateSettingsMenu();

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -367,8 +367,10 @@ forward void SRCWR_OnProcessMovementPost(int client);
// When `is_being_toggled` then `value` is empty!!!! Vice versa!!!!
// When `result == Plugin_Continue` and `type == J_Bool`
// TODO: Documentation...
forward Action SRCWR_OnEditPlayerSettings(int client, const char[] key, bool is_being_toggled, const char[] value, J_Type type);
// Editing the `value` string buffer does not do anything.
forward Action SRCWR_OnEditPlayerSettings(int client, const char[] key, bool is_being_toggled, char[] value, J_Type type);

// Client is guaranteed to be ingame and authorized by the time this forward is called.
forward void SRCWR_OnSettingsLoaded(int client);

// TODO:
Expand All @@ -393,6 +395,6 @@ public SharedPlugin __pl_srcwr_core = {
#if !defined REQUIRE_PLUGIN
public void __pl_srcwr_core_SetNTVOptional()
{

// TODO:
}
#endif
3 changes: 2 additions & 1 deletion srcwrtimer/addons/sourcemod/scripting/include/srcwr/json.inc
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ methodmap SRCWRJSON < Handle {
public native int ReplaceCellIdx(int newcell, int flags, int idx);

// Copy things from other things....
public native bool SetZss(int flags=0, const char[] key, SRCWRJSON other, const char[] key2, any ...);
// gJ_Settings[client].SetZss(0, settings_name, gJ_Settings[0], "/player_defaults/%s", settings_name);
public native bool SetZss(int flags=0, const char[] key=NULL_STRING, SRCWRJSON other, const char[] key2=NULL_STRING, any ...);
#if 0
public native bool SetZsi(const char[] key, SRCWRJSON other, int key2, int flags = 0);
public native bool SetZis(int key, SRCWRJSON other, const char[] key2, int flags = 0);
Expand Down
1 change: 0 additions & 1 deletion srcwrtimer/addons/sourcemod/scripting/srcwr-eventqueue.sp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public void OnClientPutInServer(int client)
}",
-1, 0, "eventqueue"
);
SRCWR_LoadDefaultPlayerSettings(client, "eventqueue");
}

public void OnClientDisconnect_Post(int client)
Expand Down
2 changes: 2 additions & 0 deletions srcwrtimer/addons/sourcemod/scripting/srcwr-json-explorer.sp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Action Command_Json(int client, int argc)

LowercaseString(type);

// TODO: `SRCWR_OnEditPlayerSettings()` won't be called. Figure something out...

if (StrEqual(type, "f32") || StrEqual(type, "float"))
{
j.SetF32(StringToFloat(value), J_CREATE_PARENTS, key);
Expand Down
18 changes: 15 additions & 3 deletions srcwrutil/src/string_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(dead_code)]

pub struct StringBlock {
// The strings are internally stored as "<string>\x00<string>\x00".
// The strings are internally stored as "\x00<string>\x00<string>\x00".
data: Vec<u8>,
// The byte-offset into data to where each string starts.
starting_indexes: Vec<u32>,
Expand All @@ -14,7 +14,9 @@ pub struct StringBlock {
impl StringBlock {
pub fn new() -> StringBlock {
StringBlock {
data: vec![],
// start the buffer with a '\0' so finding a string is easier...
// we can search for "\x00<string>\x00" (with `.find_delimited_str()`)
data: vec![0],
starting_indexes: vec![],
}
}
Expand All @@ -24,7 +26,9 @@ impl StringBlock {
}
/// Clears the internal backing structures but does not free memory.
pub fn clear(&mut self) {
self.data.clear();
// first byte is a '\0' so truncate it to that
self.data.truncate(1);

self.starting_indexes.clear();
}
/// Used to prevent the `push<S: AsRef<str>>()` genericization from duplicating a bunch of code.
Expand Down Expand Up @@ -56,6 +60,14 @@ impl StringBlock {
let p = self.get_str_ptr(idx)?;
Some(unsafe { std::str::from_utf8_unchecked(std::ffi::CStr::from_ptr(p).to_bytes()) })
}
/// `target` should be a string like "\x00<string>\x00"
pub fn find_delimited_str(&self, target: &str) -> Option<usize> {
let s = unsafe { std::str::from_utf8_unchecked(&self.data) };
let byte_idx = (s.find(target)? + 1) as u32; // +1 to skip the \x00
self.starting_indexes
.iter()
.position(|idx| *idx == byte_idx)
}
pub fn find_str(&self, target: &str) -> Option<usize> {
for (i, s) in self.iter_str().enumerate() {
if s == target {
Expand Down

0 comments on commit aa82825

Please sign in to comment.