Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
303 changes: 303 additions & 0 deletions .output.txt

Large diffs are not rendered by default.

553 changes: 276 additions & 277 deletions CMakeLists.txt

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/ApplicationContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "ApplicationContext.h"

#include "common/serialization-definitions.h"
#include "common/LoggerUtil.h"

#include "engine/EngineContext.h"
#include "core/glfw/GLFWContext.h"

namespace Metal {
ApplicationContext::ApplicationContext(bool debugMode) : debugMode(debugMode) {
}

void ApplicationContext::onInitialize() {
for (auto &instance : instances) {
instance->setDependencies(*this);
}

for (auto &instance : instances) {
auto *init = dynamic_cast<IInit *>(instance.get());
if (init) {
init->onInitialize();
}
}
}

bool ApplicationContext::isDebugMode() const {
return debugMode;
}

void ApplicationContext::dispose() {
try {
for (auto it = instances.rbegin(); it != instances.rend(); ++it) {
auto *disposable = dynamic_cast<IDisposable *>(it->get());
if (disposable) {
disposable->dispose();
}
}
} catch (std::exception &e) {
LOG_ERROR(e.what());
}
}
}
89 changes: 89 additions & 0 deletions src/ApplicationContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef METAL_ENGINE_APPLICATIONCONTEXT_H
#define METAL_ENGINE_APPLICATIONCONTEXT_H
#include <string>
#include <memory>
#include <unordered_map>
#include <typeindex>
#include <stdexcept>
#include <filesystem>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>

#include "common/IInit.h"
#include "common/IContextMember.h"
#include "common/IDisposable.h"

#define ENGINE_NAME "Metal Engine"

namespace Metal {
namespace fs = std::filesystem;

class ApplicationContext : public IContextMember, public IInit, public IDisposable {
std::unordered_map<std::string, IContextMember *> singletons;
std::vector<std::shared_ptr<IContextMember> > instances;
bool debugMode;

template<typename T>
static std::string getTypeName() {
std::string name = typeid(T).name();
size_t lastSpace = name.find_last_of(' ');
if (lastSpace != std::string::npos) {
name = name.substr(lastSpace + 1);
}
size_t lastColon = name.find_last_of(':');
if (lastColon != std::string::npos) {
name = name.substr(lastColon + 1);
}
std::transform(name.begin(), name.end(), name.begin(),
[](unsigned char c) { return std::tolower(c); });
return name;
}

public:
explicit ApplicationContext(bool debugMode);

void onInitialize() override;

template<typename T>
void registerSingleton(std::shared_ptr<T> instance) {
static_assert(std::is_base_of_v<IContextMember, T>, "T must derive from IContextMember");
T *ptr = instance.get();
singletons[getTypeName<T>()] = static_cast<IContextMember *>(ptr);
instances.push_back(std::move(instance));
}

template<typename T>
T &getSingleton() {
auto name = getTypeName<T>();
auto it = singletons.find(name);
if (it == singletons.end()) {
throw std::runtime_error(std::string("Singleton not registered: ") + name);
}
return *static_cast<T *>(it->second);
}

void *getSingletonByName(const std::string &name) {
std::string lowerName = name;
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(),
[](unsigned char c) { return std::tolower(c); });
auto it = singletons.find(lowerName);
if (it == singletons.end()) {
throw std::runtime_error(std::string("Singleton not registered: ") + lowerName);
}
return it->second;
}

void injectDependencies(IContextMember *member) {
std::cout << "Injecting dependencies for: " << typeid(*member).name() << std::endl;
member->setDependencies(*this);
}

[[nodiscard]] bool isDebugMode() const;

void dispose() override;
};
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
#include <string>
#include <vector>

#include "../../dto/file/ImportSettingsDTO.h"
#include "../../common/AbstractRuntimeComponent.h"
#include "../editor/dto/ImportSettingsDTO.h"
#include "IService.h"

