src/file/cocoa/SDL_rwopsbundlesupport.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Aug 2017 01:42:18 -0700
changeset 11386 d5c2d689bf6d
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed build when Wayland is dynamically loaded
slouken@8616
     1
/*
slouken@8616
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@8616
     4
slouken@8616
     5
  This software is provided 'as-is', without any express or implied
slouken@8616
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@8616
     7
  arising from the use of this software.
slouken@8616
     8
slouken@8616
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@8616
    10
  including commercial applications, and to alter it and redistribute it
slouken@8616
    11
  freely, subject to the following restrictions:
slouken@8616
    12
slouken@8616
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@8616
    14
     claim that you wrote the original software. If you use this software
slouken@8616
    15
     in a product, an acknowledgment in the product documentation would be
slouken@8616
    16
     appreciated but is not required.
slouken@8616
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@8616
    18
     misrepresented as being the original software.
slouken@8616
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@8616
    20
*/
slouken@8616
    21
#include "../../SDL_internal.h"
slouken@8616
    22
slouken@8627
    23
#ifdef __APPLE__
ewing@4447
    24
#import <Foundation/Foundation.h>
ewing@4447
    25
slouken@4472
    26
#include "SDL_rwopsbundlesupport.h"
slouken@4472
    27
ewing@4447
    28
/* For proper OS X applications, the resources are contained inside the application bundle.
ewing@4447
    29
 So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
ewing@4447
    30
 Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
ewing@4447
    31
 We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
ewing@4447
    32
 but we would somehow need to know what the bundle identifiers we need to search are.
ewing@4447
    33
 Also, note the bundle layouts are different for iPhone and Mac.
ewing@4447
    34
*/
ewing@4447
    35
FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
slouken@9087
    36
{ @autoreleasepool
ewing@4447
    37
{
ewing@4447
    38
    FILE* fp = NULL;
ewing@4447
    39
slouken@7191
    40
    /* If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only. */
slouken@8986
    41
    if(strcmp("r", mode) && strcmp("rb", mode)) {
slouken@7191
    42
        return fopen(file, mode);
slouken@7191
    43
    }
ewing@4449
    44
slouken@7191
    45
    NSFileManager* file_manager = [NSFileManager defaultManager];
slouken@7191
    46
    NSString* resource_path = [[NSBundle mainBundle] resourcePath];
slouken@6848
    47
slouken@7191
    48
    NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
ewing@4447
    49
slouken@7191
    50
    NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
slouken@8986
    51
    if([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
slouken@7191
    52
        fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
slime73@9515
    53
    } else {
slouken@7191
    54
        fp = fopen(file, mode);
slouken@7191
    55
    }
slouken@6848
    56
slouken@7191
    57
    return fp;
slouken@9087
    58
}}
slouken@8616
    59
slime73@9487
    60
#endif /* __APPLE__ */
slouken@8616
    61
slouken@8616
    62
/* vi: set ts=4 sw=4 expandtab: */