Skip to content

Add access to CGameRules and its properties #363

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

Merged
merged 11 commits into from
Dec 21, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
engines.gamerules module
=========================

.. automodule:: engines.gamerules
:members:
:undoc-members:
:show-inheritance:
23 changes: 23 additions & 0 deletions addons/source-python/packages/source-python/engines/gamerules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ../engines/gamerules.py

"""Provides access to the gamerules instance."""

# =============================================================================
# >> FORWARD IMPORTS
# =============================================================================
# Source.Python Imports
# Engines
from _engines._gamerules import GameRules
from _engines._gamerules import find_game_rules_property_offset
from _engines._gamerules import find_game_rules
from _engines._gamerules import find_game_rules_proxy_name


# =============================================================================
# >> ALL DECLARATION
# =============================================================================
__all__ = (
'GameRules',
'find_game_rules_property_offset',
'find_game_rules',
'find_game_rules_proxy_name',)
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ Set(SOURCEPYTHON_ENGINES_MODULE_HEADERS
core/modules/engines/engines_server.h
core/modules/engines/${SOURCE_ENGINE}/engines.h
core/modules/engines/${SOURCE_ENGINE}/engines_wrap.h
core/modules/engines/engines_gamerules.h
)

Set(SOURCEPYTHON_ENGINES_MODULE_SOURCES
Expand All @@ -203,6 +204,8 @@ Set(SOURCEPYTHON_ENGINES_MODULE_SOURCES
core/modules/engines/engines_server_wrap.cpp
core/modules/engines/engines_sound_wrap.cpp
core/modules/engines/engines_trace_wrap.cpp
core/modules/engines/engines_gamerules.cpp
core/modules/engines/engines_gamerules_wrap.cpp
)

# ------------------------------------------------------------------
Expand Down Expand Up @@ -435,9 +438,11 @@ Set(SOURCEPYTHON_STEAM_MODULE_SOURCES
# StringTables module.
# ------------------------------------------------------------------
Set(SOURCEPYTHON_STRINGTABLES_MODULE_HEADERS
core/modules/stringtables/stringtables.h
)

Set(SOURCEPYTHON_STRINGTABLES_MODULE_SOURCES
core/modules/stringtables/stringtables.cpp
core/modules/stringtables/stringtables_wrap.cpp
)

Expand Down
125 changes: 125 additions & 0 deletions src/core/modules/engines/engines_gamerules.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2020 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/

//-----------------------------------------------------------------------------
// Includes.
//-----------------------------------------------------------------------------
// Source.Python
#include "engines_gamerules.h"
#include "modules/entities/entities_props.h"
#include "modules/stringtables/stringtables.h"
#include "utilities/wrap_macros.h"

// Boost.Python
#include "boost/python.hpp"
using namespace boost::python;

// SDK
#include "server_class.h"


//---------------------------------------------------------------------------------
// External variables to use.
//---------------------------------------------------------------------------------
extern INetworkStringTableContainer *networkstringtable;


//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
int find_game_rules_property_offset(const char* name)
{
static SendTable* s_table = NULL;
if (!s_table)
{
ServerClass* cls = ServerClassExt::find_server_class(find_game_rules_proxy_name());
s_table = cls->m_pTable;
}

int offset = SendTableSharedExt::find_offset(s_table, name);

if (offset == -1)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to find property '%s'.", name)

return offset;
}

const char* find_game_rules_proxy_name()
{
static std::string s_proxy_name;
if (!s_proxy_name.empty())
// Use cache
return s_proxy_name.c_str();

INetworkStringTable* table = networkstringtable->FindTable("GameRulesCreation");
if (!table)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to find string table 'GameRulesCreation'.")

s_proxy_name = INetworkStringTableExt::GetStringUserData(table, "classname");
if (s_proxy_name.empty())
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "'classname' of string table 'GameRulesCreation' is NULL.")

s_proxy_name += "Proxy";
return s_proxy_name.c_str();
}

SendTableProxyFn find_game_rules_proxy_function()
{
SendTableProxyFn s_proxy_func = NULL;
if (s_proxy_func)
// Use cache
return s_proxy_func;

ServerClass* cls = ServerClassExt::find_server_class(find_game_rules_proxy_name());
SendTable* table = cls->m_pTable;

for (int i=0; i < table->GetNumProps(); i++)
{
SendProp* prop = table->GetProp(i);
if (!V_stristr(prop->GetName(), "gamerules_data"))
continue;

s_proxy_func = prop->GetDataTableProxyFn();
break;
}

if (!s_proxy_func)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Game rules proxy function is NULL.");

return s_proxy_func;
}

CGameRulesWrapper* find_game_rules()
{
SendTableProxyFn proxy_func = find_game_rules_proxy_function();
static CSendProxyRecipients recipients;

CGameRulesWrapper* game_rules = (CGameRulesWrapper*) proxy_func(NULL, NULL, NULL, &recipients, 0);
if (!game_rules)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Game rules pointer is NULL.");

return game_rules;
}
101 changes: 101 additions & 0 deletions src/core/modules/engines/engines_gamerules.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2020 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/

#ifndef _ENGINES_GAMERULES_H
#define _ENGINES_GAMERULES_H

//-----------------------------------------------------------------------------
// Includes.
//-----------------------------------------------------------------------------
// SDK
#include "strtools.h"


//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
class CGameRulesWrapper;

int find_game_rules_property_offset(const char* name);
const char* find_game_rules_proxy_name();
CGameRulesWrapper* find_game_rules();


//-----------------------------------------------------------------------------
// Classes.
//-----------------------------------------------------------------------------
class CGameRulesWrapper
{
public:

// Getter methods
template<class T>
T GetProperty(const char* name)
{
return GetPropertyByOffset<T>(find_game_rules_property_offset(name));
}

template<class T>
T GetPropertyByOffset(int offset)
{
return *(T *) (((unsigned long) this) + offset);
}

const char* GetPropertyStringArray(const char* name)
{
return GetPropertyStringArrayByOffset(find_game_rules_property_offset(name));
}

const char* GetPropertyStringArrayByOffset(int offset)
{
return (const char*) (((unsigned long) this) + offset);
}

// Setter methods
template<class T>
void SetProperty(const char* name, T value)
{
SetPropertyByOffset<T>(find_game_rules_property_offset(name), value);
}

template<class T>
void SetPropertyByOffset(int offset, T value)
{
*(T *) (((unsigned long) this) + offset) = value;
}

void SetPropertyStringArray(const char* name, const char* value)
{
SetPropertyStringArrayByOffset(find_game_rules_property_offset(name), value);
}

void SetPropertyStringArrayByOffset(int offset, const char* value)
{
strcpy((char*) (((unsigned long) this) + offset), value);
}
};

#endif // _ENGINES_GAMERULES_H
Loading