@@ -23,21 +23,42 @@ using love::graphics;
23
23
24
24
namespace love {
25
25
26
+ std::string script::findModule (const std::string& filename) {
27
+ ChaiLove* app = ChaiLove::getInstance ();
28
+ std::string possibilities[5 ] = {
29
+ filename,
30
+ filename + " .chai" ,
31
+ // Allow loading lua files as ChaiScript?
32
+ // filename + ".lua",
33
+ // Attempt to load a directory's init.chai, if available.
34
+ filename + " /init.chai"
35
+ // Allow loading .lua files?
36
+ // filename + "/init.lua"
37
+ };
38
+ for (const std::string& possibility : possibilities) {
39
+ // Make sure the file exists and is a file.
40
+ if (app->filesystem .exists (possibility) && app->filesystem .isFile (possibility)) {
41
+ return possibility;
42
+ }
43
+ }
44
+ return " " ;
45
+ }
46
+
26
47
bool script::loadModule (const std::string& moduleName) {
27
48
#ifdef __HAVE_CHAISCRIPT__
28
49
ChaiLove* app = ChaiLove::getInstance ();
29
50
30
- // Store a filename for the module.
31
- std::string filename = moduleName;
51
+ // Ensure we're loading a valid module name.
52
+ if (moduleName.empty ()) {
53
+ std::cout << " [ChaiLove] [script] loadModule was called with an empty moduleName." << std::endl;
54
+ return false ;
55
+ }
32
56
33
- // Make sure it exists.
34
- if (!app->filesystem .exists (filename)) {
35
- // See if we are to append .chai.
36
- filename = filename + " .chai" ;
37
- if (!app->filesystem .exists (filename)) {
38
- std::cout << " [ChaiLove] [script] Module " << filename << " not found." << std::endl;
39
- return false ;
40
- }
57
+ // Store a filename for the module.
58
+ std::string filename = findModule (moduleName);
59
+ if (filename.empty ()) {
60
+ std::cout << " [ChaiLove] [script] Module " << moduleName << " not found." << std::endl;
61
+ return false ;
41
62
}
42
63
43
64
// Load the contents of the file.
@@ -49,16 +70,19 @@ bool script::loadModule(const std::string& moduleName) {
49
70
return false ;
50
71
}
51
72
73
+ // Run the script.
52
74
eval (contents, filename);
53
75
return true ;
54
-
55
76
#endif
56
77
return false ;
57
78
}
58
79
59
- bool script::loadModuleRequire (const std::string& moduleName) {
60
- // Check if the module has already been loaded.
61
- std::string filename = replaceString (replaceString (moduleName, " .chai" , " " ), " ." , " /" );
80
+ bool script::require (const std::string& moduleName) {
81
+ // Find what the cleansed module name is.
82
+ std::string noExtension = replaceString (replaceString (moduleName, " .chai" , " " ), " .lua" , " " );
83
+ std::string filename = replaceString (noExtension, " ." , " /" );
84
+
85
+ // Ensure we only load the script once.
62
86
if (std::find (m_requiremodules.begin (), m_requiremodules.end (), filename) != m_requiremodules.end ()) {
63
87
return true ;
64
88
}
@@ -68,6 +92,7 @@ bool script::loadModuleRequire(const std::string& moduleName) {
68
92
if (loaded) {
69
93
m_requiremodules.push_back (filename);
70
94
}
95
+
71
96
return loaded;
72
97
}
73
98
@@ -310,7 +335,7 @@ script::script(const std::string& file) {
310
335
chai.add (fun<std::vector<std::string>, filesystem, const std::string&>(&filesystem::lines), " lines" );
311
336
chai.add (fun<std::vector<std::string>, filesystem, const std::string&, const std::string&>(&filesystem::lines), " lines" );
312
337
chai.add (fun (&filesystem::load), " load" );
313
- chai.add (fun (&script::loadModuleRequire , this ), " require" );
338
+ chai.add (fun (&script::require , this ), " require" );
314
339
chai.add (fun (&filesystem::getFileExtension), " getFileExtension" );
315
340
chai.add (fun (&filesystem::getBasename), " getBasename" );
316
341
chai.add (fun (&filesystem::getParentDirectory), " getParentDirectory" );
@@ -395,15 +420,15 @@ script::script(const std::string& file) {
395
420
mainLoaded = true ;
396
421
} else {
397
422
// Load the main.chai file.
398
- loadModuleRequire (" conf" );
423
+ require (" conf" );
399
424
400
425
std::string extension (app->filesystem .getFileExtension (file));
401
426
if (extension == " chailove" || extension == " chaigame" ) {
402
- mainLoaded = loadModuleRequire (" main" );
427
+ mainLoaded = require (" main" );
403
428
} else {
404
429
// Otherwise, load the actual file.
405
430
std::string filename (app->filesystem .getBasename (file));
406
- mainLoaded = loadModuleRequire (filename);
431
+ mainLoaded = require (filename);
407
432
}
408
433
}
409
434
0 commit comments