Files
o3de/Code/Legacy/CryCommon/CryPath.h
T
lumberyard-employee-dm f648cb1fd8 Update the FileIO Aliases (#4186)
* Update the FileIOAlias naming to make the cache, project root and engine
root paths more clear

The alias of `@root@`, `@assets@`, and `@projectplatformcache@` has been
collapsed to `@projectproductassets@`

The alias of `@devroot@` and `@engroot@` has been collapsed to
`@engroot@`

The alias of `@devassets@` and `@projectroot@` has been collapsed to
`@projectroot@`

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated use of devassets and devroot properties in python

Those properties now use projectroot and engroot

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updating the alias @engroot@ alias path comment in each platform specific LocalFileIO_*.cpp file

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed hardcoded size of 9 for the product asset alias.

The ResolvePath function now just appends the @projectproductassets@
alias with the input path

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Remove duplicate @projectproductassets@ check in ProcessFileTreeRequest

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fix for typos in Hydra python test

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated LocalFileIO::Copy call on Windows to use the Unicode aware CopyFileW API

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the AWSMetreicsGemAllocatorFixture to properly suppress asset
cache write errors for Test file creation.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed unneeded call to set the @projectproductasstes@ alias at the bottom of the AssetSeedManagerTest SetUp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Added a deprecated alias map to the FileIO System

When a deprecated alias is accessed, the FileIO System logs an AZ_Error and indicates the alias that should be used

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated python test scripts to use the projectroot binding

Retrieving the AutomatedTesting project path based on "<devroot>/AutomatedTesting" has been removed.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated references to devroot and devgame within the codebase

The GetAbsoluteDevGameFolderPath functions has been replaced with direct call to AZ::Utils::GetProjectPath
The GetAbsoluteDevRootFolderPath functions has been replaced with direct calls to AZ::Utils::GetEnginePath

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated <engroot>/AutomatedTesting references to projectroot


Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced references that assumes the project path is <engroot>/AutomatedTesting with <projectroot> in the AutomatedTesting python test

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Correct casing in emfxworkspace file


Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed newly added AppendPathParts function
Removed the Path constructors which accepts a PathIterable instance

The PathIterable isn't safe to return to a user of the Path class as it might be referencing temporary paths supplied via PathView arguments

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed unused parameter warning

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Undid change to the LexicallyProximate function to set the path result to the base path.

It needs to return the *this path if the pathResult is empty

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Moved the LocalFileIO ConvertToAbsolutePath implementations to AZ::Utils

Fixed the ConvertToAbsolutePath implementation for Unix platforms to use a buffer that is size PATH_MAX(4096 on all our supported Unix platforms).
Because the buffer before was AZ::IO::MaxPathLength which as a size of 1024, this was resulting in the Unix `realpath` function smashing the stack when it converts a path to an absolute path that is longer than 1024 characters

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the EditorCore.Tests to attach the AZ Environment to the EditorCore shared library that is statically loaded on launch.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed for DeprecatedAlaisesKeyVisitor Visit function causing the non string_view overloads being hidden causing a hidden base class function warning

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Changed the AWSMetricsGemMock to use a temporary for writing test assets

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the LocalFileIO::ResolvePath function to use HasRootPath to determine if a path starts with a root path such as "D:", "D:/" or "/"

IsAbsolute was not the corect check as the path of "D:" is a relative
path.
To be absolute according to the Windows the path must have a root
directory. i.e "D:/" or "D:\"

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed absolute path comment from LocalFile_UnixLike.cpp and LocalFile_Android.cpp FindFiles implementations
Updated the ConvertToAlias to supply an AZ::IO::FixedMaxPath

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced usage of the @projectproductassets@ alias with @engroot@ when referring to the LmbrCentral source folder in the CopyDependencyBuilderTest and the SeedBuilderTests

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the ScriptCanvas Upgrade Tool to output backed up files to the
Project User Path instead of the engine root

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed whitespacing issues in Application.cpp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Remove unnecessary creation of a FixedMaxPath in the UpgradeTool.cpp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Modified testSeedFolder variable in the SeedBuilderTests to use the
@engroot@ alias instead of @projectproductassets@/.. alias when
referring to the LmbrCentral Gem source path

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated references to the Project Asset Cache in the PythonTests.

Those tests no longer use the logic `azlmbr.paths.projectroot / "Cache" / "pc"` to retrieve a path to the cache root but instead the `azlmbr.paths.projectproductassets` constant

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed the FileIO Deprecated Alias test on Windows

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removing @projectsourceassets@ alias, as it is only used once.

Updated the PhysX EditorSystemComponent.cpp to query the ProjectPath
form the SettingsRegistry.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced @projectproductassets@ alias with @products@

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Rollback changes to the PhysX EditorSystemComponent.cpp

The changes to use the ProjectPath from the SettingsRegistry has been implemented in PR #4497

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2021-10-05 15:12:53 -05:00

628 lines
19 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
// Description : Defines namespace PathUtil for operations on files paths.
#ifndef CRYINCLUDE_CRYCOMMON_CRYPATH_H
#define CRYINCLUDE_CRYCOMMON_CRYPATH_H
#pragma once
#include <ISystem.h>
#include <AzFramework/Archive/IArchive.h>
#include <IConsole.h>
#include <AzCore/std/string/string.h>
#include <AzCore/std/string/conversions.h>
#include <AzCore/StringFunc/StringFunc.h>
#include "platform.h"
#define UNIX_PATH_SEP_STR "/"
#define UNIX_PATH_SEP_CHR '/'
#define DOS_PATH_SEP_STR "\\"
#define DOS_PATH_SEP_CHR '\\'
#if AZ_LEGACY_CRYCOMMON_TRAIT_USE_UNIX_PATHS
#define CRY_NATIVE_PATH_SEPSTR UNIX_PATH_SEP_STR
#else
#define CRY_NATIVE_PATH_SEPSTR DOS_PATH_SEP_STR
#endif
typedef AZStd::fixed_string<512> stack_string;
namespace PathUtil
{
const static int maxAliasLength = 32;
inline AZStd::string GetLocalizationFolder()
{
return gEnv->pCryPak->GetLocalizationFolder();
}
inline AZStd::string GetLocalizationRoot()
{
return gEnv->pCryPak->GetLocalizationRoot();
}
//! Convert a path to the uniform form.
inline AZStd::string ToUnixPath(const AZStd::string& strPath)
{
if (strPath.find(DOS_PATH_SEP_CHR) != AZStd::string::npos)
{
AZStd::string path = strPath;
AZ::StringFunc::Replace(path, DOS_PATH_SEP_CHR, UNIX_PATH_SEP_CHR);
return path;
}
return strPath;
}
//! Convert a path to the uniform form in place on stack
inline void ToUnixPath(stack_string& rConv)
{
const char* const cpEnd = &(rConv.c_str()[rConv.size()]);
char* __restrict pC = rConv.begin();
while (pC != cpEnd)
{
char c = *pC;
if (c == DOS_PATH_SEP_CHR)
{
c = UNIX_PATH_SEP_CHR;
}
*pC++ = c;
}
}
//! Convert a path to the DOS form.
inline AZStd::string ToDosPath(const AZStd::string& strPath)
{
if (strPath.find(UNIX_PATH_SEP_CHR) != AZStd::string::npos)
{
AZStd::string path = strPath;
AZ::StringFunc::Replace(path, UNIX_PATH_SEP_CHR, DOS_PATH_SEP_CHR);
return path;
}
return strPath;
}
//! Convert a path to the Native form.
inline AZStd::string ToNativePath(const AZStd::string& strPath)
{
#if AZ_LEGACY_CRYCOMMON_TRAIT_USE_UNIX_PATHS
return ToUnixPath(strPath);
#else
return ToDosPath(strPath);
#endif
}
//! Convert a path to lowercase form
inline AZStd::string ToLower(const AZStd::string& strPath)
{
AZStd::string path = strPath;
AZStd::to_lower(path.begin(), path.end());
return path;
}
//! Split full file name to path and filename
//! @param filepath [IN] Full file name including path.
//! @param path [OUT] Extracted file path.
//! @param filename [OUT] Extracted file (without extension).
//! @param ext [OUT] Extracted files extension.
inline void Split(const AZStd::string& filepath, AZStd::string& path, AZStd::string& filename, AZStd::string& fext)
{
path = filename = fext = AZStd::string();
if (filepath.empty())
{
return;
}
const char* str = filepath.c_str();
const char* pext = str + filepath.length() - 1;
const char* p;
for (p = str + filepath.length() - 1; p >= str; --p)
{
switch (*p)
{
case ':':
case '/':
case '\\':
path = filepath.substr(0, p - str + 1);
filename = filepath.substr(p - str + 1, pext - p);
return;
case '.':
// there's an extension in this file name
fext = filepath.substr(p - str + 1);
pext = p;
break;
}
}
filename = filepath.substr(p - str + 1, pext - p);
}
//! Split full file name to path and filename
//! @param filepath [IN] Full file name inclusing path.
//! @param path [OUT] Extracted file path.
//! @param file [OUT] Extracted file (with extension).
inline void Split(const AZStd::string& filepath, AZStd::string& path, AZStd::string& file)
{
AZStd::string fext;
Split(filepath, path, file, fext);
file += fext;
}
// Extract extension from full specified file path
// Returns
// pointer to the extension (without .) or pointer to an empty 0-terminated AZStd::string
inline const char* GetExt(const char* filepath)
{
const char* str = filepath;
size_t len = strlen(filepath);
for (const char* p = str + len - 1; p >= str; --p)
{
switch (*p)
{
case ':':
case '/':
case '\\':
// we've reached a path separator - it means there's no extension in this name
return "";
case '.':
// there's an extension in this file name
return p + 1;
}
}
return "";
}
//! Extract path from full specified file path.
inline AZStd::string GetPath(const AZStd::string& filepath)
{
const char* str = filepath.c_str();
for (const char* p = str + filepath.length() - 1; p >= str; --p)
{
switch (*p)
{
case ':':
case '/':
case '\\':
return filepath.substr(0, p - str + 1);
}
}
return "";
}
//! Extract path from full specified file path.
inline AZStd::string GetPath(const char* filepath)
{
return GetPath(AZStd::string(filepath));
}
//! Extract path from full specified file path.
inline stack_string GetPath(const stack_string& filepath)
{
const char* str = filepath.c_str();
for (const char* p = str + filepath.length() - 1; p >= str; --p)
{
switch (*p)
{
case ':':
case '/':
case '\\':
return filepath.substr(0, p - str + 1);
}
}
return "";
}
//! Extract file name with extension from full specified file path.
inline AZStd::string GetFile(const AZStd::string& filepath)
{
const char* str = filepath.c_str();
for (const char* p = str + filepath.length() - 1; p >= str; --p)
{
switch (*p)
{
case ':':
case '/':
case '\\':
return filepath.substr(p - str + 1);
}
}
return filepath;
}
inline const char* GetFile(const char* filepath)
{
const size_t len = strlen(filepath);
for (const char* p = filepath + len - 1; p >= filepath; --p)
{
switch (*p)
{
case ':':
case '/':
case '\\':
return p + 1;
}
}
return filepath;
}
//! Replace extension for given file.
inline void RemoveExtension(AZStd::string& filepath)
{
const char* str = filepath.c_str();
for (const char* p = str + filepath.length() - 1; p >= str; --p)
{
switch (*p)
{
case ':':
case '/':
case '\\':
// we've reached a path separator - it means there's no extension in this name
return;
case '.':
// there's an extension in this file name
filepath = filepath.substr(0, p - str);
return;
}
}
// it seems the file name is a pure name, without path or extension
}
//! Replace extension for given file.
inline void RemoveExtension(stack_string& filepath)
{
const char* str = filepath.c_str();
for (const char* p = str + filepath.length() - 1; p >= str; --p)
{
switch (*p)
{
case ':':
case '/':
case '\\':
// we've reached a path separator - it means there's no extension in this name
return;
case '.':
// there's an extension in this file name
filepath = filepath.substr(0, p - str);
return;
}
}
// it seems the file name is a pure name, without path or extension
}
//! Extract file name without extension from full specified file path.
inline AZStd::string GetFileName(const AZStd::string& filepath)
{
AZStd::string file = filepath;
RemoveExtension(file);
return GetFile(file);
}
//! Removes the trailing slash or backslash from a given path.
inline AZStd::string RemoveSlash(const AZStd::string& path)
{
if (path.empty() || (path[path.length() - 1] != '/' && path[path.length() - 1] != '\\'))
{
return path;
}
return path.substr(0, path.length() - 1);
}
//! get slash
inline AZStd::string GetSlash()
{
return CRY_NATIVE_PATH_SEPSTR;
}
//! add a backslash if needed
inline AZStd::string AddSlash(const AZStd::string& path)
{
if (path.empty() || path[path.length() - 1] == '/')
{
return path;
}
if (path[path.length() - 1] == '\\')
{
return path.substr(0, path.length() - 1) + "/";
}
return path + "/";
}
//! add a backslash if needed
inline stack_string AddSlash(const stack_string& path)
{
if (path.empty() || path[path.length() - 1] == '/')
{
return path;
}
if (path[path.length() - 1] == '\\')
{
return path.substr(0, path.length() - 1) + "/";
}
return path + "/";
}
//! add a backslash if needed
inline AZStd::string AddSlash(const char* path)
{
return AddSlash(AZStd::string(path));
}
inline stack_string ReplaceExtension(const stack_string& filepath, const char* ext)
{
stack_string str = filepath;
if (ext != 0)
{
RemoveExtension(str);
if (ext[0] != 0 && ext[0] != '.')
{
str += ".";
}
str += ext;
}
return str;
}
//! Replace extension for given file.
inline AZStd::string ReplaceExtension(const AZStd::string& filepath, const char* ext)
{
AZStd::string str = filepath;
if (ext != 0)
{
RemoveExtension(str);
if (ext[0] != 0 && ext[0] != '.')
{
str += ".";
}
str += ext;
}
return str;
}
//! Replace extension for given file.
inline AZStd::string ReplaceExtension(const char* filepath, const char* ext)
{
return ReplaceExtension(AZStd::string(filepath), ext);
}
//! Makes a fully specified file path from path and file name.
inline AZStd::string Make(const AZStd::string& path, const AZStd::string& file)
{
return AddSlash(path) + file;
}
//! Makes a fully specified file path from path and file name.
inline AZStd::string Make(const AZStd::string& dir, const AZStd::string& filename, const AZStd::string& ext)
{
AZStd::string path = filename;
AZ::StringFunc::Path::ReplaceExtension(path, ext.c_str());
path = AddSlash(dir) + path;
return path;
}
//! Makes a fully specified file path from path and file name.
inline AZStd::string Make(const AZStd::string& dir, const AZStd::string& filename, const char* ext)
{
return Make(dir, filename, AZStd::string(ext));
}
//! Makes a fully specified file path from path and file name.
inline stack_string Make(const stack_string& path, const stack_string& file)
{
return AddSlash(path) + file;
}
//! Makes a fully specified file path from path and file name.
inline stack_string Make(const stack_string& dir, const stack_string& filename, const stack_string& ext)
{
AZStd::string path = filename.c_str();
AZ::StringFunc::Path::ReplaceExtension(path, ext.c_str());
path = AddSlash(dir.c_str()) + path;
return stack_string(path.c_str());
}
//! Makes a fully specified file path from path and file name.
inline AZStd::string Make(const char* path, const AZStd::string& file)
{
return Make(AZStd::string(path), file);
}
//! Makes a fully specified file path from path and file name.
inline AZStd::string Make(const AZStd::string& path, const char* file)
{
return Make(path, AZStd::string(file));
}
//! Makes a fully specified file path from path and file name.
inline AZStd::string Make(const char path[], const char file[])
{
return Make(AZStd::string(path), AZStd::string(file));
}
//! Makes a fully specified file path from path and file name.
inline AZStd::string Make(const char* path, const char* file, const char* ext)
{
return Make(AZStd::string(path), AZStd::string(file), AZStd::string(ext));
}
//! Makes a fully specified file path from path and file name.
inline AZStd::string MakeFullPath(const AZStd::string& relativePath)
{
return relativePath;
}
inline AZStd::string GetParentDirectory (const AZStd::string& strFilePath, int nGeneration = 1)
{
for (const char* p = strFilePath.c_str() + strFilePath.length() - 2; // -2 is for the possible trailing slash: there always must be some trailing symbol which is the file/directory name for which we should get the parent
p >= strFilePath.c_str();
--p)
{
switch (*p)
{
case ':':
return AZStd::string(strFilePath.c_str(), p);
case '/':
case '\\':
// we've reached a path separator - return everything before it.
if (!--nGeneration)
{
return AZStd::string(strFilePath.c_str(), p);
}
break;
}
}
// it seems the file name is a pure name, without path or extension
return AZStd::string();
}
template<typename T, size_t SIZE>
inline AZStd::basic_fixed_string<T, SIZE> GetParentDirectoryStackString(const AZStd::basic_fixed_string<T, SIZE>& strFilePath, int nGeneration = 1)
{
for (const char* p = strFilePath.c_str() + strFilePath.length() - 2; // -2 is for the possible trailing slash: there always must be some trailing symbol which is the file/directory name for which we should get the parent
p >= strFilePath.c_str();
--p)
{
switch (*p)
{
case ':':
return AZStd::basic_fixed_string<T, SIZE> (strFilePath.c_str(), p);
case '/':
case '\\':
// we've reached a path separator - return everything before it.
if (!--nGeneration)
{
return AZStd::basic_fixed_string<T, SIZE>(strFilePath.c_str(), p);
}
break;
}
}
// it seems the file name is a pure name, without path or extension
return AZStd::basic_fixed_string<T, SIZE>();
}
//////////////////////////////////////////////////////////////////////////
// Description:
// Make a game correct path out of any input path.
inline stack_string MakeGamePath(const stack_string& path)
{
stack_string relativePath = path;
ToUnixPath(relativePath);
if ((!gEnv) || (!gEnv->pFileIO))
{
return relativePath;
}
unsigned int index = 0;
if (relativePath.length() && relativePath[index] == '@') // already aliased
{
if (AZ::StringFunc::Equal(relativePath.c_str(), "@products@/", false, 9))
{
return relativePath.substr(9); // assets is assumed.
}
return relativePath;
}
const char* rootValue = gEnv->pFileIO->GetAlias("@products@");
if (rootValue)
{
stack_string rootPath(ToUnixPath(rootValue));
if (
(rootPath.size() > 0) &&
(rootPath.size() < relativePath.size()) &&
(AZ::StringFunc::Equal(relativePath.c_str(), rootPath.c_str(), false, rootPath.size()))
)
{
stack_string chopped_string = relativePath.substr(rootPath.size());
stack_string rooted = stack_string("@products@") + chopped_string;
return rooted;
}
}
return relativePath;
}
//////////////////////////////////////////////////////////////////////////
// Description:
// Make a game correct path out of any input path.
inline AZStd::string MakeGamePath(const AZStd::string& path)
{
stack_string stackPath(path.c_str());
return MakeGamePath(stackPath).c_str();
}
// returns true if the AZStd::string matches the wildcard
inline bool MatchWildcard (const char* szString, const char* szWildcard)
{
const char* pString = szString, * pWildcard = szWildcard;
// skip the obviously the same starting substring
while (*pWildcard && *pWildcard != '*' && *pWildcard != '?')
{
if (*pString != *pWildcard)
{
return false; // must be exact match unless there's a wildcard character in the wildcard string
}
else
{
++pString, ++pWildcard;
}
}
if (!*pString)
{
// this will only match if there are no non-wild characters in the wildcard
for (; *pWildcard; ++pWildcard)
{
if (*pWildcard != '*' && *pWildcard != '?')
{
return false;
}
}
return true;
}
switch (*pWildcard)
{
case '\0':
return false; // the only way to match them after the leading non-wildcard characters is !*pString, which was already checked
// we have a wildcard with wild character at the start.
case '*':
{
// merge consecutive ? and *, since they are equivalent to a single *
while (*pWildcard == '*' || *pWildcard == '?')
{
++pWildcard;
}
if (!*pWildcard)
{
return true; // the rest of the AZStd::string doesn't matter: the wildcard ends with *
}
for (; *pString; ++pString)
{
if (MatchWildcard(pString, pWildcard))
{
return true;
}
}
return false;
}
case '?':
return MatchWildcard(pString + 1, pWildcard + 1) || MatchWildcard(pString, pWildcard + 1);
default:
assert (0);
return false;
}
}
};
#endif // CRYINCLUDE_CRYCOMMON_CRYPATH_H