Skip to content
This repository was archived by the owner on Jan 18, 2021. It is now read-only.

Commit bd128f2

Browse files
committed
fix transferserver command
1 parent d491355 commit bd128f2

23 files changed

+383
-7
lines changed

CommandSupport/dllmain.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ template <> typeid_t<CommandRegistry> Mod::CommandSupport::GetParameterTypeId<Co
4343
template <> typeid_t<CommandRegistry> Mod::CommandSupport::GetParameterTypeId<Json::Value>() {
4444
return GETID("?id@?1???$type_id@VCommandRegistry@@VValue@Json@@@@YA?AV?$typeid_t@VCommandRegistry@@@@XZ@4V1@A");
4545
}
46+
template <> typeid_t<CommandRegistry> Mod::CommandSupport::GetParameterTypeId<CommandSelector<Actor>>() {
47+
return GETID(
48+
"?id@?1???$type_id@VCommandRegistry@@V?$CommandSelector@VActor@@@@@@YA?AV?$typeid_t@VCommandRegistry@@@@XZ@4V1@"
49+
"A");
50+
}
51+
template <> typeid_t<CommandRegistry> Mod::CommandSupport::GetParameterTypeId<CommandSelector<Player>>() {
52+
return GETID(
53+
"?id@?1???$type_id@VCommandRegistry@@V?$CommandSelector@VPlayer@@@@@@YA?AV?$typeid_t@VCommandRegistry@@@@XZ@4V1@"
54+
"A");
55+
}
4656

4757
void dllenter() {}
4858
void dllexit() {}

Essentials/dllmain.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22

33
// Working in progress
44
class TransferCommand : public Command {
5-
void execute(CommandOrigin const &, CommandOutput &) { std::wcout << "Executed (WIP)" << std::endl; }
5+
public:
6+
CommandSelector<Player> selector;
7+
std::string hostname = "127.0.0.1";
8+
int port = 19132;
9+
TransferCommand() { selector.setIncludeDeadPlayers(true); }
10+
void execute(CommandOrigin const &origin, CommandOutput &output) {
11+
if (port <= 0 || port > 65535) {
12+
output.error("commands.transferserver.invalid.port");
13+
return;
14+
}
15+
auto results = selector.results(origin);
16+
for (auto &player : results) {
17+
TransferPacket pkt{hostname, port};
18+
player->sendNetworkPacket(pkt);
19+
}
20+
output.success("commands.transferserver.successful");
21+
}
622
};
723

