From 8a58411dd5612ee5b01070d86aba7e3d65662b2d Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 4 Mar 2004 00:19:42 +0000 Subject: [PATCH] move InitialWorkingDirectory, DirOfExecutable, ChangeToDirOfExecutable into RageFileManager --- stepmania/src/RageFileManager.cpp | 54 +++++++++++++++++++++++++++- stepmania/src/RageFileManager.h | 5 ++- stepmania/src/StepMania.cpp | 58 ++----------------------------- stepmania/src/StepMania.h | 3 -- 4 files changed, 59 insertions(+), 61 deletions(-) diff --git a/stepmania/src/RageFileManager.cpp b/stepmania/src/RageFileManager.cpp index f8221606e8..af0d9a4f68 100644 --- a/stepmania/src/RageFileManager.cpp +++ b/stepmania/src/RageFileManager.cpp @@ -17,6 +17,9 @@ RageFileManager *FILEMAN = NULL; /* Lock this before touching any of these globals (except FILEMAN itself). */ static RageMutex *g_Mutex; +CString InitialWorkingDirectory; +CString DirOfExecutable; + // Mountpoints as directories cause a problem. If "Themes/default" is a mountpoint, and // doesn't exist anywhere else, then GetDirListing("Themes/*") must return "default". The // driver containing "Themes/default" won't do this; its world view begins at "BGAnimations" @@ -58,8 +61,57 @@ struct LoadedDriver static vector g_Drivers; -RageFileManager::RageFileManager() +static void ChangeToDirOfExecutable( CString argv0 ) { +#ifdef _XBOX + DirOfExecutable = "D:\\"; + InitialWorkingDirectory = "D:\\"; +#else + + char tmp[1024]; + getcwd( tmp, 1024 ); + tmp[1023] = 0; + InitialWorkingDirectory = tmp; + + DirOfExecutable = argv0; + DirOfExecutable.Replace( "\\", "/" ); + + bool IsAbsolutePath = false; + if( DirOfExecutable.size() == 0 || DirOfExecutable[0] == '/' ) + IsAbsolutePath = true; +#if defined(_WIN32) + if( DirOfExecutable.size() > 2 && DirOfExecutable[1] == ':' && DirOfExecutable[2] == '/' ) + IsAbsolutePath = true; +#endif + + // strip off executable name + unsigned n = DirOfExecutable.find_last_of("/"); + if( n != DirOfExecutable.npos ) + DirOfExecutable.erase(n); + else + DirOfExecutable.erase(); + + if( !IsAbsolutePath ) + { + DirOfExecutable = GetCwd() + "/" + DirOfExecutable; + DirOfExecutable.Replace( "\\", "/" ); + } + + /* Set the CWD. Any effects of this is platform-specific; most files are read and + * written through RageFile. See also RageFileManager::RageFileManager. */ +#if defined(_WIN32) + chdir( DirOfExecutable + "/.." ); +#elif defined(DARWIN) + chdir(DirOfExecutable + "/../../.."); +#endif + +#endif +} + +RageFileManager::RageFileManager( CString argv0 ) +{ + ChangeToDirOfExecutable( argv0 ); + g_Mutex = new RageMutex; g_Mountpoints = new RageFileDriverMountpoints; diff --git a/stepmania/src/RageFileManager.h b/stepmania/src/RageFileManager.h index 5a562cef8b..5ddf0e44dc 100644 --- a/stepmania/src/RageFileManager.h +++ b/stepmania/src/RageFileManager.h @@ -3,11 +3,14 @@ #include "RageFile.h" +extern CString InitialWorkingDirectory; +extern CString DirOfExecutable; + class RageFileObj; class RageFileManager { public: - RageFileManager(); + RageFileManager( CString argv0 ); ~RageFileManager(); void GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ); diff --git a/stepmania/src/StepMania.cpp b/stepmania/src/StepMania.cpp index e28f1e041f..47aa5a9605 100644 --- a/stepmania/src/StepMania.cpp +++ b/stepmania/src/StepMania.cpp @@ -89,57 +89,6 @@ char **g_argv = NULL; static bool g_bHasFocus = true; static bool g_bQuitting = false; -CString InitialWorkingDirectory; -CString DirOfExecutable; - -static void ChangeToDirOfExecutable(const char *argv0) -{ -#ifdef _XBOX - DirOfExecutable = "D:\\"; - InitialWorkingDirectory = "D:\\"; - return; -#else - - char tmp[1024]; - getcwd( tmp, 1024 ); - tmp[1023] = 0; - InitialWorkingDirectory = tmp; - - DirOfExecutable = argv0; - DirOfExecutable.Replace( "\\", "/" ); - - bool IsAbsolutePath = false; - if( DirOfExecutable.size() == 0 || DirOfExecutable[0] == '/' ) - IsAbsolutePath = true; -#if defined(_WIN32) - if( DirOfExecutable.size() > 2 && DirOfExecutable[1] == ':' && DirOfExecutable[2] == '/' ) - IsAbsolutePath = true; -#endif - - // strip off executable name - unsigned n = DirOfExecutable.find_last_of("/"); - if( n != DirOfExecutable.npos ) - DirOfExecutable.erase(n); - else - DirOfExecutable.erase(); - - if( !IsAbsolutePath ) - { - DirOfExecutable = GetCwd() + "/" + DirOfExecutable; - DirOfExecutable.Replace( "\\", "/" ); - } - - /* Set the CWD. Any effects of this is platform-specific; most files are read and - * written through RageFile. See also RageFileManager::RageFileManager. */ -#if defined(_WIN32) - chdir( DirOfExecutable + "/.." ); -#elif defined(DARWIN) - chdir(DirOfExecutable + "/../../.."); -#endif - -#endif -} - void UpdateHWnd() { #ifdef _WINDOWS @@ -888,13 +837,10 @@ int main(int argc, char* argv[]) #endif - ChangeToDirOfExecutable(argv[0]); - /* Almost everything uses this to read and write files. Load this early. */ - FILEMAN = new RageFileManager; + FILEMAN = new RageFileManager( argv[0] ); - /* Set this up next. Do this early, since it's needed for RageException::Throw. - * Do it after ChangeToDirOfExecutable, so the log ends up in the right place. */ + /* Set this up next. Do this early, since it's needed for RageException::Throw. */ LOG = new RageLog(); /* Whew--we should be able to crash safely now! */ diff --git a/stepmania/src/StepMania.h b/stepmania/src/StepMania.h index 7768634daf..1f5028ab19 100644 --- a/stepmania/src/StepMania.h +++ b/stepmania/src/StepMania.h @@ -29,9 +29,6 @@ CString SaveScreenshot( CString sDir, bool bSaveCompressed, bool bMakeSignature, extern HWND g_hWndMain; #endif -extern CString DirOfExecutable; -extern CString InitialWorkingDirectory; - extern int g_argc; extern char **g_argv;