-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluapi.cpp
More file actions
145 lines (138 loc) · 2.96 KB
/
luapi.cpp
File metadata and controls
145 lines (138 loc) · 2.96 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "luapi.hpp"
extern std::mutex mtx;
extern Settings_t settings;
extern Event_t event;
//extern int CanRead, Waiting;
extern bool LuaRun, Terminate;
//extern std::string Message;
//extern std::vector<std::string> opts;
//extern int choice;
//extern std::string imgpath;
int LuaServer()
{
sol::state lua;
lua.open_libraries(
sol::lib::base,
sol::lib::io,
sol::lib::os,
sol::lib::math,
sol::lib::package
);
std::cout << "[LUA] ==Loading Settings==" << std::endl;
lua.script_file("settings.lua");
Settings_t temps;
temps.font = lua["Font"].get<std::string>();
temps.fontSize = lua["FontSize"].get<float>();
temps.shader = lua["Shader"].get<bool>();
mtx.lock();
settings = temps;
settings.loaded = true;
mtx.unlock();
std::cout << "[LUA] ==Starting Scripts=="<<std::endl;
lua["say"] = say;
lua["ask"] = ask;
lua["clr"] = clr;
lua["loadImg"] = loadImg;
lua.script_file("./index.lua");
while(lua["State"]["fin"] != true && !Terminate)
{
if(Terminate)
{
lua["State"]["fin"] = true;
}
else{
lua["NextState"]();
}
}
return 0;
}
bool ask(const sol::table& pos)
{
//std::cout << "Tried asking" << std::endl;
if(Terminate)
{
return false;
}
std::vector<sol::function> fs;
for(const auto& pair: pos)
{
if(pair.second.is<sol::table>())
{
sol::table subt = pair.second.as<sol::table>();
if(subt[1].is<std::string>())
{
//work with the string here
std::string s = subt[1].get<std::string>();
mtx.lock();
event.type = ASK;
event.msg = s;
event.data = 9;
mtx.unlock();
while(event.type == ASK){}
}
if(subt[2].is<sol::function>())
{
//work with the associated function
sol::function f = subt[2].get<sol::function>();
fs.push_back(f);
}
}
}
mtx.lock();
event.type = ASK;
event.msg = "";
event.data = 0;
mtx.unlock();
mtx.lock();
LuaRun = false;
event.type = WAIT;
mtx.unlock();
while(!LuaRun && !Terminate && event.type != RESPONSE)
{}
std::cout << "Validated by [LUA]" << std::endl;
int temp_data = event.data;
mtx.lock();
event.data = 0;
event.msg = "";
event.type = NOTHING;
mtx.unlock();
if(!Terminate)
fs[temp_data]();
//std::cout << "[C++] End of SAY" << std::endl;
return true;
}
void say(std::string msg, float s)
{
if(!Terminate){
while(event.type != NOTHING && !Terminate){}
mtx.lock();
event.type = SAY;
LuaRun = false;
event.msg = msg;
mtx.unlock();
while(event.type != NOTHING){}
mtx.lock();
event.msg = "";
event.data = (int)(s*10);
event.type = WAIT;
mtx.unlock();
while(!LuaRun && !Terminate){}
}
}
void clr()
{
mtx.lock();
event.type = CLR;
event.data = 0;
event.msg = "";
mtx.unlock();
}
void loadImg(std::string path)
{
if(Terminate){return;}
mtx.lock();
event.msg = path;
event.type = IMG;
event.data = 0;
mtx.unlock();
}