1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #include "common/version.h" |
22 | #include "common/runtime.h" |
23 | #include "modules/love/love.h" |
24 | #include <SDL.h> |
25 | |
26 | #ifdef LOVE_BUILD_EXE |
27 | |
28 | // Lua |
29 | extern "C" { |
30 | #include <lua.h> |
31 | #include <lualib.h> |
32 | #include <lauxlib.h> |
33 | } |
34 | |
35 | #ifdef LOVE_WINDOWS |
36 | #include <windows.h> |
37 | #endif // LOVE_WINDOWS |
38 | |
39 | #ifdef LOVE_MACOSX |
40 | #include "common/macosx.h" |
41 | #include <unistd.h> |
42 | #endif // LOVE_MACOSX |
43 | |
44 | #ifdef LOVE_IOS |
45 | #include "common/ios.h" |
46 | #endif |
47 | |
48 | #ifdef LOVE_WINDOWS |
49 | extern "C" |
50 | { |
51 | |
52 | // Prefer the higher performance GPU on Windows systems that use nvidia Optimus. |
53 | // http://developer.download.nvidia.com/devzone/devcenter/gamegraphics/files/OptimusRenderingPolicies.pdf |
54 | // TODO: Re-evaluate in the future when the average integrated GPU in Optimus |
55 | // systems is less mediocre? |
56 | LOVE_EXPORT DWORD NvOptimusEnablement = 1; |
57 | |
58 | // Same with AMD GPUs. |
59 | // https://community.amd.com/thread/169965 |
60 | LOVE_EXPORT DWORD AmdPowerXpressRequestHighPerformance = 1; |
61 | } |
62 | #endif // LOVE_WINDOWS |
63 | |
64 | #ifdef LOVE_LEGENDARY_APP_ARGV_HACK |
65 | |
66 | #include <vector> |
67 | |
68 | // Explicitly instantiate std::vector<std::string> to work around linker issues |
69 | // with libc++ when symbols are hidden-by-default. |
70 | // https://stackoverflow.com/a/48273604 |
71 | template class std::vector<std::string>; |
72 | |
73 | static void get_app_arguments(int argc, char **argv, int &new_argc, char **&new_argv) |
74 | { |
75 | std::vector<std::string> temp_argv; |
76 | for (int i = 0; i < argc; i++) |
77 | { |
78 | // Don't copy -psn_xxx argument from argv. |
79 | if (i == 0 || strncmp(argv[i], "-psn_" , 5) != 0) |
80 | temp_argv.push_back(std::string(argv[i])); |
81 | } |
82 | |
83 | // If it exists, add the love file in love.app/Contents/Resources/ to argv. |
84 | std::string loveResourcesPath; |
85 | bool fused = true; |
86 | #if defined(LOVE_MACOSX) |
87 | loveResourcesPath = love::macosx::getLoveInResources(); |
88 | #elif defined(LOVE_IOS) |
89 | loveResourcesPath = love::ios::getLoveInResources(fused); |
90 | #endif |
91 | if (!loveResourcesPath.empty()) |
92 | { |
93 | std::vector<std::string>::iterator it = temp_argv.begin(); |
94 | it = temp_argv.insert(it + 1, loveResourcesPath); |
95 | |
96 | // Run in pseudo-fused mode. |
97 | if (fused) |
98 | temp_argv.insert(it + 1, std::string("--fused" )); |
99 | } |
100 | #ifdef LOVE_MACOSX |
101 | else |
102 | { |
103 | // Check for a drop file string, if the app wasn't launched in a |
104 | // terminal. Checking for the terminal is a pretty big hack, but works |
105 | // around an issue where OS X will switch Spaces if the terminal |
106 | // launching love is in its own full-screen Space. |
107 | if (!isatty(STDIN_FILENO)) |
108 | { |
109 | // Static to keep the same value after love.event.equit("restart"). |
110 | static std::string dropfilestr = love::macosx::checkDropEvents(); |
111 | if (!dropfilestr.empty()) |
112 | temp_argv.insert(temp_argv.begin() + 1, dropfilestr); |
113 | } |
114 | } |
115 | #endif |
116 | |
117 | // Copy temp argv vector to new argv array. |
118 | new_argc = (int) temp_argv.size(); |
119 | new_argv = new char *[new_argc+1]; |
120 | |
121 | for (int i = 0; i < new_argc; i++) |
122 | { |
123 | new_argv[i] = new char[temp_argv[i].length() + 1]; |
124 | strcpy(new_argv[i], temp_argv[i].c_str()); |
125 | } |
126 | |
127 | new_argv[new_argc] = NULL; |
128 | } |
129 | |
130 | #endif // LOVE_LEGENDARY_APP_ARGV_HACK |
131 | |
132 | static int love_preload(lua_State *L, lua_CFunction f, const char *name) |
133 | { |
134 | lua_getglobal(L, "package" ); |
135 | lua_getfield(L, -1, "preload" ); |
136 | lua_pushcfunction(L, f); |
137 | lua_setfield(L, -2, name); |
138 | lua_pop(L, 2); |
139 | return 0; |
140 | } |
141 | |
142 | enum DoneAction |
143 | { |
144 | DONE_QUIT, |
145 | DONE_RESTART, |
146 | }; |
147 | |
148 | static DoneAction runlove(int argc, char **argv, int &retval) |
149 | { |
150 | // Oh, you just want the version? Okay! |
151 | if (argc > 1 && strcmp(argv[1], "--version" ) == 0) |
152 | { |
153 | #ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK |
154 | const char *err = nullptr; |
155 | love_openConsole(err); |
156 | #endif |
157 | printf("LOVE %s (%s)\n" , love_version(), love_codename()); |
158 | retval = 0; |
159 | return DONE_QUIT; |
160 | } |
161 | |
162 | // Create the virtual machine. |
163 | lua_State *L = luaL_newstate(); |
164 | luaL_openlibs(L); |
165 | |
166 | // LuaJIT-specific setup needs to be done as early as possible - before |
167 | // get_app_arguments because that loads external library code. This is also |
168 | // loaded inside require("love"). Note that it doesn't use the love table. |
169 | love_preload(L, luaopen_love_jitsetup, "love.jitsetup" ); |
170 | lua_getglobal(L, "require" ); |
171 | lua_pushstring(L, "love.jitsetup" ); |
172 | lua_call(L, 1, 0); |
173 | |
174 | #ifdef LOVE_LEGENDARY_APP_ARGV_HACK |
175 | int hack_argc = 0; |
176 | char **hack_argv = nullptr; |
177 | get_app_arguments(argc, argv, hack_argc, hack_argv); |
178 | argc = hack_argc; |
179 | argv = hack_argv; |
180 | #endif // LOVE_LEGENDARY_APP_ARGV_HACK |
181 | |
182 | // Add love to package.preload for easy requiring. |
183 | love_preload(L, luaopen_love, "love" ); |
184 | |
185 | // Add command line arguments to global arg (like stand-alone Lua). |
186 | { |
187 | lua_newtable(L); |
188 | |
189 | if (argc > 0) |
190 | { |
191 | lua_pushstring(L, argv[0]); |
192 | lua_rawseti(L, -2, -2); |
193 | } |
194 | |
195 | lua_pushstring(L, "embedded boot.lua" ); |
196 | lua_rawseti(L, -2, -1); |
197 | |
198 | for (int i = 1; i < argc; i++) |
199 | { |
200 | lua_pushstring(L, argv[i]); |
201 | lua_rawseti(L, -2, i); |
202 | } |
203 | |
204 | lua_setglobal(L, "arg" ); |
205 | } |
206 | |
207 | // require "love" |
208 | lua_getglobal(L, "require" ); |
209 | lua_pushstring(L, "love" ); |
210 | lua_call(L, 1, 1); // leave the returned table on the stack. |
211 | |
212 | // Add love._exe = true. |
213 | // This indicates that we're running the standalone version of love, and not |
214 | // the library version. |
215 | { |
216 | lua_pushboolean(L, 1); |
217 | lua_setfield(L, -2, "_exe" ); |
218 | } |
219 | |
220 | // Pop the love table returned by require "love". |
221 | lua_pop(L, 1); |
222 | |
223 | // require "love.boot" (preloaded when love was required.) |
224 | lua_getglobal(L, "require" ); |
225 | lua_pushstring(L, "love.boot" ); |
226 | lua_call(L, 1, 1); |
227 | |
228 | // Turn the returned boot function into a coroutine and call it until done. |
229 | lua_newthread(L); |
230 | lua_pushvalue(L, -2); |
231 | int stackpos = lua_gettop(L); |
232 | int nres; |
233 | while (love::luax_resume(L, 0, &nres) == LUA_YIELD) |
234 | #if LUA_VERSION_NUM >= 504 |
235 | lua_pop(L, nres); |
236 | #else |
237 | lua_pop(L, lua_gettop(L) - stackpos); |
238 | #endif |
239 | |
240 | retval = 0; |
241 | DoneAction done = DONE_QUIT; |
242 | |
243 | // if love.boot() returns "restart", we'll start up again after closing this |
244 | // Lua state. |
245 | if (lua_type(L, -1) == LUA_TSTRING && strcmp(lua_tostring(L, -1), "restart" ) == 0) |
246 | done = DONE_RESTART; |
247 | if (lua_isnumber(L, -1)) |
248 | retval = (int) lua_tonumber(L, -1); |
249 | |
250 | lua_close(L); |
251 | |
252 | #if defined(LOVE_LEGENDARY_APP_ARGV_HACK) && !defined(LOVE_IOS) |
253 | if (hack_argv) |
254 | { |
255 | for (int i = 0; i<hack_argc; ++i) |
256 | delete [] hack_argv[i]; |
257 | delete [] hack_argv; |
258 | } |
259 | #endif // LOVE_LEGENDARY_APP_ARGV_HACK |
260 | |
261 | return done; |
262 | } |
263 | |
264 | int main(int argc, char **argv) |
265 | { |
266 | if (strcmp(LOVE_VERSION_STRING, love_version()) != 0) |
267 | { |
268 | printf("Version mismatch detected!\nLOVE binary is version %s\n" |
269 | "LOVE library is version %s\n" , LOVE_VERSION_STRING, love_version()); |
270 | return 1; |
271 | } |
272 | |
273 | int retval = 0; |
274 | DoneAction done = DONE_QUIT; |
275 | |
276 | do |
277 | { |
278 | done = runlove(argc, argv, retval); |
279 | |
280 | #ifdef LOVE_IOS |
281 | // on iOS we should never programmatically exit the app, so we'll just |
282 | // "restart" when that is attempted. Games which use threads might cause |
283 | // some issues if the threads aren't cleaned up properly... |
284 | done = DONE_RESTART; |
285 | #endif |
286 | } while (done != DONE_QUIT); |
287 | |
288 | #ifdef LOVE_ANDROID |
289 | SDL_Quit(); |
290 | #endif |
291 | |
292 | return retval; |
293 | } |
294 | |
295 | #endif // LOVE_BUILD_EXE |
296 | |