Initial commit
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
|
||||
# Include directories
|
||||
!/docs
|
||||
!/include
|
||||
!/src
|
||||
|
||||
# Include files
|
||||
!/.gitignore
|
||||
!/CMakeLists.txt
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
cmake_minimum_required(VERSION 3.24)
|
||||
project(magic_scrolls)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
file(GLOB_RECURSE sources
|
||||
"${CMAKE_SOURCE_DIR}/src/*.cpp")
|
||||
|
||||
add_executable(magic_scrolls ${sources})
|
||||
|
||||
target_include_directories(magic_scrolls PUBLIC "${CMAKE_SOURCE_DIR}/include")
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
# Сборка проекта
|
||||
|
||||
Для сборки нужно иметь CMake минимум `3.24`, make и, соответственно, компилятор C (подойдёт как Clang, так и GCC).
|
||||
|
||||
```bash
|
||||
cmake -B build
|
||||
cd build
|
||||
make
|
||||
```
|
||||
|
||||
В результате сборки получится исполняемый файл `magic_scrolls`.
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# Класс `Engine`
|
||||
|
||||
Класс `Engine` является основной сущностью, которая управляет всем процессом игры.
|
||||
Для его инициализации нужен список подключаемых модулей, которые он провалидирует на совместимость, а затем запустит.
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# Модули движка
|
||||
|
||||
Движок Magic Scrolls строится на основе модульной архитектуры. По-умолчанию, без модулей, движок предоставляет:
|
||||
|
||||
- Событийную шину
|
||||
- Реестр модулей
|
||||
- Публичный API для модулей
|
||||
|
||||
Вся остальная логика, включая REST API, realtime коммуникации и игровые фичи реализуются через модули, которые могут общаться между собой посредством событийной шины.
|
||||
|
||||
Игровой модуль реализуется через свой основной класс, который наследуется от `MscEngineModule`. Сам модуль хранит о себе основную информацию в виде:
|
||||
|
||||
- Имени модуля, для записи в реестр и обращении;
|
||||
- Ревизии модуля, для проверки зависимостей;
|
||||
- Список зависимостей, описываемых как карта "название требуемого модуля" <-> "диапазон допустимых ревизий" (`MscRevisionRange`)
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef __MCS_ENGINE_HPP__
|
||||
#define __MCS_ENGINE_HPP__
|
||||
|
||||
#include "engine/core/engine_module.hpp"
|
||||
#include "engine/core/engine_module_api.hpp"
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
/// Magic Srolls engine core
|
||||
class MscEngine : public IMscEngineModuleAPI {
|
||||
private:
|
||||
/// Loaded modules list
|
||||
std::vector<std::shared_ptr<MscEngineModule>> _modules;
|
||||
|
||||
public:
|
||||
/// Actual version
|
||||
static uint16_t REV;
|
||||
|
||||
MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules);
|
||||
|
||||
UNIMPL getBus() const override;
|
||||
UNIMPL getLoadedModules() const override;
|
||||
};
|
||||
|
||||
#endif // __MCS_ENGINE_HPP__
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef __MCS_ENGINE_MODULE_HPP__
|
||||
#define __MCS_ENGINE_MODULE_HPP__
|
||||
|
||||
#include "engine/utils/rev_range.hpp"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
/// Engine module base class
|
||||
class MscEngineModule {
|
||||
public:
|
||||
virtual ~MscEngineModule() = default;
|
||||
|
||||
/// Get module name
|
||||
virtual std::string getName() const = 0;
|
||||
|
||||
/// Get module revision
|
||||
virtual uint16_t getRev() const = 0;
|
||||
|
||||
/// Module dependency list
|
||||
virtual const std::unordered_map<std::string, MscRevisionRange> &
|
||||
getDependencies() const = 0;
|
||||
|
||||
/// Module initialization phase
|
||||
virtual void onInitialize();
|
||||
};
|
||||
|
||||
#endif // __MCS_ENGINE_MODULE_HPP__
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef __MSC_ENGINE_MODULE_API_HPP__
|
||||
#define __MSC_ENGINE_MODULE_API_HPP__
|
||||
|
||||
typedef void UNIMPL;
|
||||
|
||||
class IMscEngineModuleAPI {
|
||||
public:
|
||||
virtual ~IMscEngineModuleAPI() = default;
|
||||
|
||||
/// Get access to engine event bus
|
||||
virtual UNIMPL getBus() const = 0;
|
||||
|
||||
/// Get access to loaded module list
|
||||
virtual UNIMPL getLoadedModules() const = 0;
|
||||
};
|
||||
|
||||
#endif // __MSC_ENGINE_MODULE_API_HPP__
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef __MSC_TEST_1_MODULE_HPP__
|
||||
#define __MSC_TEST_1_MODULE_HPP__
|
||||
|
||||
#include "engine/core/engine_module.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
class Test1Module : public MscEngineModule {
|
||||
public:
|
||||
~Test1Module() = default;
|
||||
|
||||
std::string getName() const override;
|
||||
uint16_t getRev() const override;
|
||||
const std::unordered_map<std::string, MscRevisionRange> &
|
||||
getDependencies() const override;
|
||||
|
||||
void onInitialize() override;
|
||||
};
|
||||
|
||||
#endif // __MSC_TEST_1_MODULE_HPP__
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef __MSC_REV_RANGE_HPP__
|
||||
#define __MSC_REV_RANGE_HPP__
|
||||
|
||||
#include <cstdint>
|
||||
struct MscRevisionRange {
|
||||
uint16_t minRevision;
|
||||
uint16_t maxRevision;
|
||||
|
||||
/// Check provided revision to be in range
|
||||
bool isSatisfied(uint16_t rev) const {
|
||||
return rev >= this->minRevision && rev <= this->maxRevision;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __MSC_REV_RANGE_HPP__
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <cstdint>
|
||||
#include <engine/core/engine.hpp>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
uint16_t MscEngine::REV = 0;
|
||||
|
||||
MscEngine::MscEngine(std::vector<std::shared_ptr<MscEngineModule>> modules) {
|
||||
std::unordered_map<std::string, uint16_t> registry;
|
||||
for (const auto &module : modules) {
|
||||
if (!module)
|
||||
continue;
|
||||
|
||||
auto [it, inserted] =
|
||||
registry.try_emplace(module->getName(), module->getRev());
|
||||
if (!inserted) {
|
||||
throw std::runtime_error("Duplicate module detected: " +
|
||||
module->getName());
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &module : modules) {
|
||||
if (!module)
|
||||
continue;
|
||||
|
||||
for (const auto &[dep_name, range] : module->getDependencies()) {
|
||||
auto it = registry.find(dep_name);
|
||||
if (it == registry.end()) {
|
||||
throw std::runtime_error("Module " + module->getName() +
|
||||
" requires missing module: " + dep_name);
|
||||
}
|
||||
|
||||
int current_rev = it->second;
|
||||
if (!range.isSatisfied(current_rev)) {
|
||||
throw std::runtime_error(
|
||||
"Module " + module->getName() + " requires " + dep_name +
|
||||
" in range [" + std::to_string(range.minRevision) + " <-> " +
|
||||
std::to_string(range.maxRevision) + "], but got rev" +
|
||||
std::to_string(current_rev));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_modules = std::move(modules);
|
||||
}
|
||||
|
||||
UNIMPL MscEngine::getBus() const { throw std::runtime_error("Unimplemented"); }
|
||||
|
||||
UNIMPL MscEngine::getLoadedModules() const {
|
||||
throw std::runtime_error("Unimplemented");
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "engine/modules/test1/test1.hpp"
|
||||
#include "engine/utils/rev_range.hpp"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
std::string Test1Module::getName() const { return "test1"; }
|
||||
|
||||
uint16_t Test1Module::getRev() const { return 1; }
|
||||
|
||||
const std::unordered_map<std::string, MscRevisionRange> &
|
||||
Test1Module::getDependencies() const {
|
||||
static const std::unordered_map<std::string, MscRevisionRange> deps{};
|
||||
return deps;
|
||||
}
|
||||
|
||||
void Test1Module::onInitialize() {}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "engine/modules/test1/test1.hpp"
|
||||
#include <cstdio>
|
||||
#include <engine/core/engine.hpp>
|
||||
#include <memory>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
auto engine =
|
||||
std::make_unique<MscEngine>(std::vector<std::shared_ptr<MscEngineModule>>{
|
||||
std::make_shared<Test1Module>()});
|
||||
|
||||
printf("Hello, CMake, %p!\n", &engine);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user