Skip to content

Commit

Permalink
wtf
Browse files Browse the repository at this point in the history
  • Loading branch information
Soreepeong committed May 11, 2022
1 parent 34db2ef commit 81499b5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
64 changes: 61 additions & 3 deletions XivAlexander/Apps/MainApp/Internal/GameResourceOverrider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct XivAlexander::Apps::MainApp::Internal::GameResourceOverrider::Implementat
std::optional<Internal::VirtualSqPacks> Sqpacks;

std::vector<std::unique_ptr<Misc::Hooks::PointerFunction<size_t, uint32_t, const char*, size_t>>> FoundPathHashFunctions{};
std::vector<std::unique_ptr<Misc::Hooks::PointerFunction<const char8_t*, const char8_t*>>> FoundStringIndirectionResolverFunctions{};
std::vector<std::string> LastLoggedPaths;
std::mutex LastLoggedPathMtx;
Utils::CallOnDestruction::Multiple Cleanup;
Expand Down Expand Up @@ -233,9 +234,7 @@ struct XivAlexander::Apps::MainApp::Internal::GameResourceOverrider::Implementat
});


for (auto ptr : Misc::Signatures::LookupForData([](const IMAGE_SECTION_HEADER& p) {
return strncmp(reinterpret_cast<const char*>(p.Name), ".text", 5) == 0;
},
for (auto ptr : Misc::Signatures::LookupForData(Misc::Signatures::SectionFilterTextOnly,
"\x40\x57\x48\x8d\x3d\x00\x00\x00\x00\x00\x8b\xd8\x4c\x8b\xd2\xf7\xd1\x00\x85\xc0\x74\x25\x41\xf6\xc2\x03\x74\x1f\x41\x0f\xb6\x12\x8b\xc1",
"\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
34,
Expand Down Expand Up @@ -339,6 +338,65 @@ struct XivAlexander::Apps::MainApp::Internal::GameResourceOverrider::Implementat
return res;
});
}

for (auto ptr : Misc::Signatures::LookupForData(
Misc::Signatures::SectionFilterTextOnly,
"\x8b\x01\x25\xff\xff\xff\x00\x48\x03\xc1",
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
10, {})) {
FoundStringIndirectionResolverFunctions.emplace_back(std::make_unique<Misc::Hooks::PointerFunction<const char8_t*, const char8_t*>>(
"FFXIV::StringIndirectionResolverFunctions",
reinterpret_cast<const char8_t* (__stdcall*)(const char8_t*)>(ptr)
));
Cleanup += FoundStringIndirectionResolverFunctions.back()->SetHook([this, ptr, self = FoundStringIndirectionResolverFunctions.back().get()](const char8_t* s) {
auto error = true;

__try {
s = self->bridge(s);
error = false;
goto done;
} __except(EXCEPTION_EXECUTE_HANDLER) {
// pass
}

Logger->Format<LogLevel::Warning>(LogCategory::GameResourceOverrider, "Invalid ptr to string indirection resolving function(0x{:x}); trying again with -0xE", reinterpret_cast<size_t>(ptr));
__try {
s = self->bridge(s - 0x0e);
goto done;
} __except (EXCEPTION_EXECUTE_HANDLER) {
// pass
}

Logger->Format<LogLevel::Warning>(LogCategory::GameResourceOverrider, "Attempt failed, just returning +2");
s = s + 2;
static const auto SeStringTester = [](const char8_t* ptr) {
try {
void(Sqex::SeString(reinterpret_cast<const char*>(ptr)).Parsed());
return true;
} catch (...) {
return false;
}
};

done:
if (error) {
auto pass = false;
__try {
pass = SeStringTester(s);
} __except (EXCEPTION_EXECUTE_HANDLER) {
// pass
}

if (pass)
Logger->Format<LogLevel::Warning>(LogCategory::GameResourceOverrider, "Rolling with {}", (char*)s);
else {
s = u8"<error>";
Logger->Format<LogLevel::Warning>(LogCategory::GameResourceOverrider, "Could not resolve text; displaying <error>");
}
}
return s;
});
};
}

