-
Notifications
You must be signed in to change notification settings - Fork 37
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
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
76e3b5a
Add back game rules
Ayuto 3a9bcb3
Initial commit
Ayuto c9a84fc
Merge branch 'master' into gamerules
Ayuto 0f13f64
Moved stringtables stuff to the appropriate files
Ayuto 7777708
Added static method ServerClass.find_server_class()
Ayuto 4c0b667
Added back those two
Ayuto fceb3a6
Reimplemented gamerules functions using string tables
Ayuto 1764880
Merge branch 'master' into gamerules
Ayuto c48d7dc
Cleanup, sanity checks and typos
Ayuto 7a72777
Fixed spacing
Ayuto 4a466cc
Cache SendTable
Ayuto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...ource-python/docs/source-python/source/developing/modules/engines.gamerules.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
addons/source-python/packages/source-python/engines/gamerules.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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',) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* ============================================================================= | ||
* 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) | ||
{ | ||
ServerClass* cls = ServerClassExt::find_server_class(find_game_rules_proxy_name()); | ||
int offset = SendTableSharedExt::find_offset(cls->m_pTable, 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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.