From ecc69bbd2e84e9d8a502702121d8d7d8aa677f8a Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 5 Feb 2025 11:52:44 +0300 Subject: [PATCH] [CKPE] SSE: - Final patch check .dds texture --- ...reation Kit Platform Extended Core.vcxproj | 1 + ...Kit Platform Extended Core.vcxproj.filters | 3 + .../Editor API/SSE/BSResources.h | 44 ++++++++++ .../Patches/SSE/LoadDDSFile.cpp | 81 ++++++++++++------ .../Patches/SSE/LoadDDSFile.h | 2 +- .../Version/build_version.txt | Bin 12 -> 12 bytes .../Version/resource_version2.h | Bin 2004 -> 2004 bytes 7 files changed, 106 insertions(+), 25 deletions(-) create mode 100644 Creation Kit Platform Extended Core/Editor API/SSE/BSResources.h diff --git a/Creation Kit Platform Extended Core/Creation Kit Platform Extended Core.vcxproj b/Creation Kit Platform Extended Core/Creation Kit Platform Extended Core.vcxproj index 5e3ddf7..f52c78e 100644 --- a/Creation Kit Platform Extended Core/Creation Kit Platform Extended Core.vcxproj +++ b/Creation Kit Platform Extended Core/Creation Kit Platform Extended Core.vcxproj @@ -560,6 +560,7 @@ + diff --git a/Creation Kit Platform Extended Core/Creation Kit Platform Extended Core.vcxproj.filters b/Creation Kit Platform Extended Core/Creation Kit Platform Extended Core.vcxproj.filters index 342a8b0..4629425 100644 --- a/Creation Kit Platform Extended Core/Creation Kit Platform Extended Core.vcxproj.filters +++ b/Creation Kit Platform Extended Core/Creation Kit Platform Extended Core.vcxproj.filters @@ -2203,6 +2203,9 @@ Dependecies\DirectXTexUtils + + Editor API\SSE + diff --git a/Creation Kit Platform Extended Core/Editor API/SSE/BSResources.h b/Creation Kit Platform Extended Core/Editor API/SSE/BSResources.h new file mode 100644 index 0000000..9ac4583 --- /dev/null +++ b/Creation Kit Platform Extended Core/Editor API/SSE/BSResources.h @@ -0,0 +1,44 @@ +// Copyright © 2023-2024 aka perchik71. All rights reserved. +// Contacts: +// License: https://www.gnu.org/licenses/gpl-3.0.html + +#pragma once + +#include "BSFixedString.h" + +#pragma pack(push, 1) + +namespace CreationKitPlatformExtended +{ + namespace EditorAPI + { + namespace SkyrimSpectialEdition + { + namespace BSResource + { + class LooseFileStream + { + char pad08[0x20]; + EditorAPI::SkyrimSpectialEdition::BSFixedString FileName; + public: + virtual ~LooseFileStream(); + + const EditorAPI::SkyrimSpectialEdition::BSFixedString GetFileName() const noexcept(true) { return FileName; } + }; + } + + class BSResourceNiBinaryStream + { + char pad00[0x10]; + void* ReadFuncPtr; + void* WriteFuncPtr; + BSResource::LooseFileStream* Stream; + public: + inline const BSResource::LooseFileStream* GetStream() const noexcept(true) { return Stream; } + }; + static_assert(sizeof(BSResourceNiBinaryStream) == 0x28); + } + } +} + +#pragma pack(pop) \ No newline at end of file diff --git a/Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.cpp b/Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.cpp index 6d2dabd..ccdafb4 100644 --- a/Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.cpp +++ b/Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.cpp @@ -4,6 +4,9 @@ #include "Core/Engine.h" #include "LoadDDSFile.h" +#include "Editor API/SSE/BSResources.h" + +#include namespace CreationKitPlatformExtended { @@ -62,10 +65,7 @@ namespace CreationKitPlatformExtended // // Replace direct crash with an assertion when an incompatible texture format is used in the renderer // - if (lpRelocator->GetEngine()->GetEditorVersion() >= EDITOR_EXECUTABLE_TYPE::EDITOR_SKYRIM_SE_1_6_1378_1) - lpRelocator->DetourCall(lpRelocationDatabaseItem->At(0), (uintptr_t)&sub_1378); - else - lpRelocator->DetourCall(lpRelocationDatabaseItem->At(0), (uintptr_t)&sub); + lpRelocator->DetourCall(lpRelocationDatabaseItem->At(0), (uintptr_t)&sub); pointer_LoadDDSFile_sub = lpRelocator->Rav2Off(lpRelocationDatabaseItem->At(1)); @@ -81,36 +81,69 @@ namespace CreationKitPlatformExtended return false; } - HRESULT LoadDDSFilePatch::sub(__int64 a1, __int64 a2, __int64 a3, __int64 a4, unsigned int a5, int a6) + HRESULT LoadDDSFilePatch::TryCheckArgs(__int64 a2) { - // Modified DirectX::LoadFromDDSFile from DDSTextureLoader (DirectXTex) - HRESULT hr = ((HRESULT(__fastcall*)(__int64, __int64, __int64, __int64, unsigned int, int))pointer_LoadDDSFile_sub)(a1, a2, a3, a4, a5, a6); + auto Resource = ((EditorAPI::SkyrimSpectialEdition::BSResourceNiBinaryStream*)a2); - const char* fileName = *(const char**)(*(__int64*)(a2 + 0x20) + 0x20); - AssertMsgVa(SUCCEEDED(hr), - "Fatal error while trying to load texture \"%s\" due to an incompatible file format. This " - "indicates a problem with your mod or game files. Note that B5G6R5 and B5G5R5A1 texture " - "formats are not supported on Windows 7. HR = 0x%08X.", - fileName, hr); + __try + { + if (!Resource->GetStream()) + { + _CONSOLE("ERROR: DirectX::LoadFromDDSFile empty stream."); + return E_FAIL; + } + + auto FileName = Resource->GetStream()->GetFileName(); + if (!FileName.data) + { + _CONSOLE("ERROR: DirectX::LoadFromDDSFile empty filename."); + return E_FAIL; + } + + FileName.data[0] = FileName.data[0]; + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + _CONSOLE("ERROR: DirectX::LoadFromDDSFile fatal error has occurred (invalid pointer Filename or NiStream)."); + return E_FAIL; + } - // This return value is ignored. If it fails it returns a null pointer (a3) and crashes later on. - return hr; + return S_OK; } - HRESULT LoadDDSFilePatch::sub_1378(__int64 a1, __int64 a2, __int64 a3, __int64 a4, unsigned int a5, int a6) + HRESULT LoadDDSFilePatch::sub(__int64 a1, __int64 a2, __int64 a3, __int64 a4, unsigned int a5, int a6) { + if (!a1 || !a2) + return E_FAIL; + + auto Resource = ((EditorAPI::SkyrimSpectialEdition::BSResourceNiBinaryStream*)a2); + AssertMsg(SUCCEEDED(TryCheckArgs(a2)), "Fatal load unknown texture"); + + if (!Resource->GetStream()->GetFileName().data[0]) + // Skips empty filename + return E_FAIL; + // Modified DirectX::LoadFromDDSFile from DDSTextureLoader (DirectXTex) - HRESULT hr = ((HRESULT(__fastcall*)(__int64, __int64, __int64, __int64, unsigned int, int))pointer_LoadDDSFile_sub)(a1, a2, a3, a4, a5, a6); + HRESULT hr = ((HRESULT(__fastcall*)(__int64, __int64, __int64, __int64, unsigned int, int))pointer_LoadDDSFile_sub) + (a1, a2, a3, a4, a5, a6); + if (FAILED(hr)) + { + _CONSOLE("ERROR: DirectX::LoadFromDDSFile returned failed (0x%08X) \"%s\" \"%s\".", + hr, _com_error(hr).ErrorMessage(), Resource->GetStream()->GetFileName().data); + + AssertMsgVa((hr == E_FAIL), + "Fatal error while trying to load texture \"%s\" due to an incompatible file format. This " + "indicates a problem with your mod or game files. Note that B5G6R5 and B5G5R5A1 texture " + "formats are not supported on Windows 7. HR = 0x%08X.", + Resource->GetStream()->GetFileName().data, hr); + + return hr; + } - const char* fileName = *(const char**)(*(__int64*)(a2 + 0x20) + 0x28); - AssertMsgVa(SUCCEEDED(hr), - "Fatal error while trying to load texture \"%s\" due to an incompatible file format. This " - "indicates a problem with your mod or game files. Note that B5G6R5 and B5G5R5A1 texture " - "formats are not supported on Windows 7. HR = 0x%08X.", - fileName, hr); + //_CONSOLE("DirectX::LoadFromDDSFile opened file \"%s\".", Resource->GetStream()->GetFileName().data); // This return value is ignored. If it fails it returns a null pointer (a3) and crashes later on. - return hr; + return S_OK; } } } diff --git a/Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.h b/Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.h index a79d6d8..c45c656 100644 --- a/Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.h +++ b/Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.h @@ -28,8 +28,8 @@ namespace CreationKitPlatformExtended virtual bool HasDependencies() const; virtual Array GetDependencies() const; + static HRESULT TryCheckArgs(__int64 a2); static HRESULT sub(__int64 a1, __int64 a2, __int64 a3, __int64 a4, unsigned int a5, int a6); - static HRESULT sub_1378(__int64 a1, __int64 a2, __int64 a3, __int64 a4, unsigned int a5, int a6); protected: virtual bool QueryFromPlatform(EDITOR_EXECUTABLE_TYPE eEditorCurrentVersion, const char* lpcstrPlatformRuntimeVersion) const; diff --git a/Creation Kit Platform Extended Core/Version/build_version.txt b/Creation Kit Platform Extended Core/Version/build_version.txt index 47225a90c389b10b5a013f5b9c0e744bcbbebd97..96f7b18961a1f62175b520e36619a084cbeb7ce8 100644 GIT binary patch literal 12 QcmezW&yoQIc^SAE033<}zyJUM literal 12 TcmezW&yvBC!H9vEfr|kE9AE;u diff --git a/Creation Kit Platform Extended Core/Version/resource_version2.h b/Creation Kit Platform Extended Core/Version/resource_version2.h index ae2250c7194b3d0d6184ba56f8f6f96e5e89b3dd..f3fedf675b4350132770e5f14fe9b766930ec170 100644 GIT binary patch delta 26 icmcb@e}#X;BW6|$23`iP$&Ad>lOtHAHp{T=UlOtHAHp{T=U