-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
137 lines (111 loc) · 3.1 KB
/
main.cpp
File metadata and controls
137 lines (111 loc) · 3.1 KB
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
/************************************************************
demo.c
@author Christophe Berbizier (cberbizier@peersuasive.com)
@license GPLv3
@copyright
(c) 2014, Peersuasive Technologies
*************************************************************/
// realpath
#ifndef __MINGW32__
#include <limits.h> /* PATH_MAX */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#include <iostream>
#include <lua.hpp>
#include <string>
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include "oResult.h"
#if defined XSTATIC || FULL_XSTATIC
#if LUCE_ANDROID
#include <jni.h>
extern int luaopen_core_and(lua_State*, JNIEnv*, jobject);
#else
extern int luaopen_core(lua_State*);
#endif
#endif
#if LUCE_ANDROID
void Java_org_peersuasive_luce_luce_luceResumeApp( JNIEnv* env, jobject activity ) {
int argc = 1;
const char **argv;
#else
int main(int argc, char *argv[]) {
#endif
int status = 0;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
#if defined XSTATIC || FULL_XSTATIC
#if LUCE_ANDROID
luaopen_core_and(L, env, activity);
#else
luaopen_core(L);
#endif
lua_pop(L,1);
#endif
status = luaL_loadbuffer(L, luaJIT_BC_oResult, luaJIT_BC_oResult_SIZE, NULL);
if(status) {
fprintf(stderr, "Error while loading luce class\n");
lua_error(L);
}
// set relative path to resources
// TODO: guess os type to get path sep and .so ext
// possibly extend to FULL_STATIC also
// and set _CPATH as well
#ifndef FULL_XSTATIC
lua_getglobal( L, "package" );
lua_getfield( L, -1, "path" );
std::string cur_path( "./?.lua;classes/?.lua;" );
cur_path.append( lua_tostring( L, -1 ) );
lua_pushstring( L, cur_path.c_str() );
lua_setfield( L, -3, "path" );
lua_pop( L, 2 ); // field + package
status = luaL_loadfile(L, "main.lua");
#endif
if(!status) {
#ifdef __MINGW32__
const char *prog = argv[0];
#else
// get real path of program: WARNING: Linux only
// TODO: make it portable
// @see: http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
char prog[PATH_MAX + 1] = {0};
char *res = realpath(argv[0], prog);
if (!res) {
char proc[20];
sprintf( proc, "/proc/%d/exe", getpid() );
res = realpath(proc, prog);
if(!res) {
perror("main: Failed to get program path.");
exit(EXIT_FAILURE);
}
}
#endif
lua_pushstring(L, prog);
for(int i=1;i<argc;++i)
lua_pushstring(L, argv[i]);
if ((status = lua_pcall(L, argc, 1, 0)) )
lua_error(L);
else
status = lua_tonumber(L, -1);
} else {
fprintf(stderr, "Missing or error with main file: %s\n", "main.lua");
lua_error(L);
}
#if ! LUCE_ANDROID
lua_close(L);
return status;
#endif
}
#ifdef __MINGW32__
#include "wmain.c"
#endif // MINGW32
//#endif // LUCE_ANDROID
#ifdef __cplusplus
}
#endif