Game Development Community

Enhancement: OSX Get Working Directory

by David Wyand · in Torque Game Engine · 08/18/2004 (9:19 am) · 2 replies

Greetings!

My current project uses the Platform::getWorkingDirectory() method in a few places and I've found that under OSX it didn't return a valid directory pathway. What I present here is a streamlined approach that works (only) under OSX.

Open up platformMacCarb/macCarbFileio.cc and head to the Platform::getWorkingDirectory() method near the bottom of the file. Here's what my new code block looks like (with changes in bold):

StringTableEntry Platform::getWorkingDirectory()
{
   if (!cwd)
   {
[b]// DAW: Added improved section for OSX
#if defined(TORQUE_OS_MAC_OSX)
      // Get the application bundle
      CFBundleRef appBundle = CFBundleGetMainBundle();

      // Get the bundles URL
      CFURLRef bundleURL = CFBundleCopyBundleURL(appBundle);
	  
      // The bundleURL includes the name of the bundle at the end.  We need to
      // strip this off.
      CFURLRef cleanURL = CFURLCreateCopyDeletingLastPathComponent(NULL, bundleURL);
	  
      // Now convert to a C string
      char cwd_buf[MAX_MAC_PATH_LONG];
      CFURLGetFileSystemRepresentation(cleanURL, true, (unsigned char *)cwd_buf, sizeof(cwd_buf));

      // Clean up OSX specific components
      CFRelease(cleanURL);
      CFRelease(bundleURL);
      CFRelease(appBundle);
	  
      // Put the working directory onto the string table
      cwd = StringTable->insert(cwd_buf);
	  
#else[/b]
      OSErr err;
      CInfoPBRec catinfo;
      Str255 dirname;
      char cwd_buf[MAX_MAC_PATH_LONG];
      U32 len = 0, totallen = 0;

      *cwd_buf = 0;

      // clear the paramblock.
      dMemset((void*)&catinfo, 0L, sizeof(catinfo));
   
      catinfo.dirInfo.ioVRefNum = platState.volRefNum;
      catinfo.dirInfo.ioDrParID = platState.dirID;
      catinfo.dirInfo.ioNamePtr = (StringPtr)&dirname;
      // ask for info about this directory.
      catinfo.dirInfo.ioFDirIndex = -1;

      do
      {
         dirname[0] = 0; // make sure to null the dirname!
         // reset dir id each time through to the parent's id.
         catinfo.dirInfo.ioDrDirID = catinfo.dirInfo.ioDrParID;
         err = PBGetCatInfoSync(&catinfo);
         if (err!=noErr) break;
         
         // capture st255 len and null terminate it.
         len = dirname[0]+1;
         dirname[len] = 0;
         
         // put a slash as the first char (eff converting it into a cstr...).
         dirname[0] = '/';
         
         // copy the current path forward in the buffer to allow us to prepend the new dir.
         // MUST USE MEMMOVE, as we are copying in-place, and memmove allows moving within
         // a given buffer.  memcpy doesn't.
         // and remember to allow for the null on the end...
         if (totallen) dMemmove(cwd_buf + len, cwd_buf, totallen+1);
         
         // copy the new path in, prepending the string.  don't copy the null here...
         dMemmove(cwd_buf, dirname, len);
         
         // inc the total size.
         totallen += len;
         
         // make damn sure we're null terminated.
         cwd_buf[totallen] = 0;
      }
      while (catinfo.dirInfo.ioDrDirID != fsRtDirID);
   
      cwd = StringTable->insert(cwd_buf);
[b]#endif[/b]
   }
   return cwd;
}

For this to compile correctly, you'll need TORQUE_OS_MAC_OSX to be defined in the Xcode target options. Other parts of the TGE Mac code make use of this define so you may want to make sure everything is working OK if you currently don't have it defined. I've not had any problems under OSX 10.3.x.

Please let me know if you discover any problems with the new method. As it stands, it should be safe to integrate this with the current head.

Enjoy!

- LightWave Dave

About the author

A long time Associate of the GarageGames' community and author of the Torque 3D Game Development Cookbook. Buy it today from Packt Publishing!


#1
08/18/2004 (12:28 pm)
Thanks Dave, I'll try to take a look at this soon-ish and get it into HEAD if it all checks out.

Nice :)
#2
08/18/2004 (3:16 pm)
Nice! I'll have to play when I finally get home from work (IF I finally get home...)