This repository was archived by the owner on Nov 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac1dce5
commit 93c06a5
Showing
31 changed files
with
2,278 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.