~Implementation() {
Expand Down
13 changes: 10 additions & 3 deletions XivAlexanderCommon/Sqex/Excel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ namespace Sqex::Excel {
BE<uint32_t> RowCountWithSkip;
};

enum Depth : uint8_t {
enum Depth : uint16_t {
Unknown = 0,
Level2 = 1,
Level3 = 2,
};

struct ExhFlag {
uint16_t SomeCount : 14;
uint16_t SomeMode : 2;

// SomeCount is treated as 0 if SomeMode is not 1
// only 0 and 1 are valid values for SomeMode
};

struct Header {
static const char Signature_Value[4];
static constexpr uint16_t Version_Value = 3;
Expand All @@ -57,8 +65,7 @@ namespace Sqex::Excel {
BE<uint16_t> ColumnCount;
BE<uint16_t> PageCount;
BE<uint16_t> LanguageCount;
BE<uint16_t> SomeSortOfBufferSize;
BE<uint8_t> Padding_0x010;
BE<ExhFlag> Flags;
BE<Depth> Depth;
BE<uint16_t> Padding_0x012;
BE<uint32_t> RowCountWithoutSkip;
Expand Down
12 changes: 5 additions & 7 deletions XivAlexanderCommon/Sqex/Excel/Generator.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "pch.h"
#include "XivAlexanderCommon/Sqex/Excel/Generator.h"

Sqex::Excel::Depth2ExhExdCreator::Depth2ExhExdCreator(std::string name, std::vector<Exh::Column> columns, int someSortOfBufferSize, size_t divideUnit)
Sqex::Excel::Depth2ExhExdCreator::Depth2ExhExdCreator(std::string name, std::vector<Exh::Column> columns, const Exh::ExhFlag& flag)
: Name(std::move(name))
, Columns(std::move(columns))
, SomeSortOfBufferSize(someSortOfBufferSize)
, DivideUnit(divideUnit)
, Flags(flag)
, FixedDataSize([&columns = Columns]() {
uint16_t size = 0;
for (const auto& col : columns) {
Expand Down Expand Up @@ -130,14 +129,13 @@ std::pair<Sqex::Sqpack::EntryPathSpec, std::vector<char>> Sqex::Excel::Depth2Exh
);
}

std::map<Sqex::Sqpack::EntryPathSpec, std::vector<char>, Sqex::Sqpack::EntryPathSpec::FullPathComparator> Sqex::Excel::Depth2ExhExdCreator::Compile() {
std::map<Sqex::Sqpack::EntryPathSpec, std::vector<char>, Sqex::Sqpack::EntryPathSpec::FullPathComparator> Sqex::Excel::Depth2ExhExdCreator::Compile(size_t divideUnit) {
std::map<Sqpack::EntryPathSpec, std::vector<char>, Sqpack::EntryPathSpec::FullPathComparator> result;

std::vector<std::pair<Exh::Pagination, std::vector<uint32_t>>> pages;
for (const auto id : Data | std::views::keys) {
if (pages.empty()) {
pages.emplace_back();
} else if (pages.back().second.size() == DivideUnit || DivideAtIds.find(id) != DivideAtIds.end()) {
} else if (pages.back().second.size() == divideUnit || DivideAtIds.find(id) != DivideAtIds.end()) {
pages.back().first.RowCountWithSkip = pages.back().second.back() - pages.back().second.front() + 1;
pages.emplace_back();
}
Expand Down Expand Up @@ -262,7 +260,7 @@ std::map<Sqex::Sqpack::EntryPathSpec, std::vector<char>, Sqex::Sqpack::EntryPath
exhHeader.ColumnCount = static_cast<uint16_t>(Columns.size());
exhHeader.PageCount = static_cast<uint16_t>(pages.size());
exhHeader.LanguageCount = static_cast<uint16_t>(Languages.size());
exhHeader.SomeSortOfBufferSize = SomeSortOfBufferSize;
exhHeader.Flags = Flags;
exhHeader.Depth = Exh::Level2;
exhHeader.RowCountWithoutSkip = static_cast<uint32_t>(Data.size());

Expand Down
7 changes: 3 additions & 4 deletions XivAlexanderCommon/Sqex/Excel/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ namespace Sqex::Excel {
public:
const std::string Name;
const std::vector<Exh::Column> Columns;
const int SomeSortOfBufferSize;
const size_t DivideUnit;
const Exh::ExhFlag Flags;
const uint32_t FixedDataSize;
std::map<uint32_t, std::map<Language, std::vector<ExdColumn>>> Data;
std::set<uint32_t> DivideAtIds;
std::vector<Language> Languages;
std::vector<Language> FillMissingLanguageFrom;

Depth2ExhExdCreator(std::string name, std::vector<Exh::Column> columns, int someSortOfBufferSize, size_t divideUnit = SIZE_MAX);
Depth2ExhExdCreator(std::string name, std::vector<Exh::Column> columns, const Exh::ExhFlag& flag);

void AddLanguage(Language language);

Expand All @@ -26,6 +25,6 @@ namespace Sqex::Excel {
std::pair<Sqpack::EntryPathSpec, std::vector<char>> Flush(uint32_t startId, std::map<uint32_t, std::vector<char>> rows, Language language);

public:
std::map<Sqpack::EntryPathSpec, std::vector<char>, Sqpack::EntryPathSpec::FullPathComparator> Compile();
std::map<Sqpack::EntryPathSpec, std::vector<char>, Sqpack::EntryPathSpec::FullPathComparator> Compile(size_t divideUnit = SIZE_MAX);
};
}
6 changes: 3 additions & 3 deletions XivAlexanderCommon/Sqex/Excel/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ Sqex::Sqpack::EntryPathSpec Sqex::Excel::ExhReader::GetDataPathSpec(const Exh::P
void Sqex::Excel::ExhReader::Dump() const {
std::cout << std::format("Signature: {}\n", std::string(Header.Signature, 4));
std::cout << std::format("Version: {}\n", Header.Version.Value());
std::cout << std::format("FixedDataSize: {}\n", Header.FixedDataSize.Value());
std::cout << std::format("FixedDataSize: {:x}\n", Header.FixedDataSize.Value());
std::cout << std::format("ColumnCount: {}\n", Header.ColumnCount.Value());
std::cout << std::format("PageCount: {}\n", Header.PageCount.Value());
std::cout << std::format("LanguageCount: {}\n", Header.LanguageCount.Value());
std::cout << std::format("SomeSortOfBufferSize: {}\n", Header.SomeSortOfBufferSize.Value());
std::cout << std::format("Padding_0x010: {}\n", Header.Padding_0x010.Value());
const auto flag = Header.Flags.Value();
std::cout << std::format("Flags: {:02x}\n", *reinterpret_cast<const uint16_t*>(&flag));
std::cout << std::format("Depth: {}\n", static_cast<uint8_t>(Header.Depth.Value()));
std::cout << std::format("Padding_0x012: {}\n", Header.Padding_0x012.Value());
std::cout << std::format("RowCountWithoutSkip: {}\n", Header.RowCountWithoutSkip.Value());
Expand Down

0 comments on commit 81499b5

Please sign in to comment.