From 3a1e6744fbcd69d16a7314a51248f540b0efa3ad Mon Sep 17 00:00:00 2001 From: Steve Pham <82231385+spham-amzn@users.noreply.github.com> Date: Tue, 30 Nov 2021 08:47:21 -0800 Subject: [PATCH] Save Data linux support (#6014) Implementation of SaveData for Linux Signed-off-by: Steve Pham <82231385+spham-amzn@users.noreply.github.com> --- .../Platform/Linux/AzTest_Traits_Linux.h | 2 - .../Linux/SaveData_SystemComponent_Linux.cpp | 173 ++++++++++++++++++ .../Platform/Linux/platform_linux_files.cmake | 2 +- 3 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 Gems/SaveData/Code/Source/Platform/Linux/SaveData_SystemComponent_Linux.cpp diff --git a/Code/Framework/AzTest/AzTest/Platform/Linux/AzTest_Traits_Linux.h b/Code/Framework/AzTest/AzTest/Platform/Linux/AzTest_Traits_Linux.h index d9b48b7835..755888e1d9 100644 --- a/Code/Framework/AzTest/AzTest/Platform/Linux/AzTest_Traits_Linux.h +++ b/Code/Framework/AzTest/AzTest/Platform/Linux/AzTest_Traits_Linux.h @@ -13,8 +13,6 @@ #define AZ_TRAIT_UNIT_TEST_ENTITY_ID_GEN_TEST_COUNT 10000 #define AZ_TRAIT_UNIT_TEST_DILLER_TRIGGER_EVENT_COUNT 100000 -#define AZ_TRAIT_DISABLE_ALL_SAVE_DATA_TESTS true - #define AZ_TRAIT_DISABLE_FAILED_AP_CONNECTION_TESTS true #define AZ_TRAIT_DISABLE_FAILED_ASSET_LOAD_TESTS true diff --git a/Gems/SaveData/Code/Source/Platform/Linux/SaveData_SystemComponent_Linux.cpp b/Gems/SaveData/Code/Source/Platform/Linux/SaveData_SystemComponent_Linux.cpp new file mode 100644 index 0000000000..caa9e9bf54 --- /dev/null +++ b/Gems/SaveData/Code/Source/Platform/Linux/SaveData_SystemComponent_Linux.cpp @@ -0,0 +1,173 @@ +/* + * 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 + * + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +namespace SaveData +{ + //////////////////////////////////////////////////////////////////////////////////////////////// + //! Platform specific implementation for the save data system component on Linux + class SaveDataSystemComponentLinux : public SaveDataSystemComponent::Implementation + { + public: + //////////////////////////////////////////////////////////////////////////////////////////// + static constexpr const char* DefaultSaveDataDirectoryName = "SaveData"; + + //////////////////////////////////////////////////////////////////////////////////////////// + // Allocator + AZ_CLASS_ALLOCATOR(SaveDataSystemComponentLinux, AZ::SystemAllocator, 0); + + //////////////////////////////////////////////////////////////////////////////////////////// + //! Constructor + //! \param[in] saveDataSystemComponent Reference to the parent being implemented + SaveDataSystemComponentLinux(SaveDataSystemComponent& saveDataSystemComponent); + + //////////////////////////////////////////////////////////////////////////////////////////// + //! Destructor + ~SaveDataSystemComponentLinux() override; + + protected: + //////////////////////////////////////////////////////////////////////////////////////////// + //! \ref SaveData::SaveDataSystemComponent::Implementation::SaveDataBuffer + void SaveDataBuffer(const SaveDataRequests::SaveDataBufferParams& saveDataBufferParams) override; + + //////////////////////////////////////////////////////////////////////////////////////////// + //! \ref SaveData::SaveDataSystemComponent::Implementation::LoadDataBuffer + void LoadDataBuffer(const SaveDataRequests::LoadDataBufferParams& loadDataBufferParams) override; + + //////////////////////////////////////////////////////////////////////////////////////////// + //! \ref SaveData::SaveDataSystemComponent::Implementation::SetSaveDataDirectoryPath + void SetSaveDataDirectoryPath(const char* saveDataDirectoryPath) override; + + private: + //////////////////////////////////////////////////////////////////////////////////////////// + //! Convenience function to construct the full save data file path. + //! \param[in] dataBufferName The name of the save data buffer. + //! \param[in] localUserId The local user id the save data buffer is associated with. + AZ::IO::Path GetSaveDataFilePath(const AZStd::string& dataBufferName, + AzFramework::LocalUserId localUserId); + + //////////////////////////////////////////////////////////////////////////////////////////// + //! The absolute path to the application's save data dircetory. + AZ::IO::Path m_saveDataDirectoryPathAbsolute; + }; + + //////////////////////////////////////////////////////////////////////////////////////////////// + AZ::IO::Path GetDefaultLinuxUserSaveDataPath() + { + // First priority for the home directory is the 'HOME' environment variable + const char* homeDir = getenv("HOME"); + if (homeDir == nullptr) + { + // If the 'HOME' environment variable is not set, then retrieve it from the 'getpwuid' + // system call + auto uid = getuid(); + auto pwuid = getpwuid(uid); + homeDir = pwuid->pw_dir; + } + + AZ_Assert(homeDir, "Unable to determine home directory for current Linux user"); + if (homeDir == nullptr) + { + homeDir = "/tmp"; + } + + AZ::IO::Path homePath {homeDir}; + + // $HOME/.local/share is the standard directory where user data is stored on Ubuntu + return homePath / ".local" / "share"; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + AZStd::string GetExecutableName() + { + char moduleFileName[AZ_MAX_PATH_LEN]; + AZ::Utils::GetExecutablePath(moduleFileName, AZ_MAX_PATH_LEN); + + AZ::IO::Path executableFullPath {moduleFileName}; + AZStd::string moduleFileNameString {executableFullPath.Filename().Native()}; + return moduleFileNameString; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + SaveDataSystemComponent::Implementation* SaveDataSystemComponent::Implementation::Create(SaveDataSystemComponent& saveDataSystemComponent) + { + return aznew SaveDataSystemComponentLinux(saveDataSystemComponent); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + SaveDataSystemComponentLinux::SaveDataSystemComponentLinux(SaveDataSystemComponent& saveDataSystemComponent) + : SaveDataSystemComponent::Implementation(saveDataSystemComponent) + , m_saveDataDirectoryPathAbsolute(GetDefaultLinuxUserSaveDataPath() / + GetExecutableName().c_str() / + DefaultSaveDataDirectoryName) + { + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + SaveDataSystemComponentLinux::~SaveDataSystemComponentLinux() + { + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + void SaveDataSystemComponentLinux::SaveDataBuffer(const SaveDataRequests::SaveDataBufferParams& saveDataBufferParams) + { + const AZStd::string absoluteFilePath = GetSaveDataFilePath(saveDataBufferParams.dataBufferName, + saveDataBufferParams.localUserId).c_str(); + SaveDataBufferToFileSystem(saveDataBufferParams, absoluteFilePath); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + void SaveDataSystemComponentLinux::LoadDataBuffer(const SaveDataRequests::LoadDataBufferParams& loadDataBufferParams) + { + const AZStd::string absoluteFilePath = GetSaveDataFilePath(loadDataBufferParams.dataBufferName, + loadDataBufferParams.localUserId).c_str(); + LoadDataBufferFromFileSystem(loadDataBufferParams, absoluteFilePath); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + void SaveDataSystemComponentLinux::SetSaveDataDirectoryPath(const char* saveDataDirectoryPath) + { + AZ::IO::Path saveDataDirectoryBasicPath { saveDataDirectoryPath }; + + if (saveDataDirectoryBasicPath.IsAbsolute()) + { + m_saveDataDirectoryPathAbsolute = saveDataDirectoryBasicPath; + } + else + { + m_saveDataDirectoryPathAbsolute = GetDefaultLinuxUserSaveDataPath() / saveDataDirectoryBasicPath; + } + + AZ_Assert(!m_saveDataDirectoryPathAbsolute.empty(), "Cannot set an empty save data directory path."); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + AZ::IO::Path SaveDataSystemComponentLinux::GetSaveDataFilePath(const AZStd::string& dataBufferName, + AzFramework::LocalUserId localUserId) + { + AZ::IO::Path saveDataFilePath = m_saveDataDirectoryPathAbsolute; + if (localUserId != AzFramework::LocalUserIdNone) + { + saveDataFilePath /= AZStd::string::format("User_%u", localUserId); + } + saveDataFilePath /= dataBufferName; + return saveDataFilePath; + } +} // namespace SaveData diff --git a/Gems/SaveData/Code/Source/Platform/Linux/platform_linux_files.cmake b/Gems/SaveData/Code/Source/Platform/Linux/platform_linux_files.cmake index 2ef4de6b91..42209a111c 100644 --- a/Gems/SaveData/Code/Source/Platform/Linux/platform_linux_files.cmake +++ b/Gems/SaveData/Code/Source/Platform/Linux/platform_linux_files.cmake @@ -7,7 +7,7 @@ # set(FILES - ../Common/Unimplemented/SaveData_SystemComponent_Unimplemented.cpp + SaveData_SystemComponent_Linux.cpp SaveData_Traits_Platform.h SaveData_Traits_Linux.h )