Unverified Commit 73baadef authored by Rob Loach's avatar Rob Loach Committed by GitHub
Browse files

0.30.0 (#350)

* Add loading lib/init.chai through require() (#348)
* Update ChaiScript_Extras and fix module load warning
* Update libretro-common
parent 3ca62ba5
......@@ -4,10 +4,13 @@ All notable changes to [ChaiLove](https://github.com/RobLoach/ChaiLove) will be
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## 0.29.2 - Unreleased
## 0.30.0 - 2018-11-14
### Features
- Added support for classic_armv7_a7
- By [@classicmods](https://github.com/classicmods) and [@swingflip](https://github.com/swingflip)
- Added `lib/init.chai` loading with `require("lib")`
- Updated ChaiScript/ChaiScript_Extras
- Updated libretro/libretro-common
## 0.29.1 - 2018-11-05
### Chores
......
......@@ -23,7 +23,7 @@ PROJECT_NAME = "ChaiLove API"
# This could be handy for archiving the generated documentation or
# if some version control system is used.
PROJECT_NUMBER = "0.29.1"
PROJECT_NUMBER = "0.30.0"
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
# base path where the generated documentation will be put.
......
......@@ -47,9 +47,9 @@
#define SRC_CHAILOVE_H_
#define CHAILOVE_VERSION_MAJOR 0
#define CHAILOVE_VERSION_MINOR 29
#define CHAILOVE_VERSION_PATCH 1
#define CHAILOVE_VERSION_STRING "0.29.1"
#define CHAILOVE_VERSION_MINOR 30
#define CHAILOVE_VERSION_PATCH 0
#define CHAILOVE_VERSION_STRING "0.30.0"
#include "SDL.h"
#include "libretro.h"
......
......@@ -71,11 +71,16 @@ class List {
* @see love.filesystem.load
*
* @code
* // The following will load lib/player.chai.
* // The following will load "lib/player.chai".
* require("lib.player")
*
* // Calling require("lib.player") again will not re-load it.
* require("lib.player")
*
* // Calling a directory name will attempt to load the directory's "init.chai"
* // file. The following example will load "lib/init.chai" if "lib.chai"
* // doesn't exist.
* require("lib")
* @endcode
*/
bool require(const std::string& module);
......@@ -110,10 +115,10 @@ class String {
* var hello = " Hello World! "
* var result = hello.trim()
* // => "Hello World!"
* @endcode
* @endcode
*/
std::string trim();
/**
* Splits a string by the given token.
*/
......
......@@ -23,21 +23,42 @@ using love::graphics;
namespace love {
std::string script::findModule(const std::string& filename) {
ChaiLove* app = ChaiLove::getInstance();
std::string possibilities[5] = {
filename,
filename + ".chai",
// Allow loading lua files as ChaiScript?
// filename + ".lua",
// Attempt to load a directory's init.chai, if available.
filename + "/init.chai"
// Allow loading .lua files?
// filename + "/init.lua"
};
for (const std::string& possibility : possibilities) {
// Make sure the file exists and is a file.
if (app->filesystem.exists(possibility) && app->filesystem.isFile(possibility)) {
return possibility;
}
}
return "";
}
bool script::loadModule(const std::string& moduleName) {
#ifdef __HAVE_CHAISCRIPT__
ChaiLove* app = ChaiLove::getInstance();
// Store a filename for the module.
std::string filename = moduleName;
// Ensure we're loading a valid module name.
if (moduleName.empty()) {
std::cout << "[ChaiLove] [script] loadModule was called with an empty moduleName." << std::endl;
return false;
}
// 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 " << filename << " not found." << std::endl;
return false;
}
// Store a filename for the module.
std::string filename = findModule(moduleName);
if (filename.empty()) {
std::cout << "[ChaiLove] [script] Module " << moduleName << " not found." << std::endl;
return false;
}
// Load the contents of the file.
......@@ -49,16 +70,19 @@ bool script::loadModule(const std::string& moduleName) {
return false;
}
// Run the script.
eval(contents, filename);
return true;
#endif
return false;
}
bool script::loadModuleRequire(const std::string& moduleName) {
// Check if the module has already been loaded.
std::string filename = replaceString(replaceString(moduleName, ".chai", ""), ".", "/");
bool script::require(const std::string& moduleName) {
// Find what the cleansed module name is.
std::string noExtension = replaceString(replaceString(moduleName, ".chai", ""), ".lua", "");
std::string filename = replaceString(noExtension, ".", "/");
// Ensure we only load the script once.
if (std::find(m_requiremodules.begin(), m_requiremodules.end(), filename) != m_requiremodules.end()) {
return true;
}
......@@ -68,6 +92,7 @@ bool script::loadModuleRequire(const std::string& moduleName) {
if (loaded) {
m_requiremodules.push_back(filename);
}
return loaded;
}
......@@ -310,7 +335,7 @@ script::script(const std::string& file) {
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");
chai.add(fun(&script::loadModuleRequire, this), "require");
chai.add(fun(&script::require, this), "require");
chai.add(fun(&filesystem::getFileExtension), "getFileExtension");
chai.add(fun(&filesystem::getBasename), "getBasename");
chai.add(fun(&filesystem::getParentDirectory), "getParentDirectory");
......@@ -395,15 +420,15 @@ script::script(const std::string& file) {
mainLoaded = true;
} else {
// Load the main.chai file.
loadModuleRequire("conf");
require("conf");
std::string extension(app->filesystem.getFileExtension(file));
if (extension == "chailove" || extension == "chaigame") {
mainLoaded = loadModuleRequire("main");
mainLoaded = require("main");
} else {
// Otherwise, load the actual file.
std::string filename(app->filesystem.getBasename(file));
mainLoaded = loadModuleRequire(filename);
mainLoaded = require(filename);
}
}
......
......@@ -471,8 +471,11 @@ class script {
bool hascheatset = true;
#endif
std::string findModule(const std::string& filename);
std::string replaceString(std::string subject, const std::string& search, const std::string& replace);
bool loadModuleRequire(const std::string& moduleName);
bool require(const std::string& moduleName);
// Properties
std::list<std::string> m_requiremodules;
};
......
requiretestFileLoaded = true
......@@ -34,7 +34,7 @@ assert_not(love.filesystem.isSymlink("keyboard.chai"), "love.filesystem.isSymlin
// getDirectoryItems()
var getDirectoriesItems = love.filesystem.getDirectoryItems("assets").size()
assert_equal(getDirectoriesItems, 5, "love.filesystem.getDirectoryItems()")
assert_equal(getDirectoriesItems, 6, "love.filesystem.getDirectoryItems()")
// lines()
var theLines = love.filesystem.lines("filesystem.chai")
......@@ -103,6 +103,11 @@ requireReturn = require("assets.requiretest")
assert(requireReturn, " double call")
assert_not(requiretestFileLoaded, " not loaded twice")
// require() - dir/init.chai
requiretestFileLoaded = false
require("assets")
assert(requiretestFileLoaded, " loaded assets/init.chai")
// getFileExtension()
assert_equal(love.filesystem.getFileExtension("/opt/var/something.txt"), "txt", "love.filesystem.getFileExtension()")
assert_equal(love.filesystem.getFileExtension("/opt/var/something.tar.gz"), "gz", "love.filesystem.getFileExtension()")
......
Subproject commit 4f3ee02194411edc5b7aa80d109e400d3b94c15d
Subproject commit 2aee0d2dc338d9a548e2c766da27112f2f6bca17
Subproject commit 4f99997d94bbfed63d957153976dd9a43c73aced
Subproject commit 87ae44c172d7c8e04c64e26fdda868508582b4f6
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment