Skip to content

Commit

Permalink
feat: add members of EntitySystems
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Dec 4, 2023
1 parent 10eff15 commit 4ae5d14
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 130 deletions.
14 changes: 9 additions & 5 deletions src/ll/api/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ static bool checkLogLevel(int level, int outLevel) {

void Logger::OutputStream::print(std::string_view s) const noexcept {
try {
auto lock = Logger::lock();
auto time = fmt::localtime(_time64(nullptr));
auto lock = Logger::lock();
static auto zone = std::chrono::current_zone();
auto now = zone->to_local(std::chrono::system_clock::now());
auto time = std::chrono::floor<std::chrono::seconds>(now);
auto ms = (std::chrono::floor<std::chrono::milliseconds>(now) - time).count();

if (checkLogLevel(logger->consoleLevel, level)) {
std::string str = fmt::format(
fmt::runtime(consoleFormat[0]),
applyTextStyle(style[0], fmt::format(fmt::runtime(consoleFormat[1]), time)),
applyTextStyle(style[0], fmt::format(fmt::runtime(consoleFormat[1]), time, ms)),
applyTextStyle(style[1], fmt::format(fmt::runtime(consoleFormat[2]), levelPrefix)),
applyTextStyle(style[2], fmt::format(fmt::runtime(consoleFormat[3]), logger->title)),
applyTextStyle(style[3], fmt::format(fmt::runtime(consoleFormat[4]), replaceMcToAnsiCode(s)))
Expand All @@ -40,7 +44,7 @@ void Logger::OutputStream::print(std::string_view s) const noexcept {
if (logger->getFile().is_open() && checkLogLevel(logger->fileLevel, level)) {
logger->getFile() << removeEscapeCode(fmt::format(
fmt::runtime(fileFormat[0]),
fmt::format(fmt::runtime(fileFormat[1]), time),
fmt::format(fmt::runtime(fileFormat[1]), time, ms),
fmt::format(fmt::runtime(fileFormat[2]), levelPrefix),
fmt::format(fmt::runtime(fileFormat[3]), logger->title),
fmt::format(fmt::runtime(fileFormat[4]), s)
Expand All @@ -50,7 +54,7 @@ void Logger::OutputStream::print(std::string_view s) const noexcept {
&& checkLogLevel(logger->playerLevel, level)) {
std::string str = replaceAnsiToMcCode(fmt::format(
fmt::runtime(playerFormat[0]),
applyTextStyle(style[0], fmt::format(fmt::runtime(playerFormat[1]), time)),
applyTextStyle(style[0], fmt::format(fmt::runtime(playerFormat[1]), time, ms)),
applyTextStyle(style[1], fmt::format(fmt::runtime(playerFormat[2]), levelPrefix)),
applyTextStyle(style[2], fmt::format(fmt::runtime(playerFormat[3]), logger->title)),
applyTextStyle(style[3], fmt::format(fmt::runtime(playerFormat[4]), s))
Expand Down
8 changes: 4 additions & 4 deletions src/ll/api/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class Logger {
Logger& logger,
std::string levelPrefix,
int level,
std::array<fmt::text_style, 4> const& style = {{}},
std::array<std::string, 5> const& playerFormat = {"<{2}|{1}> [{0}] {3}", "{:%T}", "{}", "{}", "{}"},
std::array<std::string, 5> const& consoleFormat = {"{0} {1} {2} {3}", "{:%T}", "{}", "[{}]", "{}"},
std::array<std::string, 5> const& fileFormat = {"[{0} {1}][{2}] {3}", "{:%F %T}", "{}", "{}", "{}"}
std::array<fmt::text_style, 4> const& style = {{}},
std::array<std::string, 5> const& playerFormat = {"<{2}|{1}> [{0}] {3}", "{:%T}", "{}", "{}", "{}"},
std::array<std::string, 5> const& consoleFormat = {"{0} {1} {2} {3}", "{:%T}.{:0>3}", "{}", "[{}]", "{}"},
std::array<std::string, 5> const& fileFormat = {"[{0} {1}][{2}] {3}", "{:%F %T}.{:0>3}", "{}", "{}", "{}"}
);

template <ll::concepts::IsString S, class... Args>
Expand Down
23 changes: 8 additions & 15 deletions src/ll/api/io/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,8 @@ bool writeFile(const fs::path& filePath, std::string_view content, bool isBinary
return true;
}

static bool getFileVersion(
const wchar_t* filePath,
ushort* ver1,
ushort* ver2,
ushort* ver3,
ushort* ver4,
uint* flag
) {
static bool
getFileVersion(const wchar_t* filePath, ushort* ver1, ushort* ver2, ushort* ver3, ushort* ver4, uint* flag) {

DWORD dwHandle = 0;
DWORD dwLen = GetFileVersionInfoSizeW(filePath, &dwHandle);
Expand All @@ -65,7 +59,7 @@ static bool getFileVersion(
}

VS_FIXEDFILEINFO* lpBuffer;
uint uLen = 0;
uint uLen = 0;
if (!VerQueryValueW(path.c_str(), L"\\", (void**)&lpBuffer, &uLen)) {
return false;
}
Expand All @@ -80,12 +74,11 @@ static bool getFileVersion(
}

Version getVersion(std::filesystem::path const& filePath) {
ll::Version version;
auto ModuleName = filePath.c_str();
ushort build_ver{};
uint flag{};
if (!getFileVersion(ModuleName, &version.major, &version.minor, &version.patch, &build_ver, &flag)) {
version = Version{};
ll::Version version;
ushort build_ver{};
uint flag{};
if (!getFileVersion(filePath.c_str(), &version.major, &version.minor, &version.patch, &build_ver, &flag)) {
return Version{};
} else {
version.preRelease = PreRelease{};
auto& vec = version.preRelease.value().values;
Expand Down
25 changes: 10 additions & 15 deletions src/ll/api/memory/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ll::memory {

FuncPtr resolveSymbol(char const* symbol) { return pl::symbol_provider::pl_resolve_symbol(symbol); }

FuncPtr resolveSymbol(std::string_view symbol, bool) // TODO: add support in preloader
FuncPtr resolveSymbol(std::string_view symbol, bool disableErrorOutput) // TODO: add support in preloader
{
return pl::symbol_provider::pl_resolve_symbol(symbol.data());
}
Expand All @@ -34,22 +34,17 @@ FuncPtr resolveSignature(std::string_view signature) {
if (span.empty()) {
return nullptr;
}
for (auto& c : signature) {
if (!isxdigit(c) && c != ' ' && c != '?') {
return nullptr;
}
}

std::vector<std::optional<uchar>> pattern;
for (;;) {
while (signature.starts_with(' ')) signature.remove_prefix(1);
if (signature.empty()) break;
if (signature.starts_with('?')) {
for (size_t i = 0; i < signature.size(); ++i) {
auto& c = signature[i];
if (c == ' ') {
continue;
} else if (c == '?') {
pattern.emplace_back(std::nullopt);
signature.remove_prefix(1);
} else if (isxdigit(c) && (++i < signature.size() && isxdigit(signature[i]))) {
pattern.emplace_back(string_utils::svtouc(signature.substr(i - 1, 2), nullptr, 16));
} else {
pattern.emplace_back(string_utils::svtouc(signature.substr(0, 2), nullptr, 16));
signature.remove_prefix(2);
return nullptr;
}
}
if (pattern.empty()) {
Expand All @@ -59,7 +54,7 @@ FuncPtr resolveSignature(std::string_view signature) {
bool match = true;
size_t iter = 0;
for (auto& c : pattern) {
if (c && span[i + iter] != *c) {
if (span[i + iter] != c) {
match = false;
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/schedule/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ll/api/base/ErrorInfo.h"

namespace ll::schedule {
inline namespace task {
std::atomic_ullong TaskId{0};

std::optional<time_t> tryParseTime(std::string const& expression, std::string_view format) {
Expand Down Expand Up @@ -30,7 +31,7 @@ std::chrono::system_clock::time_point parseTime(std::string const& expression) {
.value_or(tryParseTime(expression, "%Y/%m/%d %H:%M:%S").value_or(0))
);
}

} // namespace task
namespace detail {
void printScheduleError() noexcept {
static Logger logger("Scheduler");
Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/schedule/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string_view>

namespace ll::schedule {
inline namespace task {
LLETAPI std::atomic_ullong TaskId;

template <class Clock>
Expand Down Expand Up @@ -126,7 +127,7 @@ class DelayTask : public Task<Clock> {
return time;
}
};

} // namespace task
namespace detail {
LLAPI void printScheduleError() noexcept;
}
Expand Down
17 changes: 0 additions & 17 deletions src/ll/api/service/GlobalService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

#include "mc/deps/raknet/RakPeer.h"
#include "mc/deps/raknet/RakPeerInterface.h"
#include "mc/entity/systems/DefaultEntitySystemsCollection.h"
#include "mc/entity/systems/EntitySystems.h"
#include "mc/entity/systems/IEntitySystemsCollection.h"
#include "mc/network/RakNetConnector.h"
#include "mc/network/ServerNetworkHandler.h"
#include "mc/resources/ResourcePackRepository.h"
Expand Down Expand Up @@ -211,18 +208,4 @@ LL_AUTO_INSTANCE_HOOK(CommandRegistryDestructor, HookPriority::High, "??1Command
ll::Global<CommandRegistry>.init(nullptr);
origin();
}

// EntitySystemsCollection

LL_AUTO_STATIC_HOOK(EntitySystemsCollectionCreater, HookPriority::High, EntitySystemsCollection::create, std::unique_ptr<IEntitySystemsCollection>) {
auto res = origin();
ll::Global<DefaultEntitySystemsCollection>.init((DefaultEntitySystemsCollection*)res.get());
return res;
}

LL_AUTO_INSTANCE_HOOK(EntitySystemsCollectionDestructor, HookPriority::High, "??1EntitySystems@@UEAA@XZ", void) {
ll::Global<DefaultEntitySystemsCollection>.init(nullptr);
origin();
}

} // namespace
4 changes: 1 addition & 3 deletions src/ll/api/service/GlobalService.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class StructureManager;
class ResourcePackRepository;
class CommandRegistry;
class NetworkSystem;
class DefaultEntitySystemsCollection;

namespace ll {

Expand All @@ -34,8 +33,7 @@ concept IsGlobalService = concepts::IsOneOf<
RakNet::RakPeer,
NetworkSystem,
ResourcePackRepository,
CommandRegistry,
DefaultEntitySystemsCollection>;
CommandRegistry>;

template <IsGlobalService T>
class GlobalService {
Expand Down
4 changes: 2 additions & 2 deletions src/ll/core/CrashLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ static LONG unhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS* e) {
crashInfo.logger.playerLevel = -2;
crashInfo.logger.fileLevel = INT32_MAX;
crashInfo.logger.consoleLevel = INT32_MAX;
crashInfo.logger.info.consoleFormat = {"{0} [{1}] {3}", "{:%T}", "{}", "", "{}"};
crashInfo.logger.info.consoleFormat = {"{0} [{1}] {3}", "{:%T}.{:0>3}", "{}", "", "{}"};
crashInfo.logger.info.style = {
fmt::fg(fmt::color::light_blue),
fmt::fg(fmt::color::light_green),
Expand All @@ -253,7 +253,7 @@ static LONG unhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS* e) {
u8str2str((crashInfo.path / (crashInfo.settings.logPrefix + crashInfo.date + ".log")).u8string())
);

crashInfo.logger.info.fileFormat = {"{0} [{1}] {3}", "{:%F %T}", "{}", "", "{}"};
crashInfo.logger.info.fileFormat = {"{0} [{1}] {3}", "{:%F %T}.{:0>3}", "{}", "", "{}"};
crashInfo.logger.error = crashInfo.logger.info;
crashInfo.process = GetCurrentProcess();
crashInfo.thread = GetCurrentThread();
Expand Down
30 changes: 15 additions & 15 deletions src/ll/test/PtrTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ll/api/memory/Hook.h"
#include "mc/entity/gamerefs_entity/EntityRegistry.h"
#include "mc/server/ServerInstance.h"
#include "mc/world/events/ServerInstanceEventCoordinator.h"
#include "mc/world/level/block/Block.h"
Expand All @@ -16,16 +17,17 @@
struct SystemCategory;

#include "ll/api/service/GlobalService.h"
#include "mc/entity/gamerefs_entity/EntityRegistry.h"
#include "mc/entity/systems/EntitySystems.h"
#include "mc/world/level/Level.h"

#include "mc/entity/systems/DefaultEntitySystemsCollection.h"

auto test() {
ll::logger.warn("mAllSystemsInfo");
for (auto& info : ll::Global<DefaultEntitySystemsCollection>->mAllSystemsInfo) {
ll::logger.warn("info {}", info.mName);
}
// ll::logger.warn("mAllSystemsInfo");
// auto& systems = ll::Global<Level>->getEntitySystems().getDefaultCollection();

// for (auto& info : systems.mAllSystemsInfo) {
// ll::logger.warn("info {}", info.mName);
// }

// struct EntitySystems::EditorSystemCategory
// struct EntitySystems::GameSystemCategory
Expand All @@ -41,11 +43,11 @@ auto test() {
// struct VanillaSystemCategories::UpdateWaterState
// struct VanillaSystemCategories::UsedByClientAndServerAuth

// auto& vec = ll::Global<DefaultEntitySystemsCollection>->mTickingSystemCategories;
// auto& vec = systems.mTickingSystemCategories;

// ll::logger.warn("info safe:{} ", ((char*)&*vec.end() - (char*)&*vec.begin()) / vec.size());
// for (auto& category :
// ll::Global<DefaultEntitySystemsCollection>->mTickingSystemCategories) {
// systems.mTickingSystemCategories) {
// ll::logger.warn("category :{} ", category.mCategory.value);
// for (auto id : category.mSystems) {
// ll::logger.warn("category sys:{} ", id);
Expand All @@ -55,11 +57,7 @@ auto test() {
// ll::logger.warn("category sys:{} {}", timing.mMsTime,timing.mCount);
// }
// }
ll::logger.warn(
"mAllSystemsInfo size {} {}",
ll::Global<DefaultEntitySystemsCollection>->mAllSystemsInfo.size(),
ll::Global<DefaultEntitySystemsCollection>->mAllSystems.size()
);
// ll::logger.warn("mAllSystemsInfo size {} {}", systems.mAllSystemsInfo.size(), systems.mAllSystems.size());
}

LL_AUTO_TYPED_INSTANCE_HOOK(
Expand Down Expand Up @@ -161,7 +159,9 @@ LL_AUTO_TYPED_INSTANCE_HOOK(
// std::cout << "hii " << bool(ptr) << ' ' << ptr->getTypeName() << std::endl;
ll::logger.warn(
"{}",
ll::memory::resolveIdentifier("`anonymous namespace'::DefaultEntitySystemsCollection::internalGatherSystemTimings")
ll::memory::resolveIdentifier(
"`anonymous namespace'::DefaultEntitySystemsCollection::internalGatherSystemTimings"
)
);

origin();
Expand All @@ -180,4 +180,4 @@ LL_AUTO_TYPED_INSTANCE_HOOK(
// ) {
// ll::logger.debug("tick {} {}", registry.mName, std::chrono::system_clock::now());
// origin(registry);
// }
// }
Loading

0 comments on commit 4ae5d14

Please sign in to comment.