Skip to content
Snippets Groups Projects
Commit 696e77f5 authored by dg's avatar dg
Browse files

bootstrapping code

get self path for linux windows and osx to find our own executable


git-svn-id: http://svn.net-core.org/repos/t-engine4@158 51575b47-30f0-44d4-a5cc-537603b46e54
parent 61eb9e95
No related branches found
No related tags found
No related merge requests found
-- This file is the very first lua file loaded
-- It will be called before anything else is setup, including paths
-- Usualy it will be put inside a zip that is concatenated to the executable itself
print("Booting T-Engine from: "..tostring(__SELFEXE))
-- Mount the engine, either from a path guessed from SELFEXE, or directly from current dir
if __SELFEXE then
local dir = __SELFEXE
-- Remove bin/Debug from the path, to make dev easier
dir = dir:gsub("bin"..fs.getPathSeparator().."Debug"..fs.getPathSeparator(), "")
-- Now remove executable name
dir = dir:gsub("(.*"..fs.getPathSeparator()..").+", "%1")
print("SelfExe gave us app directory of:", dir)
fs.mount(dir..fs.getPathSeparator().."game"..fs.getPathSeparator().."thirdparty", "/", true)
fs.mount(dir..fs.getPathSeparator().."game", "/", true)
else
fs.mount("game"..fs.getPathSeparator().."thirdparty", "/", true)
fs.mount("game", "/", true)
end
-- We need it no more, lets forget about it just it case some malovelant script tried something silly
__SELFEXE = nil
......@@ -50,14 +50,17 @@ configuration "macosx"
"/Library/Frameworks/SDL_gfx.framework/Headers",
"/Library/Frameworks/SDL_mixer.framework/Headers"
}
defines { "USE_TENGINE_MAIN" }
defines { "USE_TENGINE_MAIN", 'SELFEXE_MACOSX' }
targetdir "."
configuration "not macosx"
links { "SDL", "SDL_ttf", "SDL_image", "SDL_mixer", "GL", "GLU" }
configuration "windows"
defines { [[TENGINE_HOME_PATH='"T-Engine"']] }
defines { [[TENGINE_HOME_PATH='"T-Engine"']], 'SELFEXE_WINDOWS' }
configuration "linux"
defines { [[TENGINE_HOME_PATH='"T-Engine"']], 'SELFEXE_LINUX' }
----------------------------------------------------------------
......
/*
* Copyright (C) 2006, Greg McIntyre
* All rights reserved. See the file named COPYING in the distribution
* for more details.
*/
#ifndef MAIN2
#define MAIN2
#include "display_sdl.h"
......
/**
* \mainpage SDL Display Library
*
* \section copyright Copyright
*
* Copyright (C) 2006, Greg McIntyre
* All rights reserved. See the file named COPYING in the distribution
* for more details.
*
*
\verbatim
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
\endverbatim
*/
/**
* \file display_sdl.h
* SDL Display library header
*/
#ifndef DISPLAY_SDL_H
#define DISPLAY_SDL_H
......
#include "getself.h"
#if defined(SELFEXE_LINUX)
#include <limits.h>
#include <stdlib.h>
const char *get_self_executable(int argc, char **argv)
{
char res[PATH_MAX];
// On linux systems /proc/self/exe is always a symlink to the real executable, so we jsut resolve it
realpath("/proc/self/exe", res);
return res;
}
#elif defined(SELFEXE_WINDOWS)
#include <windows.h>
const char *get_self_executable(int argc, char **argv)
{
TCHAR szEXEPath[MAX_PATH];
GetModuleFileName(NULL,szEXEPath,MAX_PATH);
return szEXEPath;
}
#elif defined(SELFEXE_MACOSX)
#include <mach-o/dyld.h>
#include <string.h>
const char *get_self_executable(int argc, char **argv)
{
size_t sz = 0;
char *buf;
char *sl;
_NSGetExecutablePath(NULL, &sz);
buf = (char*) malloc(++sz);
_NSGetExecutablePath(buf, &sz);
sl = strrchr(buf, '/');
*(sl + 1) = '\0';
return buf;
}
#else
const char *get_self_executable(int argc, char **argv)
{
return NULL;
}
#endif
#ifndef _GETSELF_H_
#define _GETSELF_H_
extern const char *get_self_executable(int argc, char **argv);
#endif
......@@ -12,6 +12,7 @@
#include "script.h"
#include "physfs.h"
#include "core_lua.h"
#include "getself.h"
#define WIDTH 800
#define HEIGHT 600
......@@ -369,13 +370,24 @@ int resizeWindow(int width, int height)
int main(int argc, char *argv[])
{
const char *selfexe;
// RNG init
init_gen_rand(time(NULL));
/***************** Physfs Init *****************/
PHYSFS_init(argv[0]);
PHYSFS_mount("game/thirdparty", "/", 1);
PHYSFS_mount("game/", "/", 1);
selfexe = get_self_executable(argc, argv);
if (selfexe)
{
PHYSFS_mount(selfexe, "/", 1);
}
else
{
printf("NO SELFEXE: bootstrapping from CWD\n");
PHYSFS_mount("bootstrap", "/bootstrap", 1);
}
/***************** Lua Init *****************/
L = lua_open(); /* create state */
......@@ -391,6 +403,26 @@ int main(int argc, char *argv[])
lua_newtable(L);
lua_setglobal(L, "__uids");
// Tell the boostrapping code the selfexe path
if (selfexe)
lua_pushstring(L, selfexe);
else
lua_pushnil(L);
lua_setglobal(L, "__SELFEXE");
// Run bootstrapping
if (!luaL_loadfile(L, "/bootstrap/boot.lua"))
{
docall(L, 0, 0);
}
// Could not load bootstrap! Try to mount the engine from working directory as last resort
else
{
printf("WARNING: No bootstrap code found, defaulting to working directory for engine code!\n");
PHYSFS_mount("game/thirdparty", "/", 1);
PHYSFS_mount("game/", "/", 1);
}
// initialize engine and set up resolution and depth
Uint32 flags=SDL_INIT_VIDEO | SDL_INIT_TIMER;
if (SDL_Init (flags) < 0) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment