fix BGE bug #8668: Behavior of os.getcwd() is not consistent between operating systems
authorBenoit Bolsee <benoit.bolsee@online.be>
Sun, 11 May 2008 18:45:30 +0000 (18:45 +0000)
committerBenoit Bolsee <benoit.bolsee@online.be>
Sun, 11 May 2008 18:45:30 +0000 (18:45 +0000)
Add a function GameLogic.expandPath() that works like Blender.sys.expandpath() and is also available in the BlenderPlayer.
Fix the game actuator in the BlenderPlayer to work like in Blender:
- try first to load the .blend from the current working directory
- if not found, try to load from the startup .blend or runtime base directory

source/gameengine/GamePlayer/ghost/GPG_ghost.cpp
source/gameengine/Ketsji/KX_PythonInit.cpp
source/gameengine/PyDoc/GameLogic.py

index 8817bc98886e17acc76abeecb6a48312110b6061..bc80c0a7612da8ee15e045bec1589b0faa6e64c1 100644 (file)
@@ -562,13 +562,13 @@ int main(int argc, char** argv)
                                STR_String exitstring = "";
                                GPG_Application app(system, NULL, exitstring);
                                bool firstTimeRunning = true;
+                               char *filename = get_filename(argc, argv);
+                               char *titlename;
+                               char pathname[160];
                                
                                do
                                {
                                        // Read the Blender file
-                                       char *filename = get_filename(argc, argv);
-                                       char *titlename;
-                                       char pathname[160];
                                        BlendFileData *bfd;
                                        
                                        // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
@@ -582,6 +582,17 @@ int main(int argc, char** argv)
                                                BLI_convertstringcode(basedpath, pathname);
                                                
                                                bfd = load_game_data(basedpath);
+
+                                               if (!bfd)
+                                               {
+                                                       // just add "//" in front of it
+                                                       char temppath[242];
+                                                       strcpy(temppath, "//");
+                                                       strcat(temppath, basedpath);
+                               
+                                                       BLI_convertstringcode(temppath, pathname);
+                                                       bfd = load_game_data(temppath);
+                                               }
                                        }
                                        else
                                        {
@@ -607,7 +618,6 @@ int main(int argc, char** argv)
 #endif // WIN32
                                                Main *maggie = bfd->main;
                                                Scene *scene = bfd->curscene;
-                                               strcpy (pathname, maggie->name);
                                                char *startscenename = scene->id.name + 2;
                                                G.fileflags  = bfd->fileflags;
 
@@ -651,7 +661,12 @@ int main(int argc, char** argv)
                                                if (firstTimeRunning)
                                                {
                                                        firstTimeRunning = false;
-                                                       
+
+                                                       // set the filename only the first time as in KetsjiEmbedded
+                                                       strcpy (pathname, maggie->name);
+                                                       // also copy here (used by GameLogic.getBaseDirectory)
+                                                       strcpy (G.sce, maggie->name);
+
                                                        if (fullScreen)
                                                        {
 #ifdef WIN32
index a2aff54d385c0e60bde7cab8543ea10612e8cf99..a80a7f04e8f35ae933d886cc3e6164c42c60f90c 100644 (file)
 //#include "BPY_extern.h"
 #endif 
 
+#include "BKE_utildefines.h"
+#include "BKE_global.h"
+#include "BLI_blenlib.h"
+
 static void setSandbox(TPythonSecurityLevel level);
 
 
@@ -140,6 +144,32 @@ static PyObject* gPySetGravity(PyObject*,
        return NULL;
 }
 
+static char gPyExpandPath_doc[] =
+"(path) - Converts a blender internal path into a proper file system path.\n\
+path - the string path to convert.\n\n\
+Use / as directory separator in path\n\
+You can use '//' at the start of the string to define a relative path;\n\
+Blender replaces that string by the directory of the startup .blend or runtime\n\
+file to make a full path name (doesn't change during the game, even if you load\n\
+other .blend).\n\
+The function also converts the directory separator to the local file system format.";
+
+static PyObject* gPyExpandPath(PyObject*,
+                                                               PyObject* args, 
+                                                               PyObject*)
+{
+       char expanded[FILE_MAXDIR + FILE_MAXFILE];
+       char* filename;
+       
+       if (PyArg_ParseTuple(args,"s",&filename))
+       {
+               BLI_strncpy(expanded, filename, FILE_MAXDIR + FILE_MAXFILE);
+               BLI_convertstringcode(expanded, G.sce);
+               return PyString_FromString(expanded);
+       }
+       return NULL;
+}
+
 
 static bool usedsp = false;
 
@@ -361,6 +391,7 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
 
 
 static struct PyMethodDef game_methods[] = {
+       {"expandPath", (PyCFunction)gPyExpandPath, METH_VARARGS, gPyExpandPath_doc},
        {"getCurrentController",
        (PyCFunction) SCA_PythonController::sPyGetCurrentController,
        METH_VARARGS, SCA_PythonController::sPyGetCurrentController__doc__},
index fcfd8bfc4eb00728c8f49a95e95a16d410fbf658..965c0522bd7ebea64fb0baad2fd45c3cf55e1182 100644 (file)
@@ -221,3 +221,18 @@ def setPhysicsTicRate(ticrate):
        @type ticrate: float
        """
 
+def expandPath(path):
+       """
+       Converts a blender internal path into a proper file system path.
+
+       Use / as directory separator in path
+       You can use '//' at the start of the string to define a relative path;
+       Blender replaces that string by the directory of the startup .blend or runtime file
+       to make a full path name (doesn't change during the game, even if you load other .blend).
+       The function also converts the directory separator to the local file system format.
+
+       @param path: The path string to be converted/expanded.
+       @type path: string
+       @return: The converted string
+       @rtype: string
+       """