2014-03-27 03:24:37 +01:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
2007-05-01 23:51:49 +02:00
|
|
|
//
|
2011-09-28 21:45:16 +02:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2007-05-01 23:51:49 +02:00
|
|
|
//
|
2011-09-28 21:45:16 +02:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2014-03-27 03:24:37 +01:00
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2009-07-29 07:43:02 +02:00
|
|
|
|
2014-03-27 03:24:37 +01:00
|
|
|
#include "libaegisub/lua/script_reader.h"
|
2013-09-23 03:44:00 +02:00
|
|
|
|
2014-03-27 03:24:37 +01:00
|
|
|
#include "libaegisub/file_mapping.h"
|
2014-04-26 00:40:43 +02:00
|
|
|
#include "libaegisub/log.h"
|
2014-03-27 03:24:37 +01:00
|
|
|
#include "libaegisub/lua/utils.h"
|
2015-01-11 17:11:22 +01:00
|
|
|
#include "libaegisub/split.h"
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2014-04-26 00:40:43 +02:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2014-03-27 03:24:37 +01:00
|
|
|
#include <lauxlib.h>
|
2013-02-17 04:47:31 +01:00
|
|
|
|
2014-03-27 03:24:37 +01:00
|
|
|
namespace agi { namespace lua {
|
2013-09-23 02:40:27 +02:00
|
|
|
bool LoadFile(lua_State *L, agi::fs::path const& raw_filename) {
|
2013-12-23 20:37:19 +01:00
|
|
|
auto filename = raw_filename;
|
|
|
|
try {
|
|
|
|
filename = agi::fs::Canonicalize(raw_filename);
|
|
|
|
}
|
|
|
|
catch (agi::fs::FileSystemUnknownError const& e) {
|
2014-05-29 14:57:27 +02:00
|
|
|
LOG_E("auto4/lua") << "Error canonicalizing path: " << e.GetMessage();
|
2013-12-23 20:37:19 +01:00
|
|
|
}
|
2013-09-23 02:40:27 +02:00
|
|
|
|
2014-03-21 15:22:31 +01:00
|
|
|
agi::read_file_mapping file(filename);
|
2014-03-21 19:08:26 +01:00
|
|
|
auto buff = file.read();
|
2014-03-27 03:24:37 +01:00
|
|
|
size_t size = static_cast<size_t>(file.size());
|
2013-04-30 15:16:59 +02:00
|
|
|
|
2014-04-28 19:16:28 +02:00
|
|
|
// Discard the BOM if present
|
|
|
|
if (size >= 3 && buff[0] == -17 && buff[1] == -69 && buff[2] == -65) {
|
|
|
|
buff += 3;
|
|
|
|
size -= 3;
|
|
|
|
}
|
|
|
|
|
2013-04-30 15:16:59 +02:00
|
|
|
if (!agi::fs::HasExtension(filename, "moon"))
|
2014-03-21 15:22:31 +01:00
|
|
|
return luaL_loadbuffer(L, buff, size, filename.string().c_str()) == 0;
|
2013-04-30 15:16:59 +02:00
|
|
|
|
|
|
|
// We have a MoonScript file, so we need to load it with that
|
|
|
|
// It might be nice to have a dedicated lua state for compiling
|
|
|
|
// MoonScript to Lua
|
2014-04-28 23:19:16 +02:00
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, "moonscript");
|
2014-04-27 16:20:11 +02:00
|
|
|
|
|
|
|
// Save the text we'll be loading for the line number rewriting in the
|
|
|
|
// error handling
|
2014-03-21 15:22:31 +01:00
|
|
|
lua_pushlstring(L, buff, size);
|
2014-04-27 16:20:11 +02:00
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, ("raw moonscript: " + filename.string()).c_str());
|
|
|
|
|
|
|
|
push_value(L, filename);
|
2013-05-09 15:27:35 +02:00
|
|
|
if (lua_pcall(L, 2, 2, 0))
|
|
|
|
return false; // Leaves error message on stack
|
|
|
|
|
|
|
|
// loadstring returns nil, error on error or a function on success
|
|
|
|
if (lua_isnil(L, 1)) {
|
|
|
|
lua_remove(L, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(L, 1); // Remove the extra nil for the stackchecker
|
|
|
|
return true;
|
2011-09-28 21:45:16 +02:00
|
|
|
}
|
2014-04-26 00:40:43 +02:00
|
|
|
|
|
|
|
static int module_loader(lua_State *L) {
|
|
|
|
int pretop = lua_gettop(L);
|
2014-04-27 16:20:11 +02:00
|
|
|
std::string module(check_string(L, -1));
|
2014-04-26 00:40:43 +02:00
|
|
|
boost::replace_all(module, ".", LUA_DIRSEP);
|
|
|
|
|
|
|
|
// Get the lua package include path (which the user may have modified)
|
|
|
|
lua_getglobal(L, "package");
|
|
|
|
lua_getfield(L, -1, "path");
|
2014-04-27 16:20:11 +02:00
|
|
|
std::string package_paths(check_string(L, -1));
|
2014-04-26 00:40:43 +02:00
|
|
|
lua_pop(L, 2);
|
|
|
|
|
2015-01-11 17:11:22 +01:00
|
|
|
for (auto tok : agi::Split(package_paths, ';')) {
|
|
|
|
std::string filename;
|
|
|
|
boost::replace_all_copy(std::back_inserter(filename), tok, "?", module);
|
2014-04-26 00:40:43 +02:00
|
|
|
|
|
|
|
// If there's a .moon file at that path, load it instead of the
|
|
|
|
// .lua file
|
|
|
|
agi::fs::path path = filename;
|
|
|
|
if (agi::fs::HasExtension(path, "lua")) {
|
|
|
|
agi::fs::path moonpath = path;
|
|
|
|
moonpath.replace_extension("moon");
|
|
|
|
if (agi::fs::FileExists(moonpath))
|
|
|
|
path = moonpath;
|
|
|
|
}
|
|
|
|
|
2014-05-27 04:44:33 +02:00
|
|
|
if (!agi::fs::FileExists(path))
|
|
|
|
continue;
|
|
|
|
|
2014-04-26 00:40:43 +02:00
|
|
|
try {
|
|
|
|
if (!LoadFile(L, path))
|
2014-04-27 16:20:11 +02:00
|
|
|
return error(L, "Error loading Lua module \"%s\":\n%s", path.string().c_str(), check_string(L, 1).c_str());
|
2014-04-26 00:40:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (agi::fs::FileNotFound const&) {
|
|
|
|
// Not an error so swallow and continue on
|
|
|
|
}
|
|
|
|
catch (agi::fs::NotAFile const&) {
|
|
|
|
// Not an error so swallow and continue on
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
2014-05-29 14:57:27 +02:00
|
|
|
return error(L, "Error loading Lua module \"%s\":\n%s", path.string().c_str(), e.GetMessage().c_str());
|
2014-04-26 00:40:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lua_gettop(L) - pretop;
|
|
|
|
}
|
|
|
|
|
2014-04-28 23:19:16 +02:00
|
|
|
bool Install(lua_State *L, std::vector<fs::path> const& include_path) {
|
2014-04-26 00:40:43 +02:00
|
|
|
// set the module load path to include_path
|
|
|
|
lua_getglobal(L, "package");
|
|
|
|
push_value(L, "path");
|
|
|
|
|
2014-07-07 18:06:47 +02:00
|
|
|
push_value(L, "");
|
2014-04-26 00:40:43 +02:00
|
|
|
for (auto const& path : include_path) {
|
2014-07-07 18:06:47 +02:00
|
|
|
lua_pushfstring(L, "%s/?.lua;%s/?/init.lua;", path.string().c_str(), path.string().c_str());
|
2014-04-26 00:40:43 +02:00
|
|
|
lua_concat(L, 2);
|
|
|
|
}
|
|
|
|
|
2014-07-07 18:06:47 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
// No point in checking any of the default locations on Windows since
|
|
|
|
// there won't be anything there
|
|
|
|
push_value(L, "path");
|
|
|
|
lua_gettable(L, -4);
|
|
|
|
lua_concat(L, 2);
|
|
|
|
#endif
|
|
|
|
|
2014-04-26 00:40:43 +02:00
|
|
|
lua_settable(L, -3);
|
|
|
|
|
2014-07-07 17:39:10 +02:00
|
|
|
// Replace the default lua module loader with our unicode compatible one
|
|
|
|
lua_getfield(L, -1, "loaders");
|
|
|
|
push_value(L, exception_wrapper<module_loader>);
|
|
|
|
lua_rawseti(L, -2, 2);
|
|
|
|
lua_pop(L, 2); // loaders, package
|
|
|
|
|
2021-03-27 15:22:25 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Replace the default lua IO functions with our unicode compatibile ones
|
|
|
|
luaL_loadstring(L, "require('unicode-monkeypatch')");
|
|
|
|
if (lua_pcall(L, 0, 0, 0)) {
|
|
|
|
return false; // leave error message
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 23:19:16 +02:00
|
|
|
luaL_loadstring(L, "return require('moonscript').loadstring");
|
|
|
|
if (lua_pcall(L, 0, 1, 0)) {
|
|
|
|
return false; // leave error message
|
|
|
|
}
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, "moonscript");
|
|
|
|
|
|
|
|
return true;
|
2014-04-26 00:40:43 +02:00
|
|
|
}
|
2014-03-27 03:24:37 +01:00
|
|
|
} }
|