core/metamod.cpp
changeset 601 bfbed17a3375
parent 599 5b906351da4d
child 603 f5602d4a89d6
     1.1 --- a/core/metamod.cpp	Mon Nov 24 04:13:35 2008 -0600
     1.2 +++ b/core/metamod.cpp	Mon Nov 24 07:25:46 2008 -0600
     1.3 @@ -235,22 +235,23 @@
     1.4  	return ptr;
     1.5  }
     1.6  
     1.7 -int
     1.8 -mm_LoadPluginsFromFile(const char *_file)
     1.9 +static int
    1.10 +LoadPluginsFromFile(const char *filepath, int &skipped)
    1.11  {
    1.12  	FILE *fp;
    1.13 -	int total = 0, skipped=0;
    1.14 +	int total = 0;
    1.15  	PluginId id;
    1.16  	bool already;
    1.17  
    1.18 -	fp = fopen(_file, "rt");
    1.19 +	skipped = 0;
    1.20 +
    1.21 +	fp = fopen(filepath, "rt");
    1.22  	if (!fp)
    1.23  	{
    1.24 -		mm_LogMessage("[META] Could not open plugins file %s\n", _file);
    1.25 -		return -1;
    1.26 +		return 0;
    1.27  	}
    1.28  
    1.29 -	char buffer[255], error[255], full_path[255];
    1.30 +	char buffer[255], error[255], full_path[PATH_SIZE];
    1.31  	const char *ptr, *ext, *file;
    1.32  	size_t length;
    1.33  	while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
    1.34 @@ -364,11 +365,6 @@
    1.35  		}
    1.36  	}
    1.37  	fclose(fp);
    1.38 -
    1.39 -	if (skipped)
    1.40 -		mm_LogMessage("[META] Loaded %d plugins from file (%d already loaded)", total, skipped);
    1.41 -	else
    1.42 -		mm_LogMessage("[META] Loaded %d plugins from file.", total);
    1.43  	
    1.44  	return total;
    1.45  }
    1.46 @@ -476,13 +472,11 @@
    1.47  		}
    1.48  	}
    1.49  
    1.50 -	char full_path[260];
    1.51 +	char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
    1.52  
    1.53 -	g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), pluginFile);
    1.54 -	mm_LoadPluginsFromFile(full_path);
    1.55 -
    1.56 -	g_Metamod.PathFormat(full_path, sizeof(full_path), "%s/%s", mod_path.c_str(), mmBaseDir);
    1.57 -	LookForVDFs(full_path);
    1.58 +	g_Metamod.PathFormat(filepath, sizeof(filepath), "%s/%s", mod_path.c_str(), pluginFile);
    1.59 +	g_Metamod.PathFormat(vdfpath, sizeof(vdfpath), "%s/%s", mod_path.c_str(), mmBaseDir);
    1.60 +	mm_LoadPlugins(filepath, vdfpath);
    1.61  }
    1.62  
    1.63  void
    1.64 @@ -588,21 +582,19 @@
    1.65  
    1.66  	if (!in_first_level)
    1.67  	{
    1.68 -		char full_path[255];
    1.69 +		char filepath[PATH_SIZE], vdfpath[PATH_SIZE];
    1.70  
    1.71 -		g_Metamod.PathFormat(full_path, 
    1.72 -			sizeof(full_path), 
    1.73 +		g_Metamod.PathFormat(filepath, 
    1.74 +			sizeof(filepath), 
    1.75  			"%s/%s", 
    1.76  			mod_path.c_str(),
    1.77  			provider->GetConVarString(mm_pluginsfile));
    1.78 -		mm_LoadPluginsFromFile(full_path);
    1.79 -
    1.80 -		g_Metamod.PathFormat(full_path,
    1.81 -			sizeof(full_path),
    1.82 +		g_Metamod.PathFormat(vdfpath,
    1.83 +			sizeof(vdfpath),
    1.84  			"%s/%s",
    1.85  			mod_path.c_str(),
    1.86  			provider->GetConVarString(mm_basedir));
    1.87 -		LookForVDFs(full_path);
    1.88 +		mm_LoadPlugins(filepath, vdfpath);
    1.89  	}
    1.90  	else
    1.91  	{
    1.92 @@ -977,6 +969,11 @@
    1.93  	return provider->GetConVarString(mm_pluginsfile);
    1.94  }
    1.95  
    1.96 +const char *MetamodSource::GetVDFDir()
    1.97 +{
    1.98 +	return provider->GetConVarString(mm_basedir);
    1.99 +}
   1.100 +
   1.101  IConCommandBaseAccessor *MetamodSource::GetCvarBaseAccessor()
   1.102  {
   1.103  	return provider->GetConCommandBaseAccessor();
   1.104 @@ -1109,30 +1106,43 @@
   1.105  	is_gamedll_loaded = loaded;
   1.106  }
   1.107  
   1.108 -static void
   1.109 -ProcessVDF(const char *path)
   1.110 +static bool
   1.111 +ProcessVDF(const char *path, bool &skipped)
   1.112  {
   1.113  	PluginId id;
   1.114  	bool already;
   1.115  	char alias[24], file[255], error[255];
   1.116  
   1.117  	if (!provider->ProcessVDF(path, file, sizeof(file), alias, sizeof(alias)))
   1.118 -		return;
   1.119 +	{
   1.120 +		skipped = false;
   1.121 +		return false;
   1.122 +	}
   1.123  
   1.124  	if (alias[0] != '\0')
   1.125  		g_PluginMngr.SetAlias(alias, file);
   1.126  
   1.127  	id = g_PluginMngr.Load(file, Pl_File, already, error, sizeof(error));
   1.128 +	skipped = already;
   1.129  	if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
   1.130 +	{
   1.131  		mm_LogMessage("[META] Failed to load plugin %s: %s", file, error);
   1.132 +		return false;
   1.133 +	}
   1.134 +
   1.135 +	return true;
   1.136  }
   1.137  
   1.138 -static void
   1.139 -LookForVDFs(const char *dir)
   1.140 +static int
   1.141 +LoadVDFPluginsFromDir(const char *dir, int &skipped)
   1.142  {
   1.143 +	bool success, skip;
   1.144 +	int total = 0;
   1.145  	char path[MAX_PATH];
   1.146  	char relpath[MAX_PATH * 2];
   1.147  
   1.148 +	skipped = 0;
   1.149 +
   1.150  #if defined _MSC_VER
   1.151  	HANDLE hFind;
   1.152  	WIN32_FIND_DATA fd;
   1.153 @@ -1143,7 +1153,7 @@
   1.154  	{
   1.155  		DWORD dw = GetLastError();
   1.156  		if (dw == ERROR_FILE_NOT_FOUND)
   1.157 -			return;
   1.158 +			return 0;
   1.159  
   1.160  		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
   1.161  			NULL,
   1.162 @@ -1153,14 +1163,18 @@
   1.163  			sizeof(error),
   1.164  			NULL);
   1.165  		mm_LogMessage("[META] Could not open folder \"%s\" (%s)", dir, error);
   1.166 -		return;
   1.167 +		return 0;
   1.168  	}
   1.169  
   1.170  	do
   1.171  	{
   1.172  		g_Metamod.PathFormat(path, sizeof(path), "%s\\%s", dir, fd.cFileName);
   1.173  		UTIL_Relatize(relpath, sizeof(relpath), mod_path.c_str(), path);
   1.174 -		ProcessVDF(relpath);
   1.175 +		success = ProcessVDF(relpath, skip);
   1.176 +		if (skip)
   1.177 +			skipped++;
   1.178 +		else if (success)
   1.179 +			total++;
   1.180  	} while (FindNextFile(hFind, &fd));
   1.181  
   1.182  	FindClose(hFind);
   1.183 @@ -1172,7 +1186,7 @@
   1.184  	if ((pDir = opendir(dir)) == NULL)
   1.185  	{
   1.186  		mm_LogMessage("[META] Could not open folder \"%s\" (%s)", dir, strerror(errno));
   1.187 -		return;
   1.188 +		return 0;
   1.189  	}
   1.190  
   1.191  	while ((pEnt = readdir(pDir)) != NULL)
   1.192 @@ -1189,11 +1203,38 @@
   1.193  		}
   1.194  		g_Metamod.PathFormat(path, sizeof(path), "%s/%s", dir, pEnt->d_name);
   1.195  		UTIL_Relatize(relpath, sizeof(relpath), mod_path.c_str(), path);
   1.196 -		ProcessVDF(relpath);
   1.197 +		success = ProcessVDF(relpath, skip);
   1.198 +		if (skip)
   1.199 +			skipped++;
   1.200 +		else if (success)
   1.201 +			total++;
   1.202  	}
   1.203  
   1.204  	closedir(pDir);
   1.205  #endif
   1.206 +
   1.207 +	return total;
   1.208 +}
   1.209 +
   1.210 +int
   1.211 +mm_LoadPlugins(const char *filepath, const char *vdfpath)
   1.212 +{
   1.213 +	int total, skipped, fskipped, vskipped;
   1.214 +	const char *s = "";
   1.215 +
   1.216 +	total = LoadPluginsFromFile(filepath, fskipped);
   1.217 +	total += LoadVDFPluginsFromDir(vdfpath, vskipped);
   1.218 +	skipped = fskipped + vskipped;
   1.219 +
   1.220 +	if (total == 0 || total > 1)
   1.221 +		s = "s";
   1.222 +
   1.223 +	if (skipped)
   1.224 +		mm_LogMessage("[META] Loaded %d plugin%s (%d already loaded)", total, s, skipped);
   1.225 +	else
   1.226 +		mm_LogMessage("[META] Loaded %d plugin%s.", total, s);
   1.227 +
   1.228 +	return total;
   1.229  }
   1.230  
   1.231  bool