Landed sourcepawn-1.2. The big changes:
1) JIT compilation/optimization now occurs per-function, and only when functions are first used. We're now officially a whole-method JIT rather than an AOT compiler (albiet, still a simple JIT). This has two implications: Functions are now much better abstracted internally, and loading a plugin is now much less expensive. If a function contains calls to other functions, THOSE functions are only compiled when they're invoked as well.
2) I've removed debug mode. We always show full backtraces now, as there was a very cheap way to implement this which really cleaned up everything. This is great for a number of reasons -- there's less code, the JIT is better designed, we don't need to relocate debug tables, and best of all we no longer have to tell users to enable debug mode at their own expense.
3 * =============================================================================
4 * SourceMod SQL Admins Plugin (Prefetch)
5 * Prefetches admins from an SQL database without threading.
7 * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
8 * =============================================================================
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, version 3.0, as published by the
12 * Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
22 * As a special exception, AlliedModders LLC gives you permission to link the
23 * code of this program (as well as its derivative works) to "Half-Life 2," the
24 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
25 * by the Valve Corporation. You must obey the GNU General Public License in
26 * all respects for all other code used. Additionally, AlliedModders LLC grants
27 * this exception to all derivative works. AlliedModders LLC defines further
28 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
29 * or <http://www.sourcemod.net/license.php>.
34 /* We like semicolons */
39 public Plugin:myinfo =
41 name = "SQL Admins (Prefetch)",
42 author = "AlliedModders LLC",
43 description = "Reads all admins from SQL",
44 version = SOURCEMOD_VERSION,
45 url = "http://www.sourcemod.net/"
48 public OnRebuildAdminCache(AdminCachePart:part)
50 /* First try to get a database connection */
51 decl String:error[255];
54 if (SQL_CheckConfig("admins"))
56 db = SQL_Connect("admins", true, error, sizeof(error));
58 db = SQL_Connect("default", true, error, sizeof(error));
61 if (db == INVALID_HANDLE)
63 LogError("Could not connect to database \"default\": %s", error);
67 if (part == AdminCache_Overrides)
70 } else if (part == AdminCache_Groups) {
72 } else if (part == AdminCache_Admins) {
79 FetchUserGroups(Handle:hQuery, AdminId:adm, id)
81 SQL_BindParamInt(hQuery, 0, id);
82 if (!SQL_Execute(hQuery))
84 decl String:error[255];
85 SQL_GetError(hQuery, error, sizeof(error));
86 LogError("FetchUserGroups() statement execute failed: %s", error);
92 while (SQL_FetchRow(hQuery))
94 SQL_FetchString(hQuery, 0, name, sizeof(name));
96 if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
98 /* Group wasn't found, don't bother with it. */
103 PrintToServer("Found admin group (%d, %d, %d)", id, gid, adm);
106 AdminInheritGroup(adm, gid);
110 FetchUsers(Handle:db)
112 decl String:query[255], String:error[255];
113 new Handle:hQuery, Handle:hGroupQuery;
115 Format(query, sizeof(query), "SELECT id, authtype, identity, password, flags, name, immunity FROM sm_admins");
116 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
118 SQL_GetError(db, error, sizeof(error));
119 LogError("FetchUsers() query failed: %s", query);
120 LogError("Query error: %s", error);
124 /* If this fails it's non-fatal */
127 "SELECT g.name FROM sm_admins_groups ag JOIN sm_groups g ON ag.group_id = g.id WHERE ag.admin_id = ? ORDER BY inherit_order ASC");
128 if ((hGroupQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == INVALID_HANDLE)
130 LogError("FetchUsers() group query preparation failed: %s", query);
131 LogError("Query error: %s", error);
134 decl String:authtype[16];
135 decl String:identity[80];
136 decl String:password[80];
137 decl String:flags[32];
138 decl String:name[80];
142 while (SQL_FetchRow(hQuery))
144 id = SQL_FetchInt(hQuery, 0);
145 SQL_FetchString(hQuery, 1, authtype, sizeof(authtype));
146 SQL_FetchString(hQuery, 2, identity, sizeof(identity));
147 SQL_FetchString(hQuery, 3, password, sizeof(password));
148 SQL_FetchString(hQuery, 4, flags, sizeof(flags));
149 SQL_FetchString(hQuery, 5, name, sizeof(name));
150 immunity = SQL_FetchInt(hQuery, 6);
152 /* Use a pre-existing admin if we can */
153 if ((adm = FindAdminByIdentity(authtype, identity)) == INVALID_ADMIN_ID)
155 adm = CreateAdmin(name);
156 if (!BindAdminIdentity(adm, authtype, identity))
158 LogError("Could not bind prefetched SQL admin (authtype \"%s\") (identity \"%s\")", authtype, identity);
164 PrintToServer("Found SQL admin (%d,%s,%s,%s,%s,%s,%d):%d", id, authtype, identity, password, flags, name, immunity, adm);
167 /* See if this admin wants a password */
168 if (password[0] != '\0')
170 SetAdminPassword(adm, password);
173 /* Apply each flag */
174 new len = strlen(flags);
176 for (new i=0; i<len; i++)
178 if (!FindFlagByChar(flags[i], flag))
182 SetAdminFlag(adm, flag, true);
185 SetAdminImmunityLevel(adm, immunity);
188 if (hGroupQuery != INVALID_HANDLE)
190 FetchUserGroups(hGroupQuery, adm, id);
194 CloseHandle(hGroupQuery);
198 FetchGroups(Handle:db)
200 decl String:query[255];
203 Format(query, sizeof(query), "SELECT flags, name, immunity_level FROM sm_groups");
205 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
207 decl String:error[255];
208 SQL_GetError(db, error, sizeof(error));
209 LogError("FetchGroups() query failed: %s", query);
210 LogError("Query error: %s", error);
214 /* Now start fetching groups */
215 decl String:flags[32];
216 decl String:name[128];
218 while (SQL_FetchRow(hQuery))
220 SQL_FetchString(hQuery, 0, flags, sizeof(flags));
221 SQL_FetchString(hQuery, 1, name, sizeof(name));
222 immunity = SQL_FetchInt(hQuery, 2);
225 PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name);
228 /* Find or create the group */
230 if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
232 gid = CreateAdmGroup(name);
235 /* Add flags from the database to the group */
236 new num_flag_chars = strlen(flags);
237 for (new i=0; i<num_flag_chars; i++)
240 if (!FindFlagByChar(flags[i], flag))
244 SetAdmGroupAddFlag(gid, flag, true);
247 /* Set the immunity level this group has */
248 SetAdmGroupImmunityLevel(gid, immunity);
254 * Get immunity in a big lump. This is a nasty query but it gets the job done.
257 len += Format(query[len], sizeof(query)-len, "SELECT g1.name, g2.name FROM sm_group_immunity gi");
258 len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g1 ON g1.id = gi.group_id ");
259 len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g2 ON g2.id = gi.other_id");
261 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
263 decl String:error[255];
264 SQL_GetError(db, error, sizeof(error));
265 LogError("FetchGroups() query failed: %s", query);
266 LogError("Query error: %s", error);
270 while (SQL_FetchRow(hQuery))
272 decl String:group1[80];
273 decl String:group2[80];
274 new GroupId:gid1, GroupId:gid2;
276 SQL_FetchString(hQuery, 0, group1, sizeof(group1));
277 SQL_FetchString(hQuery, 1, group2, sizeof(group2));
279 if (((gid1 = FindAdmGroup(group1)) == INVALID_GROUP_ID)
280 || (gid2 = FindAdmGroup(group2)) == INVALID_GROUP_ID)
285 SetAdmGroupImmuneFrom(gid1, gid2);
287 PrintToServer("SetAdmGroupImmuneFrom(%d, %d)", gid1, gid2);
294 * Fetch overrides in a lump query.
296 Format(query, sizeof(query), "SELECT g.name, go.type, go.name, go.access FROM sm_group_overrides go LEFT JOIN sm_groups g ON go.group_id = g.id");
298 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
300 decl String:error[255];
301 SQL_GetError(db, error, sizeof(error));
302 LogError("FetchGroups() query failed: %s", query);
303 LogError("Query error: %s", error);
307 decl String:type[16];
309 decl String:access[16];
310 while (SQL_FetchRow(hQuery))
312 SQL_FetchString(hQuery, 0, name, sizeof(name));
313 SQL_FetchString(hQuery, 1, type, sizeof(type));
314 SQL_FetchString(hQuery, 2, cmd, sizeof(cmd));
315 SQL_FetchString(hQuery, 3, access, sizeof(access));
318 if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
323 new OverrideType:o_type = Override_Command;
324 if (StrEqual(type, "group"))
326 o_type = Override_CommandGroup;
329 new OverrideRule:o_rule = Command_Deny;
330 if (StrEqual(access, "allow"))
332 o_rule = Command_Allow;
336 PrintToServer("AddAdmGroupCmdOverride(%d, %s, %d, %d)", gid, cmd, o_type, o_rule);
339 AddAdmGroupCmdOverride(gid, cmd, o_type, o_rule);
345 FetchOverrides(Handle:db)
347 decl String:query[255];
350 Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides");
352 if ((hQuery = SQL_Query(db, query)) == INVALID_HANDLE)
354 decl String:error[255];
355 SQL_GetError(db, error, sizeof(error));
356 LogError("FetchOverrides() query failed: %s", query);
357 LogError("Query error: %s", error);
361 decl String:type[64];
362 decl String:name[64];
363 decl String:flags[32];
365 while (SQL_FetchRow(hQuery))
367 SQL_FetchString(hQuery, 0, type, sizeof(type));
368 SQL_FetchString(hQuery, 1, name, sizeof(name));
369 SQL_FetchString(hQuery, 2, flags, sizeof(flags));
372 PrintToServer("Adding override (%s, %s, %s)", type, name, flags);
375 flag_bits = ReadFlagString(flags);
376 if (StrEqual(type, "command"))
378 AddCommandOverride(name, Override_Command, flag_bits);
379 } else if (StrEqual(type, "group")) {
380 AddCommandOverride(name, Override_CommandGroup, flag_bits);