forked from ForsakenX/forsaken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_common.c
167 lines (152 loc) · 4.08 KB
/
lua_common.c
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
/* vim:set sw=4 ts=4:
*
* Copyright (C) 2010 Daniel Aquino
* Copyright (C) 2010 Pim Goossens
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include "lua_common.h"
#include "luasocket.h"
#include "mime.h"
#include "main.h"
lua_State *L1;
int lua_dofile(lua_State *L, const char *name)
{
char path[100];
snprintf(path, 100, "scripts/%s", name);
if (luaL_dofile(L,path))
{
if (lua_isstring(L, -1))
DebugPrintf("Error while loading %s: %s\n", path, lua_tostring(L, -1));
else
DebugPrintf("Unknown error while loading %s\n", path);
lua_pop(L, 1);
return 1;
}
return 0;
}
static int lua_debug_str(lua_State *state)
{
char * str = (char*) lua_tostring(state,1); // get argument
DebugPrintf(str);
return 0; // number of results
}
static int lua_alert(lua_State *state)
{
char * str = (char*) lua_tostring(state,1); // get argument
Msg( str );
return 0; // number of results
}
static int lua_touch_file(lua_State *state)
{
char* path = (char*) lua_tostring(state,1); // get argument
touch_file( path );
return 0; // number of results
}
static int lua_register_funcs(void)
{
lua_register(L1,"touch_file",lua_touch_file);
lua_register(L1,"debug",lua_debug_str);
lua_register(L1,"alert",lua_alert);
return 0;
}
static int load_init_file( void )
{
if(lua_dofile(L1, "init.lua"))
{
Msg("error failed to load init.lua");
return 1;
}
return 0;
}
static int run_init_func( void )
{
int err = 0;
lua_settop(L1, 0);
lua_getglobal(L1, "init"); /* [bottom] init [top] */
#ifdef DEBUG_ON
lua_pushboolean(L1, 1);
#else
lua_pushboolean(L1, 0);
#endif
err = lua_pcall(L1, 1, 0, 0);
if (err)
{
const char * ptr = lua_tostring(L1, -1);
if(!ptr)
ptr = "Unknown Error";
Write_File( "Logs/lua_init_error.txt", (char*) ptr, (long)strlen(ptr) );
Msg("error lua init: %s\n", (char*) ptr );
}
return err;
}
static void assign_loader( char * name, lua_CFunction loader )
{
// package.preload[ name ] = loader
lua_getglobal(L1, "package");
lua_getfield(L1, -1, "preload");
lua_pushcfunction(L1, loader);
lua_setfield(L1, -2, name);
lua_pop(L1, 2);
}
#ifdef MINIUPNP
int luaopen_miniupnp(lua_State *L);
#endif
static void assign_loaders( void )
{
assign_loader( "socket.core", luaopen_socket_core );
assign_loader( "mime.core", luaopen_mime_core );
#ifdef MINIUPNP
assign_loader( "miniupnp", luaopen_miniupnp );
#endif
}
static int lua_create(void)
{
/* luaL_Reg x[] = { { NULL, NULL } }; */
L1 = luaL_newstate();
if (!L1)
{
Msg("error: while initializing Lua!\n");
return 1;
}
luaL_openlibs(L1);
assign_loaders();
return 0;
}
int lua_init()
{
ASSERT(lua_create());
ASSERT(lua_register_funcs());
ASSERT(load_init_file());
ASSERT(run_init_func());
return 0;
}
void lua_shutdown(void)
{
if(L1)
lua_close(L1);
}