Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
add xuiddb
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamguxiang committed Feb 16, 2021
1 parent ac1dce5 commit 93c06a5
Showing 31 changed files with 2,278 additions and 35 deletions.
2 changes: 2 additions & 0 deletions LiteLoader/LiteLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "pch.h"
#include <filesystem>
#include "framework.h"
#include <api\xuidreg\xuidreg.h>

static void PrintErrorMessage() {
DWORD errorMessageID = ::GetLastError();
@@ -66,6 +67,7 @@ static void loadPlugins() {
}
static void entry(bool fixcwd) {
loadPlugins();
XIDREG::initAll();
}

THook(int, "main", int a, void* b) {
26 changes: 25 additions & 1 deletion LiteLoader/LiteLoader.vcxproj
Original file line number Diff line number Diff line change
@@ -143,7 +143,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;LITELOADER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;LITELOADER_EXPORTS;_WINDOWS;_USRDLL;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
@@ -162,6 +162,23 @@
<ClInclude Include="..\headers\api\serviceLocate.h" />
<ClInclude Include="..\headers\api\types\helper.h" />
<ClInclude Include="..\headers\api\types\types.h" />
<ClInclude Include="..\headers\api\xuidreg\xuidreg.h" />
<ClInclude Include="..\headers\lbpch.h" />
<ClInclude Include="..\headers\leveldb\c.h" />
<ClInclude Include="..\headers\leveldb\cache.h" />
<ClInclude Include="..\headers\leveldb\comparator.h" />
<ClInclude Include="..\headers\leveldb\db.h" />
<ClInclude Include="..\headers\leveldb\dumpfile.h" />
<ClInclude Include="..\headers\leveldb\env.h" />
<ClInclude Include="..\headers\leveldb\export.h" />
<ClInclude Include="..\headers\leveldb\filter_policy.h" />
<ClInclude Include="..\headers\leveldb\iterator.h" />
<ClInclude Include="..\headers\leveldb\options.h" />
<ClInclude Include="..\headers\leveldb\slice.h" />
<ClInclude Include="..\headers\leveldb\status.h" />
<ClInclude Include="..\headers\leveldb\table.h" />
<ClInclude Include="..\headers\leveldb\table_builder.h" />
<ClInclude Include="..\headers\leveldb\write_batch.h" />
<ClInclude Include="..\headers\liteloader.h" />
<ClInclude Include="..\headers\loader\hash.h" />
<ClInclude Include="..\headers\loader\hook.h" />
@@ -220,9 +237,16 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="stl\DBImpl.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="xuidreg\xuidreg.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Library Include="..\lib\detours.lib" />
<Library Include="..\lib\leveldb.lib" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="LiteLoader.rc" />
70 changes: 70 additions & 0 deletions LiteLoader/LiteLoader.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -37,6 +37,18 @@
<Filter Include="头文件\mc\CommandReg">
<UniqueIdentifier>{c56a4f7b-2ab1-43d8-8bec-c2499ff2849a}</UniqueIdentifier>
</Filter>
<Filter Include="源文件\regxuid">
<UniqueIdentifier>{baae125c-c66a-4a35-8904-a6f265b0c009}</UniqueIdentifier>
</Filter>
<Filter Include="头文件\api\regxuid">
<UniqueIdentifier>{b7601fcb-67fc-4825-b832-6f7abefd9269}</UniqueIdentifier>
</Filter>
<Filter Include="头文件\leveldb">
<UniqueIdentifier>{d47f43c3-4f3c-4844-85cc-40127e88fdb8}</UniqueIdentifier>
</Filter>
<Filter Include="源文件\stl">
<UniqueIdentifier>{10e46cc5-3d6e-4a46-b0c2-decce08adbfa}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="framework.h">
@@ -168,6 +180,57 @@
<ClInclude Include="..\headers\api\commands.h">
<Filter>头文件\api</Filter>
</ClInclude>
<ClInclude Include="..\headers\api\xuidreg\xuidreg.h">
<Filter>头文件\api\regxuid</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\c.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\cache.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\comparator.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\db.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\dumpfile.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\env.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\export.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\filter_policy.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\iterator.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\options.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\slice.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\status.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\table.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\table_builder.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\leveldb\write_batch.h">
<Filter>头文件\leveldb</Filter>
</ClInclude>
<ClInclude Include="..\headers\lbpch.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
@@ -191,9 +254,16 @@
<ClCompile Include="api\Basic_Event.cpp">
<Filter>源文件\api</Filter>
</ClCompile>
<ClCompile Include="xuidreg\xuidreg.cpp">
<Filter>源文件\regxuid</Filter>
</ClCompile>
<ClCompile Include="stl\DBImpl.cpp">
<Filter>源文件\stl</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Library Include="..\lib\detours.lib" />
<Library Include="..\lib\leveldb.lib" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="LiteLoader.rc">
2 changes: 1 addition & 1 deletion LiteLoader/api/Basic_Event.cpp
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
#include <string>
#include <liteloader.h>
#include <api/Basic_Event.h>
#include <loader/Loader.h>
#include <lbpch.h>
#include <api/types/helper.h>
#include <mc/OffsetHelper.h>
#include <mc/Player.h>
13 changes: 6 additions & 7 deletions LiteLoader/api/helper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//#include <lbpch.h>
//#include<api\types\types.h>
#include <lbpch.h>
#include<api\types\types.h>
#include <loader/Loader.h>
#include <sstream>
#include <liteloader.h>
@@ -54,18 +54,17 @@ namespace liteloader {
bool rv = MinecraftCommands::_runcmd(&origin, cmd, 4, 1);
return { rv, std::move(val) };
}
//static void* FAKE_PORGVTBL[26];
/*
LIAPI bool runcmdAs(WPlayer wp, const string& cmd) {
static void* FAKE_PORGVTBL[26];
LIAPI bool runcmdAs(Player* pl, string& cmd) {
void** filler[5];
SymCall("??0PlayerCommandOrigin@@QEAA@AEAVPlayer@@@Z", void, void*, ServerPlayer*)(filler, wp);
SymCall("??0PlayerCommandOrigin@@QEAA@AEAVPlayer@@@Z", void, void*, ServerPlayer*)(filler, (ServerPlayer*)pl);
if (FAKE_PORGVTBL[1] == NULL) {
memcpy(FAKE_PORGVTBL, ((void**)filler[0])-1, sizeof(FAKE_PORGVTBL));
FAKE_PORGVTBL[1] = (void*)dummy;
}
filler[0] = FAKE_PORGVTBL+1;
return MinecraftCommands::_runcmd(filler, cmd, 4, 1);
}*/
}
LIAPI string getIP(class ::NetworkIdentifier& ni) {
string rv = LocateS<RakPeer_t>()->getAdr(ni).toString();
return rv.substr(0,rv.find('|'));
10 changes: 10 additions & 0 deletions LiteLoader/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include<lbpch.h>
#include "framework.h"
#include<iostream>
#include<filesystem>

LIAPI string GetDataPath(const string& myname) {
using namespace std::filesystem;
create_directory("data");
return "data/" + myname;
}

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
76 changes: 76 additions & 0 deletions LiteLoader/stl/DBImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <lbpch.h>
#include <stl\KVDB.h>

LIAPI std::unique_ptr<KVDBImpl> MakeKVDB(const string& path, bool read_cache, int cache_sz, int Bfilter_bit) {
auto db = make_unique<KVDBImpl>();
db->__init(path.c_str(),read_cache,cache_sz,Bfilter_bit);
return db;
}
void KVDBImpl::__init(const char* path, bool read_cache, int cache_sz, int Bfilter_bit) {
rdopt = leveldb::ReadOptions();
wropt = leveldb::WriteOptions();
options = leveldb::Options();
rdopt.fill_cache = read_cache;
rdopt.verify_checksums = false;
wropt.sync = false;
if (cache_sz) {
options.block_cache = leveldb::NewLRUCache(cache_sz);
}
options.reuse_logs = true; //WARN:EXPERIMENTAL
if (Bfilter_bit)
options.filter_policy = leveldb::NewBloomFilterPolicy(Bfilter_bit);
options.create_if_missing = true;
dpath = path;
leveldb::Status status = leveldb::DB::Open(options, path, &db);
if (!status.ok()) {
printf("cannot load %s reason: %s", path, status.ToString().c_str());
exit(1);
}
}
KVDBImpl::~KVDBImpl() {
if (options.filter_policy)
delete options.filter_policy;
delete db;
}
bool KVDBImpl::get(string_view key, string& val) {
auto s = db->Get(rdopt, leveldb::Slice(key.data(), key.size()), &val);
if (!s.ok()) {
if (s.IsNotFound())
return false;
printf("[DB Error]get %s %s\n",dpath.c_str(),s.ToString().c_str());
}
return true;
}
void KVDBImpl::put(string_view key, string_view val) {
//WATCH_ME("put kvdb " + dpath);
auto s=db->Put(wropt, leveldb::Slice(key.data(), key.size()), leveldb::Slice(val.data(), val.size()));
if (!s.ok()) {
printf("[DB Error]put %s %s\n", dpath.c_str(), s.ToString().c_str());
}
}
void KVDBImpl::del(string_view key) {
//WATCH_ME("del kvdb " + dpath);
auto s=db->Delete(wropt, leveldb::Slice(key.data(), key.size()));
if (!s.ok()) {
printf("[DB Error]del %s %s\n", dpath.c_str(), s.ToString().c_str());
}
}
void KVDBImpl::iter(std::function<bool(string_view key)> const& fn) {
leveldb::Iterator* it = db->NewIterator(rdopt);
for (it->SeekToFirst(); it->Valid(); it->Next()) {
auto k = it->key();
if (!fn({ k.data(), k.size() }))
break;
}
delete it;
}
void KVDBImpl::iter(std::function<bool(string_view key, string_view val)> const& fn) {
leveldb::Iterator* it = db->NewIterator(rdopt);
for (it->SeekToFirst(); it->Valid(); it->Next()) {
auto k = it->key();
auto v = it->value();
if (!fn({ k.data(), k.size() }, { v.data(), v.size() }))
break;
}
delete it;
}
90 changes: 90 additions & 0 deletions LiteLoader/xuidreg/xuidreg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include<stl\KVDB.h>
#include<stl\viewhelper.h>
#include<api\xuidreg\xuidreg.h>
#include<mc/Certificate.h>
#include<mc/Core.h>
#include<api/serviceLocate.h>
#include<stl/views.h>
#include<api/types/types.h>
#include<api/Basic_Event.h>
#include<mc/OffsetHelper.h>
#include<stl\useful.h>
namespace XIDREG {
static std::unique_ptr<KVDBImpl> xuiddb;

/* LIAPI optional<string> id2str(xuid_t xid);
LIAPI optional<xuid_t> str2id(string const& name);
LIAPI void foreach (std::function<bool(xuid_t, string_view)>&&);*/
LIAPI optional<xuid_t> str2id(string_view _name) {
if (_name.size() >= 512)
return {};
if (_name == "system")
return { 0 };
char buf[512];
for (int i = 0; i < _name.size(); ++i) {
buf[i] = std::tolower(_name[i]);
}
string_view name(buf, _name.size());
string rv;
if (xuiddb->get(name, rv) && rv.size() == 8) {
return { *(xuid_t*)(rv.data()) };
}
return {};
}
LIAPI optional<string> id2str(xuid_t id) {
if (id == 0) {
return { "system" };
}
string val;
if (xuiddb->get(to_view(id), val))
return { val };
return {};
}
LIAPI void foreach(std::function<bool(xuid_t, string_view)>&&x) {
xuiddb->iter([&](string_view k, string_view v) -> bool {
if (k.size() == 8 && v.size() != 8) {
return x(*(xuid_t*)k.data(), v);
}
return true;
});
}
static void insert(xuid_t id, string_view _name) {
if (_name.size() >= 512)
return;
char buf[512];
for (int i = 0; i < _name.size(); ++i) {
buf[i] = std::tolower(_name[i]);
}
string_view name(buf, _name.size());
string val;
if (xuiddb->get(name, val)) {
if (val != to_view(id)) {
//LOG("[BASE/XUID] update", _name, "'s xuid->", id);
xuiddb->del(val);
xuiddb->put(name, to_view(id));
xuiddb->put(to_view(id), name);
}
}
else {
//LOG("[BASE/XUID] insert", _name, "'s xuid", id);
xuiddb->put(name, to_view(id));
xuiddb->put(to_view(id), name);
}
}
void initAll() {
Event::addEventListener([](ServerStartedEV) {
xuiddb = MakeKVDB(GetDataPath("xuiddb"), true, 2);
});
Event::addEventListener([](JoinEV var) {
auto sp = var.Player;
auto name = offPlayer::getRealName((Player*)sp);
auto _xuid = offPlayer::getXUID((Player*)sp);
xuid_t xuid;
if (_xuid.size() <= 1)
xuid = do_hash(name);
else
xuid = std::stoull(_xuid);
insert(xuid, name);
});
}
};
Loading

0 comments on commit 93c06a5

Please # to comment.