824
static struct Settings {
@@ -31,11 +47,15 @@ template <> struct convert<Settings> {
3147
extern "C" __declspec(dllexport) void ApplySettings(YAML::Node const &node) { yaml_assign(settings, node); }
3248

3349
static void startRegister(CommandRegistry *registry) {
50+
using namespace commands;
3451
if (settings.commands.transferserver) {
3552
std::string name = "transferserver";
3653
registry->registerCommand(
37-
name, "commands.transferserver.description", CommandPermissionLevel::Normal, CommandFlagNone, CommandFlagNone);
38-
registry->registerOverload(name, &CommandRegistry::allocateCommand<TransferCommand>, {});
54+
name, "commands.transferserver.description", CommandPermissionLevel::Privileged, CommandFlagNone,
55+
CommandFlagNone);
56+
registry->registerOverload<TransferCommand>(
57+
name, mandatory(&TransferCommand::selector, "target"), mandatory(&TransferCommand::hostname, "hostname"),
58+
optional(&TransferCommand::port, "port"));
3959
}
4060
}
4161

Essentials/pch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
#include <Command/CommandPermissionLevel.h>
77
#include <Command/CommandFlag.h>
88
#include <Command/CommandParameterData.h>
9+
#include <Command/CommandOutput.h>
10+
#include <Actor/ServerPlayer.h>
11+
#include <Packet/TransferPacket.h>
912
#include <iostream>
1013
#include <yaml-cpp/yaml.h>
1114
#include <hook.h>
15+
#include <log.h>
1216
#include <boost/format.hpp>
1317
#include <dllentry.h>

MinecraftHeaders/Actor/Actor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
class Actor {};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include "../Core/HashedString.h"
5+
#include "ActorType.h"
6+
7+
class ActorDefinitionIdentifier {
8+
std::string ns; // 0
9+
std::string identifier; // 32
10+
std::string event; // 64
11+
std::string fullname; // 96
12+
HashedString canonicalHash; // 128
13+
14+
public:
15+
inline HashedString const &getCanonicalHash() const { return canonicalHash; }
16+
inline std::string const &getCanonicalName() const { return canonicalHash.getString(); }
17+
inline std::string const &getFullName() const { return fullname; }
18+
inline std::string const &getIdentifier() const { return identifier; }
19+
inline std::string const &getInitEvent() const { return event; }
20+
inline std::string const &getNamespace() const { return ns; }
21+
inline bool isEmpty() const { return ns.empty() && identifier.empty(); }
22+
inline bool isVanilla() const { return ns == "minecraft"; }
23+
inline void setIdentifier(std::string const &id) { identifier = id; }
24+
inline void setInitEvent(std::string const &e) { event = e; }
25+
26+
__declspec(dllimport) ActorDefinitionIdentifier(ActorDefinitionIdentifier const &);
27+
__declspec(dllimport) ActorDefinitionIdentifier(ActorDefinitionIdentifier &&);
28+
__declspec(dllimport) ActorDefinitionIdentifier(std::string, std::string, std::string);
29+
__declspec(dllimport) ActorDefinitionIdentifier(std::string const &);
30+
__declspec(dllimport) ActorDefinitionIdentifier(ActorType);
31+
__declspec(dllimport) ActorDefinitionIdentifier &operator=(ActorDefinitionIdentifier const &);
32+
__declspec(dllimport) bool operator==(ActorDefinitionIdentifier const &);
33+
};

MinecraftHeaders/Actor/ActorType.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
enum class ActorType {};

MinecraftHeaders/Actor/Player.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "Actor.h"
4+
#include <hook.h>
5+
6+
class Packet;
7+
class ServerPlayer;
8+
9+
class Player : public Actor {
10+
public:
11+
inline ServerPlayer *asServerPlayer() const noexcept {
12+
return const_cast<ServerPlayer *>(reinterpret_cast<ServerPlayer const *>(this));
13+
}
14+
15+
inline void sendNetworkPacket(Packet &pkt) {
16+
CallServerFunction<void>("?sendNetworkPacket@ServerPlayer@@UEBAXAEAVPacket@@@Z", this, &pkt);
17+
}
18+
};

MinecraftHeaders/Actor/ServerPlayer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include "Player.h"
4+
5+
class ServerPlayer : public Player {
6+
public:
7+
// place holder.. all implement in Player.h
8+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include "CommandOutputParameter.h"
6+
7+
class CommandOutput {
8+
public:
9+
__declspec(dllimport) void success(std::string const &, std::vector<CommandOutputParameter> const &params = {});
10+
__declspec(dllimport) void error(std::string const &, std::vector<CommandOutputParameter> const &params = {});
11+
__declspec(dllimport) void addToResultList(std::string const &, Actor const &);
12+
template <typename T> void set(char const *name, T value);
13+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include "../Core/BlockPos.h"
6+
7+
class Player;
8+
9+
class CommandOutputParameter {
10+
std::string str;
11+
int type;
12+
13+
public:
14+
__declspec(dllimport) CommandOutputParameter(std::string const &);
15+
__declspec(dllimport) CommandOutputParameter(int);
16+
__declspec(dllimport) CommandOutputParameter(BlockPos);
17+
__declspec(dllimport) CommandOutputParameter(std::vector<Player const *> const &);
18+
__declspec(dllimport) CommandOutputParameter(std::vector<std::string> const &);
19+
20+
inline CommandOutputParameter(std::string str, int type) : str(str), type(type) {}
21+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include "../Core/Vec3.h"
4+
5+
class CommandPosition {
6+
public:
7+
Vec3 vec;
8+
bool mx, my, mz, op;
9+
};

MinecraftHeaders/Command/CommandRegistry.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class HardNonTerminal;
1818
class CommandRegistry {
1919
public:
2020
#pragma region struct
21-
class ParseToken;
21+
struct ParseToken;
2222
struct Symbol {
2323
unsigned val;
2424
};
@@ -62,9 +62,6 @@ class CommandRegistry {
6262
return true;
6363
}
6464

65-
template <typename Type>
66-
bool parse(void *, ParseToken const &, CommandOrigin const &, int, std::string &, std::vector<std::string> &) const;
67-
6865
__declspec(dllimport) void registerCommand(
6966
std::string const &, char const *, CommandPermissionLevel, CommandFlag, CommandFlag);
7067
__declspec(dllimport) void registerAlias(std::string const &, std::string const &);
@@ -73,6 +70,10 @@ class CommandRegistry {
7370
__declspec(dllimport) Signature const *findCommand(std::string const &) const;
7471
__declspec(dllimport) void registerOverloadInternal(Signature &, Overload &);
7572

73+
template <typename Type>
74+
__declspec(dllimport) bool parse(
75+
void *, ParseToken const &, CommandOrigin const &, int, std::string &, std::vector<std::string> &) const;
76+
7677
__declspec(dllimport) Symbol addEnumValuesInternal(
7778
std::string const &, std::vector<std::pair<unsigned long, unsigned long>> const &, typeid_t<CommandRegistry>,
7879
bool (CommandRegistry::*)(
@@ -87,11 +88,16 @@ class CommandRegistry {
8788
unsigned addEnumValues(std::string const &, std::vector<std::string> const &);
8889

8990
public:
91+
template <typename T> inline static auto getParseFn() { return &CommandRegistry::parse<T>; }
92+
9093
template <typename T> inline static std::unique_ptr<Command> allocateCommand() { return std::make_unique<T>(); }
9194
inline void registerOverload(
9295
std::string const &name, Overload::FactoryFn factory, std::initializer_list<CommandParameterData> args) {
9396
Signature *signature = const_cast<Signature *>(findCommand(name));
9497
auto &overload = signature->overloads.emplace_back(CommandVersion{}, factory, args);
9598
registerOverloadInternal(*signature, overload);
9699
}
100+
template <typename T, typename... Params> inline void registerOverload(std::string const &name, Params... params) {
101+
registerOverload(name, &allocateCommand<T>, {params...});
102+
}
97103
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "CommandSelectorBase.h"
4+
#include "CommandSelectorResults.h"
5+
#include <type_traits>
6+
7+
class Player;
8+
class Actor;
9+
class CommandOrigin;
10+
11+
template <typename T> class CommandSelector : public CommandSelectorBase {
12+
public:
13+
inline CommandSelector() : CommandSelectorBase(std::is_same_v<T, Player>) {}
14+
__declspec(dllimport) CommandSelectorResults<T> results(CommandOrigin const &) const;
15+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <vector>
5+
#include <string>
6+
#include <cstdint>
7+
#include "../Core/Vec3.h"
8+
#include "../Core/BlockPos.h"
9+
#include "CommandPosition.h"
10+
#include "InvertableFilter.h"
11+
#include "../Actor/ActorDefinitionIdentifier.h"
12+
13+
class CommandOrigin;
14+
class Actor;
15+
class ActorDefinitionIdentifier;
16+
17+
class CommandSelectorBase {
18+
uint32_t version; // 0
19+
uint32_t type; // 4
20+
uint32_t order; // 8
21+
std::vector<InvertableFilter<std::string>> namefilters; // 16
22+
std::vector<InvertableFilter<ActorDefinitionIdentifier>> typefilter2; // 40
23+
std::vector<InvertableFilter<std::string>> tagfilters; // 64
24+
std::vector<std::function<bool(CommandOrigin const &, Actor const &)>> customfilters; // 88
25+
CommandPosition position; // 112
26+
BlockPos box; // 128
27+
float radiusMin; // 140
28+
float radiusMax; // 144 = 0x7f7fffff (float max)
29+
uint64_t resultCount; // 152 = 0xFFFFFFFF
30+
bool includeDeadPlayers; // 160
31+
bool flag161; // 161
32+
bool flag162; // 162
33+
bool flag163; // 163
34+
bool playerOnly; // 164
35+
bool explicitIdSelector; // 165
36+
37+
public:
38+
inline bool isExplicitIdSelector() const { return explicitIdSelector; }
39+
inline void addNameFilter(InvertableFilter<std::string> const &filter) { namefilters.emplace_back(filter); }
40+
inline void addTagFilter(InvertableFilter<std::string> const &filter) {
41+
if (isExplicitIdSelector()) explicitIdSelector = false;
42+
tagfilters.emplace_back(filter);
43+
}
44+
inline void setIncludeDeadPlayers(bool value) { includeDeadPlayers = value; }
45+
inline void setResultCount(uint64_t value) { resultCount = value; }
46+
47+
__declspec(dllimport) void addFilter(std::function<bool(CommandOrigin const &, Actor const &)>);
48+
__declspec(dllimport) void addTypeFilter(InvertableFilter<std::string> const &);
49+
__declspec(dllimport) CommandSelectorBase();
50+
__declspec(dllimport) void setBox(BlockPos);
51+
__declspec(dllimport) void setPosition(CommandPosition);
52+
__declspec(dllimport) void setRadiusMin(float);
53+
__declspec(dllimport) void setRadiusMax(float);
54+
__declspec(dllimport) bool compile(CommandOrigin const &, std::string &);
55+
56+
protected:
57+
__declspec(dllimport) CommandSelectorBase(bool isPlayer);
58+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <memory>
5+
6+
template <typename T> class CommandSelectorResults {
7+
std::shared_ptr<std::vector<T *>> data;
8+
9+
public:
10+
auto begin() { return data->begin(); }
11+
auto end() { return data->end(); }
12+
auto count() const { return data->size(); }
13+
auto empty() const { return data->empty(); }
14+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
template <typename T> struct InvertableFilter {
4+
T value;
5+
bool inverted;
6+
};

MinecraftHeaders/Core/BlockPos.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
class BlockPos {
4+
public:
5+
int x, y, z;
6+
};

MinecraftHeaders/Core/HashedString.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <cstdint>
5+
6+
class HashedString {
7+
std::string str;
8+
uint64_t hash;
9+
10+
public:
11+
std::string const &getString() const { return str; }
12+
13+
__declspec(dllimport) HashedString(HashedString const &rhs);
14+
__declspec(dllimport) HashedString(HashedString &&rhs);
15+
__declspec(dllimport) HashedString(char const *rhs);
16+
__declspec(dllimport) HashedString(std::string const &rhs);
17+
__declspec(dllimport) bool operator==(HashedString const &rhs) const;
18+
__declspec(dllimport) bool operator!=(HashedString const &rhs) const;
19+
};

0 commit comments

Comments
 (0)