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

--HG--
branch : hotfix
hg/compatibility
dfighter1985 10 years ago
parent 5e55a7b7bb
commit 46cc31d0d0

@ -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);

@ -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

@ -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")
{ {

Loading…
Cancel
Save