Skip to content

Commit

Permalink
Add GameConf methodmap
Browse files Browse the repository at this point in the history
Wrap gamedata natives in a pretty methodmap.

This fixes bad documentation on `GameConfGetAddress` and not closing the gameconfig file if handle creation fails as well.
  • Loading branch information
peace-maker committed Feb 5, 2018
1 parent e57dce7 commit ab99b8a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 51 deletions.
11 changes: 10 additions & 1 deletion core/logic/smn_gameconfigs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ static cell_t smn_LoadGameConfigFile(IPluginContext *pCtx, const cell_t *params)
return pCtx->ThrowNativeError("Unable to open %s: %s", filename, error);
}

return handlesys->CreateHandle(g_GameConfigsType, gc, pCtx->GetIdentity(), g_pCoreIdent, NULL);
Handle_t hndl = handlesys->CreateHandle(g_GameConfigsType, gc, pCtx->GetIdentity(), g_pCoreIdent, NULL);
if (hndl == BAD_HANDLE)
g_GameConfigs.CloseGameConfigFile(gc);
return hndl;
}

static cell_t smn_GameConfGetOffset(IPluginContext *pCtx, const cell_t *params)
Expand Down Expand Up @@ -167,5 +170,11 @@ REGISTER_NATIVES(gameconfignatives)
{"GameConfGetOffset", smn_GameConfGetOffset},
{"GameConfGetKeyValue", smn_GameConfGetKeyValue},
{"GameConfGetAddress", smn_GameConfGetAddress},

// Transitional syntax support.
{"GameConf.GameConf", smn_LoadGameConfigFile},
{"GameConf.GetOffset", smn_GameConfGetOffset},
{"GameConf.GetKeyValue", smn_GameConfGetKeyValue},
{"GameConf.GetAddress", smn_GameConfGetAddress},
{NULL, NULL}
};
22 changes: 11 additions & 11 deletions plugins/funcommands.sp
Original file line number Diff line number Diff line change
Expand Up @@ -181,60 +181,60 @@ void HookEvents()

public void OnMapStart()
{
Handle gameConfig = LoadGameConfigFile("funcommands.games");
GameConf gameConfig = new GameConf("funcommands.games");
if (gameConfig == null)
{
SetFailState("Unable to load game config funcommands.games");
return;
}

if (GameConfGetKeyValue(gameConfig, "SoundBlip", g_BlipSound, sizeof(g_BlipSound)) && g_BlipSound[0])
if (gameConfig.GetKeyValue("SoundBlip", g_BlipSound, sizeof(g_BlipSound)) && g_BlipSound[0])
{
PrecacheSound(g_BlipSound, true);
}

if (GameConfGetKeyValue(gameConfig, "SoundBeep", g_BeepSound, sizeof(g_BeepSound)) && g_BeepSound[0])
if (gameConfig.GetKeyValue("SoundBeep", g_BeepSound, sizeof(g_BeepSound)) && g_BeepSound[0])
{
PrecacheSound(g_BeepSound, true);
}

if (GameConfGetKeyValue(gameConfig, "SoundFinal", g_FinalSound, sizeof(g_FinalSound)) && g_FinalSound[0])
if (gameConfig.GetKeyValue("SoundFinal", g_FinalSound, sizeof(g_FinalSound)) && g_FinalSound[0])
{
PrecacheSound(g_FinalSound, true);
}

if (GameConfGetKeyValue(gameConfig, "SoundBoom", g_BoomSound, sizeof(g_BoomSound)) && g_BoomSound[0])
if (gameConfig.GetKeyValue("SoundBoom", g_BoomSound, sizeof(g_BoomSound)) && g_BoomSound[0])
{
PrecacheSound(g_BoomSound, true);
}

if (GameConfGetKeyValue(gameConfig, "SoundFreeze", g_FreezeSound, sizeof(g_FreezeSound)) && g_FreezeSound[0])
if (gameConfig.GetKeyValue("SoundFreeze", g_FreezeSound, sizeof(g_FreezeSound)) && g_FreezeSound[0])
{
PrecacheSound(g_FreezeSound, true);
}

char buffer[PLATFORM_MAX_PATH];
if (GameConfGetKeyValue(gameConfig, "SpriteBeam", buffer, sizeof(buffer)) && buffer[0])
if (gameConfig.GetKeyValue("SpriteBeam", buffer, sizeof(buffer)) && buffer[0])
{
g_BeamSprite = PrecacheModel(buffer);
}

if (GameConfGetKeyValue(gameConfig, "SpriteBeam2", buffer, sizeof(buffer)) && buffer[0])
if (gameConfig.GetKeyValue("SpriteBeam2", buffer, sizeof(buffer)) && buffer[0])
{
g_BeamSprite2 = PrecacheModel(buffer);
}

if (GameConfGetKeyValue(gameConfig, "SpriteExplosion", buffer, sizeof(buffer)) && buffer[0])
if (gameConfig.GetKeyValue("SpriteExplosion", buffer, sizeof(buffer)) && buffer[0])
{
g_ExplosionSprite = PrecacheModel(buffer);
}

if (GameConfGetKeyValue(gameConfig, "SpriteGlow", buffer, sizeof(buffer)) && buffer[0])
if (gameConfig.GetKeyValue("SpriteGlow", buffer, sizeof(buffer)) && buffer[0])
{
g_GlowSprite = PrecacheModel(buffer);
}

if (GameConfGetKeyValue(gameConfig, "SpriteHalo", buffer, sizeof(buffer)) && buffer[0])
if (gameConfig.GetKeyValue("SpriteHalo", buffer, sizeof(buffer)) && buffer[0])
{
g_HaloSprite = PrecacheModel(buffer);
}
Expand Down
72 changes: 36 additions & 36 deletions plugins/include/entity_prop_stocks.inc
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ stock MoveType GetEntityMoveType(int entity)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_MoveType", datamap, sizeof(datamap));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_MoveType", datamap, sizeof(datamap));
delete gc;

