Skip to content

Commit

Permalink
refactor: refactoring codes
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Oct 27, 2023
1 parent c1089d3 commit 631ba80
Show file tree
Hide file tree
Showing 39 changed files with 300 additions and 892 deletions.
64 changes: 44 additions & 20 deletions src/ll/api/LLAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,64 @@
#include "ll/core/Version.h"


using ll::StringUtils::str2wstr;
namespace ll {

namespace {
inline ushort stoi(std::string_view str) {
ushort ret = -1;
ushort ret = UINT16_MAX;
std::from_chars(str.data(), str.data() + str.size(), ret);
return ret;
}
} // namespace

std::string ll::getDataPath(std::string const& pluginName) {
bool registerPlugin(
std::string const& name,
std::string const& desc,
ll::Version const& version,
std::string const& git ,
std::string const& license ,
std::string const& website ,
HMODULE handle
) {
std::map<std::string, std::string> others;
if (!git.empty()) others.emplace("Git", git);
if (!license.empty()) others.emplace("License", license);
if (!website.empty()) others.emplace("Website", website);
return PluginManager::registerPlugin(handle, name, desc, version, others);
}

bool registerPlugin(
std::string const& name,
std::string const& desc,
ll::Version const& version,
std::map<std::string, std::string> const& others,
HMODULE handle
) {
return PluginManager::registerPlugin(handle, name, desc, version, others);
}

std::string getDataPath(std::string const& pluginName) {
std::string dataPath = "plugins\\LeviLamina\\" + pluginName;
if (!std::filesystem::exists(str2wstr(dataPath))) {
if (!std::filesystem::exists(StringUtils::str2wstr(dataPath))) {
std::error_code ec;
std::filesystem::create_directories(str2wstr(dataPath), ec);
std::filesystem::create_directories(StringUtils::str2wstr(dataPath), ec);
}
return dataPath;
}

std::string ll::getLoaderVersionString() { return getLoaderVersion().toString(); }
std::string getLoaderVersionString() { return getLoaderVersion().toString(); }

bool ll::isDebugMode() { return ll::globalConfig.debugMode; }
bool isDebugMode() { return globalConfig.debugMode; }

ll::Plugin* ll::getPlugin(std::string const& name) { return PluginManager::getPlugin(name); }
Plugin* getPlugin(std::string const& name) { return PluginManager::getPlugin(name); }

ll::Plugin* ll::getPlugin(HMODULE handle) { return PluginManager::getPlugin(handle); }
Plugin* getPlugin(HMODULE handle) { return PluginManager::getPlugin(handle); }

bool ll::hasPlugin(std::string const& name) { return PluginManager::hasPlugin(name); }
bool hasPlugin(std::string const& name) { return PluginManager::hasPlugin(name); }

std::unordered_map<std::string, ll::Plugin*> ll::getAllPlugins() { return PluginManager::getAllPlugins(); }
std::unordered_map<std::string, Plugin*> getAllPlugins() { return PluginManager::getAllPlugins(); }

HMODULE ll::getLoaderHandle() { return GetCurrentModule(); }

namespace ll {
HMODULE getLoaderHandle() { return GetCurrentModule(); }

// region ### Version ###
Version::Version(ushort major, ushort minor, ushort patch, PreRelease preRelease)
Expand Down Expand Up @@ -93,7 +117,7 @@ Version Version::parse(std::string const& str) {
}
}

auto res = ll::StringUtils::splitByPattern(&basic, ".");
auto res = StringUtils::splitByPattern(&basic, ".");

if (!res.empty()) result.mMajor = stoi(res[0]);
if (res.size() >= 2) result.mMinor = stoi(res[1]);
Expand All @@ -107,12 +131,12 @@ Version getLoaderVersion() {
return {LL_VERSION_MAJOR, LL_VERSION_MINOR, LL_VERSION_PATCH, (Version::PreRelease)LL_VERSION_PRE_RELEASE};
}

} // namespace ll
ServerStatus getServerStatus() { return globalRuntimeConfig.serverStatus; }

ll::ServerStatus ll::getServerStatus() { return ll::globalRuntimeConfig.serverStatus; }
bool isServerStarting() { return getServerStatus() == ServerStatus::Starting; }

bool ll::isServerStarting() { return getServerStatus() == ll::ServerStatus::Starting; }
bool isServerStopping() { return getServerStatus() == ServerStatus::Stopping; }

bool ll::isServerStopping() { return getServerStatus() == ll::ServerStatus::Stopping; }
std::string getLanguage() { return globalConfig.language; }

std::string ll::getLanguage() { return ll::globalConfig.language; }
} // namespace ll
35 changes: 8 additions & 27 deletions src/ll/api/LLAPI.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
#pragma once
#include <memory>
#pragma warning(disable : 26812)
#include <string>
#include <unordered_map>

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#ifndef NOMINMAX
#define NOMINMAX
#endif

#include "ll/api/base/Global.h"
#include "ll/api/utils/WinHelper.h"
#include "ll/core/PluginManager.h"

namespace ll {

Expand Down Expand Up @@ -136,20 +123,15 @@ LLNDAPI std::string getDataPath(std::string const& pluginName);
* @return bool True if the plugin is registered successfully
* @note The implementation of this function must be in header file(because of `GetCurrentModule`)
*/
inline bool registerPlugin(
LLAPI bool registerPlugin(
std::string const& name,
std::string const& desc,
ll::Version const& version,
std::string const& git = "",
std::string const& license = "",
std::string const& website = ""
) {
std::map<std::string, std::string> others;
if (!git.empty()) others.emplace("Git", git);
if (!license.empty()) others.emplace("License", license);
if (!website.empty()) others.emplace("Website", website);
return PluginManager::registerPlugin(GetCurrentModule(), name, desc, version, others);
}
std::string const& website = "",
HMODULE handle = GetCurrentModule()
);

/**
* @brief Register a plugin
Expand All @@ -166,14 +148,13 @@ inline bool registerPlugin(
* ll::registerPlugin("Test", "A test plugin", Version(0, 0, 1, Version::Alpha), {{"Note","This is Note"}});
* @endcode
*/
inline bool registerPlugin(
LLAPI bool registerPlugin(
std::string const& name,
std::string const& desc,
ll::Version const& version,
std::map<std::string, std::string> const& others
) {
return PluginManager::registerPlugin(GetCurrentModule(), name, desc, version, others);
}
std::map<std::string, std::string> const& others,
HMODULE handle = GetCurrentModule()
);

/**
* @brief Get a loaded plugin by name
Expand Down
12 changes: 9 additions & 3 deletions src/ll/api/base/Global.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#pragma once
#pragma warning(disable : 4245)
#pragma warning(disable : 4250)
#pragma warning(disable : 4949)

#pragma clang diagnostic ignored "-Wpragma-system-header-outside-header"

#include "ll/api/base/Macro.h"
#include "mc/_HeaderOutputPredefine.h"

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#ifndef NOMINMAX
#define NOMINMAX
#endif


// windows types
#include "minwindef.h"

Expand Down
6 changes: 3 additions & 3 deletions src/ll/api/command/DynamicCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ inline DynamicCommand::Result::Result(

inline DynamicCommand::Result::Result()
: type((ParameterType)-1),
offset(-1),
offset(UINT64_MAX),
command(nullptr),
origin(nullptr),
instance(nullptr),
Expand Down Expand Up @@ -728,7 +728,7 @@ inline std::string const& DynamicCommandInstance::getEnumValue(int index) const

ParameterIndex DynamicCommandInstance::newParameter(DynamicCommand::ParameterData&& data) {
auto iter = parameterPtrs.find(data.name);
size_t offset = -1;
size_t offset = UINT64_MAX;
if (iter == parameterPtrs.end()) {
offset = commandSize;
parameterPtrs.emplace(data.name, DynamicCommand::ParameterPtr(data.type, offset));
Expand Down Expand Up @@ -773,7 +773,7 @@ inline ParameterIndex DynamicCommandInstance::findParameterIndex(std::string con
break;
++index;
}
if (index == parameterDatas.size()) index = -1;
if (index == parameterDatas.size()) index = UINT64_MAX;
return {this, index};
}

Expand Down
4 changes: 2 additions & 2 deletions src/ll/api/command/DynamicCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class DynamicCommand : public Command {
ParameterType type;

private:
size_t offset = -1;
size_t offset = UINT64_MAX;

friend struct Result;

Expand All @@ -345,7 +345,7 @@ class DynamicCommand : public Command {
struct ParameterData {
protected:
DynamicCommand::ParameterType type;
size_t offset = -1;
size_t offset = UINT64_MAX;
std::string name;
std::string description;
std::string identifier;
Expand Down
16 changes: 8 additions & 8 deletions src/ll/api/memory/MemoryUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ auto constexpr virtualCall(void const* _this, Args&&... args) -> RTN {
}

template <typename T>
[[nodiscard]] constexpr T& dAccess(void* ptr, uintptr_t off) {
return *(T*)(((uintptr_t)ptr) + off);
[[nodiscard]] constexpr T& dAccess(void* ptr, intptr_t off) {
return *(T*)(((uintptr_t)ptr) + (uintptr_t)(off));
}

template <typename T>
[[nodiscard]] constexpr T const& dAccess(void const* ptr, uintptr_t off) {
return *(T*)(((uintptr_t)ptr) + off);
[[nodiscard]] constexpr T const& dAccess(void const* ptr, intptr_t off) {
return *(T*)(((uintptr_t)ptr) + (uintptr_t)off);
}

template <typename T>
constexpr void destruct(void* ptr, uintptr_t off) noexcept {
std::destroy_at(std::launder(reinterpret_cast<T*>(((uintptr_t)ptr) + off)));
constexpr void destruct(void* ptr, intptr_t off) noexcept {
std::destroy_at(std::launder(reinterpret_cast<T*>(((uintptr_t)ptr) + (uintptr_t)off)));
}

template <typename T, class... Types>
constexpr auto construct(void* ptr, uintptr_t off, Types&&... args) {
return std::construct_at(std::launder(reinterpret_cast<T*>(((uintptr_t)ptr) + off)), std::forward<Types>(args)...);
constexpr auto construct(void* ptr, intptr_t off, Types&&... args) {
return std::construct_at(std::launder(reinterpret_cast<T*>(((uintptr_t)ptr) + (uintptr_t)off)), std::forward<Types>(args)...);
}

[[nodiscard]] inline size_t getMemSizeFromPtr(void* ptr) {
Expand Down
Loading

0 comments on commit 631ba80

Please sign in to comment.