Skip to content

Commit

Permalink
- Update scripting manager to return the loaded mod
Browse files Browse the repository at this point in the history
- Add steamworks support on mod
  • Loading branch information
edunad committed Aug 3, 2024
1 parent 1c2c5dd commit 445de2d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions rawrbox.scripting/include/rawrbox/scripting/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ namespace rawrbox {
static void init(int hotReloadMs = 0);

// LOADING ----
static std::unordered_map<std::filesystem::path, bool> loadMods(const std::filesystem::path& rootFolder);
static bool loadMod(const std::string& id, const std::filesystem::path& modFolder);
static std::unordered_map<std::filesystem::path, rawrbox::Mod*> loadMods(const std::filesystem::path& rootFolder);
static rawrbox::Mod* loadMod(const std::string& id, const std::filesystem::path& modFolder);

static bool unloadMod(const std::filesystem::path& modFolder);
static bool unloadMod(const std::string& modId);
Expand Down
12 changes: 12 additions & 0 deletions rawrbox.scripting/include/rawrbox/scripting/mod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <filesystem>
#include <memory>

#ifdef RAWRBOX_STEAMWORKS
#include <steam/steam_api.h>
#endif

namespace rawrbox {
class Mod {
// LUA ----
Expand All @@ -24,6 +28,9 @@ namespace rawrbox {
// Settings ---
std::filesystem::path _folder;
std::string _id = "UNKNOWN";
#ifdef RAWRBOX_STEAMWORKS
PublishedFileId_t _workshopId = 0;
#endif
// -----------

// OTHER ----
Expand Down Expand Up @@ -57,6 +64,11 @@ namespace rawrbox {
[[nodiscard]] virtual const glz::json_t& getMetadata() const;

[[nodiscard]] virtual lua_State* getEnvironment();

#ifdef RAWRBOX_STEAMWORKS
virtual void setWorkshopId(PublishedFileId_t id);
[[nodiscard]] virtual PublishedFileId_t getWorkshopId() const;
#endif
// -----

template <typename... CallbackArgs>
Expand Down
14 changes: 8 additions & 6 deletions rawrbox.scripting/src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ namespace rawrbox {
}

// LOADING ----
std::unordered_map<std::filesystem::path, bool> SCRIPTING::loadMods(const std::filesystem::path& rootFolder) { // Load mods
std::unordered_map<std::filesystem::path, rawrbox::Mod*> SCRIPTING::loadMods(const std::filesystem::path& rootFolder) { // Load mods
if (!std::filesystem::exists(rootFolder)) throw _logger->error("Failed to locate root folder '{}'", rootFolder.generic_string());

std::unordered_map<std::filesystem::path, bool> success = {};
std::unordered_map<std::filesystem::path, rawrbox::Mod*> success = {};
for (const auto& p : std::filesystem::directory_iterator(rootFolder)) {
if (!p.is_directory()) continue;
success[p] = loadMod(p.path().filename().generic_string(), p);
Expand All @@ -358,12 +358,12 @@ namespace rawrbox {
return success;
}

bool SCRIPTING::loadMod(const std::string& id, const std::filesystem::path& modFolder) {
rawrbox::Mod* SCRIPTING::loadMod(const std::string& id, const std::filesystem::path& modFolder) {
if (id.empty()) throw _logger->error("Mod ID cannot be empty");
if (!std::filesystem::exists(modFolder)) throw _logger->error("Failed to locate mod folder '{}'", modFolder.generic_string());
if (_mods.find(id) != _mods.end()) {
_logger->warn("Mod {} already loaded! Mod name conflict?", id);
return false;
return nullptr;
}

// LOAD METADATA ---
Expand Down Expand Up @@ -396,11 +396,13 @@ namespace rawrbox {
registerLoadedFile(mod->getID(), mod->getEntryFilePath()); // Register file for hot-reloading
} catch (const std::runtime_error& err) {
_logger->printError("{}", err.what());
return false;
return nullptr;
}

rawrbox::Mod* modPtr = mod.get();
_mods.emplace(id, std::move(mod));
return true;

return modPtr;
}

bool SCRIPTING::unloadMod(const std::filesystem::path& modFolder) {
Expand Down
5 changes: 5 additions & 0 deletions rawrbox.scripting/src/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ namespace rawrbox {
const glz::json_t& Mod::getMetadata() const { return this->_metadata; }

lua_State* Mod::getEnvironment() { return this->_L; }

#ifdef RAWRBOX_STEAMWORKS
void Mod::setWorkshopId(PublishedFileId_t id) { this->_workshopId = id; }
PublishedFileId_t Mod::getWorkshopId() const { return this->_workshopId; }
#endif
// -----
} // namespace rawrbox

0 comments on commit 445de2d

Please sign in to comment.