Cheats: Use libzip for reading archive

This commit is contained in:
Stenzek 2024-10-22 21:08:36 +10:00
parent 761c8bdf7f
commit a8d846ac8f
No known key found for this signature in database
6 changed files with 24 additions and 18 deletions

View File

@ -20,6 +20,7 @@ find_package(lunasvg 2.4.1 REQUIRED)
find_package(cpuinfo REQUIRED)
find_package(DiscordRPC 3.4.0 REQUIRED)
find_package(SoundTouch 2.3.3 REQUIRED)
find_package(libzip 1.11.1 REQUIRED)
if(NOT WIN32)
find_package(CURL REQUIRED)

View File

@ -140,7 +140,7 @@ target_precompile_headers(core PRIVATE "pch.h")
target_include_directories(core PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..")
target_include_directories(core PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..")
target_link_libraries(core PUBLIC Threads::Threads common util)
target_link_libraries(core PRIVATE xxhash imgui minizip rapidyaml rcheevos cpuinfo::cpuinfo ZLIB::ZLIB Zstd::Zstd)
target_link_libraries(core PRIVATE xxhash imgui rapidyaml rcheevos cpuinfo::cpuinfo ZLIB::ZLIB Zstd::Zstd libzip::zip)
if(CPU_ARCH_X64)
target_compile_definitions(core PUBLIC "ENABLE_RECOMPILER=1" "ENABLE_NEWREC=1" "ENABLE_MMAP_FASTMEM=1")

View File

@ -16,10 +16,10 @@
#include "common/error.h"
#include "common/file_system.h"
#include "common/log.h"
#include "common/minizip_helpers.h"
#include "common/path.h"
#include "common/small_string.h"
#include "common/string_util.h"
#include "common/zip_helpers.h"
#include "IconsEmoji.h"
#include "IconsFontAwesome5.h"
@ -99,29 +99,31 @@ class CheatArchive
public:
~CheatArchive()
{
if (m_unz_file)
unzClose(m_unz_file);
// zip has to be destroyed before data
m_zip.reset();
m_data.deallocate();
}
ALWAYS_INLINE bool IsOpen() const { return (m_unz_file != nullptr); }
ALWAYS_INLINE bool IsOpen() const { return static_cast<bool>(m_zip); }
bool Open(const char* name)
{
if (m_unz_file)
if (m_zip)
return true;
std::optional<DynamicHeapArray<u8>> data = Host::ReadResourceFile(name, false);
Error error;
std::optional<DynamicHeapArray<u8>> data = Host::ReadResourceFile(name, false, &error);
if (!data.has_value())
{
ERROR_LOG("Failed to read cheat archive {}.", name);
ERROR_LOG("Failed to read cheat archive {}: {}", name, error.GetDescription());
return false;
}
m_data = std::move(data.value());
m_unz_file = MinizipHelpers::OpenUnzMemoryFile(m_data.data(), m_data.size());
if (!m_unz_file) [[unlikely]]
m_zip = ZipHelpers::OpenManagedZipBuffer(m_data.data(), m_data.size(), 0, false, &error);
if (!m_zip) [[unlikely]]
{
ERROR_LOG("Failed to open cheat archive {}.", name);
ERROR_LOG("Failed to open cheat archive {}: {}", name, error.GetDescription());
return false;
}
@ -131,7 +133,7 @@ public:
std::optional<std::string> ReadFile(const char* name) const
{
Error error;
std::optional<std::string> ret = MinizipHelpers::ReadZipFileToString(m_unz_file, name, false, &error);
std::optional<std::string> ret = ZipHelpers::ReadFileInZipToString(m_zip.get(), name, true, &error);
if (!ret.has_value())
DEV_LOG("Failed to read {} from zip: {}", name, error.GetDescription());
return ret;
@ -141,7 +143,7 @@ private:
// Maybe counter-intuitive, but it ends up faster for reading a single game's cheats if we keep a
// copy of the archive in memory, as opposed to reading from disk.
DynamicHeapArray<u8> m_data;
unzFile m_unz_file = nullptr;
ZipHelpers::ManagedZipT m_zip;
};
} // namespace

View File

@ -22,4 +22,10 @@
<AdditionalIncludeDirectories Condition="'$(Platform)'=='ARM' Or '$(Platform)'=='ARM64'">%(AdditionalIncludeDirectories);$(SolutionDir)dep\vixl\include</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>%(AdditionalDependencies);cpuinfo.lib;zip.lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
</Project>

View File

@ -174,9 +174,6 @@
<ProjectReference Include="..\..\dep\imgui\imgui.vcxproj">
<Project>{bb08260f-6fbc-46af-8924-090ee71360c6}</Project>
</ProjectReference>
<ProjectReference Include="..\..\dep\minizip\minizip.vcxproj">
<Project>{8bda439c-6358-45fb-9994-2ff083babe06}</Project>
</ProjectReference>
<ProjectReference Include="..\..\dep\rainterface\rainterface.vcxproj" Condition="'$(Platform)'!='ARM64'">
<Project>{e4357877-d459-45c7-b8f6-dcbb587bb528}</Project>
</ProjectReference>
@ -214,7 +211,6 @@
<ClCompile>
<PreprocessorDefinitions Condition="'$(Platform)'=='x64' And $(Configuration.Contains('Debug'))">ZYDIS_DISABLE_ENCODER;ZYDIS_DISABLE_AVX512;ZYDIS_DISABLE_KNC;ZYDIS_STATIC_BUILD;ZYCORE_STATIC_BUILD;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories Condition="'$(Platform)'=='x64' And $(Configuration.Contains('Debug'))">%(AdditionalIncludeDirectories);$(SolutionDir)dep\zydis\include;$(SolutionDir)dep\zydis\dependencies\zycore\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)dep\minizip\include</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)/%(RelativeDir)/</ObjectFileName>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>

View File

@ -25,7 +25,7 @@
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(DepsIncludeDir)SDL2;$(DepsIncludeDir)spirv_cross</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalDependencies>%(AdditionalDependencies);cpuinfo.lib;freetype.lib;jpeg.lib;libpng16.lib;libwebp.lib;lunasvg.lib;SDL2.lib;soundtouch.lib;zlib.lib;zstd.lib</AdditionalDependencies>
<AdditionalDependencies>%(AdditionalDependencies);freetype.lib;jpeg.lib;libpng16.lib;libwebp.lib;lunasvg.lib;SDL2.lib;soundtouch.lib;zlib.lib;zstd.lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
@ -44,6 +44,7 @@
<DepsDLLs Include="$(DepsBinDir)shaderc_shared.dll" />
<DepsDLLs Include="$(DepsBinDir)soundtouch.dll" />
<DepsDLLs Include="$(DepsBinDir)spirv-cross-c-shared.dll" />
<DepsDLLs Include="$(DepsBinDir)zip.dll" />
<DepsDLLs Include="$(DepsBinDir)zlib1.dll" />
<DepsDLLs Include="$(DepsBinDir)zstd.dll" />
</ItemGroup>