-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCLuaScript.cpp
More file actions
executable file
·63 lines (51 loc) · 1.36 KB
/
CLuaScript.cpp
File metadata and controls
executable file
·63 lines (51 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// CLuaScript.cpp
// LuaTest4
//
// Created by Didrik Munther on 16/10/15.
// Copyright (c) 2015 Didrik Munther. All rights reserved.
//
#include "CLuaScript.h"
#include "CEntity.h"
#include "NFile.h"
CLuaScript::CLuaScript(lua_State* L, std::string path)
: _L(L)
, _path(path)
, _objectCreationFunction(init(L, path))
{
std::string dir = _path.substr(_path.find("scripts/") + 1, _path.size() - _path.find("scripts/"));
dir = dir.substr(dir.find("/") + 1, dir.size() - dir.find("/"));
_name = dir.substr(0, dir.find("."));;
}
luabridge::LuaRef CLuaScript::init(lua_State* L, std::string path) {
_isInvalid = false;
luaL_dofile(L, path.c_str());
luabridge::LuaRef createFunc = luabridge::getGlobal(L, "create");
if(createFunc.isNil()) {
_isInvalid = true;
funcError();
}
return createFunc;
}
void CLuaScript::doFile() {
luaL_dofile(_L, _path.c_str());
}
void CLuaScript::funcError() {
NFile::log(LogType::ERROR, "No creation function for script \"", _path, "\"");
}
luabridge::LuaRef* CLuaScript::getObjectCreation() {
if(_isInvalid) {
funcError();
return nullptr;
}
return &_objectCreationFunction;
}
lua_State* CLuaScript::getState() {
return _L;
}
std::string CLuaScript::getPath() {
return _path;
}
std::string CLuaScript::getName() {
return _name;
}