Skip to content

Commit

Permalink
feat: add ForceEnableCheatCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Dofes committed Feb 7, 2024
1 parent 8f4be7f commit 2155d59
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ll/core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

#include "ll/api/base/Macro.h"
#include "ll/api/reflection/Dispatcher.h"
#include "ll/core/tweak/ForceEnableCheatCommands.h"
#include "ll/core/tweak/SimpleServerLogger.h"


namespace ll {

struct LeviConfig {

int version = 11;
int version = 12;

std::string language = "system";
struct {
Expand All @@ -30,7 +32,8 @@ struct LeviConfig {
} crashLogger{};

struct {
bool disableAutoCompactionLog = true;
bool disableAutoCompactionLog = true;
ll::reflection::Dispatcher<bool, ForceEnableCheatCommands> forceEnableCheatCommands = true;
} tweak{};

reflection::Dispatcher<SimpleServerLoggerConfig, SimpleServerLogger> simpleServerLogger{};
Expand Down
83 changes: 83 additions & 0 deletions src/ll/core/tweak/ForceEnableCheatCommands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "ll/core/tweak/ForceEnableCheatCommands.h"

#include "mc/enums/SemanticConstraint.h"
#include "mc/server/commands/CommandFlag.h"
#include "mc/server/commands/CommandOrigin.h"
#include "mc/server/commands/CommandPermissionLevel.h"
#include "mc/server/commands/CommandRegistry.h"
#include "mc/server/commands/CommandSelectorBase.h"

#include "ll/api/memory/Hook.h"
#include <memory>
#include <string>
#include <vector>

namespace ll {

namespace force_enable_cheat_commands {

LL_TYPE_INSTANCE_HOOK(
CommandRegistryRegisterCommandHook,
ll::memory::HookPriority::Normal,
CommandRegistry,
&CommandRegistry::registerCommand,
void,
std::string const& name,
char const* description,
::CommandPermissionLevel requirement,
CommandFlag flag1,
CommandFlag flag2
) {
flag1 |= CommandFlagValue::NotCheat;
return origin(name, description, requirement, flag1, flag2);
}

LL_TYPE_INSTANCE_HOOK(
CommandSelectorBaseIsExpansionAllowedHook,
ll::memory::HookPriority::Normal,
CommandSelectorBase,
&CommandSelectorBase::isExpansionAllowed,
bool,
CommandOrigin const& ori
) {
origin(ori);
return true;
}

LL_TYPE_INSTANCE_HOOK(
CommandRegistryAddEnumValueConstraintsHook,
ll::memory::HookPriority::Normal,
CommandRegistry,
&CommandRegistry::addEnumValueConstraints,
void,
std::string const& enumName,
std::vector<std::string> const& enumValues,
SemanticConstraint constraint
) {
constraint = static_cast<SemanticConstraint>(
static_cast<unsigned char>(constraint) & ~static_cast<unsigned char>(SemanticConstraint::RequiresCheatsEnabled)
);
return origin(enumName, enumValues, constraint);
}

} // namespace force_enable_cheat_commands

struct ForceEnableCheatCommands::Impl {
memory::HookRegistrar<
force_enable_cheat_commands::CommandRegistryRegisterCommandHook,
force_enable_cheat_commands::CommandSelectorBaseIsExpansionAllowedHook,
force_enable_cheat_commands::CommandRegistryAddEnumValueConstraintsHook>
r;
};

void ForceEnableCheatCommands::call(bool enable) {
if (enable) {
if (!impl) impl = std::make_unique<Impl>();
} else {
impl.reset();
}
};

ForceEnableCheatCommands::ForceEnableCheatCommands() = default;
ForceEnableCheatCommands::~ForceEnableCheatCommands() = default;
} // namespace ll
16 changes: 16 additions & 0 deletions src/ll/core/tweak/ForceEnableCheatCommands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <memory>

namespace ll {

struct ForceEnableCheatCommands {
struct Impl;
std::unique_ptr<Impl> impl;

void call(bool);
ForceEnableCheatCommands();
~ForceEnableCheatCommands();
};

} // namespace ll

0 comments on commit 2155d59

Please sign in to comment.