Skip to content

Commit

Permalink
paths fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 25, 2023
1 parent 19650f9 commit a842296
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
}

auto resdir = paths->getResources();
scripting::initialize();
scripting::initialize(paths);

std::cout << "-- loading assets" << std::endl;
std::vector<fs::path> roots {resdir};
Expand Down
13 changes: 9 additions & 4 deletions src/logic/scripting/scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
#include <stdexcept>
#include <lua.hpp>

#include "../../files/engine_paths.h"
#include "../../files/files.h"
#include "../../util/timeutil.h"
#include "../../world/Level.h"
#include "../../voxels/Block.h"

#include "api_lua.h"

using namespace scripting;

namespace scripting {
extern lua_State* L;
extern EnginePaths* paths;
}

lua_State* scripting::L = nullptr;
Level* scripting::level = nullptr;
const Content* scripting::content = nullptr;
EnginePaths* scripting::paths = nullptr;

void delete_global(lua_State* L, const char* name) {
lua_pushnil(L);
Expand All @@ -44,7 +46,9 @@ void call_func(lua_State* L, int argc, const std::string& name) {
}
}

void scripting::initialize() {
void scripting::initialize(EnginePaths* paths) {
scripting::paths = paths;

L = luaL_newstate();
if (L == nullptr) {
throw std::runtime_error("could not to initialize Lua");
Expand All @@ -66,8 +70,9 @@ void scripting::on_world_load(Level* level) {
scripting::level = level;
scripting::content = level->content;

std::string src = files::read_string(fs::path("res/scripts/world.lua"));
luaL_loadbuffer(L, src.c_str(), src.length(), "res/scripts/world.lua");
fs::path file = paths->getResources()/fs::path("scripts/world.lua");
std::string src = files::read_string(file);
luaL_loadbuffer(L, src.c_str(), src.length(), file.string().c_str());
call_func(L, 0, "<script>");
}

Expand Down
3 changes: 2 additions & 1 deletion src/logic/scripting/scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace fs = std::filesystem;

class EnginePaths;
class Content;
class Level;
class Block;
Expand All @@ -13,7 +14,7 @@ namespace scripting {
extern const Content* content;
extern Level* level;

void initialize();
void initialize(EnginePaths* paths);
void on_world_load(Level* level);
void on_world_quit();
void update_block(const Block* block, int x, int y, int z);
Expand Down

0 comments on commit a842296

Please sign in to comment.