-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathMain.cpp
More file actions
117 lines (97 loc) · 3.04 KB
/
Main.cpp
File metadata and controls
117 lines (97 loc) · 3.04 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
/*
Copyright 2015-2024 Clément Gallet <clement.gallet@ens-lyon.org>
This file is part of libTAS.
libTAS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libTAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libTAS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Main.h"
#include "LuaFunctionList.h"
#include "Gui.h"
#include "Input.h"
#include "Movie.h"
#include "Memory.h"
#include "Print.h"
#include "Runtime.h"
#include "Callbacks.h"
#include "Ramsearch.h"
#include <iostream>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
/* Lua state */
// static lua_State *lua_state = nullptr;
static Context* context;
void Lua::Main::init(Context* c)
{
context = c;
}
lua_State* Lua::Main::new_state()
{
lua_State *lua_state = luaL_newstate();
luaL_openlibs(lua_state);
/* Register our functions */
Lua::Gui::registerFunctions(lua_state);
Lua::Input::registerFunctions(lua_state);
Lua::Memory::registerFunctions(lua_state);
Lua::Movie::registerFunctions(lua_state, context);
Lua::Callbacks::registerFunctions(lua_state);
Lua::Print::init(lua_state);
Lua::Runtime::registerFunctions(lua_state, context);
Lua::Ramsearch::registerFunctions(lua_state, context);
return lua_state;
}
void Lua::Main::exit()
{
Lua::Callbacks::clear();
}
std::string luaFile;
int Lua::Main::run(lua_State* lua_state, std::string filename)
{
luaFile = filename;
int status = luaL_dofile(lua_state, filename.c_str());
if (status != 0) {
std::cerr << "Error " << status << " loading lua script " << filename << std::endl;
std::cerr << lua_tostring(lua_state, -1) << std::endl;
lua_pop(lua_state, 1);
return -1;
}
else {
std::cout << "Loaded script " << filename << std::endl;
}
/* Push old-style callback methods into new-style */
lua_getglobal(lua_state, "onStartup");
if (lua_isfunction(lua_state, -1))
Lua::Callbacks::onStartup(lua_state);
else
lua_pop(lua_state, 1);
lua_getglobal(lua_state, "onInput");
if (lua_isfunction(lua_state, -1))
Lua::Callbacks::onInput(lua_state);
else
lua_pop(lua_state, 1);
lua_getglobal(lua_state, "onFrame");
if (lua_isfunction(lua_state, -1))
Lua::Callbacks::onFrame(lua_state);
else
lua_pop(lua_state, 1);
lua_getglobal(lua_state, "onPaint");
if (lua_isfunction(lua_state, -1))
Lua::Callbacks::onPaint(lua_state);
else
lua_pop(lua_state, 1);
return 0;
}
const std::string& Lua::Main::currentFile()
{
return luaFile;
}