Skip to content

Commit

Permalink
refactor: code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Feb 5, 2024
1 parent 2d8c3d6 commit 06a35ff
Show file tree
Hide file tree
Showing 50 changed files with 220 additions and 75 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# ================
# endstone::python
# ================
file(GLOB_RECURSE ENDSTONE_PYTHON_SOURCE_FILES CONFIGURE_DEPENDS "src/endstone_python/*.cpp")
file(GLOB_RECURSE ENDSTONE_PYTHON_SOURCE_FILES CONFIGURE_DEPENDS "src/endstone/python/*.cpp")
pybind11_add_module(endstone_python MODULE ${ENDSTONE_PYTHON_SOURCE_FILES})
target_include_directories(endstone_python PUBLIC include)
target_link_libraries(endstone_python PRIVATE endstone::headers)
Expand All @@ -59,7 +59,7 @@ install(TARGETS endstone_python DESTINATION "endstone/_internal/" COMPONENT ends
# ==============
# endstone::core
# ==============
file(GLOB_RECURSE ENDSTONE_CORE_SOURCE_FILES CONFIGURE_DEPENDS "src/endstone_core/*.cpp")
file(GLOB_RECURSE ENDSTONE_CORE_SOURCE_FILES CONFIGURE_DEPENDS "src/endstone/core/*.cpp")
add_library(endstone_core ${ENDSTONE_CORE_SOURCE_FILES})
add_library(endstone::core ALIAS endstone_core)
target_link_libraries(endstone_core PUBLIC endstone::headers spdlog::spdlog)
Expand All @@ -78,7 +78,7 @@ install(TARGETS endstone_core
# =================
# endstone::runtime
# =================
file(GLOB_RECURSE ENDSTONE_RUNTIME_SOURCE_FILES CONFIGURE_DEPENDS "src/endstone_runtime/*.cpp")
file(GLOB_RECURSE ENDSTONE_RUNTIME_SOURCE_FILES CONFIGURE_DEPENDS "src/endstone/runtime/*.cpp")
add_library(endstone_runtime SHARED ${ENDSTONE_RUNTIME_SOURCE_FILES})
add_library(endstone::runtime ALIAS endstone_runtime)
target_link_libraries(endstone_runtime PRIVATE endstone::core funchook::funchook magic_enum::magic_enum pybind11::embed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#include <string>
#include <string_view>



#include "endstone/core/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
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 @@ -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 @@ -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
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
File renamed without changes.
File renamed without changes.
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
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
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/core/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/core/logger_factory.h"
#include "endstone/core/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
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/core/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/core/spdlog/bedrock_log_sink.h"
#include "endstone/core/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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone_core/plugin/cpp_plugin_loader.h"
#include "endstone/core/plugin/cpp_plugin_loader.h"

#ifdef _WIN32
#include <Windows.h>
Expand All @@ -23,9 +23,11 @@
#include <filesystem>
namespace fs = std::filesystem;

#include "endstone/core/endstone_server.h"
#include "endstone/core/logger_factory.h"
#include "endstone/plugin/plugin.h"
#include "endstone_core/endstone_server.h"
#include "endstone_core/logger_factory.h"

namespace endstone::detail {

std::vector<Plugin *> CppPluginLoader::loadPlugins(const std::string &directory) noexcept
{
Expand Down Expand Up @@ -153,3 +155,5 @@ std::vector<std::string> CppPluginLoader::getPluginFileFilters() const
}

#endif

} // namespace endstone::detail
Loading

0 comments on commit 06a35ff

Please sign in to comment.