Skip to content

Commit 4a9c236

Browse files
committed
Fixed issue #146
1 parent a7c6a62 commit 4a9c236

File tree

9 files changed

+195
-38
lines changed

9 files changed

+195
-38
lines changed

addons/source-python/packages/source-python/config/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Cvars
2020
from cvars import ConVar
2121
# Engines
22-
from engines.server import engine_server
22+
from engines.server import queue_command_string
2323
# Hooks
2424
from hooks.exceptions import except_hooks
2525
# Paths
@@ -291,7 +291,7 @@ def execute(self):
291291
if name in self._commands:
292292

293293
# Execute the line
294-
engine_server.server_command(line + '\n')
294+
queue_command_string(line)
295295

296296
# Is this a cvar
297297
else:

addons/source-python/packages/source-python/core/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,8 @@ def __init__(self, infile, *args, **kwargs):
146146
# =============================================================================
147147
def echo_console(text):
148148
"""Echo a message to the server's console."""
149-
# Import engine_server
150149
# This is done here to fix an ImportError
151-
from engines.server import engine_server
150+
from engines.server import execute_server_command
152151

153-
# Loop through each line in the text
154152
for line in text.split('\n'):
155-
156-
# Echo the message
157-
engine_server.server_command(
158-
'echo "{0}"\n'.format(line.replace('"', "'")))
153+
execute_server_command('echo', '"{0}"'.format(line.replace('"', "'")))

addons/source-python/packages/source-python/core/command/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
# Cvars
2020
from cvars import ConVar
2121
# Engines
22-
from engines.server import engine_server
22+
from engines.server import execute_server_command
23+
from engines.server import queue_command_string
2324
# Paths
2425
from paths import SP_DATA_PATH
2526
# Players
@@ -143,7 +144,7 @@ def print_credits(self):
143144
@_core_command.server_sub_command(['delay'])
144145
def _sp_delay(command_info, delay:float, command, *args):
145146
"""Execute a command after a given delay."""
146-
Delay(delay, engine_server.server_command, command + ' ' + ' '.join(args))
147+
Delay(delay, queue_command_string, command + ' ' + ' '.join(args))
147148

148149
@_core_command.server_sub_command(['version'])
149150
def _sp_version(command_info):
@@ -160,7 +161,7 @@ def _sp_credits(command_info):
160161
def _sp_help(command_info, command=None, *server_sub_commands):
161162
"""Print all sp sub-commands or help for a specific command."""
162163
if command is None:
163-
engine_server.server_command('sp')
164+
execute_server_command('sp')
164165
return
165166

166167
commands = (command,) + server_sub_commands

addons/source-python/packages/source-python/engines/server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from _engines._server import Server
1313
from _engines._server import engine_server
1414
from _engines._server import server_game_dll
15+
from _engines._server import execute_server_command
16+
from _engines._server import queue_command_string
17+
from _engines._server import queue_server_command
1518
# Globals
1619
from _globals import MapLoadType
1720
from _globals import global_vars
@@ -26,7 +29,10 @@
2629
'Server',
2730
'server',
2831
'engine_server',
32+
'execute_server_command',
2933
'global_vars',
34+
'queue_command_string',
35+
'queue_server_command',
3036
'server_game_dll',
3137
)
3238

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,14 @@ Set(SOURCEPYTHON_EFFECTS_MODULE_SOURCES
174174
# ------------------------------------------------------------------
175175
Set(SOURCEPYTHON_ENGINES_MODULE_HEADERS
176176
core/modules/engines/engines.h
177+
core/modules/engines/engines_server.h
177178
core/modules/engines/${SOURCE_ENGINE}/engines.h
178179
core/modules/engines/${SOURCE_ENGINE}/engines_wrap.h
179180
)
180181

181182
Set(SOURCEPYTHON_ENGINES_MODULE_SOURCES
182183
core/modules/engines/engines_wrap.cpp
184+
core/modules/engines/engines_server.cpp
183185
core/modules/engines/engines_server_wrap.cpp
184186
core/modules/engines/engines_sound_wrap.cpp
185187
core/modules/engines/engines_trace_wrap.cpp
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* =============================================================================
3+
* Source Python
4+
* Copyright (C) 2012-2016 Source Python Development Team. All rights reserved.
5+
* =============================================================================
6+
*
7+
* This program is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, version 3.0, as published by the
9+
* Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU General Public License along with
17+
* this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* As a special exception, the Source Python Team gives you permission
20+
* to link the code of this program (as well as its derivative works) to
21+
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
22+
* by the Valve Corporation. You must obey the GNU General Public License in
23+
* all respects for all other code used. Additionally, the Source.Python
24+
* Development Team grants this exception to all derivative works.
25+
*/
26+
27+
//-----------------------------------------------------------------------------
28+
// Includes.
29+
//-----------------------------------------------------------------------------
30+
// Source.Python
31+
#include "engines_server.h"
32+
#include "utilities/wrap_macros.h"
33+
34+
// SDK
35+
#include "convar.h"
36+
#include "eiface.h"
37+
38+
39+
//-----------------------------------------------------------------------------
40+
// Externals
41+
//-----------------------------------------------------------------------------
42+
extern IVEngineServer* engine;
43+
44+
45+
//-----------------------------------------------------------------------------
46+
// Commands to execute server commands
47+
//-----------------------------------------------------------------------------
48+
void prepare_command(tuple args, dict kwargs, ConCommand** pCommandOut, std::string* szCommandOut)
49+
{
50+
if (kwargs)
51+
BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Function does not accept keywords.")
52+
53+
const char* command = extract<const char*>(args[0]);
54+
ConCommand* pCommand = cvar->FindCommand(command);
55+
if (!pCommand)
56+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to find command '%s'.", command);
57+
58+
*pCommandOut = pCommand;
59+
60+
*szCommandOut = "";
61+
for(int i=0; i < len(args); ++i)
62+
{
63+
const char* temp = extract<const char*>(str(args[i]));
64+
*szCommandOut += temp;
65+
*szCommandOut += " ";
66+
}
67+
}
68+
69+
object execute_server_command(tuple args, dict kwargs)
70+
{
71+
std::string szCommand;
72+
ConCommand* pCommand;
73+
prepare_command(args, kwargs, &pCommand, &szCommand);
74+
75+
CCommand c;
76+
if (!c.Tokenize(szCommand.c_str()))
77+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to tokenize '%s'.", szCommand.c_str())
78+
79+
pCommand->Dispatch(c);
80+
return object();
81+
}
82+
83+
object queue_server_command(tuple args, dict kwargs)
84+
{
85+
std::string szCommand;
86+
ConCommand* pCommand;
87+
prepare_command(args, kwargs, &pCommand, &szCommand);
88+
szCommand += ";";
89+
engine->ServerCommand(szCommand.c_str());
90+
return object();
91+
}
92+
93+
void queue_command_string(const char* szCommand)
94+
{
95+
std::string command = szCommand;
96+
command += ";";
97+
engine->ServerCommand(command.c_str());
98+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* =============================================================================
3+
* Source Python
4+
* Copyright (C) 2012-2016 Source Python Development Team. All rights reserved.
5+
* =============================================================================
6+
*
7+
* This program is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, version 3.0, as published by the
9+
* Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU General Public License along with
17+
* this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
* As a special exception, the Source Python Team gives you permission
20+
* to link the code of this program (as well as its derivative works) to
21+
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
22+
* by the Valve Corporation. You must obey the GNU General Public License in
23+
* all respects for all other code used. Additionally, the Source.Python
24+
* Development Team grants this exception to all derivative works.
25+
*/
26+
27+
#ifndef _ENGINES_SERVER_H
28+
#define _ENGINES_SERVER_H
29+
30+
//-----------------------------------------------------------------------------
31+
// Includes.
32+
//-----------------------------------------------------------------------------
33+
#include "boost/python.hpp"
34+
using namespace boost::python;
35+
36+
37+
//-----------------------------------------------------------------------------
38+
// Functions.
39+
//-----------------------------------------------------------------------------
40+
// Example: execute_server_command('echo', 'hi', 123, 4.5)
41+
object execute_server_command(tuple args, dict kwargs);
42+
43+
// Example: queue_server_command('echo', 'hi', 123, 4.5)
44+
object queue_server_command(tuple args, dict kwargs);
45+
46+
// Example: queue_command_string('blah hi 123 4.5')
47+
void queue_command_string(const char* szCommand);
48+
49+
#endif // _ENGINES_SERVER_H

src/core/modules/engines/engines_server_wrap.cpp

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <Windows.h>
3333
#endif
3434

35+
#include "engines_server.h"
3536
#include "export_main.h"
3637
#include "utilities/conversions.h"
3738

@@ -65,10 +66,11 @@ extern IServerGameDLL* servergamedll;
6566
//---------------------------------------------------------------------------------
6667
// Forward declarations.
6768
//---------------------------------------------------------------------------------
68-
void export_engine_server(scope);
69-
void export_query_cvar_status(scope);
70-
void export_server_game_dll(scope);
71-
void export_iserver(scope);
69+
static void export_engine_server(scope);
70+
static void export_query_cvar_status(scope);
71+
static void export_server_game_dll(scope);
72+
static void export_iserver(scope);
73+
static void export_functions(scope);
7274

7375

7476
//---------------------------------------------------------------------------------
@@ -80,13 +82,14 @@ DECLARE_SP_SUBMODULE(_engines, _server)
8082
export_query_cvar_status(_server);
8183
export_server_game_dll(_server);
8284
export_iserver(_server);
85+
export_functions(_server);
8386
}
8487

8588

8689
//---------------------------------------------------------------------------------
8790
// Exports IVEngineServer.
8891
//---------------------------------------------------------------------------------
89-
void export_engine_server(scope _server)
92+
static void export_engine_server(scope _server)
9093
{
9194
// Call engine specific implementation function
9295
IVEngineServer_Visitor(
@@ -248,17 +251,6 @@ void export_engine_server(scope _server)
248251
args("sentence_index")
249252
)
250253

251-
.def("server_command",
252-
&IVEngineServerExt::ServerCommand,
253-
"Issues a command to the command parser as if it was typed at the server console.",
254-
args("command")
255-
)
256-
257-
.def("server_execute",
258-
&IVEngineServer::ServerExecute,
259-
"Executes any commands currently in the command parser immediately (instead of once per frame)."
260-
)
261-
262254
.def("client_command",
263255
&IVEngineServerExt::ClientCommand,
264256
"Runs a command on the client.",
@@ -555,12 +547,6 @@ void export_engine_server(scope _server)
555547
args("edict", "cvar_name")
556548
)
557549

558-
.def("insert_server_command",
559-
&IVEngineServerExt::InsertServerCommand,
560-
"Inserts a command into the server's command buffer.",
561-
args("command_string")
562-
)
563-
564550
.def("get_player_info",
565551
&IVEngineServer::GetPlayerInfo,
566552
"Fill in the player info structure for the specified player.",
@@ -777,7 +763,7 @@ void export_engine_server(scope _server)
777763
//---------------------------------------------------------------------------------
778764
// Exports EQueryCvarValueStatus.
779765
//---------------------------------------------------------------------------------
780-
void export_query_cvar_status(scope _server)
766+
static void export_query_cvar_status(scope _server)
781767
{
782768
enum_<EQueryCvarValueStatus> QueryCvarStatus("QueryCvarStatus");
783769

@@ -792,7 +778,7 @@ void export_query_cvar_status(scope _server)
792778
//-----------------------------------------------------------------------------
793779
// Exports IServerGameDLL.
794780
//-----------------------------------------------------------------------------
795-
void export_server_game_dll(scope _server)
781+
static void export_server_game_dll(scope _server)
796782
{
797783
class_<IServerGameDLL, boost::noncopyable> ServerGameDLL("_ServerGameDLL", no_init);
798784

@@ -826,7 +812,7 @@ void export_server_game_dll(scope _server)
826812
//-----------------------------------------------------------------------------
827813
// Exports IServer.
828814
//-----------------------------------------------------------------------------
829-
void export_iserver(scope _server)
815+
static void export_iserver(scope _server)
830816
{
831817
class_<IConnectionlessPacketHandler, IConnectionlessPacketHandler*, boost::noncopyable> _IConnectionlessPacketHandler("ConnectionlessPacketHandler", no_init);
832818

@@ -1003,3 +989,23 @@ void export_iserver(scope _server)
1003989

1004990
_IServer ADD_MEM_TOOLS(IServer)
1005991
}
992+
993+
994+
//-----------------------------------------------------------------------------
995+
// Exports functions.
996+
//-----------------------------------------------------------------------------
997+
static void export_functions(scope _server)
998+
{
999+
def("execute_server_command",
1000+
raw_function(execute_server_command, 1)
1001+
);
1002+
1003+
def("queue_server_command",
1004+
raw_function(queue_server_command, 1)
1005+
);
1006+
1007+
def("queue_command_string",
1008+
&queue_command_string,
1009+
"Queue a string for execution."
1010+
);
1011+
}

src/core/sp_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ void CSourcePython::UnPause( void )
311311
//-----------------------------------------------------------------------------
312312
const char *CSourcePython::GetPluginDescription( void )
313313
{
314-
return "Source.Python, (C) 2012-2016, Source Python Team.";
314+
return "Source.Python, (C) 2012-2016, Source.Python Team.";
315315
}
316316

317317
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)