Esempio n. 1
0
static bool
mm_DetectGameInformation()
{
	char game_path[PLATFORM_MAX_PATH];

	if (game_info_detected)
		return game_info_detected == 1 ? true : false;

	game_info_detected = -1;

	mm_GetGameName(game_name, sizeof(game_name));

	if (!mm_GetFileOfAddress((void*)mm_DetectGameInformation, mm_path, sizeof(mm_path)))
	{
		mm_LogFatal("Could not locate Metamod loader library path");
		return false;
	}

	if (!mm_ResolvePath(game_name, game_path, sizeof(game_path)))
	{
		mm_LogFatal("Could not resolve path: %s", game_name);
		return false;
	}

	FILE *fp;
	char gameinfo_path[PLATFORM_MAX_PATH];

	mm_PathFormat(gameinfo_path, sizeof(gameinfo_path), "%s/gameinfo.txt", game_path);
	if ((fp = fopen(gameinfo_path, "rt")) == NULL)
	{
		mm_LogFatal("Could not read file: %s", gameinfo_path);
		return false;
	}

	char temp_path[PLATFORM_MAX_PATH];
	char cur_path[PLATFORM_MAX_PATH];
	getcwd(cur_path, sizeof(cur_path));

	char *ptr;
	const char *lptr;
	bool search = false;
	char buffer[255], key[128], val[128];
	while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
	{
		mm_TrimComments(buffer);
		mm_TrimLeft(buffer);
		mm_TrimRight(buffer);

		if (stricmp(buffer, "SearchPaths") == 0)
			search = true;

		if (!search)
			continue;

		mm_KeySplit(buffer, key, sizeof(key) - 1, val, sizeof(val) - 1);
		if (stricmp(key, "Game") != 0 && stricmp(key, "GameBin") != 0)
			continue;

		if (strncmp(val, "|gameinfo_path|", sizeof("|gameinfo_path|") - 1) == 0)
		{
			ptr = &val[sizeof("|gameinfo_path|") - 1];
			if (ptr[0] == '.')
				ptr++;
			lptr = game_path;
		}
		else
		{
			ptr = val;
			lptr = cur_path;
		}

		if (stricmp(key, "GameBin") == 0)
			mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/" SERVER_NAME, lptr, ptr);
		else if (!ptr[0])
			mm_PathFormat(temp_path, sizeof(temp_path), "%s/bin/" SERVER_NAME, lptr);
		else
			mm_PathFormat(temp_path, sizeof(temp_path), "%s/%s/bin/" SERVER_NAME, lptr, ptr);

		if (mm_PathCmp(mm_path, temp_path))
			continue;

		FILE *exists = fopen(temp_path, "rb");
		if (!exists)
			continue;
		fclose(exists);

		/* exists is still non-NULL... use this as a flag */
		for (unsigned int i = 0; i < gamedll_path_count; i++)
		{
			if (mm_PathCmp(gamedll_paths[i], temp_path))
			{
				exists = NULL;
				break;
			}
		}

		if (!exists)
			continue;

		mm_Format(gamedll_paths[gamedll_path_count],
				  PLATFORM_MAX_PATH,
				  "%s",
				  temp_path);
		gamedll_path_count++;

		if (gamedll_path_count == MAX_GAMEDLL_PATHS)
			break;
	}
	fclose(fp);

	game_info_detected = 1;

	if (gamedll_path_count == 0)
	{
		mm_LogFatal("Could not detect any valid game paths in gameinfo.txt");
		return false;
	}

	return true;
}
	virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
	{
		if (!load_allowed)
			return false;

		load_allowed = false;

		/* Backend should already filled in if loaded as gamedll */
		if (gamedll_bridge == NULL)
		{
			mm_GetGameName(game_name, sizeof(game_name));

			mm_backend = mm_DetermineBackend(engineFactory, gsFactory, game_name);
		}

		if (mm_backend == MMBackend_UNKNOWN)
		{
			mm_LogFatal("Could not detect engine version");
			return false;
		}
		
		void **this_vtable;
		this_vtable = (void **)*(void **)this;

		if (mm_backend != MMBackend_Episode1
			&& mm_backend != MMBackend_DarkMessiah)
		{
			/* We need to insert the right type of call into this vtable */
			void **vtable_src;
			IRandomThings sample;
			SourceHook::MemFuncInfo mfp_dest, mfp_src;

			mfp_dest.isVirtual = false;
			mfp_src.isVirtual = false;

			SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
			SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);

			assert(mfp_dest.isVirtual);
			assert(mfp_dest.thisptroffs == 0);
			assert(mfp_dest.vtbloffs == 0);
			assert(mfp_src.isVirtual);
			assert(mfp_src.thisptroffs == 0);
			assert(mfp_src.vtbloffs == 0);

			vtable_src = (void **)*(void **)&sample;
			SourceHook::SetMemAccess(&this_vtable[mfp_dest.vtblindex],
									 sizeof(void*),
									 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
			this_vtable[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
		}

		/* AS inserted ClientFullyConnect into vtable, so move entries up on older engines */
		if (mm_backend != MMBackend_AlienSwarm
			&& mm_backend != MMBackend_Portal2
			&& mm_backend != MMBackend_Blade
			&& mm_backend != MMBackend_Insurgency
			&& mm_backend != MMBackend_DOI
			&& mm_backend != MMBackend_CSGO
			&& mm_backend != MMBackend_DOTA)
		{
			SourceHook::MemFuncInfo mfp_fconnect;
			mfp_fconnect.isVirtual = false;

			SourceHook::GetFuncInfo(&ServerPlugin::ClientFullyConnect, mfp_fconnect);

			assert(mfp_fconnect.isVirtual);
			assert(mfp_fconnect.thisptroffs == 0);
			assert(mfp_fconnect.vtbloffs == 0);

			/* Shifting ClientDisconnect through OnQueryCvarValueFinished up into slot for
			 * ClientFullyConnect (8 entries)
			 */
			SourceHook::SetMemAccess(&this_vtable[mfp_fconnect.vtblindex],
									 sizeof(void *) * 8,
									 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
			memmove(&this_vtable[mfp_fconnect.vtblindex],
					&this_vtable[mfp_fconnect.vtblindex + 1],
					sizeof(void *) * 8);
		}

		char error[255];
		if (gamedll_bridge == NULL)
		{
			if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
			{
				mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
				return false;
			}
		}

		typedef IVspBridge *(*GetVspBridge)();
		GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
		if (get_bridge == NULL)
		{
			if (gamedll_bridge == NULL)
			{
				mm_UnloadMetamodLibrary();
			}
			mm_LogFatal("Detected engine %d but could not find GetVspBridge callback", mm_backend);
			return false;
		}

		vsp_bridge = get_bridge();

		vsp_bridge_info info;

		info.engineFactory = engineFactory;
		info.gsFactory = gsFactory;
		info.vsp_callbacks = (IServerPluginCallbacks*)this;
		info.vsp_version = vsp_version;

		strcpy(error, "Unknown error");
		if (!vsp_bridge->Load(&info, error, sizeof(error)))
		{
			vsp_bridge = NULL;
			if (gamedll_bridge == NULL)
			{
				mm_UnloadMetamodLibrary();
			}
			mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);	
			return false;
		}

		return true;
	}