script.cpp 28.1 KB
Newer Older
RobLoach's avatar
RobLoach committed
1
#include "script.h"
RobLoach's avatar
RobLoach committed
2
#include "../ChaiLove.h"
RobLoach's avatar
RobLoach committed
3
#include <filesystem/path.h>
4
#include <algorithm>
RobLoach's avatar
RobLoach committed
5

6
#ifdef __HAVE_CHAISCRIPT__
RobLoach's avatar
RobLoach committed
7
#include "chaiscript/extras/math.hpp"
RobLoach's avatar
RobLoach committed
8
using namespace chaiscript;
RobLoach's avatar
RobLoach committed
9
#endif
RobLoach's avatar
RobLoach committed
10

RobLoach's avatar
RobLoach committed
11
using ::ChaiLove;
RobLoach's avatar
RobLoach committed
12
13
14
15
16
17
using love::Types::Graphics::Point;
using love::Types::Graphics::Image;
using love::Types::Graphics::Font;
using love::Types::Graphics::Point;
using love::Types::Graphics::Color;
using love::Types::Input::Joystick;
RobLoach's avatar
RobLoach committed
18
19
using love::Types::Config::WindowConfig;
using love::Types::Config::ModuleConfig;
RobLoach's avatar
RobLoach committed
20
using love::Types::Audio::SoundData;
21
using love::Types::FileSystem::FileData;
RobLoach's avatar
RobLoach committed
22
using love::graphics;
RobLoach's avatar
RobLoach committed
23

