Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
odorajbotoj committed Oct 13, 2024
2 parents 4b421b8 + 7139270 commit 2923035
Show file tree
Hide file tree
Showing 22 changed files with 361 additions and 296 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2024-10-13

### Added

+ Added some new native api

### Changed

+ `MANAGER_VERSION` changed to `2` (warning: you cannot use old simplayer data now)
+ Changed `build` subcommand and api

## [1.0.0] - 2024-10-11

### Added

+ Move CoralFans SimulatedPlayer System from [CoralFans](https://github.com/CoralFans-Dev/CoralFans) to here
+ Added Script Arg

[2.0.0]: https://github.com/CoralFans-Dev/CFSP/compare/v1.0.0...v2.0.0
[1.0.0]: https://github.com/CoralFans-Dev/CFSP/releases/tag/v1.0.0
8 changes: 0 additions & 8 deletions src/cfsp/CFSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ bool CFSP::enable() {

hookSimPlayer(true);

// get DefaultDataLoadHelper
mod.getDefaultDataLoadHelper() =
static_cast<DefaultDataLoadHelper*>(ll::memory::resolveSymbol("??_7DefaultDataLoadHelper@@6B@"));
if (!mod.getDefaultDataLoadHelper()) {
getSelf().getLogger().error("Cannot get DefaultDataLoadHelper from symbol.");
return false;
}

if (mod.getConfig().command.sp.enabled) commands::registerSpCommand(mod.getConfig().command.sp.permission);

// load simplayer data
Expand Down
42 changes: 22 additions & 20 deletions src/cfsp/base/Macros.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#define COMMAND_CHECK_PLAYER \
auto* entity = origin.getEntity(); \
if (entity == nullptr || !entity->isType(ActorType::Player)) { \
Expand Down Expand Up @@ -42,10 +44,10 @@
auto it = this -> mNameSimPlayerMap.find(spname); \
if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; \
if (player->getCommandPermissionLevel() >= coral_fans::cfsp::mod().getConfig().simPlayer.adminPermission \
|| noCheck || uuid == it->second.ownerUuid) { \
if (it->second.status != SimPlayerStatus::Alive) \
|| noCheck || uuid == it->second->ownerUuid) { \
if (it->second->status != SimPlayerStatus::Alive) \
return {"translate.simplayer.error.statuserror"_tr(), false}; \
if (it->second.simPlayer) it->second.ACTION(arg); \
if (it->second->simPlayer) it->second->ACTION(arg); \
return {"translate.simplayer.success"_tr(), true}; \
} \
return {"translate.simplayer.error.permissiondenied"_tr(), false}; \
Expand Down Expand Up @@ -78,10 +80,10 @@
auto it = this -> mNameSimPlayerMap.find(spname); \
if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; \
if (player->getCommandPermissionLevel() >= coral_fans::cfsp::mod().getConfig().simPlayer.adminPermission \
|| noCheck || uuid == it->second.ownerUuid) { \
if (it->second.status != SimPlayerStatus::Alive) \
|| noCheck || uuid == it->second->ownerUuid) { \
if (it->second->status != SimPlayerStatus::Alive) \
return {"translate.simplayer.error.statuserror"_tr(), false}; \
if (it->second.simPlayer) it->second.ACTION(); \
if (it->second->simPlayer) it->second->ACTION(); \
return {"translate.simplayer.success"_tr(), true}; \
} \
return {"translate.simplayer.error.permissiondenied"_tr(), false}; \
Expand Down Expand Up @@ -113,17 +115,17 @@
auto it = this -> mNameSimPlayerMap.find(spname); \
if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; \
if (player->getCommandPermissionLevel() >= coral_fans::cfsp::mod().getConfig().simPlayer.adminPermission \
|| noCheck || uuid == it->second.ownerUuid) { \
if (it->second.status != SimPlayerStatus::Alive) \
|| noCheck || uuid == it->second->ownerUuid) { \
if (it->second->status != SimPlayerStatus::Alive) \
return {"translate.simplayer.error.statuserror"_tr(), false}; \
if (!it->second.isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; \
if (it->second.simPlayer) it->second.ACTION(arg); \
if (!it->second->isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; \
if (it->second->simPlayer) it->second->ACTION(arg); \
if (interval >= 1) { \
it->second.taskid = \
it->second->taskid = \
this->mScheduler->add(interval, [times, sp = it->second, arg](unsigned long long t) mutable { \
if (times > 0 && t > (unsigned long long)times - 1) return false; \
if (sp.simPlayer) [[likely]] { \
sp.ACTION(arg); \
if (sp->simPlayer) [[likely]] { \
sp->ACTION(arg); \
return true; \
} \
return false; \
Expand Down Expand Up @@ -165,17 +167,17 @@
auto it = this -> mNameSimPlayerMap.find(spname); \
if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; \
if (player->getCommandPermissionLevel() >= coral_fans::cfsp::mod().getConfig().simPlayer.adminPermission \
|| noCheck || uuid == it->second.ownerUuid) { \
if (it->second.status != SimPlayerStatus::Alive) \
|| noCheck || uuid == it->second->ownerUuid) { \
if (it->second->status != SimPlayerStatus::Alive) \
return {"translate.simplayer.error.statuserror"_tr(), false}; \
if (!it->second.isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; \
if (it->second.simPlayer) it->second.ACTION(); \
if (!it->second->isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; \
if (it->second->simPlayer) it->second->ACTION(); \
if (interval >= 1) { \
it->second.taskid = \
it->second->taskid = \
this->mScheduler->add(interval, [times, sp = it->second](unsigned long long t) mutable { \
if (times > 0 && t > (unsigned long long)times - 1) return false; \
if (sp.simPlayer) [[likely]] { \
sp.ACTION(); \
if (sp->simPlayer) [[likely]] { \
sp->ACTION(); \
return true; \
} \
return false; \
Expand Down
18 changes: 12 additions & 6 deletions src/cfsp/base/Mod.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#pragma once

#include "cfsp/Config.h"
#include "mc/dataloadhelper/DefaultDataLoadHelper.h"


namespace coral_fans::cfsp {

class CFSPMod {
private:
config::Config mConfig;
DefaultDataLoadHelper* mDefaultDataLoadHelper;
config::Config mConfig;

public:
inline config::Config& getConfig() { return this->mConfig; }

private:
CFSPMod() {}
~CFSPMod() {}
CFSPMod(const CFSPMod&);
CFSPMod& operator=(const CFSPMod&);

public:
inline config::Config& getConfig() { return this->mConfig; }
inline DefaultDataLoadHelper*& getDefaultDataLoadHelper() { return this->mDefaultDataLoadHelper; }
friend CFSPMod& mod();
friend class CFSP;
};

CFSPMod& mod();
Expand Down
15 changes: 4 additions & 11 deletions src/cfsp/commands/Sp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,22 +694,15 @@ void registerSpCommand(CommandPermissionLevel permission) {
spCommand,
"build",
{},
taskArg,
{},
[](Player* player, ll::command::RuntimeCommand const& self) {
return coral_fans::cfsp::SimPlayerManager::getInstance().simPlayerBuild(
player,
self["name"].get<ll::command::ParamKind::SoftEnum>(),
false,
self["interval"].has_value() ? self["interval"].get<ll::command::ParamKind::Int>() : 20,
self["times"].has_value() ? self["times"].get<ll::command::ParamKind::Int>() : 1
);
return coral_fans::cfsp::SimPlayerManager::getInstance()
.simPlayerBuild(player, self["name"].get<ll::command::ParamKind::SoftEnum>(), false);
},
[](Player* player, ll::command::RuntimeCommand const& self) {
return coral_fans::cfsp::SimPlayerManager::getInstance().groupBuild(
player,
self["name"].get<ll::command::ParamKind::SoftEnum>(),
self["interval"].has_value() ? self["interval"].get<ll::command::ParamKind::Int>() : 20,
self["times"].has_value() ? self["times"].get<ll::command::ParamKind::Int>() : 1
self["name"].get<ll::command::ParamKind::SoftEnum>()
);
}
);
Expand Down
53 changes: 36 additions & 17 deletions src/cfsp/simplayer/CFSP.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@
#include "mc/world/level/BlockPos.h"
#include "mc/world/phys/HitResultType.h"
#include <boost/serialization/access.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/unordered_set.hpp>
#include <boost/serialization/version.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>

#define MANAGER_VERSION 1
#define MANAGER_VERSION 2
#define INFO_VERSION 1

namespace coral_fans::cfsp {
Expand All @@ -57,14 +60,14 @@ class SimPlayerManager {
float offlineRotY;
std::string offlineGameType;
bool offlineEmptyInv;
SimulatedPlayer* simPlayer;
std::shared_ptr<timewheel::TimeWheel> scheduler;
unsigned long long taskid;
unsigned long long scriptid;
SimulatedPlayer* simPlayer; // no-save
std::shared_ptr<timewheel::TimeWheel> scheduler; // no-save
unsigned long long taskid; // no-save
unsigned long long scriptid; // no-save
// serialization
template <typename Archive>
void serialize(Archive& ar, const unsigned int version) {
if (version == 1) {
if (version == INFO_VERSION) {
ar & name;
ar & xuid;
ar & ownerUuid;
Expand Down Expand Up @@ -278,15 +281,15 @@ class SimPlayerManager {
};

private:
std::unordered_map<std::string, SimPlayerInfo> mNameSimPlayerMap;
std::unordered_map<std::string, std::unordered_set<std::string>> mOwnerNameMap;
std::unordered_map<std::string, std::unordered_set<std::string>> mGroupNameMap;
std::unordered_map<std::string, std::unordered_set<std::string>> mGroupAdminMap;
unsigned long long mOnlineCount;
unsigned long long mSpawnCount;
std::shared_ptr<timewheel::TimeWheel> mScheduler;
bool autorespawn;
bool autojoin;
std::unordered_map<std::string, boost::shared_ptr<SimPlayerInfo>> mNameSimPlayerMap;
std::unordered_map<std::string, std::unordered_set<std::string>> mOwnerNameMap;
std::unordered_map<std::string, std::unordered_set<std::string>> mGroupNameMap;
std::unordered_map<std::string, std::unordered_set<std::string>> mGroupAdminMap;
unsigned long long mOnlineCount; // no-save
unsigned long long mSpawnCount; // no-save
std::shared_ptr<timewheel::TimeWheel> mScheduler; // no-save
bool autorespawn;
bool autojoin;

private:
SimPlayerManager()
Expand All @@ -309,7 +312,7 @@ class SimPlayerManager {
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive& ar, const unsigned int version) {
if (version == 1) {
if (version == MANAGER_VERSION) {
ar & mNameSimPlayerMap;
ar & mOwnerNameMap;
ar & mGroupNameMap;
Expand Down Expand Up @@ -358,6 +361,22 @@ class SimPlayerManager {
public:
CFSP_API void setDead(std::string const&);

public:
CFSP_API bool hasGroup(std::string const& gname) {
return mGroupAdminMap.contains(gname) && mGroupNameMap.contains(gname);
}
CFSP_API bool isGroupAdmin(Player* player, std::string const& gname) {
return hasGroup(gname) && mGroupAdminMap[gname].contains(player->getUuid().asString());
}
CFSP_API bool inGroup(std::string const& spname, std::string const& gname) {
return hasGroup(gname) && mGroupNameMap[gname].contains(spname);
}
CFSP_API bool hasSimPlayer(std::string const& spname) { return mNameSimPlayerMap.contains(spname); }
CFSP_API bool isSimPlayerOwner(Player* player, std::string const& spname) {
return hasSimPlayer(spname) && mNameSimPlayerMap[spname]->ownerUuid == player->getUuid().asString();
}
CFSP_API std::optional<boost::shared_ptr<SimPlayerManager::SimPlayerInfo>> fetchSimPlayer(std::string const&);

public:
SP_REG_DEF(Stop)
SP_REG_DEF(Sneaking, bool)
Expand All @@ -373,7 +392,7 @@ class SimPlayerManager {
SP_REG_DEF(Interact, int, int)
SP_REG_DEF(Jump, int, int)
SP_REG_DEF(Use, int, int, int)
SP_REG_DEF(Build, int, int)
SP_REG_DEF(Build)
SP_REG_DEF(LookAt, Vec3 const&)
SP_REG_DEF(MoveTo, Vec3 const&)
SP_REG_DEF(NavTo, Vec3 const&)
Expand Down
Loading

0 comments on commit 2923035

Please sign in to comment.