Skip to content

Commit

Permalink
refactor: remove hardcoded ll manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 27, 2025
1 parent 5fd4a3f commit 387e8a4
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 49 deletions.
58 changes: 48 additions & 10 deletions src/ll/core/CrashLogger_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "DbgHelp.h"

#include "Psapi.h"

namespace ll::inline utils::error_utils {
Stacktrace stacktraceFromContext(optional_ref<_CONTEXT const> context, size_t skip = 0, size_t maxDepth = ~0ull);
}
Expand Down Expand Up @@ -109,16 +111,7 @@ void CrashLogger::submitCrashInfo() {
getLoaderVersion().to_string().find('+') != std::string::npos,
getLoaderVersion().to_string()
};
bool isLLSus = std::find(susModules.begin(), susModules.end(), getSelfModIns()->getManifest().entry)
!= susModules.end();
// Adding LeviLamina's sentry info
sentryUploader.addModSentryInfo(
getSelfModIns()->getName(),
"https://43a888504c33385bfd2e570c9ac939aa@o4508652421906432.ingest.us.sentry.io/4508652563398656",
getLoaderVersion().to_string(),
isLLSus
);
for (auto& mod : ll::mod::ModManagerRegistry::getInstance().mods()) { // Adding mods' sentry info
auto infoAdder = [&](mod::Mod& mod) {
bool isModSus =
std::find(susModules.begin(), susModules.end(), mod.getManifest().entry) != susModules.end();
if (mod.getManifest().sentry_dsn
Expand All @@ -130,6 +123,10 @@ void CrashLogger::submitCrashInfo() {
isModSus
);
}
};
infoAdder(*getSelfModIns());
for (auto& mod : mod::ModManagerRegistry::getInstance().mods()) { // Adding mods' sentry info
infoAdder(mod);
}
sentryUploader.uploadAll();
std::filesystem::remove(entry.path());
Expand Down Expand Up @@ -206,6 +203,24 @@ static struct CrashInfo {
CrashInfo() : loggerPtr(io::LoggerRegistry::getInstance().getOrCreate("CrashLogger")), logger(*loggerPtr) {}
} crashInfo;

[[maybe_unused]] static std::string memStr(size_t mem) {
double r = (double)mem;
r /= 1024;
if (r < 1024) {
return ::fmt::format("{:>8.1f} KiB", r);
}
r /= 1024;
if (r < 1024) {
return ::fmt::format("{:>8.1f} MiB", r);
}
r /= 1024;
if (r < 1024) {
return ::fmt::format("{:>8.1f} GiB", r);
}
r /= 1024;
return ::fmt::format("{:>8.1f} TiB", r);
}

static void dumpSystemInfo() {
crashInfo.logger.info("System Info:");
crashInfo.logger.info(
Expand Down Expand Up @@ -266,6 +281,25 @@ static void dumpSystemInfo() {
}()));
crashInfo.logger.info(" |LocalTime: {}", fmt::format("{0:%F %T} (UTC{0:%z})", fmt::localtime(_time64(nullptr))));
}

static void dumpMemoryInfo() {
crashInfo.logger.info("Memory Info:");
PROCESS_MEMORY_COUNTERS_EX info{.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX)};
GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&info, info.cb);
crashInfo.logger.info(" |heap stats: {:>12} {:>12}", "peak", "current");
crashInfo.logger.info(" |workingset: {:>12} {:>12}", memStr(info.PeakWorkingSetSize), memStr(info.WorkingSetSize));
crashInfo.logger
.info(" | pagedpool: {:>12} {:>12}", memStr(info.QuotaPeakPagedPoolUsage), memStr(info.QuotaPagedPoolUsage));
crashInfo.logger.info(
" | nonpaged: {:>12} {:>12}",
memStr(info.QuotaPeakNonPagedPoolUsage),
memStr(info.QuotaNonPagedPoolUsage)
);
crashInfo.logger.info(" | pagefile: {:>12} {:>12}", memStr(info.PeakPagefileUsage), memStr(info.PagefileUsage));
crashInfo.logger.info(" | private: {:>12} {:>12}", "", memStr(info.PrivateUsage));
crashInfo.logger.info(" | pagefault: {:>12} {:>8}", "", info.PageFaultCount);
}

