EXPORT void *CreateInterface(const char *pName, int *pReturnCode)
{
	if (strcmp(pName, METAMOD_PLAPI_NAME) == 0)
	{
		char our_file[256];
		if (!GetFileOfAddress((void *)CreateInterface_MMS, our_file, sizeof(our_file)))
		{
			return NULL;
		}

		/* Go backwards and get the first token */
		size_t len = strlen(our_file);
		for (size_t i = len; i-- > 0;)
		{
			if (IsPathSepChar(our_file[i]))
			{
				our_file[i] = '\0';
				break;
			}
		}

		char new_file[256];
		UTIL_Format(new_file, sizeof(new_file), "%s" PATH_SEP_CHAR MMS_1_4_EP1_FILE, our_file);

		return TryAndLoadLibrary(new_file);
	}

	return NULL;
}
Beispiel #2
0
DLL_EXPORT void *CreateInterface(const char *iface, int *ret)
{
	/**
	 * If a load has already been attempted, bail out immediately.
	 */
	if (load_attempted)
	{
		return NULL;
	}

	if (strcmp(iface, METAMOD_PLAPI_NAME) == 0)
	{
		char thisfile[256];
		char targetfile[256];

		if (!GetFileOfAddress((void *)CreateInterface_MMS, thisfile, sizeof(thisfile)))
		{
			return NULL;
		}

		size_t len = strlen(thisfile);
		for (size_t iter=len-1; iter<len; iter--)
		{
			if (IsPathSepChar(thisfile[iter]))
			{
				thisfile[iter] = '\0';
				break;
			}
		}

		UTIL_Format(targetfile, sizeof(targetfile), "%s" PATH_SEP_CHAR FILENAME_1_4_EP1, thisfile);

		return _GetPluginPtr(targetfile, METAMOD_FAIL_API_V1);
	}

	return NULL;
}
Beispiel #3
0
bool SM_ExecuteConfig(CPlugin *pl, AutoConfig *cfg, bool can_create)
{
	bool will_create = false;
	
	/* See if we should be creating */
	if (can_create && cfg->create)
	{
		will_create = true;
		
		/* If the folder does not exist, attempt to create it.  
		 * We're awfully nice.
		 */
		const char *folder = cfg->folder.c_str();
		char path[PLATFORM_MAX_PATH];
		char build[PLATFORM_MAX_PATH];

		g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "cfg/%s", folder);

		if (!g_LibSys.IsPathDirectory(path))
		{
			char *cur_ptr = path;
			size_t len;
			
			g_LibSys.PathFormat(path, sizeof(path), "%s", folder);
			len = g_SourceMod.BuildPath(Path_Game, build, sizeof(build), "cfg");

			do
			{
				/* Find next suitable path */
				char *next_ptr = cur_ptr;
				while (*next_ptr != '\0')
				{
					if (IsPathSepChar(*next_ptr))
					{
						*next_ptr = '\0';
						next_ptr++;
						break;
					}
					next_ptr++;
				}
				if (*next_ptr == '\0')
				{
					next_ptr = NULL;
				}
				len += g_LibSys.PathFormat(&build[len], 
					sizeof(build)-len,
					"/%s",
					cur_ptr);
				if (!g_LibSys.CreateFolder(build))
				{
					break;
				}
				cur_ptr = next_ptr;
			} while (cur_ptr);
		}
	}

	/* Check if the file exists. */
	char file[PLATFORM_MAX_PATH];
	char local[PLATFORM_MAX_PATH];

	if (cfg->folder.size())
	{
		g_LibSys.PathFormat(local, 
			sizeof(local), 
			"%s/%s.cfg",
			cfg->folder.c_str(),
			cfg->autocfg.c_str());
	} else {
		g_LibSys.PathFormat(local,
			sizeof(local),
			"%s.cfg",
			cfg->autocfg.c_str());
	}
	
	g_SourceMod.BuildPath(Path_Game, file, sizeof(file), "cfg/%s", local);

	bool file_exists = g_LibSys.IsPathFile(file);
	if (!file_exists && will_create)
	{
		List<const ConVar *> *convars = NULL;
		if (pl->GetProperty("ConVarList", (void **)&convars, false) && convars)
		{
			/* Attempt to create it */
			FILE *fp = fopen(file, "wt");
			if (fp)
			{
				fprintf(fp, "// This file was auto-generated by SourceMod (v%s)\n", SOURCEMOD_VERSION);
				fprintf(fp, "// ConVars for plugin \"%s\"\n", pl->GetFilename());
				fprintf(fp, "\n\n");

				List<const ConVar *>::iterator iter;
				float x;
				for (iter = convars->begin(); iter != convars->end(); iter++)
				{
					const ConVar *cvar = (*iter);

					if ((cvar->GetFlags() & FCVAR_DONTRECORD) == FCVAR_DONTRECORD)
					{
						continue;
					}

					char descr[255];
					char *dptr = descr;

					/* Print comments until there is no more */
					strncopy(descr, cvar->GetHelpText(), sizeof(descr));
					while (*dptr != '\0')
					{
						/* Find the next line */
						char *next_ptr = dptr;
						while (*next_ptr != '\0')
						{
							if (*next_ptr == '\n')
							{
								*next_ptr = '\0';
								next_ptr++;
								break;
							}
							next_ptr++;
						}
						fprintf(fp, "// %s\n", dptr);
						dptr = next_ptr;
					}

					fprintf(fp, "// -\n");
					fprintf(fp, "// Default: \"%s\"\n", cvar->GetDefault());
					if (cvar->GetMin(x))
					{
						fprintf(fp, "// Minimum: \"%02f\"\n", x);
					}
					if (cvar->GetMax(x))
					{
						fprintf(fp, "// Maximum: \"%02f\"\n", x);
					}
					fprintf(fp, "%s \"%s\"\n", cvar->GetName(), cvar->GetDefault());
					fprintf(fp, "\n");
				}
				fprintf(fp, "\n");

				file_exists = true;
				can_create = false;
				fclose(fp);
			}
			else
			{
				g_Logger.LogError("Failed to auto generate config for %s, make sure the directory has write permission.", pl->GetFilename());
				return can_create;
			}
		}
	}

	if (file_exists)
	{
		char cmd[255];
		UTIL_Format(cmd, sizeof(cmd), "exec %s\n", local);
		engine->ServerCommand(cmd);
	}

	return can_create;
}
Beispiel #4
0
IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *error, size_t maxlength)
{
	/* We wrap most of the open process in a mutex just to be safe */
	m_pOpenLock->Lock();

	/* Format our path */
	char path[PLATFORM_MAX_PATH];
	size_t len = libsys->PathFormat(path, sizeof(path), "sqlite/%s", info->database);

	/* Chop any filename off */
	for (size_t i = len-1;
		 i <= len-1;
		 i--)
	{
		if (IsPathSepChar(path[i]))
		{
			path[i] = '\0';
			break;
		}
	}

	/* Test the full path */
	char fullpath[PLATFORM_MAX_PATH];
	g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data/%s", path);
	if (!libsys->IsPathDirectory(fullpath))
	{
		/* Make sure the data folder exists */
		len = g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data");
		if (!libsys->IsPathDirectory(fullpath))
		{
			if (!libsys->CreateFolder(fullpath))
			{
				strncopy(error, "Could not create or open \"data\" folder\"", maxlength);
				m_pOpenLock->Unlock();
				return NULL;
			}
		}

		/* The data folder exists - create each subdir as needed! */
		char *cur_ptr = path;

		do
		{
			/* Find the next suitable path */
			char *next_ptr = cur_ptr;
			while (*next_ptr != '\0')
			{
				if (IsPathSepChar(*next_ptr))
				{
					*next_ptr = '\0';
					next_ptr++;
					break;
				}
				next_ptr++;
			}
			if (*next_ptr == '\0')
			{
				next_ptr = NULL;
			}
			len += libsys->PathFormat(&fullpath[len], sizeof(fullpath)-len, "/%s", cur_ptr);
			if (!libsys->IsPathDirectory(fullpath) && !libsys->CreateFolder(fullpath))
			{
				break;
			}
			cur_ptr = next_ptr;
		} while (cur_ptr);
	}

	/* Build the FINAL path. */
	g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "data/sqlite/%s.sq3", info->database);

	/* If we're requesting a persistent connection, see if something is already open */
	if (persistent)
	{
		/* See if anything in the cache matches */
		List<SqDbInfo>::iterator iter;
		for (iter = m_Cache.begin(); iter != m_Cache.end(); iter++)
		{
			if ((*iter).path.compare(fullpath) == 0)
			{
				(*iter).db->IncReferenceCount();
				m_pOpenLock->Unlock();
				return (*iter).db;
			}
		}
	}

	/* Try to open a new connection */
	sqlite3 *sql;
	int err = sqlite3_open(fullpath, &sql);
	if (err != SQLITE_OK)
	{
		strncopy(error, sqlite3_errmsg(sql), maxlength);
		sqlite3_close(sql);
		m_pOpenLock->Unlock();
		return NULL;
	}

	sqlite3_busy_handler(sql, busy_handler, NULL);

	SqDatabase *pdb = new SqDatabase(sql, persistent);

	if (persistent)
	{
		SqDbInfo pinfo;
		pinfo.path = fullpath;
		pinfo.db = pdb;
		m_Cache.push_back(pinfo);
	}

	m_pOpenLock->Unlock();

	return pdb;
}