Files
o3de/Code/Legacy/CryCommon/CryFile.h
T
lumberyard-employee-dm 447832dd81 Updated the GameApplication to mount the engine.pak (#4128)
* Updated the GameApplication to mount the engine.pak

This allows loading the autoexec.cfg and bootstrap.game.<config>.<platform>.setreg from the engine.pak files
The engine.pak is searched for in the following order: <ExecutableDirectory>/engine.pak, followed by <ProjectCacheRoot>/engine.pak

Removed a lot of unused APIs from the AZ::IO::Archive feature suite
Updated many of the AZ::IO::Archive classes to use AZ::IO::Path internally.
The logic to search for files within an Archive has been updated to use AZ::IO::Path and to remove case-insensitve string comparisons
Somehow removed the CryFile dependency on anything Cry

Updated the Settings Registry to support reading from the FileIOBase and therefore Archive files in the GameLauncher via the `SetUseFileIO` function

Removed AzFramework Dependency on md5 3rdParty library

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

* Linux build fix

Added an include of <stdio.h> before the <rapidxml/rapidxml.h> include as it usesnprintf.

Added `static` to the constexpr constants in ExtractFileDescription in SettingsRegistryImpl.cpp to fix clang compile issue

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

* Updated the case used to mount the Engine PAK file in the GameApplication to be Engine.pak to match the other locations where it is mounted

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

* Updated the proper FFont call to FileIOBase::Size to supply the correct
integer type of AZ::u64 instead of size_t
This fixes building on platforms where size_t is type defined to be
unsigned long

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

* Fixed segmentation fault in Archive::Unregister when outputing the filename of the Archive file being closed

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

* Fix calls to OpenPack in the Legacy LevelSystem

The LevelSystem was calling the incorrect overload of OpenPack that
accepts BindRoot for the mounted level.pak instead of the overload that
that passes a memory block object.

This was causing the level pak files to be mounted using an invalid
directory, causing file accesses inside the level pak to fail.

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

* Updated the error messages in the ZipDir CacheFactory class to use AZ_Warning directly

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

* Updated the ArchiveFileIO m_trackedFiles container to store mapped type as an AZ::IO::Path

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2021-09-16 11:01:00 -05:00

209 lines
5.9 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 : File wrapper.
#pragma once
#include <AzCore/Console/IConsole.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/IO/FileIO.h>
#include <AzCore/IO/Path/Path.h>
#include <AzFramework/Archive/IArchive.h>
//////////////////////////////////////////////////////////////////////////
#define CRYFILE_MAX_PATH 260
//////////////////////////////////////////////////////////////////////////
// Summary:
// Wrapper on file system.
class CCryFile
{
public:
CCryFile();
CCryFile(const char* filename, const char* mode);
~CCryFile();
bool Open(const char* filename, const char* mode);
void Close();
// Summary:
// Writes data in a file to the current file position.
size_t Write(const void* lpBuf, size_t nSize);
// Summary:
// Reads data from a file at the current file position.
size_t ReadRaw(void* lpBuf, size_t nSize);
// Summary:
// Automatic endian-swapping version.
template<class T>
inline size_t ReadType(T* pDest, size_t nCount = 1)
{
size_t nRead = ReadRaw(pDest, sizeof(T) * nCount);
SwapEndian(pDest, nCount);
return nRead;
}
// Summary:
// Retrieves the length of the file.
size_t GetLength();
// Summary:
// Moves the current file pointer to the specified position.
size_t Seek(size_t seek, int mode);
// Description:
// Retrieves the filename of the selected file.
const char* GetFilename() const { return m_filename.c_str(); };
// Summary:
// Checks if file is opened from Archive file.
bool IsInPak() const;
// Summary:
// Gets path of archive this file is in.
AZ::IO::PathView GetPakPath() const;
private:
AZ::IO::FixedMaxPath m_filename;
AZ::IO::HandleType m_fileHandle;
};
// Summary:
// CCryFile implementation.
inline CCryFile::CCryFile()
{
m_fileHandle = AZ::IO::InvalidHandle;
}
//////////////////////////////////////////////////////////////////////////
inline CCryFile::CCryFile(const char* filename, const char* mode)
{
m_fileHandle = AZ::IO::InvalidHandle;
Open(filename, mode);
}
//////////////////////////////////////////////////////////////////////////
inline CCryFile::~CCryFile()
{
Close();
}
//////////////////////////////////////////////////////////////////////////
// Notes:
// For nOpenFlagsEx see IArchive::EFOpenFlags
// See also:
// IArchive::EFOpenFlags
inline bool CCryFile::Open(const char* filename, const char* mode)
{
m_filename = filename;
#if !defined (_RELEASE)
if (auto console = AZ::Interface<AZ::IConsole>::Get(); console != nullptr)
{
if (bool lowercasePaths{}; console->GetCvarValue("ed_lowercasepaths", lowercasePaths) == AZ::GetValueResult::Success)
{
if (lowercasePaths)
{
AZStd::to_lower(m_filename.Native().begin(), m_filename.Native().end());
}
}
}
#endif
if (m_fileHandle != AZ::IO::InvalidHandle)
{
Close();
}
AZ::IO::FileIOBase::GetInstance()->Open(m_filename.c_str(), AZ::IO::GetOpenModeFromStringMode(mode), m_fileHandle);
return m_fileHandle != AZ::IO::InvalidHandle;
}
//////////////////////////////////////////////////////////////////////////
inline void CCryFile::Close()
{
if (m_fileHandle != AZ::IO::InvalidHandle)
{
AZ::IO::FileIOBase::GetInstance()->Close(m_fileHandle);
m_fileHandle = AZ::IO::InvalidHandle;
m_filename.clear();
}
}
//////////////////////////////////////////////////////////////////////////
inline size_t CCryFile::Write(const void* lpBuf, size_t nSize)
{
AZ_Assert(m_fileHandle != AZ::IO::InvalidHandle, "File Handle is invalid. Cannot Write");
if (AZ::IO::FileIOBase::GetInstance()->Write(m_fileHandle, lpBuf, nSize))
{
return nSize;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
inline size_t CCryFile::ReadRaw(void* lpBuf, size_t nSize)
{
AZ_Assert(m_fileHandle != AZ::IO::InvalidHandle, "File Handle is invalid. Cannot Read");
AZ::u64 bytesRead = 0;
AZ::IO::FileIOBase::GetInstance()->Read(m_fileHandle, lpBuf, nSize, false, &bytesRead);
return static_cast<size_t>(bytesRead);
}
//////////////////////////////////////////////////////////////////////////
inline size_t CCryFile::GetLength()
{
AZ_Assert(m_fileHandle != AZ::IO::InvalidHandle, "File Handle is invalid. Cannot query file length");
//long curr = ftell(m_file);
AZ::u64 size = 0;
AZ::IO::FileIOBase::GetInstance()->Size(m_fileHandle, size);
return static_cast<size_t>(size);
}
//////////////////////////////////////////////////////////////////////////
inline size_t CCryFile::Seek(size_t seek, int mode)
{
AZ_Assert(m_fileHandle != AZ::IO::InvalidHandle, "File Handle is invalid. Cannot seek in unopen file");
if (AZ::IO::FileIOBase::GetInstance()->Seek(m_fileHandle, seek, AZ::IO::GetSeekTypeFromFSeekMode(mode)))
{
return 0;
}
return 1;
}
//////////////////////////////////////////////////////////////////////////
inline bool CCryFile::IsInPak() const
{
if (auto archive = AZ::Interface<AZ::IO::IArchive>::Get();
m_fileHandle != AZ::IO::InvalidHandle && archive != nullptr)
{
return !archive->GetFileArchivePath(m_fileHandle).empty();
}
return false;
}
//////////////////////////////////////////////////////////////////////////
inline AZ::IO::PathView CCryFile::GetPakPath() const
{
if (auto archive = AZ::Interface<AZ::IO::IArchive>::Get();
m_fileHandle != AZ::IO::InvalidHandle && archive != nullptr)
{
if (AZ::IO::PathView sPath(archive->GetFileArchivePath(m_fileHandle)); sPath.empty())
{
return sPath;
}
}
return {};
}