RobLoach's avatar
RobLoach committed
24
namespace love {
RobLoach's avatar
RobLoach committed
25

RobLoach's avatar
RobLoach committed
26
27
28
bool script::loadModule(const std::string& moduleName) {
	#ifdef __HAVE_CHAISCRIPT__
	ChaiLove* app = ChaiLove::getInstance();
RobLoach's avatar
RobLoach committed
29

RobLoach's avatar
RobLoach committed
30
31
	// Store a filename for the module.
	std::string filename = moduleName;
RobLoach's avatar
RobLoach committed
32

RobLoach's avatar
RobLoach committed
33
34
35
36
37
38
	// Make sure it exists.
	if (!app->filesystem.exists(filename)) {
		// See if we are to append .chai.
		filename = filename + ".chai";
		if (!app->filesystem.exists(filename)) {
			std::cout << "[ChaiLove] [script] Module " << moduleName << " not found." << std::endl;
RobLoach's avatar
RobLoach committed
39
			return false;
RobLoach's avatar
Updates    
RobLoach committed
40
		}
RobLoach's avatar
RobLoach committed
41
	}
RobLoach's avatar
RobLoach committed
42

RobLoach's avatar
RobLoach committed
43
44
	// Load the contents of the file.
	std::string contents = app->filesystem.read(filename);
RobLoach's avatar
RobLoach committed
45

RobLoach's avatar
RobLoach committed
46
47
48
	// Make sure it was not empty.
	if (contents.empty()) {
		std::cout << "[ChaiLove] [script] Module " << moduleName << " was loaded, but empty." << std::endl;
RobLoach's avatar
RobLoach committed
49
		return false;
RobLoach's avatar
RobLoach committed
50
51
	}

RobLoach's avatar
RobLoach committed
52
	eval(contents, filename);
RobLoach's avatar
RobLoach committed
53
54
55
56
57
58
	return true;

	#endif
	return false;
}

RobLoach's avatar
RobLoach committed
59
bool script::loadModuleRequire(const std::string& moduleName) {
60
61
62
63
64
65
66
67
68
69
70
71
	// Check if the module has already been loaded.
	std::string filename = replaceString(moduleName, ".", "/");
	if (std::find(m_requiremodules.begin(), m_requiremodules.end(), filename) != m_requiremodules.end()) {
		return true;
	}

	// Attempt to load the module.
	bool loaded = loadModule(filename);
	if (loaded) {
		m_requiremodules.push_back(filename);
	}
	return loaded;
RobLoach's avatar
RobLoach committed
72
73
}

RobLoach's avatar
RobLoach committed
74
75
76
77
78
chaiscript::Boxed_Value script::eval(const std::string& code, const std::string& filename) {
	// Replace possible problematic tabs, and evaluate the script.
	std::string contents = replaceString(code, "\t", "  ");
	return chai.eval(contents, Exception_Handler(), filename);
}
RobLoach's avatar
RobLoach committed
79

RobLoach's avatar
RobLoach committed
80
81
82
83
84
85
std::string script::evalString(const std::string& code, const std::string& filename) {
	// Replace possible problematic tabs, and evaluate the script.
	std::string contents = replaceString(code, "\t", "  ");
	return chai.eval<std::string>(contents, Exception_Handler(), filename);
}

RobLoach's avatar
RobLoach committed
86
87
88
89
90
91
92
93
94
script::script(const std::string& file) {
	#ifdef __HAVE_CHAISCRIPT__

	// ChaiScript Standard Library Additions
	// This adds some basic type definitions to ChaiScript.
	chai.add(bootstrap::standard_library::vector_type<std::vector<int>>("VectorInt"));
	chai.add(bootstrap::standard_library::vector_type<std::vector<std::string>>("StringVector"));
	chai.add(bootstrap::standard_library::map_type<std::map<std::string, bool>>("StringBoolMap"));

RobLoach's avatar
0.28.0    
RobLoach committed
95
96
97
98
99
100
101
102
103
104
105
	// Global Helpers
	// string::replace(std::string search, std::string replace)
	chai.add(fun([](const std::string& subject, const std::string& search, const std::string& replace) {
		std::string newSubject(subject);
		size_t pos = 0;
		while ((pos = newSubject.find(search, pos)) != std::string::npos) {
			newSubject.replace(pos, search.length(), replace);
			pos += replace.length();
		}
		return newSubject;
	}), "replace");
RobLoach's avatar
RobLoach committed
106

RobLoach's avatar
RobLoach committed
107
	// string::replace(char search, char replace)
RobLoach's avatar
0.28.0    
RobLoach committed
108
109
110
111
112
113
	chai.add(fun([](const std::string& subject, char search, char replace) {
		std::string newSubject(subject);
		std::replace(newSubject.begin(), newSubject.end(), search, replace);
		return newSubject;
	}), "replace");

RobLoach's avatar
RobLoach committed
114
	// string::trim()
RobLoach's avatar
RobLoach committed
115
116
117
118
119
120
121
122
	chai.add(fun([](const std::string& subject) {
		std::string result(subject);
		std::string chars = "\t\n\v\f\r ";
		result.erase(0, result.find_first_not_of(chars));
		result.erase(0, result.find_last_not_of(chars));
		return result;
	}), "trim");

RobLoach's avatar
RobLoach committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
	// string::split()
	chai.add(fun([](const std::string& subject, const std::string& token) {
		std::string str(subject);
		std::vector<std::string> result;
		while (str.size()) {
			int index = str.find(token);
			if (index != std::string::npos) {
				result.push_back(str.substr(0, index));
				str = str.substr(index + token.size());
				if (str.size() == 0) {
					result.push_back(str);
				}
			} else {
				result.push_back(str);
				str = "";
			}
		}
		return result;
	}), "split");

RobLoach's avatar
RobLoach committed
143
144
145
146
147
	// List
	auto listModule = std::make_shared<chaiscript::Module>();
	chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List", *listModule);
	chai.add(listModule);

RobLoach's avatar
RobLoach committed
148
149
150
151
	// Add the "love" namespace.
	chai.register_namespace([](chaiscript::Namespace& love) {
		ChaiLove* app = ChaiLove::getInstance();
		love["audio"] = var(std::ref(app->audio));
RobLoach's avatar
RobLoach committed
152
		love["config"] = var(std::ref(app->config));
RobLoach's avatar
RobLoach committed
153
		love["console"] = var(std::ref(app->console));
154
		love["data"] = var(std::ref(app->data));
RobLoach's avatar
RobLoach committed
155
156
		love["event"] = var(std::ref(app->event));
		love["filesystem"] = var(std::ref(app->filesystem));
RobLoach's avatar
RobLoach committed
157
		love["font"] = var(std::ref(app->font));
RobLoach's avatar
RobLoach committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
		love["graphics"] = var(std::ref(app->graphics));
		love["image"] = var(std::ref(app->image));
		love["joystick"] = var(std::ref(app->joystick));
		love["keyboard"] = var(std::ref(app->keyboard));
		love["math"] = var(std::ref(app->math));
		love["mouse"] = var(std::ref(app->mouse));
		love["sound"] = var(std::ref(app->sound));
		love["system"] = var(std::ref(app->system));
		love["timer"] = var(std::ref(app->timer));
		love["window"] = var(std::ref(app->window));
	},
	"love");

	// Quad Object.
RobLoach's avatar
RobLoach committed
172
173
174
175
176
177
178
179
180
	chai.add(user_type<Quad>(), "Quad");
	chai.add(constructor<Quad()>(), "Quad");
	chai.add(fun(&Quad::x), "x");
	chai.add(fun(&Quad::y), "y");
	chai.add(fun(&Quad::width), "width");
	chai.add(fun(&Quad::height), "height");
	chai.add(fun(&Quad::sw), "sw");
	chai.add(fun(&Quad::sh), "sh");

RobLoach's avatar
RobLoach committed
181
	// Point Object.
RobLoach's avatar
RobLoach committed
182
183
184
185
186
187
188
	chai.add(user_type<Point>(), "Point");
	chai.add(fun(&Point::x), "x");
	chai.add(fun(&Point::y), "y");
	chai.add(constructor<Point()>(), "Point");
	chai.add(constructor<Point(float)>(), "Point");
	chai.add(constructor<Point(float, float)>(), "Point");

189
190
191
192
193
194
195
196
197
198
	// FileInfo Object.
	chai.add(user_type<FileInfo>(), "FileInfo");
	chai.add(fun(&FileInfo::type), "type");
	chai.add(fun(&FileInfo::size), "size");
	chai.add(fun(&FileInfo::modtime), "modtime");
	chai.add(constructor<FileInfo()>(), "FileInfo");
	chai.add(constructor<FileInfo(const std::string&)>(), "FileInfo");
	chai.add(constructor<FileInfo(const std::string&, int)>(), "FileInfo");
	chai.add(constructor<FileInfo(const std::string&, int, int)>(), "FileInfo");

199
200
201
202
203
204
205
206
	// FileData Object.
	chai.add(user_type<FileData>(), "FileData");
	chai.add(fun(&FileData::getSize), "getSize");
	chai.add(fun(&FileData::getFilename), "getFilename");
	chai.add(fun(&FileData::getString), "getString");
	chai.add(fun(&FileData::getExtension), "getExtension");
	chai.add(constructor<FileData(const std::string&)>(), "FileData");

RobLoach's avatar
RobLoach committed
207
208
209
210
211
212
213
214
215
216
	// Color Object.
	chai.add(user_type<Color>(), "Color");
	chai.add(fun(&Color::r), "r");
	chai.add(fun(&Color::g), "g");
	chai.add(fun(&Color::b), "b");
	chai.add(fun(&Color::a), "a");
	chai.add(constructor<Color()>(), "Color");
	chai.add(constructor<Color(int, int, int)>(), "Color");
	chai.add(constructor<Color(int, int, int, int)>(), "Color");

RobLoach's avatar
RobLoach committed
217
	// Image Object.
RobLoach's avatar
RobLoach committed
218
219
220
221
	chai.add(user_type<Image>(), "Image");
	chai.add(fun(&Image::getWidth), "getWidth");
	chai.add(fun(&Image::getHeight), "getHeight");

RobLoach's avatar
RobLoach committed
222
	// SoundData Object.
RobLoach's avatar
RobLoach committed
223
224
225
226
227
228
	chai.add(user_type<SoundData>(), "SoundData");
	chai.add(fun(&SoundData::isLooping), "isLooping");
	chai.add(fun(&SoundData::isPlaying), "isPlaying");
	chai.add(fun(&SoundData::setLooping), "setLooping");
	chai.add(fun(&SoundData::stop), "stop");
	chai.add(fun(&SoundData::play), "play");
RobLoach's avatar
RobLoach committed
229
230
	chai.add(fun(&SoundData::setVolume), "setVolume");
	chai.add(fun(&SoundData::getVolume), "getVolume");
RobLoach's avatar
RobLoach committed
231

RobLoach's avatar
RobLoach committed
232
	// Font.
RobLoach's avatar
RobLoach committed
233
234
235
236
237
238
	chai.add(user_type<Font>(), "Font");
	chai.add(fun(&Font::loaded), "loaded");
	chai.add(fun<int, Font>(&Font::getHeight), "getHeight");
	chai.add(fun<int, Font, const std::string&>(&Font::getHeight), "getHeight");
	chai.add(fun<int, Font, const std::string&>(&Font::getWidth), "getWidth");

RobLoach's avatar
RobLoach committed
239
	// Config
RobLoach's avatar
RobLoach committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
	chai.add(user_type<WindowConfig>(), "WindowConfig");
	chai.add(fun(&WindowConfig::width), "width");
	chai.add(fun(&WindowConfig::height), "height");
	chai.add(fun(&WindowConfig::bbp), "bbp");
	chai.add(fun(&WindowConfig::title), "title");
	chai.add(fun(&WindowConfig::asyncblit), "asyncblit");
	chai.add(fun(&WindowConfig::hwsurface), "hwsurface");
	chai.add(fun(&WindowConfig::doublebuffering), "doublebuffering");
	chai.add(user_type<ModuleConfig>(), "ModuleConfig");
	chai.add(fun(&ModuleConfig::sound), "sound");
	chai.add(user_type<config>(), "Config");
	chai.add(fun(&config::identity), "identity");
	chai.add(fun(&config::version), "version");
	chai.add(fun(&config::window), "window");
	chai.add(fun(&config::modules), "modules");
	chai.add(fun(&config::options), "options");
256
	chai.add(fun(&config::console), "console");
RobLoach's avatar
RobLoach committed
257

RobLoach's avatar
RobLoach committed
258
259
260
	// Console
	chai.add(fun(&console::isEnabled), "isEnabled");
	chai.add(fun(&console::setEnabled), "setEnabled");
RobLoach's avatar
RobLoach committed
261
	chai.add(fun(&console::isShown), "isShown");
RobLoach's avatar
RobLoach committed
262

RobLoach's avatar
RobLoach committed
263
	// Joystick
RobLoach's avatar
RobLoach committed
264
265
266
267
	chai.add(user_type<Joystick>(), "Joystick");
	chai.add(fun<bool, Joystick, const std::string&>(&Joystick::isDown), "isDown");
	chai.add(fun<bool, Joystick, int>(&Joystick::isDown), "isDown");
	chai.add(fun(&Joystick::getName), "getName");
RobLoach's avatar
RobLoach committed
268
269
	chai.add(fun(&Joystick::isConnected), "isConnected");
	chai.add(fun(&Joystick::getID), "getID");
RobLoach's avatar
RobLoach committed
270

RobLoach's avatar
RobLoach committed
271
	// Graphics
RobLoach's avatar
RobLoach committed
272
273
	chai.add(fun(&graphics::rectangle), "rectangle");
	chai.add(fun(&graphics::newImage), "newImage");
RobLoach's avatar
RobLoach committed
274
275
	chai.add(fun<love::graphics&, graphics, const std::string&, int, int>(&graphics::print), "print");
	chai.add(fun<love::graphics&, graphics, const std::string&>(&graphics::print), "print");
RobLoach's avatar
RobLoach committed
276
277
	chai.add(fun<love::graphics&, graphics, int, int>(&graphics::point), "point");
	chai.add(fun<love::graphics&, graphics, Point*>(&graphics::point), "point");
RobLoach's avatar
RobLoach committed
278
279
280
281
282
283
284
285
	// chai.add(bootstrap::standard_library::vector_type<std::vector<Point*>>("VectorPointPointer"));
	// chai.add(bootstrap::standard_library::vector_type<std::vector<Point>>("VectorPoint"));
	// chai.add(fun<void, graphics, std::vector<Point*>>(&graphics::points), "points");
	// chai.add(fun<void, graphics, std::vector<Point>>(&graphics::points), "points");
	chai.add(fun(&graphics::arc), "arc");
	chai.add(fun(&graphics::ellipse), "ellipse");
	chai.add(fun(&graphics::getWidth), "getWidth");
	chai.add(fun(&graphics::getHeight), "getHeight");
286
	chai.add(fun(&graphics::getDimensions), "getDimensions");
RobLoach's avatar
RobLoach committed
287
288
289
	chai.add(fun(&graphics::circle), "circle");
	chai.add(fun(&graphics::line), "line");
	chai.add(fun(&graphics::newQuad), "newQuad");
RobLoach's avatar
RobLoach committed
290
291
	chai.add(fun(&graphics::setDefaultFilter), "setDefaultFilter");
	chai.add(fun(&graphics::getDefaultFilter), "getDefaultFilter");
RobLoach's avatar
RobLoach committed
292
293
294
295
	chai.add(fun<Font*, graphics, const std::string&, int>(&graphics::newFont), "newFont");
	chai.add(fun<Font*, graphics, const std::string&>(&graphics::newFont), "newFont");
	chai.add(fun<Font*, graphics, const std::string&, int, int, const std::string&>(&graphics::newFont), "newFont");
	chai.add(fun<Font*, graphics>(&graphics::newFont), "newFont");
RobLoach's avatar
RobLoach committed
296
297
	chai.add(fun<love::graphics&, graphics, Font*>(&graphics::setFont), "setFont");
	chai.add(fun<love::graphics&, graphics>(&graphics::setFont), "setFont");
RobLoach's avatar
RobLoach committed
298
	chai.add(fun<Font*, graphics>(&graphics::getFont), "getFont");
RobLoach's avatar
RobLoach committed
299
300
301
302
303
304
	chai.add(fun<love::graphics&, graphics, int, int, int, int>(&graphics::setColor), "setColor");
	chai.add(fun<love::graphics&, graphics, int, int, int>(&graphics::setColor), "setColor");
	chai.add(fun<love::graphics&, graphics, int, int, int, int>(&graphics::setBackgroundColor), "setBackgroundColor");
	chai.add(fun<love::graphics&, graphics, int, int, int>(&graphics::setBackgroundColor), "setBackgroundColor");

	chai.add(fun<love::graphics&, graphics, Image*, int, int>(&graphics::draw), "draw");
RobLoach's avatar
RobLoach committed
305
	chai.add(fun<love::graphics&, graphics, Image*>(&graphics::draw), "draw");
RobLoach's avatar
RobLoach committed
306
307
308
309
310
311
312
	chai.add(fun<love::graphics&, graphics, Image*, int, int, float, float, float, float, float>(&graphics::draw), "draw");
	chai.add(fun<love::graphics&, graphics, Image*, int, int, float, float, float, float>(&graphics::draw), "draw");
	chai.add(fun<love::graphics&, graphics, Image*, int, int, float, float, float>(&graphics::draw), "draw");
	chai.add(fun<love::graphics&, graphics, Image*, int, int, float, float>(&graphics::draw), "draw");
	chai.add(fun<love::graphics&, graphics, Image*, int, int, float>(&graphics::draw), "draw");

	chai.add(fun<love::graphics&, graphics, Image*, Quad, int, int>(&graphics::draw), "draw");
RobLoach's avatar
RobLoach committed
313
314
	chai.add(fun<love::graphics&, graphics, Image*, Quad>(&graphics::draw), "draw");

RobLoach's avatar
RobLoach committed
315
316
317
	chai.add(fun<love::graphics&, graphics, int, int, int, int>(&graphics::clear), "clear");
	chai.add(fun<love::graphics&, graphics, int, int, int>(&graphics::clear), "clear");
	chai.add(fun<love::graphics&, graphics>(&graphics::clear), "clear");
RobLoach's avatar
RobLoach committed
318

RobLoach's avatar
RobLoach committed
319
	// Font
RobLoach's avatar
RobLoach committed
320
321
	chai.add(fun(&font::isOpen), "isOpen");

RobLoach's avatar
RobLoach committed
322
	// Keyboard
RobLoach's avatar
RobLoach committed
323
	chai.add(fun<bool, keyboard, const std::string&>(&keyboard::isDown), "isDown");
324
325
	chai.add(fun<bool, keyboard, int>(&keyboard::isDown), "isDown");
	chai.add(fun(&keyboard::isScancodeDown), "isScancodeDown");
RobLoach's avatar
RobLoach committed
326
327
328
	chai.add(fun(&keyboard::getKeyFromScancode), "getKeyFromScancode");
	chai.add(fun(&keyboard::getScancodeFromKey), "getScancodeFromKey");

RobLoach's avatar
RobLoach committed
329
	// Event
RobLoach's avatar
RobLoach committed
330
331
	chai.add(fun(&event::quit), "quit");

RobLoach's avatar
RobLoach committed
332
	// Image
RobLoach's avatar
RobLoach committed
333
334
	chai.add(fun(&image::newImageData), "newImageData");

RobLoach's avatar
RobLoach committed
335
	// Filesystem
RobLoach's avatar
RobLoach committed
336
337
338
	chai.add(fun(&filesystem::unmount), "unmount");
	chai.add(fun(&filesystem::read), "read");
	chai.add(fun(&filesystem::isDirectory), "isDirectory");
RobLoach's avatar
RobLoach committed
339
	chai.add(fun(&filesystem::createDirectory), "createDirectory");
340
	chai.add(fun(&filesystem::isSymlink), "isSymlink");
RobLoach's avatar
RobLoach committed
341
	chai.add(fun(&filesystem::isFile), "isFile");
RobLoach's avatar
RobLoach committed
342
	chai.add(fun(&filesystem::write), "write");
343
	chai.add(fun(&filesystem::remove), "remove");
RobLoach's avatar
RobLoach committed
344
	chai.add(fun(&filesystem::exists), "exists");
RobLoach's avatar
RobLoach committed
345
	chai.add(fun(&filesystem::getExecutablePath), "getExecutablePath");
RobLoach's avatar
RobLoach committed
346
	chai.add(fun(&filesystem::getSaveDirectory), "getSaveDirectory");
347
	chai.add(fun(&filesystem::getInfo), "getInfo");
RobLoach's avatar
RobLoach committed
348
349
	chai.add(fun<FileData, filesystem, const std::string&>(&filesystem::newFileData), "newFileData");
	chai.add(fun<FileData, filesystem, const std::string&, const std::string&>(&filesystem::newFileData), "newFileData");
RobLoach's avatar
RobLoach committed
350
	chai.add(fun(&filesystem::getDirectoryItems), "getDirectoryItems");
RobLoach's avatar
RobLoach committed
351
	chai.add(fun<bool, filesystem, const std::string&, const std::string&, bool>(&filesystem::mount), "mount");
RobLoach's avatar
RobLoach committed
352
	chai.add(fun<bool, filesystem, const std::string&, const std::string&>(&filesystem::mount), "mount");
RobLoach's avatar
RobLoach committed
353
354
355
356
	chai.add(fun<int, filesystem, const std::string&>(&filesystem::getSize), "getSize");
	chai.add(fun<std::vector<std::string>, filesystem, const std::string&>(&filesystem::lines), "lines");
	chai.add(fun<std::vector<std::string>, filesystem, const std::string&, const std::string&>(&filesystem::lines), "lines");
	chai.add(fun(&filesystem::load), "load");
RobLoach's avatar
RobLoach committed
357
	chai.add(fun(&script::loadModuleRequire, this), "require");
RobLoach's avatar
RobLoach committed
358

RobLoach's avatar
RobLoach committed
359
	// System
RobLoach's avatar
RobLoach committed
360
361
362
	chai.add(fun(&system::getOS), "getOS");
	chai.add(fun(&system::getVersion), "getVersion");
	chai.add(fun(&system::getVersionString), "getVersionString");
RobLoach's avatar
RobLoach committed
363
	chai.add(fun(&system::getUsername), "getUsername");
364
365
	chai.add(fun(&system::getClipboardText), "getClipboardText");
	chai.add(fun(&system::setClipboardText), "setClipboardText");
RobLoach's avatar
RobLoach committed
366
	chai.add(fun(&system::execute), "execute");
RobLoach's avatar
RobLoach committed
367

RobLoach's avatar
RobLoach committed
368
	// Mouse
RobLoach's avatar
RobLoach committed
369
370
371
372
373
374
	chai.add(fun(&mouse::getX), "getX");
	chai.add(fun(&mouse::getY), "getY");
	chai.add(fun(&mouse::getPosition), "getPosition");
	chai.add(fun<bool, mouse, const std::string&>(&mouse::isDown), "isDown");
	chai.add(fun<bool, mouse, int>(&mouse::isDown), "isDown");

RobLoach's avatar
RobLoach committed
375
	// Sound
RobLoach's avatar
RobLoach committed
376
377
	chai.add(fun<SoundData*, sound, const std::string&>(&sound::newSoundData), "newSoundData");

RobLoach's avatar
RobLoach committed
378
	// Audio
RobLoach's avatar
RobLoach committed
379
	chai.add(fun(&audio::play), "play");
RobLoach's avatar
RobLoach committed
380
	chai.add(fun<SoundData*, audio, const std::string&, const std::string&>(&audio::newSource), "newSource");
RobLoach's avatar
RobLoach committed
381
382
383
384
	chai.add(fun<SoundData*, audio, const std::string&>(&audio::newSource), "newSource");
	chai.add(fun(&audio::getVolume), "getVolume");
	chai.add(fun(&audio::setVolume), "setVolume");

RobLoach's avatar
RobLoach committed
385
	// Window
RobLoach's avatar
RobLoach committed
386
387
	chai.add(fun(&window::setTitle), "setTitle");
	chai.add(fun(&window::getTitle), "getTitle");
RobLoach's avatar
RobLoach committed
388
389
	chai.add(fun<love::window&, window, const std::string&, int>(&window::showMessageBox), "showMessageBox");
	chai.add(fun<love::window&, window, const std::string&>(&window::showMessageBox), "showMessageBox");
RobLoach's avatar
RobLoach committed
390

RobLoach's avatar
RobLoach committed
391
	// Timer
RobLoach's avatar
RobLoach committed
392
393
394
395
	chai.add(fun(&timer::getDelta), "getDelta");
	chai.add(fun(&timer::getFPS), "getFPS");
	chai.add(fun(&timer::step), "step");

RobLoach's avatar
RobLoach committed
396
	// Joystick
RobLoach's avatar
RobLoach committed
397
398
399
400
401
402
	chai.add(fun(&joystick::getJoysticks), "getJoysticks");
	chai.add(fun(&joystick::getJoystickCount), "getJoystickCount");
	chai.add(fun<bool, joystick, int, const std::string&>(&joystick::isDown), "isDown");
	chai.add(fun<bool, joystick, int, int>(&joystick::isDown), "isDown");
	chai.add(fun(&joystick::operator[]), "[]");

RobLoach's avatar
RobLoach committed
403
	// Math
RobLoach's avatar
RobLoach committed
404
405
406
407
	auto mathlib = chaiscript::extras::math::bootstrap();
	chai.add(mathlib);
	chai.add(fun(&math::pi), "pi");
	chai.add(fun(&math::e), "e");
408
409
	chai.add(fun(&math::rad), "rad");
	chai.add(fun(&math::degrees), "degrees");
RobLoach's avatar
RobLoach committed
410
411
412
	chai.add(fun<float, math>(&math::random), "random");
	chai.add(fun<float, math, float>(&math::random), "random");
	chai.add(fun<float, math, float, float>(&math::random), "random");
RobLoach's avatar
RobLoach committed
413
414
415
416
	chai.add(fun<int, math, int>(&math::random), "random");
	chai.add(fun<int, math, int, int>(&math::random), "random");
	chai.add(fun<double, math, double>(&math::random), "random");
	chai.add(fun<double, math, double, double>(&math::random), "random");
RobLoach's avatar
RobLoach committed
417
418
	chai.add(fun<love::math&, math, int, int>(&math::setRandomSeed), "setRandomSeed");
	chai.add(fun<love::math&, math, int>(&math::setRandomSeed), "setRandomSeed");
RobLoach's avatar
RobLoach committed
419
	chai.add(fun(&math::getRandomSeed), "getRandomSeed");
420
421
422
423
424

	// Data
	chai.add(fun<std::string, data, const std::string&>(&data::compress), "compress");
	chai.add(fun<std::string, data, const std::string&, int>(&data::compress), "compress");
	chai.add(fun(&data::decompress), "decompress");
425
	chai.add(fun(&data::hash), "hash");
RobLoach's avatar
RobLoach committed
426
427
	chai.add(fun(&data::encode), "encode");
	chai.add(fun(&data::decode), "decode");
RobLoach's avatar
RobLoach committed
428

RobLoach's avatar
RobLoach committed
429
430
431
	// Ensure the love namespace is imported and ready.
	chai.import("love");

432
433
434
435
436
	// Load the desired main.chai file.
	if (file.empty()) {
		// When no content is provided, display a No Game demo.
		eval(ChaiLove::getInstance()->demo(), "demo.chai");
		mainLoaded = true;
RobLoach's avatar
RobLoach committed
437
	} else {
438
439
440
441
442
443
444
445
446
447
		// Load the main.chai file.
		::filesystem::path p(file.c_str());
		std::string extension(p.extension());
		if (extension == "chailove" || extension == "chaigame") {
			mainLoaded = loadModule("main.chai");
		} else {
			// Otherwise, load the actual file.
			std::string filename(p.filename());
			mainLoaded = loadModule(filename);
		}
RobLoach's avatar
RobLoach committed
448
449
450
451
452
453
454
	}

	// Find the game functions.
	try {
		chaiload = chai.eval<std::function<void()> >("load");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
455
		std::cout << "[ChaiLove] [script] load() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
456
457
458
459
460
461
		hasload = false;
	}
	try {
		chaireset = chai.eval<std::function<void()> >("reset");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
462
		std::cout << "[ChaiLove] [script] reset() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
463
464
465
466
467
468
		hasreset = false;
	}
	try {
		chaiupdate = chai.eval<std::function<void(float)> >("update");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
469
		std::cout << "[ChaiLove] [script] update(delta) " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
470
471
472
		hasUpdate = false;
	}
	try {
RobLoach's avatar
RobLoach committed
473
		chaiconf = chai.eval<std::function<void(config&)> >("conf");
RobLoach's avatar
RobLoach committed
474
475
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
476
		std::cout << "[ChaiLove] [script] conf(t) " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
477
478
479
480
481
482
		hasconf = false;
	}
	try {
		chaidraw = chai.eval<std::function<void()> >("draw");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
483
		std::cout << "[ChaiLove] [script] draw() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
484
485
486
487
488
489
		hasDraw = false;
	}
	try {
		chaijoystickpressed = chai.eval<std::function<void(int, const std::string&)> >("joystickpressed");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
490
		std::cout << "[ChaiLove] [script] joystickpressed() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
491
492
493
494
495
496
		hasjoystickpressed = false;
	}
	try {
		chaijoystickreleased = chai.eval<std::function<void(int, const std::string&)> >("joystickreleased");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
497
		std::cout << "[ChaiLove] [script] joystickreleased() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
498
499
500
		hasjoystickreleased = false;
	}
	try {
501
		chaimousepressed = chai.eval<std::function<void(int, int, const std::string&)> >("mousepressed");
RobLoach's avatar
RobLoach committed
502
503
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
504
		std::cout << "[ChaiLove] [script] mousepressed() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
505
506
507
		hasmousepressed = false;
	}
	try {
508
		chaimousereleased = chai.eval<std::function<void(int, int, const std::string&)> >("mousereleased");
RobLoach's avatar
RobLoach committed
509
510
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
511
		std::cout << "[ChaiLove] [script] mousereleased() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
512
513
514
		hasmousereleased = false;
	}
	try {
515
		chaimousemoved = chai.eval<std::function<void(int, int, int, int)> >("mousemoved");
RobLoach's avatar
RobLoach committed
516
517
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
518
519
		std::cout << "[ChaiLove] [script] mousemoved() " << e.what() << std::endl;
		hasmousemoved = false;
RobLoach's avatar
RobLoach committed
520
521
522
523
524
	}
	try {
		chaikeypressed = chai.eval<std::function<void(const std::string&, int)> >("keypressed");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
525
		std::cout << "[ChaiLove] [script] keypressed() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
526
527
528
529
530
531
		haskeypressed = false;
	}
	try {
		chaikeyreleased = chai.eval<std::function<void(const std::string&, int)> >("keyreleased");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
532
		std::cout << "[ChaiLove] [script] keyreleased() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
533
534
535
		haskeyreleased = false;
	}
	try {
RobLoach's avatar
RobLoach committed
536
		chailoadstate = chai.eval<std::function<bool(const std::string&)> >("loadstate");
RobLoach's avatar
RobLoach committed
537
538
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
539
		std::cout << "[ChaiLove] [script] loadstate() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
540
541
542
543
544
545
		hasloadstate = false;
	}
	try {
		chaisavestate = chai.eval<std::function<std::string()> >("savestate");
	}
	catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
546
		std::cout << "[ChaiLove] [script] savestate() " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
547
548
		hassavestate = false;
	}
RobLoach's avatar
RobLoach committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
	try {
		chaicheatreset = chai.eval<std::function<void()> >("cheatreset");
	}
	catch (const std::exception& e) {
		std::cout << "[ChaiLove] [script] cheatreset() Warning: " << e.what() << std::endl;
		hascheatreset = false;
	}
	try {
		chaicheatset = chai.eval<std::function<void(int, bool, const std::string&)> >("cheatset");
	}
	catch (const std::exception& e) {
		std::cout << "[ChaiLove] [script] cheatset() Warning: " << e.what() << std::endl;
		hascheatset = false;
	}
RobLoach's avatar
RobLoach committed
563
564
565
566
567
568
569
	try {
		chaiexit = chai.eval<std::function<void()> >("exit");
	}
	catch (const std::exception& e) {
		std::cout << "[ChaiLove] [script] exit() Warning: " << e.what() << std::endl;
		hasexit = false;
	}
RobLoach's avatar
RobLoach committed
570
571
572
	#endif
}

