source: trunk/src/lolua/baselua.cpp @ 3863

Last change on this file since 3863 was 3863, checked in by touky, 7 years ago

2nd pass on Lua integration.
First pass on EzMesh integration (doesn't work yet)

  • Property svn:eol-style set to LF
File size: 2.7 KB
Line 
1//
2//  Base Lua class for Lua script loading
3//
4//  Copyright: (c) 2009-2015 Sam Hocevar <sam@hocevar.net>
5//                 2009-2015 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
6//
7//  This program is free software. It comes without any warranty, to
8//  the extent permitted by applicable law. You can redistribute it
9//  and/or modify it under the terms of the Do What the Fuck You Want
10//  to Public License, Version 2, as published by the WTFPL Task Force.
11//  See http://www.wtfpl.net/ for more details.
12//
13
14#include <lol/engine-internal.h>
15
16#include <cstring>
17#include <cstdlib>
18#include <ctype.h>
19
20namespace lol
21{
22
23//-----------------------------------------------------------------------------
24class LuaBaseData
25{
26    friend class Lolua::Loader;
27
28    static int LuaPanic(LuaState* L)
29    {
30        char const *message = lua_tostring(L, -1);
31        Log::Error("%s\n", message);
32        DebugAbort();
33        return 0;
34    }
35
36    static int LuaDoFile(LuaState *L)
37    {
38        if (lua_isnoneornil(L, 1))
39            return LUA_ERRFILE;
40
41        LuaCharPtr var; var.Get(L, 1);
42        char const *filename = var;// lua_tostring(L, 1);
43        int status = LUA_ERRFILE;
44
45        array<String> pathlist = System::GetPathList(filename);
46        File f;
47        for (int i = 0; i < pathlist.Count(); ++i)
48        {
49            f.Open(pathlist[i], FileAccess::Read);
50            if (f.IsValid())
51            {
52                String s = f.ReadString();
53                f.Close();
54
55                Log::Debug("loading Lua file %s\n", pathlist[i].C());
56                status = luaL_dostring(L, s.C());
57                break;
58            }
59        }
60
61        if (status == LUA_ERRFILE)
62            Log::Error("could not find Lua file %s\n", filename);
63
64        lua_pop(L, 1);
65
66        return status;
67    }
68};
69
70namespace Lolua
71{
72
73//-----------------------------------------------------------------------------
74Loader::Loader()
75{
76    m_lua_state = luaL_newstate();
77    lua_atpanic(m_lua_state, LuaBaseData::LuaPanic);
78    luaL_openlibs(m_lua_state);
79
80    /* Override dofile() */
81    LuaFunction do_file(m_lua_state, "dofile", LuaBaseData::LuaDoFile);
82}
83
84//-----------------------------------------------------------------------------
85Loader::~Loader()
86{
87    lua_close(m_lua_state);
88}
89
90//-----------------------------------------------------------------------------
91bool Loader::ExecLua(String const &lua)
92{
93    const char* c = lua_pushstring(m_lua_state, lua.C());
94    int status = LuaBaseData::LuaDoFile(m_lua_state);
95    return status == 0;
96}
97
98//-----------------------------------------------------------------------------
99LuaState* Loader::GetLuaState()
100{
101    return m_lua_state;
102}
103
104} /* namespace Lolua */
105
106} /* namespace lol */
107
Note: See TracBrowser for help on using the repository browser.