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