Skip to content

Commit

Permalink
refactor: code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 2, 2024
1 parent 9433096 commit 0491e70
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 128 deletions.
1 change: 0 additions & 1 deletion include/endstone/command/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include "endstone/command/command_map.h"
#include "endstone/command/command_sender.h"
#include "endstone/detail/typeid.h"

namespace endstone {
/**
Expand Down
8 changes: 4 additions & 4 deletions include/endstone/command/command_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ namespace endstone {
class CommandMap {
public:
virtual ~CommandMap() = default;
CommandMap(const CommandMap&) = delete;
CommandMap& operator=(const CommandMap&) = delete;
CommandMap(CommandMap&&) = default;
CommandMap& operator=(CommandMap&&) = default;
CommandMap(const CommandMap &) = delete;
CommandMap &operator=(const CommandMap &) = delete;
CommandMap(CommandMap &&) = default;
CommandMap &operator=(CommandMap &&) = default;

/**
* Registers a command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

#pragma once

// The ENDSTONE_NOINLINE macro is for function DEFINITIONS.
// FORWARD DECLARATIONS should never use this macro:
// https://stackoverflow.com/questions/9317473/forward-declaration-of-inline-functions
#if defined(ENDSTONE_NOINLINE_DISABLED)
#define ENDSTONE_NOINLINE inline
#elif defined(_MSC_VER)
#define ENDSTONE_NOINLINE __declspec(noinline) inline
#else
#define ENDSTONE_NOINLINE __attribute__((noinline)) inline
#endif
namespace endstone::detail {
class EndstoneCommandMap {};
} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <string>
#include <string_view>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace endstone::detail {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

namespace endstone::detail {

class BedrockLogSink : public spdlog::sinks::base_sink<spdlog::details::console_mutex::mutex_t> {
class LogSink : 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);
explicit LogSink(FILE *target_file, spdlog::color_mode mode = spdlog::color_mode::automatic);
void setColorMode(spdlog::color_mode mode);

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

namespace endstone::detail {

class BedrockTextFormatter : public spdlog::custom_flag_formatter {
class TextFormatter : public spdlog::custom_flag_formatter {
public:
explicit BedrockTextFormatter(bool should_do_colors) : should_do_colors_(should_do_colors){};
explicit TextFormatter(bool should_do_colors) : should_do_colors_(should_do_colors){};
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;

Expand Down
77 changes: 0 additions & 77 deletions include/endstone/detail/typeid.h

This file was deleted.

15 changes: 15 additions & 0 deletions src/endstone_core/command/command_map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone/detail/command/command_map.h"
4 changes: 2 additions & 2 deletions src/endstone_core/logger_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <string>
#include <unordered_map>

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

namespace endstone::detail {
Expand All @@ -34,7 +34,7 @@ Logger &LoggerFactory::getLogger(const std::string &name)
return it->second;
}

spdlog::sink_ptr console_sink = std::make_shared<BedrockLogSink>(stdout);
spdlog::sink_ptr console_sink = std::make_shared<LogSink>(stdout);
auto console = std::make_shared<spdlog::logger>(name, console_sink);
spdlog::register_logger(console);
it = loggers.emplace(name, SpdLogAdapter(console)).first;
Expand Down
2 changes: 1 addition & 1 deletion src/endstone_core/plugin/cpp_plugin_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <filesystem>
namespace fs = std::filesystem;

#include "endstone/detail/endstone_server.h"
#include "endstone/detail/logger_factory.h"
#include "endstone/detail/server.h"
#include "endstone/plugin/plugin.h"

namespace endstone::detail {
Expand Down
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/detail/plugin/endstone_plugin_manager.h"
#include "endstone/detail/plugin/plugin_manager.h"

#include <algorithm>
#include <memory>
Expand Down
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/detail/endstone_server.h"
#include "endstone/detail/server.h"

#include <filesystem>
#include <memory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone/detail/spdlog/bedrock_level_formatter.h"
#include "endstone/detail/spdlog/level_formatter.h"

#include <spdlog/details/fmt_helper.h>

namespace endstone::detail {

void BedrockLevelFormatter::format(const spdlog::details::log_msg &msg, const tm &, spdlog::memory_buf_t &dest)
void LevelFormatter::format(const spdlog::details::log_msg &msg, const tm &, spdlog::memory_buf_t &dest)
{
static const std::unordered_map<spdlog::level::level_enum, std::string_view> level_names = {
{spdlog::level::trace, "TRACE"}, {spdlog::level::debug, "DEBUG"}, {spdlog::level::info, "INFO"},
Expand All @@ -30,9 +30,9 @@ void BedrockLevelFormatter::format(const spdlog::details::log_msg &msg, const tm
spdlog::details::fmt_helper::append_string_view(level_name, dest);
}

std::unique_ptr<spdlog::custom_flag_formatter> BedrockLevelFormatter::clone() const
std::unique_ptr<spdlog::custom_flag_formatter> LevelFormatter::clone() const
{
return spdlog::details::make_unique<BedrockLevelFormatter>();
return spdlog::details::make_unique<LevelFormatter>();
}

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone/detail/spdlog/bedrock_log_sink.h"
#include "endstone/detail/spdlog/log_sink.h"

#include <spdlog/pattern_formatter.h>
#include <spdlog/sinks/base_sink-inl.h>

#include "endstone/detail/spdlog/bedrock_level_formatter.h"
#include "endstone/detail/spdlog/bedrock_text_formatter.h"
#include "endstone/detail/spdlog/level_formatter.h"
#include "endstone/detail/spdlog/text_formatter.h"

namespace endstone::detail {

BedrockLogSink::BedrockLogSink(FILE *target_file, spdlog::color_mode mode)
LogSink::LogSink(FILE *target_file, spdlog::color_mode mode)
: target_file_(target_file), spdlog::sinks::base_sink<spdlog::details::console_mutex::mutex_t>(
spdlog::details::make_unique<spdlog::pattern_formatter>())
{
setColorMode(mode);
auto *formatter = dynamic_cast<spdlog::pattern_formatter *>(formatter_.get());
formatter->add_flag<BedrockLevelFormatter>('L');
formatter->add_flag<BedrockTextFormatter>('v', should_do_colors_);
formatter->add_flag<LevelFormatter>('L');
formatter->add_flag<TextFormatter>('v', should_do_colors_);
formatter->set_pattern("%^[%Y-%m-%d %H:%M:%S.%e %L] [%n] %v%$");
colors_.at(spdlog::level::trace) = toString(white);
colors_.at(spdlog::level::debug) = toString(cyan);
Expand All @@ -40,7 +40,7 @@ BedrockLogSink::BedrockLogSink(FILE *target_file, spdlog::color_mode mode)
colors_.at(spdlog::level::off) = toString(reset);
}

void BedrockLogSink::setColorMode(spdlog::color_mode mode)
void LogSink::setColorMode(spdlog::color_mode mode)
{
switch (mode) {
case spdlog::color_mode::always:
Expand All @@ -55,12 +55,12 @@ void BedrockLogSink::setColorMode(spdlog::color_mode mode)
}
}

std::string BedrockLogSink::toString(const spdlog::string_view_t &sv)
std::string LogSink::toString(const spdlog::string_view_t &sv)
{
return {sv.data(), sv.size()};
}

void BedrockLogSink::sink_it_(const spdlog::details::log_msg &msg)
void LogSink::sink_it_(const spdlog::details::log_msg &msg)
{
msg.color_range_start = 0;
msg.color_range_end = 0;
Expand All @@ -83,17 +83,17 @@ void BedrockLogSink::sink_it_(const spdlog::details::log_msg &msg)
fflush(target_file_);
}

void BedrockLogSink::flush_()
void LogSink::flush_()
{
fflush(target_file_);
}

void BedrockLogSink::printColorCode(const spdlog::string_view_t &color_code)
void LogSink::printColorCode(const spdlog::string_view_t &color_code)
{
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
}

void BedrockLogSink::printRange(const spdlog::memory_buf_t &formatted, size_t start, size_t end)
void LogSink::printRange(const spdlog::memory_buf_t &formatted, size_t start, size_t end)
{
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone/detail//spdlog/bedrock_text_formatter.h"
#include "endstone/detail//spdlog/text_formatter.h"

#include <spdlog/details/fmt_helper.h>

namespace endstone::detail {

void BedrockTextFormatter::format(const spdlog::details::log_msg &msg, const tm &, spdlog::memory_buf_t &dest)
void TextFormatter::format(const spdlog::details::log_msg &msg, const tm &, spdlog::memory_buf_t &dest)
{

const auto &input = msg.payload;
Expand Down Expand Up @@ -47,9 +47,9 @@ void BedrockTextFormatter::format(const spdlog::details::log_msg &msg, const tm
}
}

std::unique_ptr<spdlog::custom_flag_formatter> BedrockTextFormatter::clone() const
std::unique_ptr<spdlog::custom_flag_formatter> TextFormatter::clone() const
{
return spdlog::details::make_unique<BedrockTextFormatter>(should_do_colors_);
return spdlog::details::make_unique<TextFormatter>(should_do_colors_);
}

} // namespace endstone::detail
2 changes: 1 addition & 1 deletion src/endstone_runtime/bedrock/dedicated_server.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "bedrock/server/dedicated_server.h"

#include "endstone/detail/endstone_server.h"
#include "endstone/detail/hook.h"
#include "endstone/detail/server.h"

DedicatedServer::StartResult DedicatedServer::runDedicatedServerLoop(Core::FilePathManager &file_path_manager,
PropertiesSettings &properties_settings,
Expand Down
2 changes: 1 addition & 1 deletion src/endstone_runtime/bedrock/server_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#include "bedrock/server/server_instance.h"

#include "endstone/detail/endstone_server.h"
#include "endstone/detail/hook.h"
#include "endstone/detail/server.h"

void ServerInstance::startServerThread()
{
Expand Down
2 changes: 1 addition & 1 deletion src/endstone_runtime/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include <spdlog/spdlog.h>

#include "endstone/detail/endstone_server.h"
#include "endstone/detail/hook.h"
#include "endstone/detail/plugin/python_plugin_loader.h"
#include "endstone/detail/server.h"

#if __GNUC__
#define ENDSTONE_RUNTIME_CTOR __attribute__((constructor))
Expand Down
2 changes: 1 addition & 1 deletion test_package/src/test_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <cassert>

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

using namespace endstone::detail;

Expand Down
Loading

0 comments on commit 0491e70

Please sign in to comment.