2013-04-13 17:12:16 +02:00
|
|
|
// Copyright (c) 2013, 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.
|
2009-07-29 07:43:02 +02:00
|
|
|
|
|
|
|
/// @file auto4_lua_scriptreader.cpp
|
|
|
|
/// @brief Script-file reader for Lua 5.1-based scripting engine
|
|
|
|
/// @ingroup scripting
|
|
|
|
///
|
2007-05-01 23:51:49 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2007-05-01 23:51:49 +02:00
|
|
|
#include "auto4_lua_scriptreader.h"
|
|
|
|
|
2013-09-23 03:44:00 +02:00
|
|
|
#include "auto4_lua_utils.h"
|
|
|
|
|
2011-09-28 21:45:16 +02:00
|
|
|
#include <libaegisub/io.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
|
|
|
|
2013-02-17 04:47:31 +01:00
|
|
|
#include <fstream>
|
2013-04-30 15:16:59 +02:00
|
|
|
#include <lua.hpp>
|
|
|
|
#include <memory>
|
2013-02-17 04:47:31 +01:00
|
|
|
|
2011-09-28 21:45:16 +02:00
|
|
|
namespace Automation4 {
|
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) {
|
|
|
|
LOG_E("auto4/lua") << "Error canonicalizing path: " << e.GetChainedMessage();
|
|
|
|
}
|
2013-09-23 02:40:27 +02:00
|
|
|
|
2013-04-30 15:16:59 +02:00
|
|
|
std::unique_ptr<std::istream> file(agi::io::Open(filename, true));
|
|
|
|
file->seekg(0, std::ios::end);
|
|
|
|
size_t size = file->tellg();
|
|
|
|
file->seekg(0, std::ios::beg);
|
|
|
|
|
|
|
|
std::string buff;
|
|
|
|
buff.resize(size);
|
|
|
|
|
|
|
|
// Discard the BOM if present
|
|
|
|
file->read(&buff[0], 3);
|
|
|
|
size_t start = file->gcount();
|
|
|
|
if (start == 3 && buff[0] == -17 && buff[1] == -69 && buff[2] == -65) {
|
|
|
|
buff.resize(size - 3);
|
|
|
|
start = 0;
|
2007-05-01 23:51:49 +02:00
|
|
|
}
|
|
|
|
|
2013-04-30 15:16:59 +02:00
|
|
|
file->read(&buff[start], size - start);
|
|
|
|
|
|
|
|
if (!agi::fs::HasExtension(filename, "moon"))
|
|
|
|
return luaL_loadbuffer(L, &buff[0], buff.size(), filename.string().c_str()) == 0;
|
|
|
|
|
2013-09-23 03:44:00 +02:00
|
|
|
// Save the text we'll be loading for the line number rewriting in the
|
|
|
|
// error handling
|
|
|
|
push_value(L, buff);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, ("raw moonscript: " + filename.string()).c_str());
|
|
|
|
|
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
|
|
|
|
if (luaL_dostring(L, "return require('moonscript').loadstring"))
|
|
|
|
return false; // Leaves error message on stack
|
|
|
|
lua_pushlstring(L, &buff[0], buff.size());
|
2013-05-05 03:31:33 +02:00
|
|
|
lua_pushstring(L, filename.string().c_str());
|
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
|
|
|
}
|
2011-12-28 22:27:06 +01:00
|
|
|
}
|