Skip to content

Commit 6406f23

Browse files
author
L'In20Cible
committed
Added a stringtables_c module.
Updated the downloads package.
1 parent d2fe11d commit 6406f23

3 files changed

Lines changed: 309 additions & 10 deletions

File tree

addons/source-python/packages/source-python/downloads.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
# >> IMPORTS
55
# =============================================================================
66
# Source.Python Imports
7-
from Source import Misc
8-
from engine_c import EngineServer
7+
from stringtables_c import StringTables
98
from core import AutoUnload
109
# Events
1110
from events.manager import EventRegistry
@@ -68,19 +67,13 @@ def __init__(self):
6867

6968
def _refresh_table_instance(self):
7069
'''Gets the current instance of the downloadables table'''
71-
self.download_table = Misc.GetStringTables().FindTable('downloadables')
70+
self.download_table = StringTables.downloadables
7271

7372
def _add_to_download_table(self, item):
7473
'''Add the given file to the downloadables table'''
7574

76-
# Lock the network string tables
77-
locked = EngineServer.lock_network_string_tables(False)
78-
7975
# Add the given file
80-
self.download_table.AddString(True, item)
81-
82-
# Reset the lock status
83-
EngineServer.lock_network_string_tables(locked)
76+
self.download_table.add_string(item, item)
8477

8578
def server_spawn(self, game_event):
8679
'''Adds all items stored as downloadables to the stringtable'''

src/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ Set(SOURCEPYTHON_USERMESSAGE_MODULE_SOURCES
293293
core/modules/usermessage/${SOURCE_GAME}/usermessage_implementation.cpp
294294
)
295295

296+
# ------------------------------------------------------------------
297+
# StringTables module.
298+
# ------------------------------------------------------------------
299+
Set(SOURCEPYTHON_STRINGTABLES_MODULE_HEADERS
300+
)
301+
302+
Set(SOURCEPYTHON_STRINGTABLES_MODULE_SOURCES
303+
core/modules/stringtables/stringtables_wrap_python.cpp
304+
)
305+
296306
# ------------------------------------------------------------------
297307
# All module source files
298308
# ------------------------------------------------------------------
@@ -345,6 +355,8 @@ Set(SOURCEPYTHON_MODULE_FILES
345355
${SOURCEPYTHON_USERMESSAGE_MODULE_HEADERS}
346356
${SOURCEPYTHON_USERMESSAGE_MODULE_SOURCES}
347357
${SOURCEPYTHON_MODULE_USERMESSAGES_GAME_SOURCES}
358+
359+
${SOURCEPYTHON_STRINGTABLES_MODULE_SOURCES}
348360
)
349361

350362
# ------------------------------------------------------------------
@@ -371,6 +383,7 @@ Source_Group("Header Files\\Module\\Memory" FILES ${SOURCEPYTHON
371383
Source_Group("Header Files\\Module\\Player" FILES ${SOURCEPYTHON_PLAYERS_MODULE_HEADERS})
372384
Source_Group("Header Files\\Module\\RecipientFilter" FILES ${SOURCEPYTHON_RECIPIENTFILTER_MODULE_HEADERS})
373385
Source_Group("Header Files\\Module\\Usermessage" FILES ${SOURCEPYTHON_USERMESSAGE_MODULE_HEADERS})
386+
Source_Group("Header Files\\Module\\StringTables" FILES ${SOURCEPYTHON_STRINGTABLES_MODULE_HEADERS})
374387

375388
Source_Group("Source Files\\Addons" FILES ${SOURCEPYTHON_ADDON_SOURCES})
376389
Source_Group("Source Files\\Core" FILES ${SOURCEPYTHON_CORE_SOURCES})
@@ -393,6 +406,7 @@ Source_Group("Source Files\\Module\\Player" FILES ${SOURCEPYTHON
393406
Source_Group("Source Files\\Module\\RecipientFilter" FILES ${SOURCEPYTHON_RECIPIENTFILTER_MODULE_SOURCES})
394407
Source_Group("Source Files\\Module\\Usermessage" FILES ${SOURCEPYTHON_USERMESSAGE_MODULE_SOURCES})
395408
Source_Group("Source Files\\Module\\Usermessage\\${SDK}" FILES ${SOURCEPYTHON_MODULE_USERMESSAGES_GAME_SOURCES})
409+
Source_Group("Source Files\\Module\\StringTables" FILES ${SOURCEPYTHON_MODULE_STRINGTABLES_GAME_SOURCES})
396410

397411
# ------------------------------------------------------------------
398412
# All SourcePython source files. Ideally we break out each group of
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
/**
2+
* =============================================================================
3+
* Source Python
4+
* Copyright (C) 2014 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+
#include "../export_main.h"
31+
#include "utility/wrap_macros.h"
32+
#include "networkstringtabledefs.h"
33+
#include "eiface.h"
34+
35+
//---------------------------------------------------------------------------------
36+
// External variables to use.
37+
//---------------------------------------------------------------------------------
38+
extern INetworkStringTableContainer *networkstringtable;
39+
extern IVEngineServer* engine;
40+
41+
//---------------------------------------------------------------------------------
42+
// Forward declarations.
43+
//---------------------------------------------------------------------------------
44+
void export_stringtable();
45+
void export_stringtable_container();
46+
47+
//---------------------------------------------------------------------------------
48+
// Declares the stringtables_c module.
49+
//---------------------------------------------------------------------------------
50+
DECLARE_SP_MODULE(stringtables_c)
51+
{
52+
export_stringtable();
53+
export_stringtable_container();
54+
}
55+
56+
//---------------------------------------------------------------------------------
57+
// Add a string to the specified table.
58+
//---------------------------------------------------------------------------------
59+
int AddString( INetworkStringTable *pTable, const char *string, const char *user_data, int length, bool is_server, bool auto_unlock )
60+
{
61+
int index = INVALID_STRING_INDEX;
62+
bool locked = false;
63+
if (auto_unlock)
64+
{
65+
locked = engine->LockNetworkStringTables(false);
66+
}
67+
if (pTable->FindStringIndex(string) == INVALID_STRING_INDEX && length == -1)
68+
{
69+
length = strlen(user_data) + 1;
70+
}
71+
index = pTable->AddString(is_server, string, length, (const void *)user_data);
72+
if (locked && auto_unlock)
73+
{
74+
engine->LockNetworkStringTables(locked);
75+
}
76+
return index;
77+
}
78+
79+
//---------------------------------------------------------------------------------
80+
// Sets the user data of the given string index.
81+
//---------------------------------------------------------------------------------
82+
void SetStringIndexUserData( INetworkStringTable *pTable, int string_index, const char *user_data, int length )
83+
{
84+
if (length == -1)
85+
{
86+
length = strlen(user_data) + 1;
87+
}
88+
pTable->SetStringUserData(string_index, length, user_data);
89+
}
90+
91+
//---------------------------------------------------------------------------------
92+
// Sets the user data of the given string.
93+
//---------------------------------------------------------------------------------
94+
void SetStringUserData( INetworkStringTable *pTable, const char *string, const char *user_data, int length )
95+
{
96+
int string_index = pTable->FindStringIndex(string);
97+
if (string_index == INVALID_STRING_INDEX)
98+
{
99+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Given string not found.")
100+
}
101+
SetStringIndexUserData(pTable, string_index, user_data, length);
102+
}
103+
104+
//---------------------------------------------------------------------------------
105+
// Returns the user data of the given string index.
106+
//---------------------------------------------------------------------------------
107+
const char *GetStringIndexUserData( INetworkStringTable *pTable, int string_index )
108+
{
109+
return (const char *)pTable->GetStringUserData(string_index, NULL);
110+
}
111+
112+
//---------------------------------------------------------------------------------
113+
// Returns the user data of the given string.
114+
//---------------------------------------------------------------------------------
115+
const char *GetStringUserData( INetworkStringTable *pTable, const char *string )
116+
{
117+
int index = pTable->FindStringIndex(string);
118+
if (index == INVALID_STRING_INDEX)
119+
{
120+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Given string not found.")
121+
}
122+
return GetStringIndexUserData(pTable, index);
123+
}
124+
125+
//---------------------------------------------------------------------------------
126+
// Returns the length of the user data of the given string index.
127+
//---------------------------------------------------------------------------------
128+
int GetStringIndexUserDataLength( INetworkStringTable *pTable, int string_index )
129+
{
130+
int length = 0;
131+
const void * user_data = pTable->GetStringUserData(string_index, &length);
132+
return length;
133+
}
134+
135+
//---------------------------------------------------------------------------------
136+
// Returns the user data of the given string.
137+
//---------------------------------------------------------------------------------
138+
int GetStringUserDataLength( INetworkStringTable *pTable, const char *string )
139+
{
140+
int index = pTable->FindStringIndex(string);
141+
if (index == INVALID_STRING_INDEX)
142+
{
143+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Given string not found.")
144+
}
145+
return GetStringIndexUserDataLength(pTable, index);
146+
}
147+
148+
//---------------------------------------------------------------------------------
149+
// Exposes INetworkStringTable.
150+
//---------------------------------------------------------------------------------
151+
void export_stringtable()
152+
{
153+
class_<INetworkStringTable, boost::noncopyable>("StringTable", no_init)
154+
155+
.add_property("name",
156+
&INetworkStringTable::GetTableName,
157+
"Returns the name of the table (read-only)."
158+
)
159+
160+
.add_property("index",
161+
&INetworkStringTable::GetTableId,
162+
"Returns the index of the table (read-only)."
163+
)
164+
165+
.def("__len__",
166+
&INetworkStringTable::GetNumStrings,
167+
"Returns the length of the table."
168+
)
169+
170+
.add_property("max_strings",
171+
&INetworkStringTable::GetMaxStrings,
172+
"Returns the max length of the table (read-only)."
173+
)
174+
175+
.add_property("entry_bits",
176+
&INetworkStringTable::GetEntryBits,
177+
"Returns the maximum entries of the table (read-only)."
178+
)
179+
180+
.def("set_tick",
181+
&INetworkStringTable::SetTick,
182+
"Set the tick on which the table has been last modified.",
183+
("tick")
184+
)
185+
186+
.def("changed_since_tick",
187+
&INetworkStringTable::SetTick,
188+
"Returns True if the table has been modified since the given tick, False otherwise.",
189+
("tick")
190+
)
191+
192+
.def("add_string",
193+
&AddString,
194+
"Adds the given string to the table.",
195+
("string", arg("user_data")=object(), arg("length")=-1, arg("is_server")=true, arg("auto_unlock")=true)
196+
)
197+
198+
.def("__getitem__",
199+
&INetworkStringTable::GetString,
200+
"Returns the string at the given index.",
201+
("string_index")
202+
)
203+
204+
.def("set_user_data",
205+
&SetStringIndexUserData,
206+
"Sets the user data of the given string index.",
207+
("string_index", "user_data", arg("length")=-1)
208+
)
209+
210+
.def("set_user_data",
211+
&SetStringUserData,
212+
"Sets the user data of the given string.",
213+
("string", "user_data", arg("length")=-1)
214+
)
215+
216+
.def("get_user_data",
217+
&GetStringIndexUserData,
218+
"Returns the user data of the given string index.",
219+
("string_index")
220+
)
221+
222+
.def("get_user_data",
223+
&GetStringUserData,
224+
"Returns the user data of the given string.",
225+
("string")
226+
)
227+
228+
.def("__getitem__",
229+
&INetworkStringTable::FindStringIndex,
230+
"Returns the index of the given string (returns INVALID_STRING_INDEX if not found).",
231+
("string")
232+
)
233+
234+
.def("get_user_data_length",
235+
&GetStringIndexUserDataLength,
236+
"Returns the length of the user data of the given string index.",
237+
("string_index")
238+
)
239+
240+
.def("get_user_data_length",
241+
&GetStringUserDataLength,
242+
"Returns the length of the user data of the given string.",
243+
("string")
244+
)
245+
;
246+
}
247+
248+
//---------------------------------------------------------------------------------
249+
// Exposes INetworkStringTableContainer.
250+
//---------------------------------------------------------------------------------
251+
void export_stringtable_container()
252+
{
253+
class_<INetworkStringTableContainer, boost::noncopyable>("_StringTables", no_init)
254+
255+
.def("__getitem__",
256+
&INetworkStringTableContainer::GetTable,
257+
"Returns the StringTable instance of the given table ID.",
258+
("table_index"),
259+
reference_existing_object_policy()
260+
)
261+
262+
.def("__getitem__",
263+
&INetworkStringTableContainer::FindTable,
264+
"Returns the StringTable instance of the given table name.",
265+
("table_name"),
266+
reference_existing_object_policy()
267+
)
268+
269+
.def("__getattr__",
270+
&INetworkStringTableContainer::FindTable,
271+
"Returns the StringTable instance of the given table name.",
272+
("table_name"),
273+
reference_existing_object_policy()
274+
)
275+
276+
.def("__len__",
277+
&INetworkStringTableContainer::GetNumTables,
278+
"Returns the length of the container."
279+
)
280+
281+
// Do we really need this?
282+
.def("set_allow_client_side_add_string",
283+
&INetworkStringTableContainer::SetAllowClientSideAddString,
284+
"Allows clients to add strings to the given table.",
285+
("table", "state")
286+
)
287+
;
288+
289+
// Singleton.
290+
scope().attr("StringTables") = object(ptr(networkstringtable));
291+
292+
}

0 commit comments

Comments
 (0)