src/filesystem/cocoa/SDL_sysfilesystem.m
author Edward Rudd <urkle@outoforder.cc>
Sun, 25 Aug 2013 11:24:01 -0400
changeset 7700 963200277a0b
parent 7674 0cfb5e88fb95
child 7771 4434498bf4b9
permissions -rw-r--r--
reworked GetBasePath on OS X to use Contents/Resource by default if bundled, or exedir if not bundled.

- also adds OS X specific magic for bundled apps adding an Info.plist property of name SDL_FILESYSTEM_BASE_DIR_TYPE to the following values will change the bahaviour.
* bundle -- use the bundle directory e.g. "/Applications/MyGame/Blah.app/"
* parent -- use the bundle parent directory e.g. "/Applications/MyGame/"
* resource -- use the bundle resource directory (default) e.g. "/Applications/MyGame/Blah.app/Contents/Resources/"
icculus@7667
     1
/*
icculus@7667
     2
  Simple DirectMedia Layer
icculus@7667
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
icculus@7667
     4
icculus@7667
     5
  This software is provided 'as-is', without any express or implied
icculus@7667
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@7667
     7
  arising from the use of this software.
icculus@7667
     8
icculus@7667
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@7667
    10
  including commercial applications, and to alter it and redistribute it
icculus@7667
    11
  freely, subject to the following restrictions:
icculus@7667
    12
icculus@7667
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@7667
    14
     claim that you wrote the original software. If you use this software
icculus@7667
    15
     in a product, an acknowledgment in the product documentation would be
icculus@7667
    16
     appreciated but is not required.
icculus@7667
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@7667
    18
     misrepresented as being the original software.
icculus@7667
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@7667
    20
*/
icculus@7667
    21
#include "SDL_config.h"
icculus@7667
    22
icculus@7667
    23
#ifdef SDL_FILESYSTEM_COCOA
icculus@7667
    24
icculus@7667
    25
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
icculus@7667
    26
/* System dependent filesystem routines                                */
icculus@7667
    27
icculus@7674
    28
#include <Foundation/Foundation.h>
icculus@7667
    29
#include <sys/stat.h>
icculus@7668
    30
#include <sys/types.h>
icculus@7667
    31
icculus@7667
    32
#include "SDL_error.h"
icculus@7667
    33
#include "SDL_stdinc.h"
icculus@7667
    34
#include "SDL_filesystem.h"
icculus@7667
    35
icculus@7667
    36
char *
icculus@7667
    37
SDL_GetBasePath(void)
icculus@7667
    38
{
icculus@7667
    39
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
urkle@7700
    40
    NSBundle *bundle = [NSBundle mainBundle];
urkle@7700
    41
    const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
urkle@7700
    42
    const char *base = NULL;
icculus@7667
    43
    char *retval = NULL;
urkle@7700
    44
    if (baseType == NULL) {
urkle@7700
    45
        baseType = "resource";
urkle@7700
    46
    }
urkle@7700
    47
    if (SDL_strcasecmp(baseType, "bundle")==0) {
urkle@7700
    48
        base = [[bundle bundlePath] UTF8String];
urkle@7700
    49
    } else if (SDL_strcasecmp(baseType, "parent")==0) {
urkle@7700
    50
        base = [[[bundle bundlePath] stringByDeletingLastPathComponent] UTF8String];
urkle@7700
    51
    } else {
urkle@7700
    52
        /* this returns the exedir for non-bundled  and the resourceDir for bundled apps */
urkle@7700
    53
        base = [[bundle resourcePath] UTF8String];
urkle@7700
    54
    }
icculus@7667
    55
    if (base) {
icculus@7667
    56
        const size_t len = SDL_strlen(base) + 2;
icculus@7667
    57
        retval = (char *) SDL_malloc(len);
icculus@7667
    58
        if (retval == NULL) {
icculus@7667
    59
            SDL_OutOfMemory();
icculus@7667
    60
        } else {
icculus@7667
    61
            SDL_snprintf(retval, len, "%s/", base);
icculus@7667
    62
        }
icculus@7667
    63
    }
icculus@7667
    64
icculus@7667
    65
    [pool release];
icculus@7667
    66
    return retval;
icculus@7667
    67
}
icculus@7667
    68
icculus@7667
    69
char *
icculus@7667
    70
SDL_GetPrefPath(const char *org, const char *app)
icculus@7667
    71
{
icculus@7667
    72
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
icculus@7667
    73
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
icculus@7667
    74
    char *retval = NULL;
icculus@7667
    75
icculus@7667
    76
    (void) org;  // unused on Mac OS X and iOS.
icculus@7667
    77
icculus@7667
    78
    if ([array count] > 0) {  // we only want the first item in the list.
icculus@7667
    79
        NSString *str = [array objectAtIndex:0];
icculus@7667
    80
        const char *base = [str UTF8String];
icculus@7667
    81
        if (base) {
icculus@7667
    82
            const size_t len = SDL_strlen(base) + SDL_strlen(app) + 3;
icculus@7667
    83
            retval = (char *) SDL_malloc(len);
icculus@7667
    84
            if (retval == NULL) {
icculus@7667
    85
                SDL_OutOfMemory();
icculus@7667
    86
            } else {
icculus@7667
    87
                char *ptr;
icculus@7667
    88
                SDL_snprintf(retval, len, "%s/%s/", base, app);
icculus@7667
    89
                for (ptr = retval+1; *ptr; ptr++) {
icculus@7667
    90
                    if (*ptr == '/') {
icculus@7667
    91
                        *ptr = '\0';
icculus@7667
    92
                        mkdir(retval, 0700);
icculus@7667
    93
                        *ptr = '/';
icculus@7667
    94
                    }
icculus@7667
    95
                }
icculus@7667
    96
                mkdir(retval, 0700);
icculus@7667
    97
            }
icculus@7667
    98
        }
icculus@7667
    99
    }
icculus@7667
   100
icculus@7667
   101
    [pool release];
icculus@7667
   102
    return retval;
icculus@7667
   103
}
icculus@7667
   104
icculus@7667
   105
#endif /* SDL_FILESYSTEM_COCOA */
icculus@7667
   106
icculus@7667
   107
/* vi: set ts=4 sw=4 expandtab: */