Skip to content

Commit

Permalink
[CKPE]
Browse files Browse the repository at this point in the history
SSE:
- Final patch check .dds texture
  • Loading branch information
Perchik71 committed Feb 5, 2025
1 parent 77cb004 commit ecc69bb
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@
<ClInclude Include="Editor API\SSE\BSGraphicsRenderTargetManager.h" />
<ClInclude Include="Editor API\SSE\BSGraphicsTypes.h" />
<ClInclude Include="Editor API\SSE\BSPointerHandleManager.h" />
<ClInclude Include="Editor API\SSE\BSResources.h" />
<ClInclude Include="Editor API\SSE\BSShaderMaterial.h" />
<ClInclude Include="Editor API\SSE\BSShaderProperty.h" />
<ClInclude Include="Editor API\SSE\BSShaderResourceManager.h" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,9 @@
<ClInclude Include="..\Dependencies\DirectXTex\include\ScreenGrab11.h">
<Filter>Dependecies\DirectXTexUtils</Filter>
</ClInclude>
<ClInclude Include="Editor API\SSE\BSResources.h">
<Filter>Editor API\SSE</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Version\resource_version.rc">
Expand Down
44 changes: 44 additions & 0 deletions Creation Kit Platform Extended Core/Editor API/SSE/BSResources.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright © 2023-2024 aka perchik71. All rights reserved.
// Contacts: <email:[email protected]>
// 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)
81 changes: 57 additions & 24 deletions Creation Kit Platform Extended Core/Patches/SSE/LoadDDSFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include "Core/Engine.h"
#include "LoadDDSFile.h"
#include "Editor API/SSE/BSResources.h"

#include <comdef.h>

namespace CreationKitPlatformExtended
{
Expand Down Expand Up @@ -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));

Expand All @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace CreationKitPlatformExtended
virtual bool HasDependencies() const;
virtual Array<String> 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;
Expand Down
Binary file modified Creation Kit Platform Extended Core/Version/build_version.txt
Binary file not shown.
Binary file modified Creation Kit Platform Extended Core/Version/resource_version2.h
Binary file not shown.

0 comments on commit ecc69bb

Please sign in to comment.