Permalink
Browse files

Refactor the code that lists map paks

  • Loading branch information...
mbasaglia committed Feb 28, 2016
1 parent 011593e commit b8d4b724fceaf458c77fddb1c7cc410c019335fb
@@ -2386,6 +2386,46 @@ const std::vector<PakInfo>& GetAvailablePaks()
return availablePaks;
}
std::vector<PakInfo> GetAvailableMapPaks()
{
std::vector<PakInfo> infos;
for ( const auto& pak : FS::GetAvailablePaks() )
{
if ( Str::IsPrefix("map-", pak.name) )
{
infos.push_back(pak);
}
}
return infos;
}
std::set<std::string> GetAvailableMaps()
{
std::set<std::string> maps;
#ifndef BUILD_VM
RefreshPaks();
#endif
std::error_code ignore;
for ( const auto& pak : GetAvailableMapPaks() )
{
FS::PakPath::LoadPakPrefix(pak, "maps", ignore);
}
static const std::string map_suffix = ".bsp";
for ( const auto& file : FS::PakPath::ListFiles("maps", ignore) )
{
if ( Str::IsSuffix(map_suffix, file) )
{
maps.insert( file.substr(0, file.size() - map_suffix.size()) );
}
}
return maps;
}
const std::string& GetHomePath()
{
return homePath;
@@ -506,6 +506,12 @@ std::string MakePakName(Str::StringRef name, Str::StringRef version, Util::optio
// Get the list of available paks
const std::vector<PakInfo>& GetAvailablePaks();
// Get the list of available paks that contain maps
std::vector<PakInfo> GetAvailableMapPaks();
// Get the list of available map names
std::set<std::string> GetAvailableMaps();
// Get the home path
const std::string& GetHomePath();
@@ -615,15 +615,9 @@ void FS_LoadBasePak()
void FS_LoadAllMapMetadata()
{
std::unordered_set<std::string> maps;
for (const auto& x: FS::GetAvailablePaks()) {
if (Str::IsPrefix("map-", x.name) && maps.find(x.name) == maps.end())
maps.insert(x.name);
}
for (const auto& x: maps) {
for (const auto& pak: FS::GetAvailableMapPaks()) {
try {
FS::PakPath::LoadPakPrefix(*FS::FindPak(x), va("meta/%s/", x.substr(4).c_str()));
FS::PakPath::LoadPakPrefix(pak, va("meta/%s/", pak.name.substr(4).c_str()));
} catch (std::system_error&) {} // ignore and move on
}
}
@@ -58,11 +58,8 @@ class MapCmd: public Cmd::StaticCmd {
const std::string& mapName = args.Argv(1);
//Detect any newly added paks
FS::RefreshPaks();
//Make sure the map exists to avoid typos that would kill the game
if (!FS::FindPak("map-" + mapName)) {
if (!FS::GetAvailableMaps().count(mapName)) {
Print("Can't find map %s", mapName);
return;
}
@@ -80,9 +77,9 @@ class MapCmd: public Cmd::StaticCmd {
Cmd::CompletionResult Complete(int argNum, const Cmd::Args& args, Str::StringRef prefix) const OVERRIDE {
if (argNum == 1) {
Cmd::CompletionResult out;
for (auto& x: FS::GetAvailablePaks()) {
if (Str::IsPrefix("map-" + prefix, x.name))
out.push_back({x.name.substr(4), ""});
for (auto& map: FS::GetAvailableMaps()) {
if (Str::IsPrefix(prefix, map))
out.push_back({map, ""});
}
return out;
} else if (argNum > 1) {
@@ -395,25 +392,7 @@ class ListMapsCmd: public Cmd::StaticCmd
void Run(const Cmd::Args&) const OVERRIDE
{
std::vector<std::string> maps;
FS::RefreshPaks();
std::error_code ignore;
for ( const auto& pak : FS::GetAvailablePaks() )
{
FS::PakPath::LoadPakPrefix(pak, "maps", ignore);
}
for ( const auto& pak : FS::PakPath::ListFiles("maps", ignore) )
{
if ( Str::IsSuffix(".bsp", pak) )
{
maps.push_back( pak.substr(0, pak.size() - 4) );
}
}
std::sort( maps.begin(), maps.end() );
maps.erase( std::unique( maps.begin(), maps.end() ), maps.end() );
auto maps = FS::GetAvailableMaps();
Print("Listing %d maps:", maps.size());

0 comments on commit b8d4b72

Please sign in to comment.