Skip to content

Commit

Permalink
[CKPE]
Browse files Browse the repository at this point in the history
SF:
- Allow open archive < 4GB;
  • Loading branch information
Perchik71 committed Dec 10, 2024
1 parent 49d8dff commit 96239f6
Show file tree
Hide file tree
Showing 19 changed files with 826 additions and 21 deletions.
63 changes: 42 additions & 21 deletions Creation Kit Platform Extended Core/Core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,30 @@ namespace CreationKitPlatformExtended

//////////////////////////////////////////////

BOOL WINAPI hk_QueryPerformanceCounter(LARGE_INTEGER* lpPerformanceCount)
static BOOL WINAPI GetVerOs(LPDWORD lpdwMajorVersion, LPDWORD lpdwMinorVersion, LPDWORD lpdwBuildNubmer)
{
if (!lpdwMajorVersion || !lpdwMinorVersion || !lpdwBuildNubmer)
return FALSE;

LONG(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW) = nullptr;
OSVERSIONINFOEXW osInfo = { 0 };
*(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");
if (RtlGetVersion)
{
osInfo.dwOSVersionInfoSize = sizeof(osInfo);
RtlGetVersion(&osInfo);

*lpdwMajorVersion = osInfo.dwMajorVersion;
*lpdwMinorVersion = osInfo.dwMinorVersion;
*lpdwBuildNubmer = osInfo.dwBuildNumber;

return TRUE;
}

return FALSE;
}

static BOOL WINAPI hk_QueryPerformanceCounter(LARGE_INTEGER* lpPerformanceCount)
{
// Выключение точки останова
(GlobalEnginePtr->*VCoreDisableBreakpoint)();
Expand All @@ -87,7 +110,7 @@ namespace CreationKitPlatformExtended
return QueryPerformanceCounter(lpPerformanceCount);
}

LONG NTAPI hk_NtSetInformationThread(HANDLE ThreadHandle, LONG ThreadInformationClass,
static LONG NTAPI hk_NtSetInformationThread(HANDLE ThreadHandle, LONG ThreadInformationClass,
PVOID ThreadInformation, ULONG ThreadInformationLength)
{
// Для Steam
Expand All @@ -108,25 +131,14 @@ namespace CreationKitPlatformExtended

auto OsVer = &_OsVersion;
ZeroMemory(OsVer, sizeof(OsVersion));

LONG(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW) = nullptr;
OSVERSIONINFOEXW osInfo = { 0 };
*(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");
if (RtlGetVersion)
{
osInfo.dwOSVersionInfoSize = sizeof(osInfo);
RtlGetVersion(&osInfo);

OsVer->MajorVersion = osInfo.dwMajorVersion;
OsVer->MinorVersion = osInfo.dwMinorVersion;
OsVer->BuildNubmer = osInfo.dwBuildNumber;
}

GetVerOs((LPDWORD)&OsVer->MajorVersion, (LPDWORD)&OsVer->MinorVersion, (LPDWORD)&OsVer->BuildNubmer);
auto str = EditorAPI::BSString::FormatString("CKPE Runtime: Initialize (Version: %s, OS: %u.%u Build %u)",
VER_FILE_VERSION_STR, OsVer->MajorVersion, OsVer->MinorVersion, OsVer->BuildNubmer);

_MESSAGE(str.c_str());

if ((OsVer->MajorVersion == 6) && (OsVer->MinorVersion < 3))
_CONSOLE("[WARNING] Your OS is not fully supported");

int info[4];
__cpuid(info, 7);
_hasAVX2 = (info[1] & (1 << 5)) != 0;
Expand Down Expand Up @@ -600,6 +612,19 @@ namespace CreationKitPlatformExtended

IResult Result = RC_OK;

DWORD dwMajor, dwMinor, dwBuild;
if (!GetVerOs(&dwMajor, &dwMinor, &dwBuild))
{
if (dwMajor < 6)
{
_ERROR("Unsupported OS version");

return RC_UNSUPPORT_VERSION_OS;
}
else if ((dwMajor == 6) && (dwMinor < 3)) // Need 8.1
_WARNING("Your OS is not fully supported");
}

// Доступные имена для Creation Kit
if (CheckFileNameProcess(lpcstrAppName))
{
Expand All @@ -611,10 +636,6 @@ namespace CreationKitPlatformExtended
GlobalTracerManagerPtr = new TracerManager();
AssertMsg(GlobalTracerManagerPtr, "Failed to initialize class \"TracerManager\".");
#endif
// Инициализация библиотеки vup
//AssertMsg(Conversion::LazUnicodePluginInit(),
// "I can't find the library: \"vup-x86_64.dll\"\nReinstall the mod.");

GlobalDebugLogPtr = new DebugLog(L"CreationKitPlatformExtended.log");
AssertMsg(GlobalDebugLogPtr, "Failed create the log file \"CreationKitPlatformExtended.log\"");

Expand Down
2 changes: 2 additions & 0 deletions Creation Kit Platform Extended Core/Core/EngineSFPatches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Patches/SF/LoadMaterialsQSplash.h"
#include "Patches/SF/FixQuoteCmdLineSF.h"
#include "Patches/SF/AllowSaveESMandMasterESPSF.h"
#include "Patches/SF/BSResourceLooseFilesPatchSF.h"

#include "Patches/Windows/SF/DataWindowSF.h"
#include "Patches/Windows/SF/AboutWindowSF.h"
Expand Down Expand Up @@ -52,6 +53,7 @@ namespace CreationKitPlatformExtended
new Patches::LoadMaterialsQSplashPatch(),
new Patches::FixQuoteCmdLinePatch(),
new Patches::AllowSaveESMandMasterESPPatch(),
new Patches::BSResourceLooseFilesPatch(),

new Patches::DataWindow(),
new Patches::AboutWindow(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace CreationKitPlatformExtended
"\nCheck out the list of supported versions on the mod page.",
"Unknown application",
"Reinitializing the engine"
"Your OS is not supported"
};

return szMessage[nErrorNo];
Expand Down
1 change: 1 addition & 0 deletions Creation Kit Platform Extended Core/Core/ResultCoreErrNo.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace CreationKitPlatformExtended
constexpr static IResult RC_DEPRECATED_VERSION_CREATIONKIT = 2;
constexpr static IResult RC_UNKNOWN_APPLICATION = 3;
constexpr static IResult RC_INITIALIZATION_ENGINE_AGAIN = 4;
constexpr static IResult RC_UNSUPPORT_VERSION_OS = 5;

inline bool Successed(IResult nErrorNo) { return nErrorNo == RC_OK; }
inline bool Failed(IResult nErrorNo) { return nErrorNo != RC_OK; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
<ClCompile Include="Editor API\FO4\TESFormF4.cpp" />
<ClCompile Include="Editor API\Settings.cpp" />
<ClCompile Include="Editor API\SF\BGSRenderWindowSF.cpp" />
<ClCompile Include="Editor API\SF\BSResourceArchive2SF.cpp" />
<ClCompile Include="Editor API\SF\BSResourceLooseFilesSF.cpp" />
<ClCompile Include="Editor API\SF\TESDataHandlerSF.cpp" />
<ClCompile Include="Editor API\SF\TESFileSF.cpp" />
<ClCompile Include="Editor API\SSE\BSGraphicsTypes.cpp" />
Expand Down Expand Up @@ -260,6 +262,8 @@
<ClCompile Include="Patches\RenameCreationKitApp.cpp" />
<ClCompile Include="Patches\RenderWindow60FPS.cpp" />
<ClCompile Include="Patches\SF\AllowSaveESMandMasterESPSF.cpp" />
<ClCompile Include="Patches\SF\BSArchiveManagerModdedSF.cpp" />
<ClCompile Include="Patches\SF\BSResourceLooseFilesPatchSF.cpp" />
<ClCompile Include="Patches\SF\FixQuoteCmdLineSF.cpp" />
<ClCompile Include="Patches\SF\LoadMaterialsQSplash.cpp" />
<ClCompile Include="Patches\SF\OptimizationLoadSF.cpp" />
Expand Down Expand Up @@ -530,6 +534,8 @@
<ClInclude Include="Editor API\SF\BSEntryString.h" />
<ClInclude Include="Editor API\SF\BSFile.h" />
<ClInclude Include="Editor API\SF\BSFixedString.h" />
<ClInclude Include="Editor API\SF\BSResourceArchive2SF.h" />
<ClInclude Include="Editor API\SF\BSResourceLooseFilesSF.h" />
<ClInclude Include="Editor API\SF\TESDataHandler.h" />
<ClInclude Include="Editor API\SF\TESFileSF.h" />
<ClInclude Include="Editor API\SF\TESForm.h" />
Expand Down Expand Up @@ -661,6 +667,8 @@
<ClInclude Include="Patches\RenameCreationKitApp.h" />
<ClInclude Include="Patches\RenderWindow60FPS.h" />
<ClInclude Include="Patches\SF\AllowSaveESMandMasterESPSF.h" />
<ClInclude Include="Patches\SF\BSArchiveManagerModdedSF.h" />
<ClInclude Include="Patches\SF\BSResourceLooseFilesPatchSF.h" />
<ClInclude Include="Patches\SF\FixQuoteCmdLineSF.h" />
<ClInclude Include="Patches\SF\LoadMaterialsQSplash.h" />
<ClInclude Include="Patches\SF\OptimizationLoadSF.h" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,18 @@
<ClCompile Include="Patches\FO4\ResponseIgnoreMaxF4.cpp">
<Filter>Patches\FO4</Filter>
</ClCompile>
<ClCompile Include="Patches\SF\BSArchiveManagerModdedSF.cpp">
<Filter>Patches\SF</Filter>
</ClCompile>
<ClCompile Include="Editor API\SF\BSResourceArchive2SF.cpp">
<Filter>Editor API\SF</Filter>
</ClCompile>
<ClCompile Include="Editor API\SF\BSResourceLooseFilesSF.cpp">
<Filter>Editor API\SF</Filter>
</ClCompile>
<ClCompile Include="Patches\SF\BSResourceLooseFilesPatchSF.cpp">
<Filter>Patches\SF</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Version">
Expand Down Expand Up @@ -2170,6 +2182,18 @@
<ClInclude Include="Patches\FO4\ResponseIgnoreMaxF4.h">
<Filter>Patches\FO4</Filter>
</ClInclude>
<ClInclude Include="Patches\SF\BSArchiveManagerModdedSF.h">
<Filter>Patches\SF</Filter>
</ClInclude>
<ClInclude Include="Editor API\SF\BSResourceArchive2SF.h">
<Filter>Editor API\SF</Filter>
</ClInclude>
<ClInclude Include="Editor API\SF\BSResourceLooseFilesSF.h">
<Filter>Editor API\SF</Filter>
</ClInclude>
<ClInclude Include="Patches\SF\BSResourceLooseFilesPatchSF.h">
<Filter>Patches\SF</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Version\resource_version.rc">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace CreationKitPlatformExtended
auto pathData = BSString::Utils::GetDataPath();

WIN32_FIND_DATA FileFindData;
ZeroMemory(&FileFindData, sizeof(WIN32_FIND_DATA));
HANDLE hFindFile = FindFirstFileExA(*(pathData + "*.ba2"), FindExInfoStandard, &FileFindData,
FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH);
if (hFindFile != INVALID_HANDLE_VALUE)
Expand Down Expand Up @@ -109,6 +110,7 @@ namespace CreationKitPlatformExtended

uint64_t fileSize = 0;
WIN32_FILE_ATTRIBUTE_DATA fileData;
ZeroMemory(&fileData, sizeof(WIN32_FILE_ATTRIBUTE_DATA));
if (GetFileAttributesExA(*filePath, GetFileExInfoStandard, &fileData))
fileSize = (uint64_t)fileData.nFileSizeLow | ((uint64_t)fileData.nFileSizeHigh << 32);

Expand Down Expand Up @@ -141,6 +143,7 @@ namespace CreationKitPlatformExtended

uint64_t fileSize = 0;
WIN32_FILE_ATTRIBUTE_DATA fileData;
ZeroMemory(&fileData, sizeof(WIN32_FILE_ATTRIBUTE_DATA));
if (GetFileAttributesExA(*filePath, GetFileExInfoStandard, &fileData))
fileSize = (uint64_t)fileData.nFileSizeLow | ((uint64_t)fileData.nFileSizeHigh << 32);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright © 2023-2024 aka perchik71. All rights reserved.
// Contacts: <email:timencevaleksej@gmail.com>
// License: https://www.gnu.org/licenses/gpl-3.0.html

#include "BSResourceArchive2SF.h"
#include "NiAPI\NiMemoryManager.h"

namespace CreationKitPlatformExtended
{
namespace EditorAPI
{
namespace Starfield
{
using namespace CreationKitPlatformExtended::Core;

BGSFileSelectorDialog::RegisterArchiveFileCallback* lpArchiveFileCallback = nullptr;

namespace BSResource
{
LocationTree* lpArchiveTree = nullptr;
uintptr_t pointer_Archive2_sub1 = 0;
uintptr_t pointer_Archive2_sub2 = 0;
Array<BSString*> g_arrayArchivesAvailable;
bool g_initCKPEPrimary = false;
std::mutex g_CKPEPrimary;

void Archive2::Initialize()
{
auto pathData = BSString::Utils::GetDataPath();

WIN32_FIND_DATA FileFindData;
ZeroMemory(&FileFindData, sizeof(WIN32_FIND_DATA));
HANDLE hFindFile = FindFirstFileExA(*(pathData + "*.ba2"), FindExInfoStandard, &FileFindData,
FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH);
if (hFindFile != INVALID_HANDLE_VALUE)
{
do
{
g_arrayArchivesAvailable.push_back(new BSString(FileFindData.cFileName));
} while (FindNextFile(hFindFile, &FileFindData));
}

auto func = [](const String& svalue) {
BSString strName;

if (svalue.length() > 0) {
LPSTR s_c = new CHAR[svalue.length() + 1];
strcpy(s_c, svalue.c_str());

LPSTR stoken = strtok(s_c, ",");
if (stoken) {
do {
auto index = g_arrayArchivesAvailable.begin();
auto fname = Utils::Trim(stoken);

for (; index != g_arrayArchivesAvailable.end(); index++)
{
if (!_stricmp(fname.c_str(), (*index)->c_str()))
break;
}

if (index != g_arrayArchivesAvailable.end()) {
delete* index;
g_arrayArchivesAvailable.erase(index);
}

stoken = strtok(NULL, ",");
} while (stoken);
}

delete[] s_c;
}
};

static const char* SC_NONE = "<NONE>";

INIConfig _conf("CreationKit.ini");
INIConfig _User_conf("CreationKitCustom.ini");

auto s = _User_conf.ReadString("Archive", "sResourceArchiveList", SC_NONE);
func((s == SC_NONE) ? _conf.ReadString("Archive", "sResourceArchiveList", "") : s);
s = _User_conf.ReadString("Archive", "sResourceArchiveList2", SC_NONE);
func((s == SC_NONE) ? _conf.ReadString("Archive", "sResourceArchiveList2", "") : s);
s = _User_conf.ReadString("Archive", "sResourceArchiveMemoryCacheList", SC_NONE);
func((s == SC_NONE) ? _conf.ReadString("Archive", "sResourceArchiveMemoryCacheList", "") : s);
s = _User_conf.ReadString("Archive", "sResourceStartUpArchiveList", SC_NONE);
func((s == SC_NONE) ? _conf.ReadString("Archive", "sResourceStartUpArchiveList", "") : s);
s = _User_conf.ReadString("Archive", "sResourceIndexFileList", SC_NONE);
func((s == SC_NONE) ? _conf.ReadString("Archive", "sResourceIndexFileList", "") : s);
}

void Archive2::GetFileSizeStr(uint64_t fileSize, BSString& fileSizeStr)
{
if (fileSize >= 0x40000000)
fileSizeStr.Format("%.3f GByte", ((long double)fileSize) / 0x40000000);
else if (fileSize >= 0x100000)
fileSizeStr.Format("%3.3f MByte", ((long double)fileSize) / 0x100000);
else if (fileSize >= 0x400)
fileSizeStr.Format("%3.3f KByte", ((long double)fileSize) / 0x400);
else
fileSizeStr.Format("%d Byte", fileSize);
}

Archive2::EResultError Archive2::HKLoadArchive(void* arrayDataList, LooseFileStream*& resFile,
void* Unk1, uint32_t Unk2)
{
auto fileName = resFile->FileName->Get<CHAR>(true);
AssertMsg(fileName, "There is no name of the load archive");

BSString filePath, fileSizeStr;
filePath.Format("%s%s%s", resFile->AppPath->Get<CHAR>(true), resFile->DataPath->Get<CHAR>(true), fileName);
AssertMsgVa(BSString::Utils::FileExists(filePath), "Can't found file %s", *filePath);

uint64_t fileSize = 0;
WIN32_FILE_ATTRIBUTE_DATA fileData;
ZeroMemory(&fileData, sizeof(WIN32_FILE_ATTRIBUTE_DATA));
if (GetFileAttributesExA(*filePath, GetFileExInfoStandard, &fileData))
fileSize = (uint64_t)fileData.nFileSizeLow | ((uint64_t)fileData.nFileSizeHigh << 32);

auto resultNo = EC_NONE;

if (_stricmp(fileName, "Fallout4 - Shaders.ba2")) // skip load Fallout4 - Shaders.ba2
{
GetFileSizeStr(fileSize, fileSizeStr);
_CONSOLE("Load an archive file \"%s\" (%s)...", fileName, *fileSizeStr);

resultNo = fastCall<EResultError, void*, LooseFileStream*&, void*, uint32_t>
(pointer_Archive2_sub1, arrayDataList, resFile, Unk1, Unk2);
AssertMsgVa(resultNo == EC_NONE, "Failed load an archive file %s", fileName);
}

return resultNo;
}

void Archive2::LoadArchive(const char* fileName)
{
if (BSString::Utils::FileExists(BSString::Utils::GetDataPath() + fileName))
fastCall<void>(pointer_Archive2_sub2, fileName, &lpArchiveTree, &lpArchiveFileCallback);
}

bool Archive2::IsAvailableForLoad(const char* fileName)
{
for (auto it = g_arrayArchivesAvailable.begin();
it != g_arrayArchivesAvailable.end(); it++)
{
if (!(*it)->Compare(fileName))
return true;
}

return false;
}
}
}
}
}
Loading

0 comments on commit 96239f6

Please sign in to comment.