static void dumpStacktrace(_CONTEXT const& c) {
try {
crashInfo.logger.info("Stacktrace:");
Expand Down Expand Up @@ -389,6 +423,10 @@ static LONG unhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS* e) {
dumpSystemInfo();
crashInfo.logger.info("");

crashInfo.logger.info("");
dumpMemoryInfo();
crashInfo.logger.info("");

crashInfo.logger.info("Exception:");
try {
auto str = error_utils::makeExceptionString(error_utils::createExceptionPtr(e->ExceptionRecord));
Expand Down
39 changes: 13 additions & 26 deletions src/ll/core/LeviLamina.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
#include "ll/api/Versions.h"
#include "ll/api/i18n/I18n.h"
#include "ll/api/io/FileUtils.h"
#include "ll/api/reflection/Deserialization.h"
#include "ll/core/Version.h"

#include "mc/platform/UUID.h"

#include "nlohmann/json.hpp"

namespace ll {
std::shared_ptr<mod::NativeMod> const& getSelfModIns() {
static auto llSelf = std::make_shared<mod::NativeMod>(
mod::Manifest{
"LeviLamina.dll",
std::string{selfModName},
std::string{mod::NativeModManagerName},
{},
getLoaderVersion()
},
::ll::reflection::deserialize_to<mod::Manifest>(
nlohmann::json::parse(
file_utils::readFile(mod::getModsRoot() / selfModName / u8"manifest.json").value(),
nullptr,
true,
true
)
)
.value(),
sys_utils::getCurrentModuleHandle()
);
return llSelf;
Expand All @@ -34,25 +39,7 @@ std::string_view getServiceUuid() {
return serverUuid;
}

data::Version getLoaderVersion() {
static auto ver = [] {
auto v = data::Version{
LL_VERSION_MAJOR,
LL_VERSION_MINOR,
LL_VERSION_PATCH,
};
#ifdef LL_VERSION_PRERELEASE
v.preRelease = ll::data::PreRelease{LL_VERSION_PRERELEASE};
#endif

#ifndef LL_VERSION_PUBLISH
v.build = LL_VERSION_TO_STRING(LL_VERSION_COMMIT_SHA);
#endif

return v;
}();
return ver;
}
data::Version getLoaderVersion() { return getSelfModIns()->getManifest().version.value(); }


void printWelcomeMsg() {
Expand Down
9 changes: 2 additions & 7 deletions src/ll/core/SentryUploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void SentryUploader::addModSentryInfo(
std::string const& modName,
std::string const& dsn,
std::string const& releaseVersion,
bool isInSuspectedModules
bool isInSuspectedModules
) try {
auto protocolEnd = dsn.find("://");
auto authEnd = dsn.find('@', protocolEnd + 3);
Expand All @@ -93,8 +93,7 @@ void SentryUploader::addModSentryInfo(
}

void SentryUploader::uploadAll() {
std::vector<std::thread> threads;

std::vector<std::jthread> threads;
threads.reserve(mModsSentryConfig.size());
sentryLogger->info("Uploading crash report to Sentry..."_tr());
for (auto const& sentryConfig : mModsSentryConfig) {
Expand Down Expand Up @@ -129,10 +128,6 @@ void SentryUploader::uploadAll() {
}
});
}

for (auto& thread : threads) {
thread.join();
}
}

std::string SentryUploader::readFile(std::string const& filePath) {
Expand Down
1 change: 0 additions & 1 deletion src/ll/core/mod/ModRegistrar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "ll/core/mod/NativeModManager.h"

#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"

#include "pl/Config.h"

Expand Down
53 changes: 50 additions & 3 deletions src/ll/core/tweak/MemoryOperators_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ namespace ll::memory {
}
class MimallocMemoryAllocator : public ::Bedrock::Memory::IMemoryAllocator {
public:
virtual void* allocate(uint64 size) try { return mi_malloc(size != 0 ? size : 1); } catch (...) {
virtual void* allocate(uint64 size) try { return mi_malloc(size != 0ull ? size : 1ull); } catch (...) {
return nullptr;
}

virtual void release(void* ptr) try { mi_free(ptr); } catch (...) {
}

virtual void* alignedAllocate(uint64 size, uint64 alignment) try {
return mi_malloc_aligned(size != 0 ? size : 1, alignment);
return mi_malloc_aligned(size != 0ull ? size : 1ull, alignment);
} catch (...) {
return nullptr;
}
Expand All @@ -56,7 +56,7 @@ class MimallocMemoryAllocator : public ::Bedrock::Memory::IMemoryAllocator {
}

virtual uint64 getUsableSize(void* ptr) try { return mi_usable_size(ptr); } catch (...) {
return 0;
return 0ull;
}

static void miOutput(char const* msg, void* /*arg*/) {
Expand All @@ -80,6 +80,53 @@ class MimallocMemoryAllocator : public ::Bedrock::Memory::IMemoryAllocator {
}
};

class StdMemoryAllocator : public ::Bedrock::Memory::IMemoryAllocator {
public:
virtual void* allocate(uint64 size) try { return malloc(size != 0ull ? size : 1ull); } catch (...) {
return nullptr;
}

virtual void release(void* ptr) try { free(ptr); } catch (...) {
}

virtual void* alignedAllocate(uint64 size, uint64 alignment) try {
return _aligned_malloc(size != 0ull ? size : 1ull, alignment);
} catch (...) {
return nullptr;
}

virtual void alignedRelease(void* ptr) try { _aligned_free(ptr); } catch (...) {
}

virtual uint64 getUsableSize(void* ptr) try { return ptr ? _msize(ptr) : 0ull; } catch (...) {
return 0ull;
}

virtual void logCurrentState() try {
HEAP_SUMMARY summary{.cb = sizeof(HEAP_SUMMARY)};
HeapSummary(GetProcessHeap(), 0, &summary);
PROCESS_MEMORY_COUNTERS_EX info{.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX)};
GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&info, info.cb);
// clang-format off
auto& logger= getLogger();
logger.info("heap stats: {:>12} {:>12}", "peak", "current");
logger.info(" reserved: {:>12} {:>12}", memStr(summary.cbMaxReserve), memStr(summary.cbReserved));
logger.info(" committed: {:>12} {:>12}", "", memStr(summary.cbCommitted));
logger.info(" allocated: {:>12} {:>12}", "", memStr(summary.cbAllocated));
logger.info(" pagefault: {:>12} {:>8}", "", info.PageFaultCount);
logger.info("workingset: {:>12} {:>12}", memStr(info.PeakWorkingSetSize), memStr(info.WorkingSetSize));
logger.info(" pagedpool: {:>12} {:>12}", memStr(info.QuotaPeakPagedPoolUsage), memStr(info.QuotaPagedPoolUsage));
logger.info(" nonpaged: {:>12} {:>12}", memStr(info.QuotaPeakNonPagedPoolUsage), memStr(info.QuotaNonPagedPoolUsage));
logger.info(" pagefile: {:>12} {:>12}", memStr(info.PeakPagefileUsage), memStr(info.PagefileUsage));
logger.info(" private: {:>12} {:>12}", "", memStr(info.PrivateUsage));
// clang-format on
} catch (...) {}

virtual void* _realloc(gsl::not_null<void*> ptr, uint64 newSize) try { return realloc(ptr, newSize); } catch (...) {
return nullptr;
}
};

#ifdef LL_MEMORY_DEBUG
struct MemSize {
size_t a{0};
Expand Down
14 changes: 12 additions & 2 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ add_requires("concurrentqueue v1.0.4")
add_requires("pcg_cpp v1.0.0")
add_requires("pfr 2.1.1")
add_requires("demangler v17.0.7")
add_requires("levibuildscript 0.2.0")
add_requires("levibuildscript 0.3.0")
add_requires("preloader v1.12.0")
add_requires("symbolprovider v1.2.0")
add_requires("trampoline 2024.11.7")
Expand Down Expand Up @@ -102,7 +102,6 @@ end

target("LeviLamina")
add_rules("@levibuildscript/linkrule")
add_rules("@levibuildscript/modpacker")
set_languages("c++20")
set_kind("shared")
set_symbols("debug")
Expand Down Expand Up @@ -342,16 +341,27 @@ target("LeviLamina")
print("Failed to parse version tag, using 0.0.0")
major, minor, patch = 0, 0, 0
end
local versionStr = major.."."..minor.."."..minor
if suffix then
prerelease = suffix:match("-(.*)")
if prerelease then
prerelease = prerelease:gsub("\n", "")
end
if prerelease then
target:set("configvar", "LL_VERSION_PRERELEASE", prerelease)
versionStr = versionStr.."-"..prerelease
end
end
target:set("configvar", "LL_VERSION_MAJOR", major)
target:set("configvar", "LL_VERSION_MINOR", minor)
target:set("configvar", "LL_VERSION_PATCH", patch)

if not has_config("publish") then
local hash = os.iorun("git rev-parse --short HEAD")
versionStr = versionStr.."+"..hash:gsub("\n", "")
end

target:add("rules", "@levibuildscript/modpacker",{
modVersion = versionStr
})
end)

0 comments on commit 387e8a4

Please sign in to comment.