On MacOSX, define the DataPath as being the Resources path in the bundle

dev-ui
Didier 'OdyX' Raboud 2013-10-31 10:35:43 +01:00
parent 17ad3e5a90
commit e78d2cce18
2 changed files with 54 additions and 0 deletions

View File

@ -21,10 +21,47 @@
#include <stdlib.h>
// MacOS-specific headers
#include <CoreFoundation/CFBundle.h>
#include <CoreServices/CoreServices.h>
#include <boost/filesystem.hpp>
inline std::string CFStringRefToStdString(CFStringRef str) {
std::string stdstr;
char *fullPath;
CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
// 1st try for English system
fullPath = const_cast<char*>(CFStringGetCStringPtr(str, encodingMethod));
if( fullPath == NULL )
{
// 2nd try for Japanese system
encodingMethod = kCFStringEncodingUTF8;
fullPath = const_cast<char*>(CFStringGetCStringPtr(str, encodingMethod));
}
// for safer operation.
if( fullPath == NULL )
{
CFIndex length = CFStringGetLength(str);
fullPath = static_cast<char *>(malloc( length + 1 ));
// TODO: Check boolean result of that conversion
CFStringGetCString(str, fullPath, length, kCFStringEncodingUTF8 );
stdstr = fullPath;
free( fullPath );
}
else
stdstr = fullPath;
return stdstr;
}
void CSystemUtilsMacOSX::Init()
{
// These functions are a deprecated way to get the 'Application Support' folder, but they do work, in plain C++
@ -41,6 +78,22 @@ void CSystemUtilsMacOSX::Init()
boost::filesystem::create_directories(m_ASPath.c_str());
}
std::string CSystemUtilsMacOSX::GetDataPath()
{
std::string dataPath;
// Get the Resources bundle URL
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyBundleURL(mainBundle);
CFStringRef str = CFURLCopyFileSystemPath( resourcesURL, kCFURLPOSIXPathStyle );
CFRelease(resourcesURL);
dataPath = CFStringRefToStdString(str);
dataPath += "/Contents/Resources";
GetLogger()->Trace("dataPath is %s\n", dataPath.c_str());
return dataPath;
}
std::string CSystemUtilsMacOSX::GetProfileFileLocation()
{
std::string profileFile = m_ASPath + "/colobot.ini";

View File

@ -28,6 +28,7 @@ class CSystemUtilsMacOSX : public CSystemUtilsOther
public:
virtual void Init() override;
virtual std::string GetDataPath() override;
virtual std::string GetProfileFileLocation() override;
virtual std::string GetSavegameDirectoryLocation() override;
private: