@@ -23,21 +23,42 @@ using love::graphics;
2323
2424namespace love {
2525
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+
2647bool script::loadModule (const std::string& moduleName) {
2748 #ifdef __HAVE_CHAISCRIPT__
2849 ChaiLove* app = ChaiLove::getInstance ();
2950
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+ }
3256
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 ;
4162 }
4263
4364 // Load the contents of the file.
@@ -49,16 +70,19 @@ bool script::loadModule(const std::string& moduleName) {
4970 return false ;
5071 }
5172
73+ // Run the script.
5274 eval (contents, filename);
5375 return true ;
54-
5576 #endif
5677 return false ;
5778}
5879
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.
6286 if (std::find (m_requiremodules.begin (), m_requiremodules.end (), filename) != m_requiremodules.end ()) {
6387 return true ;
6488 }
@@ -68,6 +92,7 @@ bool script::loadModuleRequire(const std::string& moduleName) {
6892 if (loaded) {
6993 m_requiremodules.push_back (filename);
7094 }
95+
7196 return loaded;
7297}
7398
@@ -310,7 +335,7 @@ script::script(const std::string& file) {
310335 chai.add (fun<std::vector<std::string>, filesystem, const std::string&>(&filesystem::lines), " lines" );
311336 chai.add (fun<std::vector<std::string>, filesystem, const std::string&, const std::string&>(&filesystem::lines), " lines" );
312337 chai.add (fun (&filesystem::load), " load" );
313- chai.add (fun (&script::loadModuleRequire , this ), " require" );
338+ chai.add (fun (&script::require , this ), " require" );
314339 chai.add (fun (&filesystem::getFileExtension), " getFileExtension" );
315340 chai.add (fun (&filesystem::getBasename), " getBasename" );
316341 chai.add (fun (&filesystem::getParentDirectory), " getParentDirectory" );
@@ -395,15 +420,15 @@ script::script(const std::string& file) {
395420 mainLoaded = true ;
396421 } else {
397422 // Load the main.chai file.
398- loadModuleRequire (" conf" );
423+ require (" conf" );
399424
400425 std::string extension (app->filesystem .getFileExtension (file));
401426 if (extension == " chailove" || extension == " chaigame" ) {
402- mainLoaded = loadModuleRequire (" main" );
427+ mainLoaded = require (" main" );
403428 } else {
404429 // Otherwise, load the actual file.
405430 std::string filename (app->filesystem .getBasename (file));
406- mainLoaded = loadModuleRequire (filename);
431+ mainLoaded = require (filename);
407432 }
408433 }
409434
0 commit comments