RobLoach's avatar
RobLoach committed
573
void script::conf(config& t) {
RobLoach's avatar
RobLoach committed
574
575
576
577
	#ifdef __HAVE_CHAISCRIPT__
	if (hasconf) {
		try {
			chaiconf(t);
578
		}
RobLoach's avatar
RobLoach committed
579
580
581
		catch (const std::exception& e) {
			std::cout << "[ChaiLove] [script] Failed to invoke conf(t): " << e.what() << std::endl;
			hasconf = false;
582
		}
RobLoach's avatar
RobLoach committed
583
584
585
	}
	#endif
}
RobLoach's avatar
RobLoach committed
586

RobLoach's avatar
RobLoach committed
587
588
589
void script::load() {
	#ifdef __HAVE_CHAISCRIPT__
	if (hasload) {
RobLoach's avatar
RobLoach committed
590
		try {
RobLoach's avatar
RobLoach committed
591
			chaiload();
RobLoach's avatar
RobLoach committed
592
		}
RobLoach's avatar
RobLoach committed
593
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
594
			std::cout << "[ChaiLove] [script] Failed to call load(): " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
595
			hasload = false;
RobLoach's avatar
RobLoach committed
596
		}
RobLoach's avatar
RobLoach committed
597
598
599
600
601
602
603
	}
	#endif
}

void script::reset() {
	#ifdef __HAVE_CHAISCRIPT__
	if (hasreset) {
RobLoach's avatar
RobLoach committed
604
		try {
RobLoach's avatar
RobLoach committed
605
			chaireset();
RobLoach's avatar
RobLoach committed
606
607
		}
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
608
			std::cout << "[ChaiLove] [script] Failed to call reset(): " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
609
610
			hasreset = false;
		}
RobLoach's avatar
RobLoach committed
611
612
613
614
615
616
617
	}
	#endif
}

void script::update(float delta) {
	#ifdef __HAVE_CHAISCRIPT__
	if (hasUpdate) {
RobLoach's avatar
RobLoach committed
618
		try {
RobLoach's avatar
RobLoach committed
619
			chaiupdate(delta);
RobLoach's avatar
RobLoach committed
620
		}
RobLoach's avatar
RobLoach committed
621
622
		catch (const std::exception& e) {
			hasUpdate = false;
RobLoach's avatar
RobLoach committed
623
			std::cout << "[ChaiLove] [script] Failed to call update(delta): " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
624
		}
RobLoach's avatar
RobLoach committed
625
626
627
628
629
630
631
	}
	#endif
}

void script::draw() {
	#ifdef __HAVE_CHAISCRIPT__
	if (hasDraw) {
RobLoach's avatar
RobLoach committed
632
		try {
RobLoach's avatar
RobLoach committed
633
			chaidraw();
RobLoach's avatar
RobLoach committed
634
		}
RobLoach's avatar
RobLoach committed
635
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
636
			std::cout << "[ChaiLove] [script] Failed to call draw(): " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
637
			hasDraw = false;
RobLoach's avatar
RobLoach committed
638
		}
RobLoach's avatar
RobLoach committed
639
	} else {
RobLoach's avatar
RobLoach committed
640
641
642
643
644
645
646
647
		ChaiLove::getInstance()->graphics.print("ChaiLove: def draw() not found.", 100, 100);
	}
	#endif
}

