Skip to content

Commit

Permalink
refactor!: make new mode as default
Browse files Browse the repository at this point in the history
  • Loading branch information
RimuruChan committed Dec 6, 2023
1 parent 372528b commit 973901a
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 73 deletions.
12 changes: 11 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,29 @@ AlignConsecutiveMacros:
AcrossEmptyLines: false
AcrossComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllArgumentsOnNextLine: false
AlignOperands: AlignAfterOperator
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AllowShortLambdasOnASingleLine: All
AllowShortBlocksOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakTemplateDeclarations: 'Yes'
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakBeforeBinaryOperators: NonAssignment
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerIndentWidth: 0
IndentWidth: 4
Language: Cpp
MaxEmptyLinesToKeep: 2
PackConstructorInitializers: CurrentLine
PointerAlignment: Left
TabWidth: 4
UseTab: Never
Expand Down
3 changes: 2 additions & 1 deletion src/pe_editor/CxxOptAdder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class CxxOptsAdder {

public:
inline CxxOptsAdder(cxxopts::Options& options, const std::string& groupName = "")
: options(options), adder(options.add_options(groupName)) {}
: options(options),
adder(options.add_options(groupName)) {}

inline CxxOptsAdder&
add(const std::string& opts,
Expand Down
54 changes: 18 additions & 36 deletions src/pe_editor/FakeSymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,22 @@ namespace pe_editor::FakeSymbol {
inline demangler::ms_demangle::SpecialIntrinsicKind consumeSpecialIntrinsicKind(StringView& MangledName) {
using namespace demangler::ms_demangle;
using namespace demangler;
if (MangledName.consumeFront("?_7"))
return SpecialIntrinsicKind::Vftable;
if (MangledName.consumeFront("?_8"))
return SpecialIntrinsicKind::Vbtable;
if (MangledName.consumeFront("?_9"))
return SpecialIntrinsicKind::VcallThunk;
if (MangledName.consumeFront("?_A"))
return SpecialIntrinsicKind::Typeof;
if (MangledName.consumeFront("?_B"))
return SpecialIntrinsicKind::LocalStaticGuard;
if (MangledName.consumeFront("?_C"))
return SpecialIntrinsicKind::StringLiteralSymbol;
if (MangledName.consumeFront("?_P"))
return SpecialIntrinsicKind::UdtReturning;
if (MangledName.consumeFront("?_R0"))
return SpecialIntrinsicKind::RttiTypeDescriptor;
if (MangledName.consumeFront("?_R1"))
return SpecialIntrinsicKind::RttiBaseClassDescriptor;
if (MangledName.consumeFront("?_R2"))
return SpecialIntrinsicKind::RttiBaseClassArray;
if (MangledName.consumeFront("?_R3"))
return SpecialIntrinsicKind::RttiClassHierarchyDescriptor;
if (MangledName.consumeFront("?_R4"))
return SpecialIntrinsicKind::RttiCompleteObjLocator;
if (MangledName.consumeFront("?_S"))
return SpecialIntrinsicKind::LocalVftable;
if (MangledName.consumeFront("?__E"))
return SpecialIntrinsicKind::DynamicInitializer;
if (MangledName.consumeFront("?__F"))
return SpecialIntrinsicKind::DynamicAtexitDestructor;
if (MangledName.consumeFront("?__J"))
return SpecialIntrinsicKind::LocalStaticThreadGuard;
if (MangledName.consumeFront("?_7")) return SpecialIntrinsicKind::Vftable;
if (MangledName.consumeFront("?_8")) return SpecialIntrinsicKind::Vbtable;
if (MangledName.consumeFront("?_9")) return SpecialIntrinsicKind::VcallThunk;
if (MangledName.consumeFront("?_A")) return SpecialIntrinsicKind::Typeof;
if (MangledName.consumeFront("?_B")) return SpecialIntrinsicKind::LocalStaticGuard;
if (MangledName.consumeFront("?_C")) return SpecialIntrinsicKind::StringLiteralSymbol;
if (MangledName.consumeFront("?_P")) return SpecialIntrinsicKind::UdtReturning;
if (MangledName.consumeFront("?_R0")) return SpecialIntrinsicKind::RttiTypeDescriptor;
if (MangledName.consumeFront("?_R1")) return SpecialIntrinsicKind::RttiBaseClassDescriptor;
if (MangledName.consumeFront("?_R2")) return SpecialIntrinsicKind::RttiBaseClassArray;
if (MangledName.consumeFront("?_R3")) return SpecialIntrinsicKind::RttiClassHierarchyDescriptor;
if (MangledName.consumeFront("?_R4")) return SpecialIntrinsicKind::RttiCompleteObjLocator;
if (MangledName.consumeFront("?_S")) return SpecialIntrinsicKind::LocalVftable;
if (MangledName.consumeFront("?__E")) return SpecialIntrinsicKind::DynamicInitializer;
if (MangledName.consumeFront("?__F")) return SpecialIntrinsicKind::DynamicAtexitDestructor;
if (MangledName.consumeFront("?__J")) return SpecialIntrinsicKind::LocalStaticThreadGuard;
return SpecialIntrinsicKind::None;
}

Expand All @@ -49,13 +33,11 @@ std::optional<std::string> getFakeSymbol(const std::string& fn, bool removeVirtu
Demangler demangler;
StringView name(fn.c_str());

if (!name.consumeFront('?'))
return std::nullopt;
if (!name.consumeFront('?')) return std::nullopt;

SpecialIntrinsicKind specialIntrinsicKind = consumeSpecialIntrinsicKind(name);

if (specialIntrinsicKind != SpecialIntrinsicKind::None)
return std::nullopt;
if (specialIntrinsicKind != SpecialIntrinsicKind::None) return std::nullopt;

SymbolNode* symbolNode = demangler.demangleDeclarator(name);
if (symbolNode == nullptr
Expand Down
4 changes: 2 additions & 2 deletions src/pe_editor/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const std::vector<std::string> prefix = {
};

inline bool matchSkip(std::string_view name) {
return ctre::match<R"(\?+[a-zA-Z0-9_\-]*([a-zA-Z0-9_\-]*@)*std@@.*)">(name) || ctre::match<R"(.*printf$)">(name) ||
ctre::match<R"(.*no_alloc.*)">(name);
return ctre::match<R"(\?+[a-zA-Z0-9_\-]*([a-zA-Z0-9_\-]*@)*std@@.*)">(name) || ctre::match<R"(.*printf$)">(name)
|| ctre::match<R"(.*no_alloc.*)">(name);
}

} // namespace pe_editor::filter
18 changes: 6 additions & 12 deletions src/pe_editor/PdbReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,10 @@ inline bool IsError(PDB::ErrorCode errorCode) {
}

inline bool HasValidDBIStreams(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream) {
if (IsError(dbiStream.HasValidImageSectionStream(rawPdbFile)))
return false;
if (IsError(dbiStream.HasValidPublicSymbolStream(rawPdbFile)))
return false;
if (IsError(dbiStream.HasValidGlobalSymbolStream(rawPdbFile)))
return false;
if (IsError(dbiStream.HasValidSectionContributionStream(rawPdbFile)))
return false;
if (IsError(dbiStream.HasValidImageSectionStream(rawPdbFile))) return false;
if (IsError(dbiStream.HasValidPublicSymbolStream(rawPdbFile))) return false;
if (IsError(dbiStream.HasValidGlobalSymbolStream(rawPdbFile))) return false;
if (IsError(dbiStream.HasValidSectionContributionStream(rawPdbFile))) return false;
return true;
}
} // namespace
Expand All @@ -108,11 +104,9 @@ std::unique_ptr<deque<PdbSymbol>> loadFunctions(const PDB::RawFile& rawPdbFile,
const PDB::CodeView::DBI::Record* record = publicSymbolStream.GetRecord(symbolRecordStream, hashRecord);
const uint32_t rva =
imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_PUB32.section, record->data.S_PUB32.offset);
if (rva == 0u)
continue;
if (rva == 0u) continue;
const std::string symbol(record->data.S_PUB32.name);
if (symbol.empty())
continue;
if (symbol.empty()) continue;
const bool isFunction = (bool)(record->data.S_PUB32.flags & PDB::CodeView::DBI::PublicSymbolFlags::Function);
func->push_back(PdbSymbol(symbol, rva, isFunction));
}
Expand Down
2 changes: 1 addition & 1 deletion src/pe_editor/PdbReader.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <deque>
#include <memory>
#include <string>
#include <utility>
#include <memory>

struct PdbSymbol {
std::string name;
Expand Down
24 changes: 11 additions & 13 deletions src/pe_editor/PeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
namespace pe_editor {

inline void pause() {
if (config::shouldPause)
system("pause");
if (config::shouldPause) system("pause");
}

inline void exitWith(int code) {
Expand All @@ -48,7 +47,7 @@ void parseArgs(int argc, char** argv) {
value<bool>()->default_value("false")
)
.add("p,pause", "Pause before exit (will be true if no arg passed)", value<bool>()->default_value("false"))
.add("n,new", "Use LiteLoader v3 preview mode", value<bool>()->default_value("false"))
.add("O,old", "Use old mode for LiteLoaderBDS", value<bool>()->default_value("false"))
.add(
"b,bak",
"Add a suffix \".bak\" to original server exe (will be true if no arg passed)",
Expand Down Expand Up @@ -97,7 +96,7 @@ void parseArgs(int argc, char** argv) {
config::backupBds = optionsResult["bak"].as<bool>();
config::shouldPause = optionsResult["pause"].as<bool>();
config::choosePdbFile = optionsResult["choose-pdb-file"].as<bool>();
config::liteloader3 = optionsResult["new"].as<bool>();
config::oldMode = optionsResult["old"].as<bool>();
config::outputDir = optionsResult["output-dir"].as<std::string>();
config::bdsExePath = optionsResult["exe"].as<std::string>();
config::bdsPdbPath = optionsResult["pdb"].as<std::string>();
Expand All @@ -108,8 +107,7 @@ bool filterSymbols(const PdbSymbol& symbol) {
return false;
}
for (const auto& a : pe_editor::filter::prefix) {
if (symbol.name.starts_with(a))
return false;
if (symbol.name.starts_with(a)) return false;
}
return !pe_editor::filter::matchSkip(symbol.name);
}
Expand Down Expand Up @@ -239,12 +237,12 @@ int generateSymbolListFile() {

struct ImportDllName {
static constexpr auto liteloader2 = "LLPreLoader.dll";
static constexpr auto liteloader3 = "PreLoader.dll";
static constexpr auto levilamina = "PreLoader.dll";
};

struct ImportFunctionName {
static constexpr auto liteloader2 = "dlsym_real";
static constexpr auto liteloader3 = "pl_resolve_symbol";
static constexpr auto levilamina = "pl_resolve_symbol";
};

int generateModdedBds() {
Expand Down Expand Up @@ -275,8 +273,8 @@ int generateModdedBds() {
std::unordered_set<std::string> exportedFunctionsNames;

auto names = originBds.exportedFunctions
| std::views::filter([](const exported_function& fn) { return fn.has_name(); })
| std::views::transform([](const exported_function& fn) { return fn.get_name(); });
| std::views::filter([](const exported_function& fn) { return fn.has_name(); })
| std::views::transform([](const exported_function& fn) { return fn.get_name(); });
std::ranges::copy(names, std::inserter(exportedFunctionsNames, exportedFunctionsNames.end()));

auto filtered = data::filteredSymbols | std::views::filter([&](const PdbSymbol& symbol) {
Expand Down Expand Up @@ -315,9 +313,9 @@ int generateModdedBds() {
imported_functions_list imports = get_imported_functions(*originBds.pe);
import_library preLoader;
imported_function func;
func.set_name(config::liteloader3 ? ImportFunctionName::liteloader3 : ImportFunctionName::liteloader2);
func.set_name(!config::oldMode ? ImportFunctionName::levilamina : ImportFunctionName::liteloader2);
func.set_iat_va(0x1);
preLoader.set_name(config::liteloader3 ? ImportDllName::liteloader3 : ImportDllName::liteloader2);
preLoader.set_name(!config::oldMode ? ImportDllName::levilamina : ImportDllName::liteloader2);
preLoader.add_import(func);
imports.push_back(preLoader);
section& attachedImportedSection = originBds.pe->add_section(ImportSection);
Expand Down Expand Up @@ -392,7 +390,7 @@ int main(int argc, char** argv) {
}

logger->info("Configurations:");
logger->info("\tLiteLoader v3 Mode: \t\t[{}]", config::liteloader3);
logger->info("\tOld Mode: \t\t\t[{}]", config::oldMode);
logger->info("\tBDS Executable File: \t\t[{}]", getPathUtf8(config::bdsExePath.wstring()));
logger->info("\tBDS PDB File: \t\t\t[{}]", getPathUtf8(config::bdsPdbPath.wstring()));
logger->info("\tOutput Dir: \t\t\t[{}]", getPathUtf8(config::outputDir.wstring()));
Expand Down
2 changes: 1 addition & 1 deletion src/pe_editor/PeEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ inline bool genSymbolList = false;
inline bool backupBds = false;
inline bool shouldPause = false;
inline bool choosePdbFile = false;
inline bool liteloader3 = false;
inline bool oldMode = false;

inline std::filesystem::path outputDir;
inline std::filesystem::path bdsExePath;
Expand Down
2 changes: 1 addition & 1 deletion src/pe_editor/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <Windows.h>

namespace pe_editor::StringUtils {
std::wstring str2wstr(std::string_view str, UINT codePage) {
std::wstring str2wstr(std::string_view str, uint codePage) {
int size = MultiByteToWideChar(codePage, 0, str.data(), static_cast<int>(str.size()), nullptr, 0);
std::wstring wstr(size, 0);
MultiByteToWideChar(codePage, 0, str.data(), static_cast<int>(str.size()), wstr.data(), size);
Expand Down
6 changes: 3 additions & 3 deletions src/pe_editor/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace pe_editor::StringUtils {

using UINT = unsigned int;
#define CP_UTF8 65001
using uint = unsigned int;
constexpr uint UTF8 = 65001;

std::wstring str2wstr(std::string_view str, UINT codePage = CP_UTF8);
std::wstring str2wstr(std::string_view str, uint codePage = UTF8);

std::string wstr2str(std::wstring_view wstr);

Expand Down
2 changes: 1 addition & 1 deletion xmake-requires.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
commit = "fa285a31269c7a407167ff2942d1e1da5b4bbd81",
url = "https://github.com/xmake-io/xmake-repo.git"
},
version = "3.26.4-msvc4"
version = "3.27.2-msvc1"
},
["ctre#31fecfc4"] = {
repo = {
Expand Down
2 changes: 1 addition & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ target("PeEditor")
add_files("src/**.cpp")
add_includedirs("src")
add_cxflags("/utf-8")
add_defines("UNICODE", "PE_EDITOR_VERSION=\"v3.3.0\"")
add_defines("UNICODE", "PE_EDITOR_VERSION=\"v3.4.0\"")
add_syslinks("user32", "comdlg32")
add_packages("llvm-prebuilt", "raw_pdb", "pe_bliss", "cxxopts", "fmt", "spdlog", "ctre", "demangler")

0 comments on commit 973901a

Please sign in to comment.