std::string getDataPath(std::string filename) { static std::string dataPath; if (dataPath.size()) return concat_path(dataPath, filename); const char *env_path = getenv("CRPROPA_DATA_PATH"); if (env_path) { if (is_directory(env_path)) { dataPath = env_path; KISS_LOG_INFO << "getDataPath: use environment variable, " << dataPath << std::endl; return concat_path(dataPath, filename); } } #ifdef CRPROPA_INSTALL_PREFIX { std::string _path = CRPROPA_INSTALL_PREFIX "/share/crpropa"; if (is_directory(_path)) { dataPath = _path; KISS_LOG_INFO << "getDataPath: use install prefix, " << dataPath << std::endl; return concat_path(dataPath, filename); } } #endif { std::string _path = executable_path() + "../data"; if (is_directory(_path)) { dataPath = _path; KISS_LOG_INFO << "getDataPath: use executable path, " << dataPath << std::endl; return concat_path(dataPath, filename); } } dataPath = "data"; KISS_LOG_INFO << "getDataPath: use default, " << dataPath << std::endl; return concat_path(dataPath, filename); }
const char* get_executable_directory() { static char path[FOUNDATION_MAX_PATH_LENGTH + 1]; static bool path_initialized = false; if (!path_initialized) { filesystem::path executable_path(get_executable_path()); assert(executable_path.has_filename()); executable_path.remove_filename(); assert(executable_path.string().size() <= FOUNDATION_MAX_PATH_LENGTH); strncpy(path, executable_path.string().c_str(), sizeof(path) - 1); path[sizeof(path) - 1] = '\0'; path_initialized = true; } return path; }
int neko_has_embedded_module( neko_vm *vm ) { char *exe = executable_path(); unsigned char id[8]; int pos; if( exe == NULL ) return 0; self = fopen(exe,"rb"); if( self == NULL ) return 0; fseek(self,-8,SEEK_END); if( fread(id,1,8,self) != 8 || id[0] != 'N' || id[1] != 'E' || id[2] != 'K' || id[3] != 'O' ) { fclose(self); return 0; } pos = id[4] | id[5] << 8 | id[6] << 16; fseek(self,pos,SEEK_SET); // flags if( (id[7] & 1) == 0 ) neko_vm_jit(vm,1); return 1; }
int neko_has_embedded_module( neko_vm *vm ) { char *exe = executable_path(); unsigned char id[8]; int beg=-1, end=0; if( exe == NULL ) return 0; #ifdef SEPARATE_SECTION_FOR_BYTECODE /* Look for a ,nekobytecode section in the executable... */ if ( val_true != elf_find_embedded_bytecode(exe,&beg,&end) ) { /* Couldn't find a .nekobytecode section, fallback to looking at the end of the executable... */ beg = -1; end = 0; } #endif /* Back up eight bytes to the possible bytecode signature... */ end -= 8; self = fopen(exe,"rb"); if( self == NULL ) return 0; fseek(self,end,(end<0)?SEEK_END:SEEK_SET); if( fread(id,1,8,self) != 8 || id[0] != 'N' || id[1] != 'E' || id[2] != 'K' || id[3] != 'O' ) { fclose(self); return 0; } if ( -1 == beg ) { beg = id[4] | id[5] << 8 | id[6] << 16; } fseek(self,beg,(beg<0)?SEEK_END:SEEK_SET); // flags if( (id[7] & 1) == 0 ) neko_vm_jit(vm,1); return 1; }
String Get(Key key) { switch(key) { case FILE_EXE: { #if SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_MAC // Executable path can have relative references ("..") depending on // how the app was launched. uint32_t executable_length = 0; _NSGetExecutablePath(NULL, &executable_length); std::string executable_path(executable_length, '\0'); char* executable_path_c = (char*)executable_path.c_str(); int rv = _NSGetExecutablePath(executable_path_c, &executable_length); assert(rv == 0); if ((rv != 0) || (executable_path.empty())) return ""; // _NSGetExecutablePath will return whatever gets execed, so if // the command line is ./foo, you'll get the '.'. We use the // aggressive mode here to handle '..' parts that could interfere // with finding other paths that start from FILE_EXE. return canonicalize(executable_path, true); #elif SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_LINUX // boost::filesystem can't chase symlinks, do it manually const char* selfExe = "/proc/self/exe"; char bin_dir[MAX_PATH + 1]; int bin_dir_size = readlink(selfExe, bin_dir, MAX_PATH); if (bin_dir_size < 0 || bin_dir_size > MAX_PATH) { SILOG(core,fatal,"Couldn't read self symlink to setup dynamic loading paths."); return ""; } bin_dir[bin_dir_size] = 0; return String(bin_dir, bin_dir_size); #elif SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_WINDOWS char system_buffer[MAX_PATH]; system_buffer[0] = 0; GetModuleFileName(NULL, system_buffer, MAX_PATH); return String(system_buffer); #else return ""; #endif } break; case DIR_EXE: { String exe_file = Get(FILE_EXE); if (exe_file.empty()) return ""; boost::filesystem::path exe_file_path(exe_file); return exe_file_path.parent_path().string(); } break; case DIR_EXE_BUNDLE: { #if SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_WINDOWS || SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_LINUX // Windows and Linux don't have bundles return Get(DIR_EXE); #elif SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_MAC // On mac we need to detect that we're in a .app. We assume this // only applies if the binaries are in the standard location, // i.e. foo.app/Contents/MacOS/bar_binary String exe_dir = Get(DIR_EXE); boost::filesystem::path exe_dir_path(exe_dir); // Work our way back up verifying the path names, finally // returning if we actually find the .app. if (exe_dir_path.has_filename() && exe_dir_path.filename() == "MacOS") { exe_dir_path = exe_dir_path.parent_path(); if (exe_dir_path.has_filename() && exe_dir_path.filename() == "Contents") { exe_dir_path = exe_dir_path.parent_path(); if (exe_dir_path.has_filename()) { String app_dir_name = exe_dir_path.filename(); if (app_dir_name.substr(app_dir_name.size()-4, 4) == ".app") return exe_dir_path.parent_path().string(); } } } // Otherwise dump the original return exe_dir; #endif } break; case DIR_CURRENT: { #if SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_MAC || SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_LINUX char system_buffer[MAX_PATH] = ""; if (!getcwd(system_buffer, sizeof(system_buffer))) { return ""; } return String(system_buffer); #elif SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_WINDOWS char system_buffer[MAX_PATH]; system_buffer[0] = 0; DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); if (len == 0 || len > MAX_PATH) return ""; return String(system_buffer); #else return "."; #endif } break; case DIR_USER: { #if SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_LINUX || SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_MAC uid_t uid = getuid(); passwd* pw = getpwuid(uid); if (pw != NULL && pw->pw_dir != NULL) { boost::filesystem::path homedir(pw->pw_dir); if (boost::filesystem::exists(homedir) && boost::filesystem::is_directory(homedir)) return homedir.string(); } #elif SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_WINDOWS char system_buffer[MAX_PATH]; system_buffer[0] = 0; if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, system_buffer))) return ""; std::string appdata_str(system_buffer); boost::filesystem::path user_appdata(appdata_str); user_appdata /= "Sirikata"; if (!boost::filesystem::exists(user_appdata)) boost::filesystem::create_directory(user_appdata); if (boost::filesystem::exists(user_appdata) && boost::filesystem::is_directory(user_appdata)) return user_appdata.string(); #endif // Last resort (and default for unknown platform) is to try to use // the current directory return "."; } break; case DIR_USER_HIDDEN: { #if SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_WINDOWS // On windows there's no difference from the user-specific data directory since that's already hidden. return Get(DIR_USER); #else // We just compute this as an offset from the user directory boost::filesystem::path user_dir(Get(DIR_USER)); user_dir /= ".sirikata"; if (!boost::filesystem::exists(user_dir)) boost::filesystem::create_directory(user_dir); if (boost::filesystem::exists(user_dir) && boost::filesystem::is_directory(user_dir)) return user_dir.string(); #endif return "."; } case DIR_TEMP: { #if SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_LINUX || SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_MAC // On Mac and Linux we try to work under tmp using our own directory boost::filesystem::path tmp_path("/tmp"); if (boost::filesystem::exists(tmp_path) && boost::filesystem::is_directory(tmp_path)) { tmp_path /= "sirikata"; // If it doesn't exist, try creating it if (!boost::filesystem::exists(tmp_path)) boost::filesystem::create_directory(tmp_path); if (boost::filesystem::exists(tmp_path) && boost::filesystem::is_directory(tmp_path)) return tmp_path.string(); } #elif SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_WINDOWS // Windows doesn't seem to suggest a good location for this, so we // put it under the app data directory in its own temp directory boost::filesystem::path sirikata_temp_dir = boost::filesystem::path(Get(DIR_USER_HIDDEN)) / "temp"; if (!boost::filesystem::exists(sirikata_temp_dir)) boost::filesystem::create_directory(sirikata_temp_dir); if (boost::filesystem::exists(sirikata_temp_dir) && boost::filesystem::is_directory(sirikata_temp_dir)) return sirikata_temp_dir.string(); #endif // Last resort (and default for unknown platform) is to try to use // the current directory return "."; } break; case DIR_SYSTEM_CONFIG: { #if SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_LINUX || SIRIKATA_PLATFORM == SIRIKATA_PLATFORM_MAC // This is sirikata specific, so we're looking for more // than just /etc. if (boost::filesystem::exists("/etc") && boost::filesystem::is_directory("/etc") && boost::filesystem::exists("/etc/sirikata") && boost::filesystem::is_directory("/etc/sirikata")) return "/etc/sirikata"; return ""; #else // Other platforms don't have an equivalent? return ""; #endif } break; case RESOURCE: { SILOG(core,fatal,"Can't request RESOURCE without specifiying an in-tree path and path to resource."); assert(key != RESOURCE); return ""; } break; default: return ""; } }
void initialize(int argc, char *argv[], bool setup_default_environment){ wchar_t **argv_copy; char exec_path[2048]; char home_path[2048]; wchar_t *wide_exec_path; wchar_t *wide_home_path; argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); for (int i = 0; i < argc - 1; i++) { argv_copy[i] = char2wchar(argv[i + 1]); // todo: handle the case where argv_copy is NULL } Mango::initialize(false); if (setup_default_environment) { Mango::GlobalFrame = new Mango::Core::Frame(true); Mango::Engine = new MangoPy::PyEngine(); Mango::Keyboard = new Mango::Core::CoreKeyboard(); Mango::Mouse = new Mango::Core::CoreMouse(); Mango::Camera = new Mango::Core::CoreCamera(); Mango::View = new Mango::Core::CoreCamera(); Mango::Camera->set(STEP); Mango::Camera->lookAt(Mango::Vector(0, 0, 0), 5); Mango::View->set(STEP); Mango::View->setMode(RMB_CYLINDRICAL_ROTATE | LMB_DRAG_FOCUS | ZOOM_SCALES); Mango::Engine->setCameraObject(Mango::Camera); Mango::Engine->setViewObject(Mango::View); } // Add modules - phase one PyImport_AppendInittab("_mpygen", PyInit__mpygen); PyImport_AppendInittab("Core", PyInit_Core); PyImport_AppendInittab("OpenGL", PyInit_OpenGL); PyImport_AppendInittab("Draw", PyInit_Draw); // Initialize Python if (!executable_path(exec_path, 2048)){ std::cout << "Warning: could not determine executable path." << std::endl; std::cout << " using argv[0], but this value is not reliable" << std::endl; std::cout << " and Mango may not be able to find or execute " << std::endl; std::cout << " files outside of its own directory" << std::endl; strncpy(exec_path, argv[0], 2047); exec_path[2047] = NULL; } wide_exec_path = char2wchar(exec_path); // Py_SetProgramName(wide_exec_path); #if !defined(WIN32) // Set Home Path // Windows seems to set exec_prefix just fine without this, so // it is skipped on windows until needed python_home_path(exec_path, home_path, 2048); wide_home_path = char2wchar(home_path); // Py_SetPythonHome(wide_home_path); #endif Py_Initialize(); PySys_SetArgv(argc - 1, argv_copy); // Add modules - phase two PyObject* main_module = PyImport_AddModule("__main__"); PyObject *module_mpygen = PyImport_ImportModule("_mpygen"); Py_INCREF(module_mpygen); PyModule_AddObject(main_module, "_mpygen", module_mpygen); if (module_mpygen == NULL){ std::cout << "MangoPy: Error creating module _mpygen" << std::endl; exit(1); } PyObject *module_core = PyImport_ImportModule("Core"); Py_INCREF(module_core); PyModule_AddObject(main_module, "Core", module_core); if (module_core == NULL){ std::cout << "MangoPy: Error creating module Core" << std::endl; exit(1); } PyObject *module_opengl = PyImport_ImportModule("OpenGL"); Py_INCREF(module_opengl); PyModule_AddObject(main_module, "OpenGL", module_opengl); if (module_opengl == NULL){ std::cout << "MangoPy: Error creating module OpenGL" << std::endl; exit(1); } PyObject *module_draw = PyImport_ImportModule("Draw"); Py_INCREF(module_draw); PyModule_AddObject(main_module, "Draw", module_draw); if (module_draw == NULL){ std::cout << "MangoPy: Error creating module Draw" << std::endl; exit(1); } // Add absolute path to engine to the module search path PyRun_SimpleString("import os, sys"); PyModule_AddStringConstant(module_core, "MANGO_LAUNCH_PATH", exec_path); PyRun_SimpleString("Core.MANGO_ABSOLUTE_PATH = os.path.dirname(os.path.normpath(os.path.realpath(Core.MANGO_LAUNCH_PATH)))"); PyRun_SimpleString("sys.path.append(Core.MANGO_ABSOLUTE_PATH)"); PyRun_SimpleString("sys.path.append(os.path.normpath(os.path.join(Core.MANGO_ABSOLUTE_PATH, '../script')))"); // Make the Core module globally available PyRun_SimpleString("__builtins__._mpygen = _mpygen"); PyRun_SimpleString("__builtins__.Core = Core"); PyRun_SimpleString("__builtins__.Draw = Draw"); PyRun_SimpleString("__builtins__.OpenGL = OpenGL"); PyRun_SimpleString("__builtins__.Vector = Core.Vector"); PyRun_SimpleString("__builtins__.STEP = Core.STEP"); PyRun_SimpleString("__builtins__.RENDER = Core.RENDER"); PyRun_SimpleString("__builtins__.DRAW = Core.DRAW"); PyRun_SimpleString("__builtins__.INPUT = Core.INPUT"); PyRun_SimpleString("__builtins__.CAMERA_DEFAULT_MODE = Core.CAMERA_DEFAULT_MODE"); PyRun_SimpleString("__builtins__.LOCK_PAN = Core.LOCK_PAN"); PyRun_SimpleString("__builtins__.LOCK_DISTANCE = Core.LOCK_DISTANCE"); PyRun_SimpleString("__builtins__.LOCK_ALPHA = Core.LOCK_ALPHA"); PyRun_SimpleString("__builtins__.LOCK_BETA = Core.LOCK_BETA"); PyRun_SimpleString("__builtins__.LOCK_GAMMA = Core.LOCK_GAMMA"); PyRun_SimpleString("__builtins__.RMB_CYLINDRICAL_ROTATE = Core.RMB_CYLINDRICAL_ROTATE"); PyRun_SimpleString("__builtins__.LMB_DRAG_FOCUS = Core.LMB_DRAG_FOCUS"); PyRun_SimpleString("__builtins__.LOCK_ALL = Core.LOCK_ALL"); PyRun_SimpleString("__builtins__.KEY_WINDOWS_MENU = Core.KEY_WINDOWS_MENU"); PyRun_SimpleString("__builtins__.KEY_WINDOWS_RIGHT = Core.KEY_WINDOWS_RIGHT"); PyRun_SimpleString("__builtins__.KEY_WINDOWS_LEFT = Core.KEY_WINDOWS_LEFT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_DIVIDE = Core.KEY_NUMPAD_DIVIDE"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_DECIMAL = Core.KEY_NUMPAD_DECIMAL"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_SUBTRACT = Core.KEY_NUMPAD_SUBTRACT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_SEPARATOR = Core.KEY_NUMPAD_SEPARATOR"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_ADD = Core.KEY_NUMPAD_ADD"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_MULTIPLY = Core.KEY_NUMPAD_MULTIPLY"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_EQUAL = Core.KEY_NUMPAD_EQUAL"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_DELETE = Core.KEY_NUMPAD_DELETE"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_INSERT = Core.KEY_NUMPAD_INSERT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_BEGIN = Core.KEY_NUMPAD_BEGIN"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_END = Core.KEY_NUMPAD_END"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_PAGEDOWN = Core.KEY_NUMPAD_PAGEDOWN"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_PAGEUP = Core.KEY_NUMPAD_PAGEUP"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_DOWN = Core.KEY_NUMPAD_DOWN"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_RIGHT = Core.KEY_NUMPAD_RIGHT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_UP = Core.KEY_NUMPAD_UP"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_LEFT = Core.KEY_NUMPAD_LEFT"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_HOME = Core.KEY_NUMPAD_HOME"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_F4 = Core.KEY_NUMPAD_F4"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_F3 = Core.KEY_NUMPAD_F3"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_F2 = Core.KEY_NUMPAD_F2"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_F1 = Core.KEY_NUMPAD_F1"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_ENTER = Core.KEY_NUMPAD_ENTER"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_TAB = Core.KEY_NUMPAD_TAB"); PyRun_SimpleString("__builtins__.KEY_NUMPAD_SPACE = Core.KEY_NUMPAD_SPACE"); PyRun_SimpleString("__builtins__.KEY_PAGEDOWN = Core.KEY_PAGEDOWN"); PyRun_SimpleString("__builtins__.KEY_PAGEUP = Core.KEY_PAGEUP"); PyRun_SimpleString("__builtins__.KEY_SCROLL = Core.KEY_SCROLL"); PyRun_SimpleString("__builtins__.KEY_NUMLOCK = Core.KEY_NUMLOCK"); PyRun_SimpleString("__builtins__.KEY_F24 = Core.KEY_F24"); PyRun_SimpleString("__builtins__.KEY_F23 = Core.KEY_F23"); PyRun_SimpleString("__builtins__.KEY_F22 = Core.KEY_F22"); PyRun_SimpleString("__builtins__.KEY_F21 = Core.KEY_F21"); PyRun_SimpleString("__builtins__.KEY_F20 = Core.KEY_F20"); PyRun_SimpleString("__builtins__.KEY_F19 = Core.KEY_F19"); PyRun_SimpleString("__builtins__.KEY_F18 = Core.KEY_F18"); PyRun_SimpleString("__builtins__.KEY_F17 = Core.KEY_F17"); PyRun_SimpleString("__builtins__.KEY_F16 = Core.KEY_F16"); PyRun_SimpleString("__builtins__.KEY_F15 = Core.KEY_F15"); PyRun_SimpleString("__builtins__.KEY_F14 = Core.KEY_F14"); PyRun_SimpleString("__builtins__.KEY_F13 = Core.KEY_F13"); PyRun_SimpleString("__builtins__.KEY_F12 = Core.KEY_F12"); PyRun_SimpleString("__builtins__.KEY_F11 = Core.KEY_F11"); PyRun_SimpleString("__builtins__.KEY_F10 = Core.KEY_F10"); PyRun_SimpleString("__builtins__.KEY_F9 = Core.KEY_F9"); PyRun_SimpleString("__builtins__.KEY_F8 = Core.KEY_F8"); PyRun_SimpleString("__builtins__.KEY_F7 = Core.KEY_F7"); PyRun_SimpleString("__builtins__.KEY_F6 = Core.KEY_F6"); PyRun_SimpleString("__builtins__.KEY_F5 = Core.KEY_F5"); PyRun_SimpleString("__builtins__.KEY_F4 = Core.KEY_F4"); PyRun_SimpleString("__builtins__.KEY_F3 = Core.KEY_F3"); PyRun_SimpleString("__builtins__.KEY_F2 = Core.KEY_F2"); PyRun_SimpleString("__builtins__.KEY_F1 = Core.KEY_F1"); PyRun_SimpleString("__builtins__.KEY_DIVIDE = Core.KEY_DIVIDE"); PyRun_SimpleString("__builtins__.KEY_DECIMAL = Core.KEY_DECIMAL"); PyRun_SimpleString("__builtins__.KEY_SUBTRACT = Core.KEY_SUBTRACT"); PyRun_SimpleString("__builtins__.KEY_SEPARATOR = Core.KEY_SEPARATOR"); PyRun_SimpleString("__builtins__.KEY_ADD = Core.KEY_ADD"); PyRun_SimpleString("__builtins__.KEY_MULTIPLY = Core.KEY_MULTIPLY"); PyRun_SimpleString("__builtins__.KEY_NUMPAD9 = Core.KEY_NUMPAD9"); PyRun_SimpleString("__builtins__.KEY_NUMPAD8 = Core.KEY_NUMPAD8"); PyRun_SimpleString("__builtins__.KEY_NUMPAD7 = Core.KEY_NUMPAD7"); PyRun_SimpleString("__builtins__.KEY_NUMPAD6 = Core.KEY_NUMPAD6"); PyRun_SimpleString("__builtins__.KEY_NUMPAD5 = Core.KEY_NUMPAD5"); PyRun_SimpleString("__builtins__.KEY_NUMPAD4 = Core.KEY_NUMPAD4"); PyRun_SimpleString("__builtins__.KEY_NUMPAD3 = Core.KEY_NUMPAD3"); PyRun_SimpleString("__builtins__.KEY_NUMPAD2 = Core.KEY_NUMPAD2"); PyRun_SimpleString("__builtins__.KEY_NUMPAD1 = Core.KEY_NUMPAD1"); PyRun_SimpleString("__builtins__.KEY_NUMPAD0 = Core.KEY_NUMPAD0"); PyRun_SimpleString("__builtins__.KEY_HELP = Core.KEY_HELP"); PyRun_SimpleString("__builtins__.KEY_INSERT = Core.KEY_INSERT"); PyRun_SimpleString("__builtins__.KEY_SNAPSHOT = Core.KEY_SNAPSHOT"); PyRun_SimpleString("__builtins__.KEY_EXECUTE = Core.KEY_EXECUTE"); PyRun_SimpleString("__builtins__.KEY_PRINT = Core.KEY_PRINT"); PyRun_SimpleString("__builtins__.KEY_SELECT = Core.KEY_SELECT"); PyRun_SimpleString("__builtins__.KEY_DOWN = Core.KEY_DOWN"); PyRun_SimpleString("__builtins__.KEY_RIGHT = Core.KEY_RIGHT"); PyRun_SimpleString("__builtins__.KEY_UP = Core.KEY_UP"); PyRun_SimpleString("__builtins__.KEY_LEFT = Core.KEY_LEFT"); PyRun_SimpleString("__builtins__.KEY_HOME = Core.KEY_HOME"); PyRun_SimpleString("__builtins__.KEY_END = Core.KEY_END"); PyRun_SimpleString("__builtins__.KEY_CAPITAL = Core.KEY_CAPITAL"); PyRun_SimpleString("__builtins__.KEY_PAUSE = Core.KEY_PAUSE"); PyRun_SimpleString("__builtins__.KEY_MENU = Core.KEY_MENU"); PyRun_SimpleString("__builtins__.KEY_CONTROL = Core.KEY_CONTROL"); PyRun_SimpleString("__builtins__.KEY_ALT = Core.KEY_ALT"); PyRun_SimpleString("__builtins__.KEY_SHIFT = Core.KEY_SHIFT"); PyRun_SimpleString("__builtins__.KEY_CLEAR = Core.KEY_CLEAR"); PyRun_SimpleString("__builtins__.KEY_MBUTTON = Core.KEY_MBUTTON"); PyRun_SimpleString("__builtins__.KEY_CANCEL = Core.KEY_CANCEL"); PyRun_SimpleString("__builtins__.KEY_RBUTTON = Core.KEY_RBUTTON"); PyRun_SimpleString("__builtins__.KEY_LBUTTON = Core.KEY_LBUTTON"); PyRun_SimpleString("__builtins__.KEY_START = Core.KEY_START"); PyRun_SimpleString("__builtins__.KEY_DELETE = Core.KEY_DELETE"); PyRun_SimpleString("__builtins__.KEY_SPACE = Core.KEY_SPACE"); PyRun_SimpleString("__builtins__.KEY_ESCAPE = Core.KEY_ESCAPE"); PyRun_SimpleString("__builtins__.KEY_RETURN = Core.KEY_RETURN"); PyRun_SimpleString("__builtins__.KEY_ENTER = Core.KEY_ENTER"); PyRun_SimpleString("__builtins__.KEY_TAB = Core.KEY_TAB"); PyRun_SimpleString("__builtins__.BUTTON_RIGHT = Core.BUTTON_RIGHT"); PyRun_SimpleString("__builtins__.KEY_BACKSPACE = Core.KEY_BACKSPACE"); PyRun_SimpleString("__builtins__.BUTTON_MIDDLE = Core.BUTTON_MIDDLE"); PyRun_SimpleString("__builtins__.BUTTON_LEFT = Core.BUTTON_LEFT"); PyRun_SimpleString("__builtins__.ANY = Core.ANY"); PyRun_SimpleString("__builtins__.RELEASE = Core.RELEASE"); PyRun_SimpleString("__builtins__.CLICK = Core.CLICK"); PyRun_SimpleString("__builtins__.PRESS = Core.PRESS"); PyRun_SimpleString("__builtins__.MOUSE = Core.MOUSE"); PyRun_SimpleString("__builtins__.KEYBOARD = Core.KEYBOARD"); PyRun_SimpleString("__builtins__.BUTTON_STATE_DOWN = Core.BUTTON_STATE_DOWN"); PyRun_SimpleString("__builtins__.BUTTON_STATE_UP = Core.BUTTON_STATE_UP"); PyRun_SimpleString("__builtins__.KEY_STATE_DOWN = Core.KEY_STATE_DOWN"); PyRun_SimpleString("__builtins__.KEY_STATE_UP = Core.KEY_STATE_UP"); // Make opengl methods available in the global namespace PyRun_SimpleString("__builtins__.GL_POINTS = OpenGL.GL_POINTS"); PyRun_SimpleString("__builtins__.GL_LINES = OpenGL.GL_LINES"); PyRun_SimpleString("__builtins__.GL_LINE_LOOP = OpenGL.GL_LINE_LOOP"); PyRun_SimpleString("__builtins__.GL_LINE_STRIP = OpenGL.GL_LINE_STRIP"); PyRun_SimpleString("__builtins__.GL_TRIANGLES = OpenGL.GL_TRIANGLES"); PyRun_SimpleString("__builtins__.GL_TRIANGLE_STRIP = OpenGL.GL_TRIANGLE_STRIP"); PyRun_SimpleString("__builtins__.GL_TRIANGLE_FAN = OpenGL.GL_TRIANGLE_FAN"); PyRun_SimpleString("__builtins__.GL_QUADS = OpenGL.GL_QUADS"); PyRun_SimpleString("__builtins__.GL_QUAD_STRIP = OpenGL.GL_QUAD_STRIP"); PyRun_SimpleString("__builtins__.GL_POLYGON = OpenGL.GL_POLYGON"); PyRun_SimpleString("__builtins__.glBegin = OpenGL.glBegin"); PyRun_SimpleString("__builtins__.glEnd = OpenGL.glEnd"); PyRun_SimpleString("__builtins__.glVertex = OpenGL.glVertex"); PyRun_SimpleString("__builtins__.glNormal = OpenGL.glNormal"); PyRun_SimpleString("__builtins__.glColor = OpenGL.glColor"); PyRun_SimpleString("__builtins__.glTranslate = OpenGL.glTranslate"); PyRun_SimpleString("__builtins__.glRotate = OpenGL.glRotate"); PyRun_SimpleString("__builtins__.glScale = OpenGL.glScale"); PyRun_SimpleString("__builtins__.glScale = OpenGL.glScale"); PyRun_SimpleString("__builtins__.glPushMatrix = OpenGL.glPushMatrix"); PyRun_SimpleString("__builtins__.glPopMatrix = OpenGL.glPopMatrix"); // Create global engine instance mpy_PyEngine *py_global_engine = mpy_PyEngine_NEW(); //py_global_engine->internalObject = (PyEngine *)Mango::Engine; // WARNING: this downcasting from a CoreEngine to a PyEngine is dangerous, and only works if you are SURE that Mango::Engine is actually a PyEngine! py_global_engine->internalObject = Mango::Engine; // WARNING: this downcasting from a CoreEngine to a PyEngine is dangerous, and only works if you are SURE that Mango::Engine is actually a PyEngine! PyModule_AddObject(module_core, "Engine", reinterpret_cast<PyObject *>(py_global_engine)); PyRun_SimpleString("__builtins__.Engine = Core.Engine"); // Create global keyboard instance mpy_CoreKeyboard *py_global_keyboard = mpy_CoreKeyboard_NEW(); py_global_keyboard->internalObject = Mango::Keyboard; PyModule_AddObject(module_core, "Keyboard", reinterpret_cast<PyObject *>(py_global_keyboard)); PyRun_SimpleString("__builtins__.Keyboard = Core.Keyboard"); // Create global mouse instance mpy_CoreMouse *py_global_mouse = mpy_CoreMouse_NEW(); py_global_mouse->internalObject = Mango::Mouse; PyModule_AddObject(module_core, "Mouse", reinterpret_cast<PyObject *>(py_global_mouse)); PyRun_SimpleString("__builtins__.Mouse = Core.Mouse"); // Create global frame instance PyGlobalFrame = mpy_Frame_NEW(); PyGlobalFrame->internalObject = Mango::GlobalFrame; PyGlobalFrame->parentFrame = NULL; Py_INCREF(PyGlobalFrame); PyModule_AddObject(module_core, "GlobalFrame", reinterpret_cast<PyObject *>(PyGlobalFrame)); PyRun_SimpleString("__builtins__.GlobalFrame = Core.GlobalFrame"); // Create global camera instance if (Mango::Camera != NULL){ mpy_Frame *py_camera_focus = mpy_Frame_NEW(); py_camera_focus->internalObject = Mango::Camera->parentFrame(); mpy_Frame_init(py_camera_focus, NULL, NULL); mpy_CoreCamera *py_global_camera = mpy_CoreCamera_NEW(); Py_INCREF(py_camera_focus); py_global_camera->parentFrame = py_camera_focus; mpy_Object_init((mpy_Object *)py_global_camera, NULL, NULL); py_global_camera->internalObject = Mango::Camera; PyModule_AddObject(module_core, "Camera", reinterpret_cast<PyObject *>(py_global_camera)); PyRun_SimpleString("__builtins__.Camera = Core.Camera"); } // Create global view instance if (Mango::View != NULL){ mpy_CoreCamera *py_global_view = mpy_CoreCamera_NEW(); py_global_view->internalObject = Mango::View; PyModule_AddObject(module_core, "View", reinterpret_cast<PyObject *>(py_global_view)); PyRun_SimpleString("__builtins__.View = Core.View"); } // Make environment available to extensions PyModule_AddObject(module_mpygen, "GLOBAL_FRAME", PyCapsule_New((void *)Mango::GlobalFrame, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "PY_GLOBAL_FRAME", PyCapsule_New((void *)PyGlobalFrame, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "ENGINE", PyCapsule_New((void *)Mango::Engine, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "CAMERA", PyCapsule_New((void *)Mango::Camera, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "VIEW", PyCapsule_New((void *)Mango::View, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "KEYBOARD", PyCapsule_New((void *)Mango::Keyboard, NULL, mpy_trivialDelMethod)); PyModule_AddObject(module_mpygen, "MOUSE", PyCapsule_New((void *)Mango::Mouse, NULL, mpy_trivialDelMethod)); //PyModule_AddObject(module_mpygen, "TYPE_OBJECT", PyCObject_FromVoidPtr((void *)&mpy_ObjectType, mpy_trivialDelMethod)); PyObject *global_types = PyDict_New(); PyModule_AddObject(module_mpygen, "TYPES", global_types); //PyDict_SetItemString(global_types, "mangopy.core.object", PyCObject_FromVoidPtr((void *)&mpy_ObjectType, mpy_trivialDelMethod)); MangoPy::register_py_type_object("mangopy.core.input_event", &mpy_InputEventType); MangoPy::register_py_type_object("mangopy.core.vector", &mpy_VectorType); MangoPy::register_py_type_object("mangopy.core.matrix", &mpy_MatrixType); MangoPy::register_py_type_object("mangopy.core.object", &mpy_ObjectType); MangoPy::register_py_type_object("mangopy.core.frame", &mpy_FrameType); MangoPy::register_py_type_object("mangopy.core.py_engine", &mpy_PyEngineType); MangoPy::register_py_type_object("mangopy.core.core_camera", &mpy_CoreCameraType); MangoPy::register_py_type_object("mangopy.core.core_keyboard", &mpy_CoreKeyboardType); MangoPy::register_py_type_object("mangopy.core.core_mouse", &mpy_CoreMouseType); MangoPy::register_py_type_object("mangopy.core.triangle", &mpy_TriangleType); // Setup Python error buffer PyRun_SimpleString("\n\ #import sys \n\ class MangoPy_StdErr: \n\ def write(self, msg): \n\ _mpygen.writeToPythonScriptStderr(msg) \n\ sys.stderr = MangoPy_StdErr() \n\ "); // Setup Paths Array PyRun_SimpleString("Core.SOURCES = []"); }
string App::file() { if (not initialized) throw AppNotInitialized(); return QFileInfo(executable_path()).fileName().toStdString(); }