void script::joystickpressed(int joystick, const std::string& button) {
	#ifdef __HAVE_CHAISCRIPT__
	if (hasjoystickpressed) {
RobLoach's avatar
RobLoach committed
648
		try {
RobLoach's avatar
RobLoach committed
649
			chaijoystickpressed(joystick, button);
RobLoach's avatar
RobLoach committed
650
		}
RobLoach's avatar
RobLoach committed
651
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
652
			std::cout << "[ChaiLove] [script] Failed to call joystickpressed(): " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
653
			hasjoystickpressed = false;
RobLoach's avatar
RobLoach committed
654
		}
RobLoach's avatar
RobLoach committed
655
656
657
658
659
660
661
	}
	#endif
}

void script::joystickreleased(int joystick, const std::string& button) {
	#ifdef __HAVE_CHAISCRIPT__
	if (hasjoystickreleased) {
RobLoach's avatar
RobLoach committed
662
		try {
RobLoach's avatar
RobLoach committed
663
			chaijoystickreleased(joystick, button);
RobLoach's avatar
RobLoach committed
664
		}
RobLoach's avatar
RobLoach committed
665
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
666
			std::cout << "[ChaiLove] [script] Failed to call joystickreleased(): " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
667
			hasjoystickreleased = false;
RobLoach's avatar
RobLoach committed
668
		}
RobLoach's avatar
RobLoach committed
669
670
671
672
	}
	#endif
}

