Update CS:GO team score gamedata.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
4 * SourceMod SQLite Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include <sm_platform.h>
33 #include "extension.h"
35 #include "SqDatabase.h"
39 unsigned int strncopy(char *dest, const char *src, size_t count)
47 while ((*src) && (--count))
53 return (dest - start);
56 int busy_handler(void *unused1, int unused2)
58 #if defined PLATFORM_WINDOWS
60 #elif defined PLATFORM_POSIX
68 m_Handle = BAD_HANDLE;
69 m_bThreadSafe = false;
72 void SqDriver::Initialize()
74 InitializeThreadSafety();
77 void SqDriver::Shutdown()
81 sqlite3_enable_shared_cache(0);
85 bool SqDriver::IsThreadSafe()
90 bool SqDriver::InitializeThreadSafety()
97 if (sqlite3_threadsafe() == 0)
102 if (sqlite3_enable_shared_cache(1) != SQLITE_OK)
107 m_bThreadSafe = true;
112 void SqDriver::ShutdownThreadSafety()
117 IdentityToken_t *SqDriver::GetIdentity()
119 return myself->GetIdentity();
122 const char *SqDriver::GetProductName()
127 const char *SqDriver::GetIdentifier()
132 Handle_t SqDriver::GetHandle()
134 if (m_Handle == BAD_HANDLE)
136 m_Handle = dbi->CreateHandle(DBHandle_Driver, this, myself->GetIdentity());
142 inline bool IsPathSepChar(char c)
144 #if defined PLATFORM_WINDOWS
145 return (c == '\\' || c == '/');
146 #elif defined PLATFORM_LINUX || defined PLATFORM_APPLE
151 IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *error, size_t maxlength)
153 ke::AutoLock lock(&m_OpenLock);
155 /* Format our path */
156 char path[PLATFORM_MAX_PATH];
157 size_t len = libsys->PathFormat(path, sizeof(path), "sqlite/%s", info->database);
159 /* Chop any filename off */
160 for (size_t i = len-1;
164 if (IsPathSepChar(path[i]))
171 /* Test the full path */
172 char fullpath[PLATFORM_MAX_PATH];
173 g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data/%s", path);
174 if (!libsys->IsPathDirectory(fullpath))
176 /* Make sure the data folder exists */
177 len = g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data");
178 if (!libsys->IsPathDirectory(fullpath))
180 if (!libsys->CreateFolder(fullpath))
182 strncopy(error, "Could not create or open \"data\" folder\"", maxlength);
187 /* The data folder exists - create each subdir as needed! */
188 char *cur_ptr = path;
192 /* Find the next suitable path */
193 char *next_ptr = cur_ptr;
194 while (*next_ptr != '\0')
196 if (IsPathSepChar(*next_ptr))
204 if (*next_ptr == '\0')
208 len += libsys->PathFormat(&fullpath[len], sizeof(fullpath)-len, "/%s", cur_ptr);
209 if (!libsys->IsPathDirectory(fullpath) && !libsys->CreateFolder(fullpath))
217 /* Build the FINAL path. */
218 g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data/sqlite/%s.sq3", info->database);
220 /* If we're requesting a persistent connection, see if something is already open */
223 /* See if anything in the cache matches */
224 List<SqDbInfo>::iterator iter;
225 for (iter = m_Cache.begin(); iter != m_Cache.end(); iter++)
227 if ((*iter).path.compare(fullpath) == 0)
229 (*iter).db->IncReferenceCount();
235 /* Try to open a new connection */
237 int err = sqlite3_open(fullpath, &sql);
238 if (err != SQLITE_OK)
240 strncopy(error, sqlite3_errmsg(sql), maxlength);
245 sqlite3_busy_handler(sql, busy_handler, NULL);
247 SqDatabase *pdb = new SqDatabase(sql, persistent);
252 pinfo.path = fullpath;
254 m_Cache.push_back(pinfo);
260 void SqDriver::RemovePersistent(IDatabase *pdb)
262 ke::AutoLock lock(&m_OpenLock);
264 List<SqDbInfo>::iterator iter;
265 for (iter = m_Cache.begin(); iter != m_Cache.end(); iter++)
267 if ((*iter).db == pdb)
269 iter = m_Cache.erase(iter);