namespace Metal {
class AbstractImporter : public AbstractRuntimeComponent {
class AbstractImporter : public IService {
public:
virtual std::vector<std::string> getSupportedTypes() {
return {};
}

explicit AbstractImporter()
: AbstractRuntimeComponent() {
}

virtual std::string importData(const std::string &targetDir, const std::string &pathToFile,
const std::shared_ptr<ImportSettingsDTO> &settings,
const std::stop_token &stopToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <string>
#include <unordered_map>

#include "../../common/AbstractRuntimeComponent.h"
#include "../../repository/abstract/RuntimeResource.h"
#include "IService.h"
#include "../engine/resource/RuntimeResource.h"

namespace Metal {
class VulkanContext;

template<typename T>
class AbstractResourceService : public AbstractRuntimeComponent {
class AbstractResourceService : public IService {
static_assert(std::is_base_of_v<RuntimeResource, T>, "T must be a subclass of RuntimeResource");

protected:
Expand Down
17 changes: 0 additions & 17 deletions src/common/AbstractRuntimeComponent.h

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions src/util/FileDialogUtil.h → src/common/FileDialogUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <string>
#include <vector>
#include <nfd.hpp>
#include "../service/log/LogService.h"
#include "../context/ApplicationContext.h"
#include "LoggerUtil.h"
#include "../ApplicationContext.h"

namespace Metal:: FileDialogUtil {
static std::vector<std::string> PickFiles(std::vector<nfdu8filteritem_t> filtersToApply) {
Expand Down
2 changes: 1 addition & 1 deletion src/util/FilesUtil.h → src/common/FilesUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define FILESUTIL_H
#include <filesystem>
#include <format>
#include "../dto/file/FSEntry.h"
#include "../editor/dto/FSEntry.h"

namespace fs = std::filesystem;

Expand Down
11 changes: 11 additions & 0 deletions src/common/IContextMember.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "IContextMember.h"
#include "../ApplicationContext.h"

namespace Metal {
void IContextMember::setDependencies(ApplicationContext &ctx) {
this->ctx = &ctx;
for (auto &dep : getDependencies()) {
dep.ptr = ctx.getSingletonByName(dep.name);
}
}
}
34 changes: 34 additions & 0 deletions src/common/IContextMember.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef METAL_ENGINE_CONTEXTMEMBER_H
#define METAL_ENGINE_CONTEXTMEMBER_H
#include <vector>
#include <string>

namespace Metal {
class ApplicationContext;

struct Dependency {
std::string name;
void *ptr;
};

class IContextMember {
public:
ApplicationContext *ctx = nullptr;
public:
virtual ~IContextMember() = default;

/**
* Dependency injection. Called after all the context is created and
* will be the data used to initialize the pointers
*/
virtual std::vector<Dependency> getDependencies() {
return {};
}

/**
* Will call getDependencies and set the pointers based on the singletons returned
*/
void setDependencies(ApplicationContext &ctx);
};
}
#endif //METAL_ENGINE_CONTEXTMEMBER_H
10 changes: 10 additions & 0 deletions src/common/IDisposable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef METAL_ENGINE_IDISPOSABLE_H
#define METAL_ENGINE_IDISPOSABLE_H
namespace Metal {
class IDisposable {
public:
virtual ~IDisposable() = default;
virtual void dispose() = 0;
};
}
#endif
4 changes: 2 additions & 2 deletions src/common/interface/Initializable.h → src/common/IInit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define METAL_ENGINE_INITIALIZABLE_H

namespace Metal {
class Initializable {
class IInit {
public:
virtual ~Initializable() = default;
virtual ~IInit() = default;

virtual void onInitialize() {}
};
Expand Down
13 changes: 13 additions & 0 deletions src/common/IRepository.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef METAL_ENGINE_IREPOSITORY_H
#define METAL_ENGINE_IREPOSITORY_H
#include "IContextMember.h"
#include "Inspectable.h"
#include "ISerialize.h"

namespace Metal {
class IRepository : public IContextMember, public Inspectable, public ISerialize {
public:
virtual ~IRepository() = default;
};
}
#endif //METAL_ENGINE_IREPOSITORY_H
10 changes: 5 additions & 5 deletions src/util/Serializable.cpp → src/common/ISerialize.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "Serializable.h"
#include "../context/ApplicationContext.h"
#include "ISerialize.h"
#include "../ApplicationContext.h"
#include <fstream>
#include <iostream>
#include <filesystem>
#include "../service/log/LogService.h"
#include "LoggerUtil.h"

namespace Metal {
void Serializable::saveToJson(const std::string &path) const {
void ISerialize::saveToJson(const std::string &path) const {
std::ofstream os(path);
if (os.is_open()) {
os << toJson().dump(4);
}
}

void Serializable::loadFromJson(const std::string &path) {
void ISerialize::loadFromJson(const std::string &path) {
if (std::filesystem::exists(path)) {
std::ifstream is(path);
if (is.is_open()) {
Expand Down
4 changes: 2 additions & 2 deletions src/util/Serializable.h → src/common/ISerialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <nlohmann/json.hpp>

namespace Metal {
class Serializable {
class ISerialize {
public:
virtual ~Serializable() = default;
virtual ~ISerialize() = default;

virtual nlohmann::json toJson() const = 0;

Expand Down
11 changes: 11 additions & 0 deletions src/common/IService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef METAL_ENGINE_ISERVICE_H
#define METAL_ENGINE_ISERVICE_H
#include "IContextMember.h"

namespace Metal {
class IService : public IContextMember {
public:
virtual ~IService() = default;
};
}
#endif //METAL_ENGINE_ISERVICE_H
4 changes: 2 additions & 2 deletions src/common/interface/Synchornizable.h → src/common/ISync.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define METAL_ENGINE_SYNCHORNIZABLE_H

namespace Metal {
class Syncronizable {
class ISync {
public:
virtual ~Syncronizable() = default;
virtual ~ISync() = default;

virtual void onSync() {}
};
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include "InspectedField.h"
#include "InspectedMethod.h"
#include "../../util/Util.h"
#include "../../enum/EntryType.h"
#include "../editor/util/Util.h"
#include "../editor/enum/EntryType.h"

#define DECLARATION(T, V) \
std::shared_ptr<InspectedField<T>> field = std::make_shared<InspectedField<T>>(&v);\
Expand Down
Loading