673
void script::mousepressed(int x, int y, const std::string& button) {
RobLoach's avatar
RobLoach committed
674
675
	#ifdef __HAVE_CHAISCRIPT__
	if (hasmousepressed) {
RobLoach's avatar
RobLoach committed
676
		try {
RobLoach's avatar
RobLoach committed
677
			chaimousepressed(x, y, button);
RobLoach's avatar
RobLoach committed
678
		}
RobLoach's avatar
RobLoach committed
679
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
680
			std::cout << "[ChaiLove] [script] Failed to call mousepressed(): " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
681
			hasmousepressed = false;
RobLoach's avatar
RobLoach committed
682
		}
RobLoach's avatar
RobLoach committed
683
684
685
686
	}
	#endif
}

687
void script::mousereleased(int x, int y, const std::string& button) {
RobLoach's avatar
RobLoach committed
688
689
	#ifdef __HAVE_CHAISCRIPT__
	if (hasmousereleased) {
RobLoach's avatar
RobLoach committed
690
		try {
RobLoach's avatar
RobLoach committed
691
			chaimousereleased(x, y, button);
RobLoach's avatar
RobLoach committed
692
		}
RobLoach's avatar
RobLoach committed
693
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
694
			std::cout << "[ChaiLove] [script] Failed to call mousereleased(): " << e.what() << std::endl;
RobLoach's avatar
RobLoach committed
695
			hasmousereleased = false;
RobLoach's avatar
RobLoach committed
696
		}
RobLoach's avatar
RobLoach committed
697
698
699
700
	}
	#endif
}

