Handle relative paths when loading .worldedit files in World Editor.

This commit is contained in:
dfighter1985 2015-02-08 00:24:29 +01:00
parent 23873f2e3a
commit eaf5d26bae
3 changed files with 65 additions and 0 deletions

View file

@ -507,6 +507,13 @@ public:
*/ */
static bool makePathRelative (const char *basePath, std::string &relativePath); static bool makePathRelative (const char *basePath, std::string &relativePath);
/** Make path absolute
* \param relativePath - The relative path
* \param directory - the directory to which the path is relative to
* returns the absolute path, or empty if something went wrong.
*/
static std::string makePathAbsolute (const std::string &relativePath, const std::string &directory );
/** If File in this list is added more than one in an addSearchPath, it doesn't launch a warning. /** If File in this list is added more than one in an addSearchPath, it doesn't launch a warning.
*/ */
static void addIgnoredDoubleFile(const std::string &ignoredFile); static void addIgnoredDoubleFile(const std::string &ignoredFile);

View file

@ -2544,6 +2544,57 @@ bool CPath::makePathRelative (const char *basePath, std::string &relativePath)
return false; return false;
} }
std::string CPath::makePathAbsolute( const std::string &relativePath, const std::string &directory )
{
if( relativePath.empty() )
return "";
if( directory.empty() )
return "";
#ifdef NL_OS_WINDOWS
// Windows network address. Eg.: \\someshare\path
if( ( relativePath[ 0 ] == '\\' ) && ( relativePath[ 1 ] == '\\' ) )
return relativePath;
// Normal Windows absolute path. Eg.: C:\something
//
if( isalpha( relativePath[ 0 ] ) && ( relativePath[ 1 ] == ':' ) && ( ( relativePath[ 2 ] == '\\' ) || ( relativePath[ 2 ] == '/' ) ) )
return relativePath;
#else
// Unix filesystem absolute path
if( relativePath[ 0 ] == '/' )
return relativePath;
#endif
// Add a slash to the directory if necessary.
// If the relative path starts with dots we need a slash.
// If the relative path starts with a slash we don't.
// If it starts with neither, we need a slash.
bool needSlash = true;
char c = relativePath[ 0 ];
if( ( c == '\\' ) || ( c == '/' ) )
needSlash = false;
bool hasSlash = false;
std::string npath = directory;
c = npath[ npath.size() - 1 ];
if( ( c == '\\' ) || ( c == '/' ) )
hasSlash = true;
if( needSlash && !hasSlash )
npath += '/';
else
if( hasSlash && !needSlash )
npath.resize( npath.size() - 1 );
// Now build the new absolute path
npath += relativePath;
npath = standardizePath( npath, false );
return npath;
}
bool CFile::setRWAccess(const std::string &filename) bool CFile::setRWAccess(const std::string &filename)
{ {
#ifdef NL_OS_WINDOWS #ifdef NL_OS_WINDOWS

View file

@ -59,6 +59,8 @@ bool loadWorldEditFile(const std::string &fileName, WorldEditList &worldEditList
lastError = ""; lastError = "";
std::string p = NLMISC::CFile::getPath( fileName );
// Load the document // Load the document
NLMISC::CIFile file; NLMISC::CIFile file;
if (file.open(fileName)) if (file.open(fileName))
@ -122,6 +124,8 @@ bool loadWorldEditFile(const std::string &fileName, WorldEditList &worldEditList
{ {
std::string dataDir; std::string dataDir;
NLMISC::CIXml::getPropertyString(dataDir, node, "VALUE"); NLMISC::CIXml::getPropertyString(dataDir, node, "VALUE");
dataDir = NLMISC::CPath::makePathAbsolute( dataDir, p );
worldEditList.push_back(WorldEditItem(DataDirectoryType, dataDir)); worldEditList.push_back(WorldEditItem(DataDirectoryType, dataDir));
} }
@ -149,6 +153,9 @@ bool loadWorldEditFile(const std::string &fileName, WorldEditList &worldEditList
std::string filenameChild; std::string filenameChild;
if ( NLMISC::CIXml::getPropertyString(filenameChild, node, "FILENAME")) if ( NLMISC::CIXml::getPropertyString(filenameChild, node, "FILENAME"))
{ {
filenameChild = NLMISC::CPath::makePathAbsolute( filenameChild, p );
// Is it a landscape ? // Is it a landscape ?
if (type == "landscape") if (type == "landscape")
{ {