Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions Core/GameEngine/Source/GameClient/MapUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,39 @@
#include "GameLogic/FPUControl.h"
#include "GameNetwork/GameInfo.h"
#include "GameNetwork/NetworkDefs.h"
#include "Lib/PathUtil.h"


//-------------------------------------------------------------------------------
// PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
static const char *mapExtension = ".map";

static void appendPathSeparator( AsciiString &path )
{
if (path.isNotEmpty())
{
const char last = path.getCharAt(path.getLength() - 1);
if (last != '/' && last != '\\')
{
path.concat(getNativePathSeparator());
}
}
}

static AsciiString getPathInDirectory( const AsciiString &directory, const char *filename )
{
AsciiString path = directory;
appendPathSeparator(path);
path.concat(filename);
return path;
}

static const char *getPathLeaf( const AsciiString &path )
{
const char *separator = getLastPathSeparator(path.str());
return separator ? separator + 1 : path.str();
}

static Int m_width = 0; ///< Height map width.
static Int m_height = 0; ///< Height map height (y size of array).
static Int m_borderSize = 0; ///< Non-playable border area.
Expand Down Expand Up @@ -331,12 +358,10 @@ AsciiString MapCache::getMapExtension() const

void MapCache::writeCacheINI( const AsciiString &mapDir )
{
AsciiString filepath = mapDir;
filepath.concat('\\');
AsciiString filepath = getPathInDirectory(mapDir, m_mapCacheName);

TheFileSystem->createDirectory(mapDir);

filepath.concat(m_mapCacheName);
FILE *fp = fopen(filepath.str(), "w");
DEBUG_ASSERTCRASH(fp != nullptr, ("Failed to create %s", filepath.str()));
if (fp == nullptr) {
Expand Down Expand Up @@ -499,8 +524,7 @@ Bool MapCache::clearUnseenMaps( const AsciiString &mapDir )
void MapCache::loadMapsFromMapCacheINI( const AsciiString &mapDir )
{
INI ini;
AsciiString fname;
fname.format("%s\\%s", mapDir.str(), m_mapCacheName);
AsciiString fname = getPathInDirectory(mapDir, m_mapCacheName);

if (TheFileSystem->doesFileExist(fname.str()))
{
Expand All @@ -514,8 +538,8 @@ Bool MapCache::loadMapsFromDisk( const AsciiString &mapDir, Bool isOfficial, Boo

FilenameList filepathList;
FilenameListIter filepathIt;
AsciiString toplevelPattern;
toplevelPattern.format("%s\\", mapDir.str());
AsciiString toplevelPattern = mapDir;
appendPathSeparator(toplevelPattern);
Bool mapListChanged = FALSE;
AsciiString filenamepattern;
filenamepattern.format("*.%s", getMapExtension().str());
Expand All @@ -530,14 +554,13 @@ Bool MapCache::loadMapsFromDisk( const AsciiString &mapDir, Bool isOfficial, Boo
AsciiString filepathLower = *filepathIt;
filepathLower.toLower();

const char *szFilenameLower = filepathLower.reverseFind('\\');
const char *szFilenameLower = getLastPathSeparator(filepathLower.str());
if (!szFilenameLower)
{
DEBUG_CRASH(("Couldn't find \\ in map name!"));
DEBUG_CRASH(("Couldn't find path separator in map name!"));
continue;
}

AsciiString endingStr;
AsciiString filenameLower = szFilenameLower+1;
filenameLower.truncateBy(strlen(mapExtension));

Expand All @@ -547,7 +570,8 @@ Bool MapCache::loadMapsFromDisk( const AsciiString &mapDir, Bool isOfficial, Boo
continue;
}

endingStr.format("%s\\%s%s", filenameLower.str(), filenameLower.str(), mapExtension);
AsciiString endingStr;
endingStr.format("%s%c%s%s", filenameLower.str(), *szFilenameLower, filenameLower.str(), mapExtension);

if (!filepathLower.endsWithNoCase(endingStr.str()))
{
Expand Down Expand Up @@ -592,7 +616,7 @@ Bool MapCache::addMap(
{
// unofficial maps or maps without names
AsciiString tempdisplayname;
tempdisplayname = fname.reverseFind('\\') + 1;
tempdisplayname = getPathLeaf(fname);
(*this)[lowerFname].m_displayName.translate(tempdisplayname);
if (md.m_numPlayers >= 2)
{
Expand Down Expand Up @@ -654,7 +678,7 @@ Bool MapCache::addMap(
{
DEBUG_LOG(("Missing TheKey_mapName!"));
AsciiString tempdisplayname;
tempdisplayname = fname.reverseFind('\\') + 1;
tempdisplayname = getPathLeaf(fname);
md.m_displayName.translate(tempdisplayname);
if (md.m_numPlayers >= 2)
{
Expand Down
27 changes: 25 additions & 2 deletions Core/Libraries/Include/Lib/PathUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@
#include "BaseType.h"
#include <string.h>

inline char getNativePathSeparator()
{
#ifdef _WIN32
return '\\';
#else
return '/';
#endif
}

inline const char* getLastPathSeparator(const char* path)
{
const char* forward = strrchr(path, '/');
const char* backward = strrchr(path, '\\');
return !forward ? backward : (!backward || forward > backward ? forward : backward);
}

inline const wchar_t* getLastPathSeparator(const wchar_t* path)
{
const wchar_t* forward = wcsrchr(path, L'/');
const wchar_t* backward = wcsrchr(path, L'\\');
return !forward ? backward : (!backward || forward > backward ? forward : backward);
}

inline const char* getExtension(const char* path)
{
const char* lastDot = strrchr(path, '.');
Expand All @@ -32,7 +55,7 @@ inline const char* getExtension(const char* path)
return nullptr;
}

const char* lastSeparator = maxPtr(strrchr(path, '/'), strrchr(path, '\\'));
const char* lastSeparator = getLastPathSeparator(path);

// Check if the dot is contained in the filename
if (lastSeparator && lastDot < lastSeparator)
Expand All @@ -52,7 +75,7 @@ inline const wchar_t* getExtension(const wchar_t* path)
return nullptr;
}

const wchar_t* lastSeparator = maxPtr(wcsrchr(path, L'/'), wcsrchr(path, L'\\'));
const wchar_t* lastSeparator = getLastPathSeparator(path);

// Check if the dot is contained in the filename
if (lastSeparator && lastDot < lastSeparator)
Expand Down
Loading