if (!exists)
{
Expand All @@ -235,9 +235,9 @@ stock void SetEntityMoveType(int entity, MoveType mt)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_MoveType", datamap, sizeof(datamap));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_MoveType", datamap, sizeof(datamap));
delete gc;

if (!exists)
{
Expand All @@ -264,9 +264,9 @@ stock RenderMode GetEntityRenderMode(int entity)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_nRenderMode", prop, sizeof(prop));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_nRenderMode", prop, sizeof(prop));
delete gc;

if (!exists)
{
Expand All @@ -293,9 +293,9 @@ stock void SetEntityRenderMode(int entity, RenderMode mode)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_nRenderMode", prop, sizeof(prop));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_nRenderMode", prop, sizeof(prop));
delete gc;

if (!exists)
{
Expand All @@ -322,9 +322,9 @@ stock RenderFx GetEntityRenderFx(int entity)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_nRenderFX", prop, sizeof(prop));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_nRenderFX", prop, sizeof(prop));
delete gc;

if (!exists)
{
Expand All @@ -351,9 +351,9 @@ stock void SetEntityRenderFx(int entity, RenderFx fx)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_nRenderFX", prop, sizeof(prop));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_nRenderFX", prop, sizeof(prop));
delete gc;

if (!exists)
{
Expand Down Expand Up @@ -383,9 +383,9 @@ stock void GetEntityRenderColor(int entity, int &r, int &g, int &b, int &a)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_clrRender", prop, sizeof(prop));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_clrRender", prop, sizeof(prop));
delete gc;

if (!exists)
{
Expand Down Expand Up @@ -425,9 +425,9 @@ stock void SetEntityRenderColor(int entity, int r=255, int g=255, int b=255, int

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_clrRender", prop, sizeof(prop));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_clrRender", prop, sizeof(prop));
delete gc;

if (!exists)
{
Expand Down Expand Up @@ -464,9 +464,9 @@ stock float GetEntityGravity(int entity)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_flGravity", datamap, sizeof(datamap));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_flGravity", datamap, sizeof(datamap));
delete gc;

if (!exists)
{
Expand All @@ -493,9 +493,9 @@ stock void SetEntityGravity(int entity, float amount)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_flGravity", datamap, sizeof(datamap));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_flGravity", datamap, sizeof(datamap));
delete gc;

if (!exists)
{
Expand All @@ -522,9 +522,9 @@ stock void SetEntityHealth(int entity, int amount)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_iHealth", prop, sizeof(prop));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_iHealth", prop, sizeof(prop));
delete gc;

if (!exists)
{
Expand Down Expand Up @@ -578,9 +578,9 @@ stock int GetClientButtons(int client)

if (!gotconfig)
{
Handle gc = LoadGameConfigFile("core.games");
bool exists = GameConfGetKeyValue(gc, "m_nButtons", datamap, sizeof(datamap));
CloseHandle(gc);
GameConf gc = new GameConf("core.games");
bool exists = gc.GetKeyValue("m_nButtons", datamap, sizeof(datamap));
delete gc;

if (!exists)
{
Expand Down
37 changes: 34 additions & 3 deletions plugins/include/sourcemod.inc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,37 @@ enum APLRes
APLRes_SilentFailure /**< Plugin shouldn't load but do so silently */
};

methodmap GameConf < Handle
{
// Loads a game config file.
//
// @param file File to load. The path must be relative to the 'gamedata' folder under the config folder
// and the extension should be omitted.
// @return A handle to the game config file or null on failure.
public native GameConf(const char[] file);

// Returns an offset value.
//
// @param key Key to retrieve from the offset section.
// @return An offset, or -1 on failure.
public native int GetOffset(const char[] key);

// Gets the value of a key from the "Keys" section.
//
// @param key Key to retrieve from the Keys section.
// @param buffer Destination string buffer.
// @param maxlen Maximum length of output string buffer.
// @return True if key existed, false otherwise.
public native bool GetKeyValue(const char[] key, char[] buffer, int maxlen);

// Finds an address calculation in a GameConfig file,
// performs LoadFromAddress on it as appropriate, then returns the final address.
//
// @param name Name of the property to find.
// @return An address calculated on success, or 0 on failure.
public native Address GetAddress(const char[] name);
};

/**
* Called when the plugin is fully initialized and all known external references
* are resolved. This is only called once in the lifetime of the plugin, and is
Expand Down Expand Up @@ -339,9 +370,9 @@ native void FormatTime(char[] buffer, int maxlength, const char[] format, int st
*
* @param file File to load. The path must be relative to the 'gamedata' folder under the config folder
* and the extension should be omitted.
* @return A handle to the game config file or INVALID_HANDLE in failure.
* @return A handle to the game config file or INVALID_HANDLE on failure.
*/
native Handle LoadGameConfigFile(const char[] file);
native GameConf LoadGameConfigFile(const char[] file);

/**
* Returns an offset value.
Expand All @@ -367,7 +398,7 @@ native bool GameConfGetKeyValue(Handle gc, const char[] key, char[] buffer, int
* Finds an address calculation in a GameConfig file,
* performs LoadFromAddress on it as appropriate, then returns the final address.
*
* @param gameconf GameConfig Handle, or INVALID_HANDLE to use sdktools.games.txt.
* @param gameconf Game config handle.
* @param name Name of the property to find.
* @return An address calculated on success, or 0 on failure.
*/
Expand Down

0 comments on commit ab99b8a

Please sign in to comment.