Pārlūkot izejas kodu

ScriptCanvas SourceHandle better uses, stores, and retrieves, absolute path, but still never serializes it. (#10404)

* MainWindow uses absolute path in metadata

Signed-off-by: carlitosan <[email protected]>

* Fix linux compile error

Signed-off-by: carlitosan <[email protected]>

* fix version explorer

Signed-off-by: carlitosan <[email protected]>

* remove absolute path from tab metadata

Signed-off-by: carlitosan <[email protected]>

* remove commented out line

Signed-off-by: carlitosan <[email protected]>
carlitosan 3 gadi atpakaļ
vecāks
revīzija
c4a8a446fc

+ 1 - 8
Gems/ScriptCanvas/Code/Editor/Assets/ScriptCanvasFileHandling.cpp

@@ -52,14 +52,7 @@ namespace ScriptCanvas
 
         if (!handle.Get())
         {
-            auto fullPathHandleOptional = ScriptCanvasEditor::GetFullPath(handle);
-            if (!fullPathHandleOptional)
-            {
-                return AZ::Failure(AZStd::string::format("Failure to determine the full, absolute path for relative path: %s"
-                    , handle.Path().c_str()));
-            }
-
-            auto loadResult = LoadFromFile((*fullPathHandleOptional).Native());
+            auto loadResult = LoadFromFile(handle.AbsolutePath().Native());
             if (!loadResult)
             {
                 return AZ::Failure(AZStd::string::format("LoadEditorAssetTree failed to load graph from %s: %s"

+ 10 - 33
Gems/ScriptCanvas/Code/Editor/Components/EditorUtils.cpp

@@ -48,25 +48,22 @@ namespace ScriptCanvasEditor
                 AZ::Data::AssetInfo assetInfoID;
                 if (assetSystem->GetSourceInfoBySourceUUID(source.Id(), assetInfoID, watchFolderID))
                 {
-                    return SourceHandle::FromRelativePath(source, assetInfoID.m_assetId.m_guid, assetInfoID.m_relativePath.c_str());
+                    return SourceHandle::MarkAbsolutePath
+                        ( SourceHandle::FromRelativePath(source, assetInfoID.m_assetId.m_guid, assetInfoID.m_relativePath.c_str())
+                        , (AZ::IO::Path(watchFolderID) / AZ::IO::Path(assetInfoID.m_relativePath)));
                 }
             }
 
             if (!source.Path().empty())
             {
-                AZStd::string rootFolder;
-                AZStd::string relativePath;
-                if (assetSystem->GenerateRelativeSourcePath(source.Path().c_str(), relativePath, rootFolder))
-                {
-                    AZStd::string watchFolderPath;
-                    AZ::Data::AssetInfo assetInfoPath;
-                    if (assetSystem->GetSourceInfoBySourcePath(relativePath.c_str(), assetInfoPath, watchFolderPath)
+                AZStd::string watchFolderPath;
+                AZ::Data::AssetInfo assetInfoPath;
+                if (assetSystem->GetSourceInfoBySourcePath(source.Path().c_str(), assetInfoPath, watchFolderPath)
                     && assetInfoPath.m_assetId.IsValid())
-                    {
-                        return SourceHandle::FromRelativePath(source, assetInfoPath.m_assetId.m_guid, assetInfoPath.m_relativePath.c_str());
-                    }
-
-                    return SourceHandle::FromRelativePath(source, relativePath);
+                {
+                    return SourceHandle::MarkAbsolutePath
+                        ( SourceHandle::FromRelativePath(source, assetInfoPath.m_assetId.m_guid, assetInfoPath.m_relativePath.c_str())
+                        , (AZ::IO::Path(watchFolderPath) / AZ::IO::Path(assetInfoPath.m_relativePath)));
                 }
             }
         }
@@ -87,26 +84,6 @@ namespace ScriptCanvasEditor
         }
     }
 
-    AZStd::optional<AZ::IO::Path> GetFullPath(const SourceHandle& source)
-    {
-        using namespace AzToolsFramework;
-
-        if (!source.Path().empty())
-        {
-            if (AssetSystemRequestBus::Events* assetSystem = AssetSystemRequestBus::FindFirstHandler())
-            {
-                AZStd::string rootFolder;
-                AZStd::string relativePath;
-                if (assetSystem->GenerateRelativeSourcePath(source.Path().c_str(), relativePath, rootFolder))
-                {
-                    return AZ::IO::Path(rootFolder) / AZ::IO::Path(relativePath);
-                }
-            }
-        }
-
-        return AZStd::nullopt;
-    }
-
     //////////////////////////
     // NodeIdentifierFactory
     //////////////////////////

+ 0 - 2
Gems/ScriptCanvas/Code/Editor/Include/ScriptCanvas/Components/EditorUtils.h

@@ -30,8 +30,6 @@ namespace ScriptCanvasEditor
     // accepts any sort of path and returns one with a relative path if possible
     AZStd::optional<SourceHandle> CreateFromAnyPath(const SourceHandle& source, const AZ::IO::Path& path);
 
-    AZStd::optional<AZ::IO::Path> GetFullPath(const SourceHandle& source);
-
     class EditorGraph;
     class NodePaletteModel;
 

+ 31 - 45
Gems/ScriptCanvas/Code/Editor/View/Windows/MainWindow.cpp

@@ -198,31 +198,6 @@ namespace ScriptCanvasEditor
 
             return mimeDelegateEntity->GetId();
         }
-
-        void EnsureSaveDestinationDirectory(AZStd::string directoryLocation)
-        {
-            if (AzFramework::StringFunc::Path::HasExtension(directoryLocation.c_str()))
-            {
-                size_t offset = directoryLocation.find_last_of('/');
-
-                if (offset != AZStd::string::npos)
-                {
-                    directoryLocation = directoryLocation.substr(0, offset);
-                }
-                else
-                {
-                    AzFramework::StringFunc::Path::StripComponent(directoryLocation, true);
-                }
-            }
-
-            // We just need the path to exist.
-            QDir canvasDirectory = QDir(directoryLocation.c_str());
-
-            if (!canvasDirectory.exists())
-            {
-                canvasDirectory.mkpath(".");
-            }
-        }
     } // anonymous namespace.
 
     void Workspace::Save()
@@ -1499,10 +1474,11 @@ namespace ScriptCanvasEditor
         static int scriptCanvasEditorDefaultNewNameCount = 0;
 
         AZStd::string assetPath;
+        AZStd::string newAssetName;
 
         for (;;)
         {
-            AZStd::string newAssetName = AZStd::string::format(SourceDescription::GetAssetNamePattern()
+            newAssetName = AZStd::string::format(SourceDescription::GetAssetNamePattern()
                 , ++scriptCanvasEditorDefaultNewNameCount);
             
             AZStd::array<char, AZ::IO::MaxPathLength> assetRootArray;
@@ -1521,7 +1497,7 @@ namespace ScriptCanvasEditor
             }
         }
 
-        auto createOutcome = CreateScriptCanvasAsset(assetPath);
+        auto createOutcome = CreateScriptCanvasAsset(newAssetName);
         if (!createOutcome.IsSuccess())
         {
             AZ_Warning("Script Canvas", createOutcome, "%s", createOutcome.GetError().data());
@@ -1620,7 +1596,13 @@ namespace ScriptCanvasEditor
 
     bool MainWindow::OnFileSave()
     {
-        if (auto metaData = m_tabBar->GetTabData(m_activeGraph); metaData && metaData->m_fileState == Tracker::ScriptCanvasFileState::NEW)
+        auto metaData = m_tabBar->GetTabData(m_activeGraph);
+        if (!metaData)
+        {
+            return false;
+        }
+
+        if (metaData && metaData->m_fileState == Tracker::ScriptCanvasFileState::NEW)
         {
             return SaveAssetImpl(m_activeGraph, Save::As);
         }
@@ -1677,39 +1659,50 @@ namespace ScriptCanvasEditor
         PrepareAssetForSave(sourceHandle);
 
         AZStd::string suggestedFilename;
+        AZStd::string suggestedDirectoryPath;
         AZStd::string suggestedFileFilter;
         bool isValidFileName = false;
 
         AZ::IO::FixedMaxPath projectSourcePath = AZ::Utils::GetProjectPath();
-        projectSourcePath /= "ScriptCanvas";
+        projectSourcePath /= "ScriptCanvas//";
+        QString selectedFile;
 
         if (save == Save::InPlace)
         {
             isValidFileName = true;
             suggestedFileFilter = SourceDescription::GetFileExtension();
-            suggestedFilename = sourceHandle.Path().Native();
+            
+            auto sourceHandlePath = sourceHandleIn.AbsolutePath();
+            selectedFile = sourceHandleIn.AbsolutePath().Native().c_str();
+            suggestedFilename = sourceHandleIn.AbsolutePath().Filename().Native();
+            sourceHandlePath.RemoveFilename();
+            suggestedDirectoryPath = sourceHandlePath.Native();
         }
         else
         {
             suggestedFileFilter = SourceDescription::GetFileExtension();
 
-            if (sourceHandle.Path().empty())
+            if (sourceHandle.Path().empty() || sourceHandle.Path() == sourceHandle.Path().Filename())
             {
-                suggestedFilename = projectSourcePath.Native();
+                suggestedDirectoryPath = projectSourcePath.Native();
+                suggestedFilename += sourceHandle.Path().Filename().Native();
             }
             else
             {
+                auto sourceHandlePath = sourceHandle.Path();
                 suggestedFilename = sourceHandle.Path().Native();
+                sourceHandlePath.RemoveFilename();
+                suggestedDirectoryPath = sourceHandlePath.Native();
             }
+
+            selectedFile = suggestedFilename.c_str();
         }
         
-        EnsureSaveDestinationDirectory(suggestedFilename);
         QString filter = suggestedFileFilter.c_str();
-        QString selectedFile = suggestedFilename.c_str();
-
+        
         while (!isValidFileName)
         {
-            selectedFile = AzQtComponents::FileDialog::GetSaveFileName(this, tr("Save As..."), suggestedFilename.data(), filter);
+            selectedFile = AzQtComponents::FileDialog::GetSaveFileName(this, tr("Save As..."), suggestedDirectoryPath.data(), filter);
 
             // If the selected file is empty that means we just cancelled.
             // So we want to break out.
@@ -1724,13 +1717,6 @@ namespace ScriptCanvasEditor
 
                 AZStd::string fileName;
 
-                // Verify that the path is within the project
-
-                AZStd::string assetRoot;
-                AZStd::array<char, AZ::IO::MaxPathLength> assetRootChar;
-                AZ::IO::FileIOBase::GetInstance()->ResolvePath("@engroot@", assetRootChar.data(), assetRootChar.size());
-                assetRoot = assetRootChar.data();
-
                 if (AzFramework::StringFunc::Path::GetFileName(filePath.c_str(), fileName))
                 {
                     isValidFileName = !(fileName.empty());
@@ -1803,6 +1789,7 @@ namespace ScriptCanvasEditor
         if (saveSuccess)
         {
             SourceHandle& fileAssetId = memoryAsset;
+            fileAssetId = SourceHandle::MarkAbsolutePath(fileAssetId, result.absolutePath);
             int currentTabIndex = m_tabBar->currentIndex();
 
             AZ::Data::AssetInfo assetInfo;
@@ -1829,7 +1816,6 @@ namespace ScriptCanvasEditor
 
             auto tabData = m_tabBar->GetTabData(saveTabIndex);
             tabData->m_fileState = Tracker::ScriptCanvasFileState::UNMODIFIED;
-
             tabData->m_assetId = fileAssetId;
             m_tabBar->SetTabData(*tabData, saveTabIndex);
             m_tabBar->SetTabText(saveTabIndex, tabName.c_str());
@@ -1887,7 +1873,7 @@ namespace ScriptCanvasEditor
                 , [this](const VersionExplorer::FileSaveResult& fileSaveResult) { OnSaveCallBack(fileSaveResult); });
 
         MarkRecentSave(sourceHandle);
-        m_fileSaver->Save(sourceHandle);
+        m_fileSaver->Save(sourceHandle, path);
 
         BlockCloseRequests();        
     }

+ 5 - 11
Gems/ScriptCanvas/Code/Editor/View/Windows/Tools/UpgradeTool/FileSaver.cpp

@@ -89,6 +89,7 @@ namespace ScriptCanvasEditor
                     AZ::SystemTickBus::QueueFunction([this, tmpFileName]()
                         {
                             FileSaveResult result;
+                            result.absolutePath = m_fullPath;
                             result.tempFileRemovalError = RemoveTempFile(tmpFileName);
                             m_onComplete(result);
                         });
@@ -191,20 +192,13 @@ namespace ScriptCanvasEditor
             return "";
         }
 
-        void FileSaver::Save(const SourceHandle& source)
+        void FileSaver::Save(const SourceHandle& source, const AZ::IO::Path& absolutePath)
         {
+            m_fullPath.clear();
             m_source = source;
-            auto fullPathOptional = GetFullPath(source);
-            if (fullPathOptional)
-            {
-                m_fullPath = *fullPathOptional;
-            }
-            else
-            {
-                m_fullPath.clear();
-            }
+            m_fullPath = absolutePath;
 
-            if (source.Path().empty())
+            if (m_fullPath.empty())
             {
                 FileSaveResult result;
                 result.fileSaveError = "No save location specified";

+ 2 - 2
Gems/ScriptCanvas/Code/Editor/View/Windows/Tools/UpgradeTool/FileSaver.h

@@ -18,7 +18,7 @@ namespace ScriptCanvasEditor
         {
             AZStd::string fileSaveError;
             AZStd::string tempFileRemovalError;
-
+            AZ::IO::Path absolutePath;
             bool IsSuccess() const;
         };
 
@@ -34,7 +34,7 @@ namespace ScriptCanvasEditor
                 , AZStd::function<void(const FileSaveResult& result)> onComplete);
 
             const SourceHandle& GetSource() const;
-            void Save(const SourceHandle& source);
+            void Save(const SourceHandle& source, const AZ::IO::Path& absolutePath);
 
         private:
             AZStd::mutex m_mutex;

+ 2 - 3
Gems/ScriptCanvas/Code/Editor/View/Windows/Tools/UpgradeTool/Modifier.cpp

@@ -215,7 +215,6 @@ namespace ScriptCanvasEditor
             if (m_assetIndex != m_assets.size())
             {
                 m_result.asset = m_assets[GetCurrentIndex()];
-                CompleteDescriptionInPlace(m_result.asset);
                 m_attemptedAssets.insert(m_result.asset.Id());
             }
         }
@@ -225,7 +224,7 @@ namespace ScriptCanvasEditor
             auto& handle = m_result.asset;
             if (!handle.IsGraphValid())
             {
-                auto result = ScriptCanvas::LoadFromFile(handle.Path().c_str());
+                auto result = ScriptCanvas::LoadFromFile(handle.AbsolutePath().c_str());
                 if (result)
                 {
                     handle = result.m_handle;
@@ -391,7 +390,7 @@ namespace ScriptCanvasEditor
             m_fileSaver = AZStd::make_unique<FileSaver>
                     ( m_config.onReadOnlyFile
                     , [this](const FileSaveResult& fileSaveResult) { OnFileSaveComplete(fileSaveResult); });
-            m_fileSaver->Save(result.asset);
+            m_fileSaver->Save(result.asset, result.asset.AbsolutePath());
         }
 
         void Modifier::SortGraphsByDependencies()

+ 8 - 9
Gems/ScriptCanvas/Code/Editor/View/Windows/Tools/UpgradeTool/Scanner.cpp

@@ -13,6 +13,7 @@
 #include <AzToolsFramework/AssetBrowser/AssetBrowserModel.h>
 #include <Editor/View/Windows/Tools/UpgradeTool/LogTraits.h>
 #include <Editor/View/Windows/Tools/UpgradeTool/Scanner.h>
+#include <ScriptCanvas/Components/EditorUtils.h>
 #include <ScriptCanvas/Assets/ScriptCanvasFileHandling.h>
 
 namespace ScannerCpp
@@ -34,9 +35,9 @@ namespace ScannerCpp
         && entry->GetFullPath().ends_with(".scriptcanvas"))
         {
             auto sourceEntry = azrtti_cast<const AzToolsFramework::AssetBrowser::SourceAssetBrowserEntry*>(entry);
-            AZStd::string fullPath = sourceEntry->GetRelativePath();
-            AzFramework::StringFunc::Path::Normalize(fullPath);
-            result.m_catalogAssets.push_back(SourceHandle::FromRelativePath(nullptr, sourceEntry->GetSourceUuid(), fullPath));
+            result.m_catalogAssets.push_back
+                ( SourceHandle::MarkAbsolutePath
+                    ( SourceHandle::FromRelativePath(nullptr, sourceEntry->GetRelativePath()), sourceEntry->GetFullPath()));
         }
 
         const int rowCount = model.rowCount(index);
@@ -101,15 +102,13 @@ namespace ScriptCanvasEditor
 
         SourceHandle Scanner::LoadAsset()
         {
-            auto result = ScriptCanvas::LoadFromFile(ModCurrentAsset().Path().c_str());
-            if (result)
-            {
-                return result.m_handle;
-            }
-            else
+            auto result = ScriptCanvas::LoadFromFile(ModCurrentAsset().AbsolutePath().Native());
+            if (!result)
             {
                 return {};
             }
+
+            return result.m_handle;
         }
 
         SourceHandle& Scanner::ModCurrentAsset()

+ 18 - 2
Gems/ScriptCanvas/Code/Include/ScriptCanvas/Core/Core.cpp

@@ -196,6 +196,7 @@ namespace ScriptCanvas
         : m_data(source.m_data)
         , m_id(source.Id())
         , m_relativePath(source.m_relativePath)
+        , m_absolutePath(source.m_absolutePath)
     {
         SanitizePath();
     }
@@ -232,6 +233,11 @@ namespace ScriptCanvas
         SanitizePath();
     }
 
+    const AZ::IO::Path& SourceHandle::AbsolutePath() const
+    {
+        return m_absolutePath;
+    }
+
     bool SourceHandle::AnyEquals(const SourceHandle& other) const
     {
         return m_data && m_data == other.m_data
@@ -244,6 +250,7 @@ namespace ScriptCanvas
         m_data = nullptr;
         m_id = AZ::Uuid::CreateNull();
         m_relativePath.clear();
+        m_absolutePath.clear();
     }
 
     DataPtr SourceHandle::Data() const
@@ -254,7 +261,7 @@ namespace ScriptCanvas
     // return a SourceHandle with only the Id and Path, but without a pointer to the data
     SourceHandle SourceHandle::Describe() const
     {
-        return SourceHandle(nullptr, m_id, m_relativePath);
+        return MarkAbsolutePath(SourceHandle(nullptr, m_id, m_relativePath), m_absolutePath);
     }
 
     SourceHandle SourceHandle::FromRelativePath(const SourceHandle& data, const AZ::Uuid& id, const AZ::IO::Path& path)
@@ -297,6 +304,14 @@ namespace ScriptCanvas
         return m_data != nullptr;
     }
 
+    SourceHandle SourceHandle::MarkAbsolutePath(const SourceHandle& data, const AZ::IO::Path& path)
+    {
+        SourceHandle result(data);
+        result.m_absolutePath = path;
+        result.m_absolutePath.MakePreferred();
+        return result;
+    }
+
     ScriptCanvasEditor::GraphPtr SourceHandle::Mod() const
     {
         return m_data ? m_data->ModEditorGraph() : nullptr;
@@ -311,7 +326,8 @@ namespace ScriptCanvas
     {
         return m_data.get() == other.m_data.get()
             && m_id == other.m_id
-            && m_relativePath == other.m_relativePath;
+            && m_relativePath == other.m_relativePath
+            && m_absolutePath == other.m_absolutePath;
     }
 
     bool SourceHandle::operator!=(const SourceHandle& other) const

+ 5 - 0
Gems/ScriptCanvas/Code/Include/ScriptCanvas/Core/Core.h

@@ -358,6 +358,8 @@ namespace ScriptCanvas
 
         static SourceHandle FromRelativePath(ScriptCanvas::DataPtr graph, const AZ::IO::Path& path);
 
+        static SourceHandle MarkAbsolutePath(const SourceHandle& data, const AZ::IO::Path& path);
+
         SourceHandle();
 
         SourceHandle(const SourceHandle& source);
@@ -366,6 +368,8 @@ namespace ScriptCanvas
 
         SourceHandle(ScriptCanvas::DataPtr graph, const AZ::Uuid& id);
 
+        const AZ::IO::Path& AbsolutePath() const;
+
         bool AnyEquals(const SourceHandle& other) const;
 
         void Clear();
@@ -409,6 +413,7 @@ namespace ScriptCanvas
         DataPtr m_data;
         AZ::Uuid m_id = AZ::Uuid::CreateNull();
         AZ::IO::Path m_relativePath;
+        AZ::IO::Path m_absolutePath;
 
         void SanitizePath();
     };