701
void script::mousemoved(int x, int y, int dx, int dy) {
RobLoach's avatar
RobLoach committed
702
	#ifdef __HAVE_CHAISCRIPT__
RobLoach's avatar
RobLoach committed
703
	if (hasmousemoved) {
RobLoach's avatar
RobLoach committed
704
		try {
705
			chaimousemoved(x, y, dx, dy);
RobLoach's avatar
RobLoach committed
706
707
		}
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
708
709
			std::cout << "[ChaiLove] [script] Failed to call mousemoved(): " << e.what() << std::endl;
			hasmousemoved = false;
RobLoach's avatar
RobLoach committed
710
		}
RobLoach's avatar
RobLoach committed
711
712
713
714
715
716
717
	}
	#endif
}

void script::keypressed(const std::string& key, int scancode) {
	#ifdef __HAVE_CHAISCRIPT__
	if (haskeypressed) {
718
		try {
RobLoach's avatar
RobLoach committed
719
			chaikeypressed(key, scancode);
720
721
		}
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
722
			std::cout << "[ChaiLove] [script] Failed to call keypressed(): " << e.what() << std::endl;
723
724
			haskeypressed = false;
		}
RobLoach's avatar
RobLoach committed
725
726
727
728
729
730
731
	}
	#endif
}

