Skip to content

Commit

Permalink
refactor: project code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Feb 5, 2024
1 parent 2d8c3d6 commit 3e1a3a7
Show file tree
Hide file tree
Showing 48 changed files with 247 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#include <string>
#include <string_view>



#include "endstone/detail/plugin/endstone_plugin_manager.h"
#include "endstone/plugin/plugin_manager.h"
#include "endstone/server.h"
#include "endstone_core/plugin/endstone_plugin_manager.h"

namespace endstone::detail {

class EndstoneServer : public Server {
public:
Expand Down Expand Up @@ -55,3 +55,5 @@ class EndstoneServer : public Server {
Logger &logger_;
std::unique_ptr<EndstonePluginManager> plugin_manager_;
};

} // namespace endstone::detail
23 changes: 12 additions & 11 deletions include/endstone_runtime/hook.h → include/endstone/detail/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
#include <functional>
#include <system_error>

namespace endstone::hook {
namespace endstone::detail::hook {
void install();
const std::error_category &hook_error_category() noexcept;
} // namespace endstone::hook
} // namespace endstone::detail::hook

namespace endstone::hook::detail {
namespace endstone::detail::hook {

/**
* @brief Cast a function pointer to void pointer
Expand Down Expand Up @@ -88,13 +88,13 @@ void *fp_cast(Return (Class::*fp)(Args...) const)
temp.p = fp;
return temp.v;
}
} // namespace endstone::hook::detail
} // namespace endstone::detail::hook

namespace endstone::hook::detail {
namespace endstone::detail::hook {
void *get_original(void *detour);
}

namespace endstone::hook::detail {
namespace endstone::detail::hook {
/**
* @brief Construct a std::function from a function pointer
*/
Expand Down Expand Up @@ -127,10 +127,10 @@ inline std::function<Return(const Class *, Arg...)> get_original(Return (Class::
return func(obj, std::forward<Arg>(args)...);
};
}
} // namespace endstone::hook::detail
#define ENDSTONE_HOOK_CALL_ORIGINAL(fp, ...) endstone::hook::detail::get_original(fp)(__VA_ARGS__)
} // namespace endstone::detail::hook
#define ENDSTONE_HOOK_CALL_ORIGINAL(fp, ...) endstone::detail::hook::get_original(fp)(__VA_ARGS__)

namespace endstone::hook::detail {
namespace endstone::detail::hook {
/**
* @brief Construct a std::function from a function pointer
* with Return Value Optimization (RVO).
Expand Down Expand Up @@ -180,6 +180,7 @@ inline std::function<Return *(Return *, const Class *, Arg...)> get_original_rvo
};
#endif
}
} // namespace endstone::hook::detail
} // namespace endstone::detail::hook

#define ENDSTONE_HOOK_CALL_ORIGINAL_RVO(fp, ret, ...) ret = *endstone::hook::detail::get_original_rvo(fp)(&ret, __VA_ARGS__)
#define ENDSTONE_HOOK_CALL_ORIGINAL_RVO(fp, ret, ...) \
ret = *endstone::detail::hook::get_original_rvo(fp)(&ret, __VA_ARGS__)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

#include "endstone/logger.h"

namespace endstone::detail {

class LoggerFactory {
public:
static Logger &getLogger(const std::string &name);
};

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
#include <functional>
#include <string>

namespace endstone::platform {
namespace endstone::detail::os {

void *get_module_base();
std::string get_module_pathname();
void *get_executable_base();
std::string get_executable_pathname();

} // namespace endstone::platform
} // namespace endstone::detail::os
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#include "endstone/plugin/plugin_loader.h"

namespace endstone {
namespace detail {

class CppPluginLoader : public PluginLoader {
public:
using PluginLoader::PluginLoader;
Expand All @@ -31,3 +34,6 @@ class CppPluginLoader : public PluginLoader {
private:
std::vector<std::unique_ptr<Plugin>> plugins_;
};

} // namespace detail
} // namespace endstone
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "endstone/plugin/plugin_manager.h"
#include "endstone/server.h"

namespace endstone::detail {

class EndstonePluginManager : public PluginManager {
public:
explicit EndstonePluginManager(Server &server);
Expand All @@ -44,3 +46,5 @@ class EndstonePluginManager : public PluginManager {
std::vector<Plugin *> plugins_;
std::unordered_map<std::string, Plugin *> lookup_names_;
};

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "endstone/plugin/plugin_loader.h"

namespace endstone::detail {

class PythonPluginLoader : public PluginLoader {
public:
explicit PythonPluginLoader(Server &server);
Expand All @@ -35,3 +37,5 @@ class PythonPluginLoader : public PluginLoader {
pybind11::object obj_;
pybind11::gil_scoped_release release_;
};

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

#include <pybind11/pybind11.h>

namespace py = pybind11;
namespace endstone::detail {

void def_color_format(py::module &m);
void def_logger(py::module &m);
void def_server(py::module &m);
void def_plugin(py::module &m);
void def_plugin_description(py::module &m);
void def_plugin_loader(py::module &m);
void def_plugin_manager(py::module &m);
void def_color_format(pybind11::module &m);
void def_logger(pybind11::module &m);
void def_server(pybind11::module &m);
void def_plugin(pybind11::module &m);
void def_plugin_description(pybind11::module &m);
void def_plugin_loader(pybind11::module &m);
void def_plugin_manager(pybind11::module &m);

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
#include <spdlog/pattern_formatter.h>
#include <spdlog/spdlog.h>

namespace endstone::detail {

class BedrockLevelFormatter : public spdlog::custom_flag_formatter {
public:
void format(const spdlog::details::log_msg &msg, const std::tm &, spdlog::memory_buf_t &dest) override;

[[nodiscard]] std::unique_ptr<custom_flag_formatter> clone() const override;
};

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <spdlog/sinks/base_sink.h>
#include <spdlog/spdlog.h>

namespace endstone::detail {

class BedrockLogSink : public spdlog::sinks::base_sink<spdlog::details::console_mutex::mutex_t> {
public:
explicit BedrockLogSink(FILE *target_file, spdlog::color_mode mode = spdlog::color_mode::automatic);
Expand Down Expand Up @@ -76,3 +78,5 @@ class BedrockLogSink : public spdlog::sinks::base_sink<spdlog::details::console_
bool should_do_colors_;
std::array<std::string, spdlog::level::n_levels> colors_;
};

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "endstone/util/color_format.h"

namespace endstone::detail {

class BedrockTextFormatter : public spdlog::custom_flag_formatter {
public:
explicit BedrockTextFormatter(bool should_do_colors) : should_do_colors_(should_do_colors){};
Expand Down Expand Up @@ -67,3 +69,5 @@ class BedrockTextFormatter : public spdlog::custom_flag_formatter {

bool should_do_colors_;
};

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "endstone/logger.h"

namespace endstone::detail {

class SpdLogAdapter : public Logger {
public:
explicit SpdLogAdapter(std::shared_ptr<spdlog::logger> logger);
Expand All @@ -29,3 +31,5 @@ class SpdLogAdapter : public Logger {
private:
std::shared_ptr<spdlog::logger> logger_;
};

} // namespace endstone::detail
4 changes: 4 additions & 0 deletions include/endstone/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <fmt/format.h>

namespace endstone {

class Logger {
public:
/**
Expand Down Expand Up @@ -84,3 +86,5 @@ class Logger {
log(Level::Critical, fmt::format(format, std::forward<Args>(args)...));
}
};

} // namespace endstone
3 changes: 3 additions & 0 deletions include/endstone/plugin/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "endstone/plugin/plugin_description.h"
#include "endstone/server.h"

namespace endstone {

class PluginLoader;

class Plugin {
Expand Down Expand Up @@ -123,6 +125,7 @@ class Plugin {
Server *server_ = nullptr;
Logger *logger_ = nullptr;
};
} // namespace endstone

#ifndef ENDSTONE_PLUGIN
#if defined(WIN32)
Expand Down
4 changes: 4 additions & 0 deletions include/endstone/plugin/plugin_description.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <fmt/format.h>

namespace endstone {

class PluginDescription {
public:
PluginDescription(std::string name, std::string version, std::optional<std::string> description = std::nullopt,
Expand Down Expand Up @@ -79,3 +81,5 @@ class PluginDescription {
std::optional<std::vector<std::string>> authors_;
std::optional<std::string> prefix_;
};

} // namespace endstone
4 changes: 4 additions & 0 deletions include/endstone/plugin/plugin_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "endstone/plugin/plugin.h"
#include "endstone/server.h"

namespace endstone {

class PluginLoader {
public:
explicit PluginLoader(Server &server) noexcept : server_(server) {}
Expand Down Expand Up @@ -63,3 +65,5 @@ class PluginLoader {
private:
Server &server_;
};

} // namespace endstone
4 changes: 4 additions & 0 deletions include/endstone/plugin/plugin_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "endstone/plugin/plugin.h"
#include "endstone/plugin/plugin_loader.h"

namespace endstone {

class PluginManager {
public:
PluginManager() = default;
Expand All @@ -38,3 +40,5 @@ class PluginManager {
virtual void disablePlugins() const = 0;
virtual void clearPlugins() = 0;
};

} // namespace endstone
4 changes: 4 additions & 0 deletions include/endstone/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "endstone/logger.h"

namespace endstone {

class PluginManager;

/**
Expand Down Expand Up @@ -61,3 +63,5 @@ class Server {
*/
[[nodiscard]] virtual std::string getMinecraftVersion() const = 0;
};

} // namespace endstone
4 changes: 4 additions & 0 deletions include/endstone/util/color_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <string>

namespace endstone {

struct ColorFormat {
// Escape
inline static const std::string ESCAPE = "§";
Expand Down Expand Up @@ -55,3 +57,5 @@ struct ColorFormat {
inline static const std::string ITALIC = ESCAPE + 'o';
inline static const std::string RESET = ESCAPE + 'r';
};

} // namespace endstone
10 changes: 7 additions & 3 deletions src/endstone_core/endstone_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone_core/endstone_server.h"
#include "endstone/detail/endstone_server.h"

#include <filesystem>
#include <memory>
namespace fs = std::filesystem;

#include "bedrock/common.h"
#include "endstone_core/logger_factory.h"
#include "endstone_core/plugin/cpp_plugin_loader.h"
#include "endstone/detail/logger_factory.h"
#include "endstone/detail/plugin/cpp_plugin_loader.h"

#if !defined(ENDSTONE_VERSION)
#error ENDSTONE_VERSION is not defined
#endif

namespace endstone::detail {

EndstoneServer::EndstoneServer() : logger_(LoggerFactory::getLogger("EndstoneServer"))
{
plugin_manager_ = std::make_unique<EndstonePluginManager>(*this);
Expand Down Expand Up @@ -87,3 +89,5 @@ std::string EndstoneServer::getMinecraftVersion() const
{
return Common::getGameVersionString();
}

} // namespace endstone::detail
10 changes: 7 additions & 3 deletions src/endstone_core/logger_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone_core/logger_factory.h"
#include "endstone/detail/logger_factory.h"

#include <mutex>
#include <string>
#include <unordered_map>

#include "endstone_core/spdlog/bedrock_log_sink.h"
#include "endstone_core/spdlog/spdlog_adapter.h"
#include "endstone/detail/spdlog/bedrock_log_sink.h"
#include "endstone/detail/spdlog/spdlog_adapter.h"

namespace endstone::detail {

Logger &LoggerFactory::getLogger(const std::string &name)
{
Expand All @@ -38,3 +40,5 @@ Logger &LoggerFactory::getLogger(const std::string &name)
it = loggers.emplace(name, SpdLogAdapter(console)).first;
return it->second;
}

} // namespace endstone::detail
Loading

0 comments on commit 3e1a3a7

Please sign in to comment.