File: LuaScript.cpp

package info (click to toggle)
arkrpg 0.1.4b-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,104 kB
  • ctags: 5,445
  • sloc: cpp: 28,145; sh: 9,006; ansic: 3,259; makefile: 344
file content (230 lines) | stat: -rw-r--r-- 5,214 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* $Id: LuaScript.cpp,v 1.7 2003/03/22 23:51:49 mrq Exp $
**
** Ark - Libraries, Tools & Programs for MMORPG developpements.
** Copyright (C) 1999-2000 The Contributors of the Ark Project
** Please see the file "AUTHORS" for a list of contributors
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <Modules/Lua/LuaScript.h>
#include <Modules/Lua/LuaWorld.h>
#include <Modules/Lua/LuaEntity.h>
#include <Ark/ArkSystem.h>

#include <sstream>

// memset/memcpy functions
#include <string.h>


extern "C"
{
#include <lua.h>
#include <lualib.h>
}

namespace Ark
{
   lua_State* g_LuaState;
   
   LuaScript::LuaScript ()
   {
      lua_State* L = lua_open(0);
      lua_baselibopen(L);
      lua_mathlibopen(L);
      
      g_LuaState = L;
      Luna<LuaEntity>::Register(L);
      Luna<LuaWorld>::Register(L);
      Luna<LuaEngine>::Register(L);


      ////////////
      // Set an entry in the registry.
      lua_getregistry(L);
      lua_pushstring (L, "LuaScript");
      lua_pushuserdata(L,this);
      lua_settable(L,-3);
      lua_pop (L, -1);
      
      lua_dostring(g_LuaState, "collectgarbage()");
   }
   
   LuaScript::~LuaScript ()
   {
     if (g_LuaState) 
	 lua_close(g_LuaState);
   }
   
   bool
   LuaScript::LoadLibrary (const String &file)
   {
      std::ostringstream stream;
      stream << "ark = Engine ();\n"
	     << "ark.world = World();\n"
	     << "ark.log = print;\n"
	     << "ark.true = 1;\n"
	     << "ark.false = 0;\n"
	     << "ark.play_once = " << ModelState::ONCE << ";\n"
	     << "ark.play_loop = " << ModelState::LOOP << ";\n"
	 ;
      
      lua_dostring (g_LuaState, stream.str().c_str());
      
      if (!Script::LoadLibrary (file))
	 return false;
      
      lua_dostring (g_LuaState, "ark.init();");
     
      return true;
   }
   
   bool
   LuaScript::LoadScript (const String& file)
   {
      AutoReadStream stream (file);
      std::ifstream& f = stream.Get();
      
      // use seekg/tellg to find size of buffer
      f.seekg(0, std::ios::end);
      const long size = f.tellg();
      f.seekg(0, std::ios::beg);
      
      if (size <= 0)
	 return false;
      
      char *buf = new char[size+1];
      assert (buf != NULL);
      
      memset(buf, 0, size+1);

      // Read the max number of chars possible
      f.read (buf, size);
      
      // Get the number of char read
      // On win32, there is a CRLF issue, so (readchars < size)
      const long readchars = f.gcount();
      
      // Check that we have read something
      if (readchars <= 0)
      {
	 delete[] buf;
	 return false;
      }
      
      lua_dobuffer(g_LuaState, buf, readchars, file.c_str());  
      return true;
   }
   
   bool
   LuaScript::NextFrame ()
   {
      lua_dostring (g_LuaState, "ark.nextframe();");
      return true;
   }

   bool
   LuaScript::Main ()
   {
      lua_dostring (g_LuaState, "ark.main();");
      return true;
   }
   
   EngineEntity *
   LuaScript::NewEntity ()
   {
      LuaEntity *ent;
      lua_getglobal (g_LuaState, "Entity");
      lua_call (g_LuaState, 0, 1);
      lua_getobject (g_LuaState, -1, &ent);
      lua_pop (g_LuaState, 1);
      
      return ent;
   }
   

   ////////////////////////////
   
   void lua_GetScript(lua_State *L, LuaScript **s)
   {
      lua_getregistry(L);
      lua_pushstring (L, "LuaScript");
      lua_gettable(L,-2);

      *s = (LuaScript*) lua_touserdata (L, -1);
      lua_pop (L, -2);
   }

   LuaEngine::LuaEngine(lua_State *L)
   {}

   LuaEngine::~LuaEngine()
   {
   }
   

   int
   LuaEngine::require (lua_State *L)
   {
      LuaScript *sc;
      String name = lua_tostring (L, -1);
      lua_pop (L,1);
     
      lua_GetScript(L,&sc);
      sc->LoadScript (name);
      return 0;
   }

   int
   LuaEngine::get_env (lua_State *L)
   {
      String name = lua_tostring (L, -1);
      lua_pop (L,1);
     
      lua_pushstring(L, Sys()->GetEnv(name).c_str());
      return 1;
   }


   int
   LuaEngine::version (lua_State *L)
   {
      return 0;
   }

   const char LuaEngine::className[] = "Engine";
   const Luna<LuaEngine>::RegType LuaEngine::Register[] = 
   {
      {"require",         &LuaEngine::require},
      {"version",         &LuaEngine::version},
      {"get_env",         &LuaEngine::get_env},
      {0}
   };


}

#define ARK_MODULE
#include <Ark/ArkFactoryReg.h>

class LuaScriptFactory : public Ark::ScriptFactory
{
   public:
      virtual ~LuaScriptFactory() {};
      virtual Ark::Script *NewScript() {return new Ark::LuaScript;}
};

ARK_REGISTER("ark::Script::Lua", LuaScriptFactory);