FileUtilities.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/IO/SystemFile.h>
  9. #include <AzFramework/StringFunc/StringFunc.h>
  10. #include <AzFramework/API/ApplicationAPI.h>
  11. #include <SceneAPI/SceneCore/Utilities/FileUtilities.h>
  12. namespace AZ
  13. {
  14. namespace SceneAPI
  15. {
  16. namespace Utilities
  17. {
  18. AZStd::string FileUtilities::CreateOutputFileName(
  19. const AZStd::string& groupName, const AZStd::string& outputDirectory, const AZStd::string& extension,
  20. const AZStd::string& sourceFileExtension)
  21. {
  22. // Create an initial name that looks like 'directory/groupName'
  23. AZ::IO::Path result(outputDirectory);
  24. result /= groupName;
  25. // Either add an extension or replace the existing one with the source file extension. This will typically
  26. // add the extension since most group names don't have an extension already.
  27. // This will ensure that final file name is unique based on the source file extension.
  28. // For example, 'model.fbx' and 'model.stl' would both have a groupName of 'model', and would then produce
  29. // 'model.azmodel' for example as the product asset for both. By appending the original source extension here,
  30. // we end up with the unique names of 'model.fbx.azmodel' and 'model.stl.azmodel'.
  31. // This could theoretically be fixed in the groupName generation, but that requires changes in many more places to the
  32. // code, along with knock-off effects that come from preserving the '.' which is also used by the SceneAPI to
  33. // separate node names. By adding it to the end file name, we avoid all of the negative effects and can centralize
  34. // the change to this one place.
  35. result.ReplaceExtension(AZ::IO::PathView(sourceFileExtension));
  36. // Append product extension to the file path by manipulating the string directly
  37. if (!extension.starts_with('.'))
  38. {
  39. result.Native() += '.';
  40. }
  41. result.Native() += extension;
  42. // Return the final file name.
  43. return result.LexicallyNormal().Native();
  44. }
  45. bool FileUtilities::EnsureTargetFolderExists(const AZStd::string& path)
  46. {
  47. AZStd::string folder;
  48. if (!AzFramework::StringFunc::Path::GetFullPath(path.c_str(), folder))
  49. {
  50. return false;
  51. }
  52. if (!IO::SystemFile::Exists(folder.c_str()))
  53. {
  54. if (!IO::SystemFile::CreateDir(folder.c_str()))
  55. {
  56. return false;
  57. }
  58. }
  59. return true;
  60. }
  61. AZStd::string FileUtilities::GetRelativePath(const AZStd::string& path, const AZStd::string& rootPath)
  62. {
  63. AZStd::string outputPath(path);
  64. EBUS_EVENT(AzFramework::ApplicationRequests::Bus, NormalizePath, outputPath);
  65. if (outputPath.compare(0, rootPath.size(), rootPath) == 0)
  66. {
  67. size_t offset = rootPath[rootPath.length() - 1] == '/' ? 1 : 0;
  68. AzFramework::StringFunc::RKeep(outputPath, rootPath.size() - offset);
  69. }
  70. return outputPath;
  71. }
  72. }
  73. }
  74. }