void script::keyreleased(const std::string& key, int scancode) {
	#ifdef __HAVE_CHAISCRIPT__
	if (haskeyreleased) {
732
		try {
RobLoach's avatar
RobLoach committed
733
			chaikeyreleased(key, scancode);
734
735
		}
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
736
			std::cout << "[ChaiLove] [script] Failed to call keyreleased(): " << e.what() << std::endl;
737
738
			haskeyreleased = false;
		}
RobLoach's avatar
RobLoach committed
739
740
741
742
743
744
745
746
747
748
	}
	#endif
}

/**
 * Call the script's savestate() function and retrieve a string.
 */
std::string script::savestate() {
	#ifdef __HAVE_CHAISCRIPT__
	if (hassavestate) {
RobLoach's avatar
RobLoach committed
749
		try {
RobLoach's avatar
RobLoach committed
750
			return chaisavestate();
RobLoach's avatar
RobLoach committed
751
752
		}
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
753
754
			std::cout << "[ChaiLove] [script] Failed to call savestate(): " << e.what() << std::endl;
			hassavestate = false;
RobLoach's avatar
RobLoach committed
755
		}
RobLoach's avatar
RobLoach committed
756
757
758
759
760
761
762
763
764
765
766
767
768
	}
	#endif

	// If there is no state data, return an empty string.
	return std::string("");
}

/**
 * Pass the state data string over to the script, asking it to load the state.
 */
bool script::loadstate(const std::string& data) {
	#ifdef __HAVE_CHAISCRIPT__
	if (hasloadstate) {
RobLoach's avatar
RobLoach committed
769
		try {
RobLoach's avatar
RobLoach committed
770
			return chailoadstate(data);
RobLoach's avatar
RobLoach committed
771
772
		}
		catch (const std::exception& e) {
RobLoach's avatar
RobLoach committed
773
774
			std::cout << "[ChaiLove] [script] Failed to call loadstate(): " << e.what() << std::endl;
			hasloadstate = false;
RobLoach's avatar
RobLoach committed
775
776
		}
	}
RobLoach's avatar
RobLoach committed
777
	#endif
RobLoach's avatar
RobLoach committed
778

RobLoach's avatar
RobLoach committed
779
780
781
782
	// If there is an error in loading the state, return false.
	return false;
}

RobLoach's avatar
RobLoach committed
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
void script::cheatreset() {
	#ifdef __HAVE_CHAISCRIPT__
	if (hascheatreset) {
		try {
			chaicheatreset();
		}
		catch (const std::exception& e) {
			std::cout << "[ChaiLove] [script] Failed to call cheatreset(): " << e.what() << std::endl;
			hascheatreset = false;
		}
	}
	#endif
}

void script::cheatset(int index, bool enabled, const std::string& code) {
	#ifdef __HAVE_CHAISCRIPT__
	if (hascheatreset) {
		try {
			chaicheatset(index, enabled, code);
		}
		catch (const std::exception& e) {
			std::cout << "[ChaiLove] [script] Failed to call cheatset(): " << e.what() << std::endl;
			hascheatset = false;
		}
	}
	#endif
}

RobLoach's avatar
RobLoach committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
void script::exit() {
	#ifdef __HAVE_CHAISCRIPT__
	if (hasexit) {
		try {
			chaiexit();
		}
		catch (const std::exception& e) {
			std::cout << "[ChaiLove] [script] Failed to call exit(): " << e.what() << std::endl;
			hasexit = false;
		}
	}
	#endif
}

RobLoach's avatar
RobLoach committed
825
826
827
828
829
830
831
832
/**
 * Replaces all instances of a string in a given subject string.
 */
std::string script::replaceString(std::string subject, const std::string& search, const std::string& replace) {
	size_t pos = 0;
	while ((pos = subject.find(search, pos)) != std::string::npos) {
		 subject.replace(pos, search.length(), replace);
		 pos += replace.length();
RobLoach's avatar
RobLoach committed
833
	}
RobLoach's avatar
RobLoach committed
834
	return subject;
RobLoach's avatar
RobLoach committed
835
}
RobLoach's avatar
RobLoach committed
836

RobLoach's avatar
RobLoach committed
837
}  // namespace love