Ticket #1145: windows_osx_path_fixes-02042012.patch

File windows_osx_path_fixes-02042012.patch, 17.2 KB (added by historic_bruno, 12 years ago)
  • source/lib/sysdep/os/osx/osxpaths.h

     
     1/* Copyright (c) 2012 Wildfire Games
     2 *
     3 * Permission is hereby granted, free of charge, to any person obtaining
     4 * a copy of this software and associated documentation files (the
     5 * "Software"), to deal in the Software without restriction, including
     6 * without limitation the rights to use, copy, modify, merge, publish,
     7 * distribute, sublicense, and/or sell copies of the Software, and to
     8 * permit persons to whom the Software is furnished to do so, subject to
     9 * the following conditions:
     10 *
     11 * The above copyright notice and this permission notice shall be included
     12 * in all copies or substantial portions of the Software.
     13 *
     14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     21 */
     22
     23
     24#ifndef INCLUDE_OSXPATHS_H
     25#define INCLUDE_OSXPATHS_H
     26
     27#include <CoreFoundation/CoreFoundation.h>
     28#include <CoreFoundation/CFString.h>
     29
     30CFStringRef osx_AppSupportPath();
     31
     32CFStringRef osx_CachesPath();
     33
     34CFStringRef osx_DocumentsPath();
     35
     36#endif // INCLUDE_OSXPATHS_H
  • source/lib/sysdep/os/osx/osxpaths.h

  • source/lib/sysdep/os/osx/osxpaths.mm

    Property changes on: source/lib/sysdep/os/osx/osxpaths.h
    ___________________________________________________________________
    Added: svn:eol-style
    ## -0,0 +1 ##
    +native
     
     1/* Copyright (c) 2012 Wildfire Games
     2 *
     3 * Permission is hereby granted, free of charge, to any person obtaining
     4 * a copy of this software and associated documentation files (the
     5 * "Software"), to deal in the Software without restriction, including
     6 * without limitation the rights to use, copy, modify, merge, publish,
     7 * distribute, sublicense, and/or sell copies of the Software, and to
     8 * permit persons to whom the Software is furnished to do so, subject to
     9 * the following conditions:
     10 *
     11 * The above copyright notice and this permission notice shall be included
     12 * in all copies or substantial portions of the Software.
     13 *
     14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     21 */
     22
     23#import <Foundation/Foundation.h>
     24
     25#import "osxpaths.h"
     26
     27
     28// Find ~/Library/Application Support/ directory
     29CFStringRef osx_AppSupportPath()
     30{
     31    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     32
     33    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
     34
     35    if ([paths count] == 0)
     36    {
     37        return NULL;
     38    }
     39
     40    CFStringRef path = (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)paths, 0);
     41
     42    // Caller will be responsible for freeing the string with CFRelease
     43    CFRetain(path);
     44
     45    [pool release];
     46
     47    return path;
     48}
     49
     50// Find ~/Library/Caches/ directory
     51CFStringRef osx_CachesPath()
     52{
     53    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     54
     55    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
     56
     57    if ([paths count] == 0)
     58    {
     59        return NULL;
     60    }
     61
     62    CFStringRef path = (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)paths, 0);
     63
     64    // Caller will be responsible for freeing the string with CFRelease
     65    CFRetain(path);
     66
     67    [pool release];
     68
     69    return path;
     70}
     71
     72// Find ~/Documents/ directory
     73CFStringRef osx_DocumentsPath()
     74{
     75    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     76
     77    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     78
     79    if ([paths count] == 0)
     80    {
     81        return NULL;
     82    }
     83
     84    CFStringRef path = (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)paths, 0);
     85
     86    // Caller will be responsible for freeing the string with CFRelease
     87    CFRetain(path);
     88
     89    [pool release];
     90
     91    return path;
     92}
  • source/lib/sysdep/os/win/wutil.cpp

     
    1 /* Copyright (c) 2010 Wildfire Games
     1/* Copyright (c) 2012 Wildfire Games
    22 *
    33 * Permission is hereby granted, free of charge, to any person obtaining
    44 * a copy of this software and associated documentation files (the
     
    251251// (NB: wutil_Init is called before static ctors => use placement new)
    252252static OsPath* systemPath;
    253253static OsPath* executablePath;
    254 static OsPath* appdataPath;
     254static OsPath* localAppdataPath;
     255static OsPath* roamingAppdataPath;
     256static OsPath* personalPath;
    255257
    256258const OsPath& wutil_SystemPath()
    257259{
     
    263265    return *executablePath;
    264266}
    265267
    266 const OsPath& wutil_AppdataPath()
     268const OsPath& wutil_LocalAppdataPath()
    267269{
    268     return *appdataPath;
     270    return *localAppdataPath;
    269271}
    270272
     273const OsPath& wutil_RoamingAppdataPath()
     274{
     275    return *roamingAppdataPath;
     276}
    271277
     278const OsPath& wutil_PersonalPath()
     279{
     280    return *personalPath;
     281}
     282
     283
    272284static void GetDirectories()
    273285{
    274286    WinScopedPreserveLastError s;
     
    286298    // executable's directory
    287299    executablePath = new(wutil_Allocate(sizeof(OsPath))) OsPath(sys_ExecutablePathname().Parent());
    288300
    289     // application data
     301    // roaming application data
    290302    {
    291303        HWND hwnd = 0;  // ignored unless a dial-up connection is needed to access the folder
    292304        HANDLE token = 0;
     
    295307        ENSURE(SUCCEEDED(ret));
    296308        if(GetLastError() == ERROR_NO_TOKEN)    // avoid polluting last error
    297309            SetLastError(0);
    298         appdataPath = new(wutil_Allocate(sizeof(OsPath))) OsPath(path);
     310        roamingAppdataPath = new(wutil_Allocate(sizeof(OsPath))) OsPath(path);
    299311    }
     312
     313    // local application data
     314    {
     315        HWND hwnd = 0;  // ignored unless a dial-up connection is needed to access the folder
     316        HANDLE token = 0;
     317        wchar_t path[MAX_PATH]; // mandated by SHGetFolderPathW
     318        const HRESULT ret = SHGetFolderPathW(hwnd, CSIDL_LOCAL_APPDATA, token, 0, path);
     319        ENSURE(SUCCEEDED(ret));
     320        if(GetLastError() == ERROR_NO_TOKEN)    // avoid polluting last error
     321            SetLastError(0);
     322        localAppdataPath = new(wutil_Allocate(sizeof(OsPath))) OsPath(path);
     323    }
     324
     325    // my documents
     326    {
     327        HWND hwnd = 0;  // ignored unless a dial-up connection is needed to access the folder
     328        HANDLE token = 0;
     329        wchar_t path[MAX_PATH]; // mandated by SHGetFolderPathW
     330        const HRESULT ret = SHGetFolderPathW(hwnd, CSIDL_PERSONAL, token, 0, path);
     331        ENSURE(SUCCEEDED(ret));
     332        if(GetLastError() == ERROR_NO_TOKEN)    // avoid polluting last error
     333            SetLastError(0);
     334        personalPath = new(wutil_Allocate(sizeof(OsPath))) OsPath(path);
     335    }
    300336}
    301337
    302338
     
    306342    wutil_Free(systemPath);
    307343    executablePath->~OsPath();
    308344    wutil_Free(executablePath);
    309     appdataPath->~OsPath();
    310     wutil_Free(appdataPath);
     345    localAppdataPath->~OsPath();
     346    wutil_Free(localAppdataPath);
     347    roamingAppdataPath->~OsPath();
     348    wutil_Free(roamingAppdataPath);
     349    personalPath->~OsPath();
     350    wutil_Free(personalPath);
    311351}
    312352
    313353
  • source/lib/sysdep/os/win/wutil.h

     
    1 /* Copyright (c) 2010 Wildfire Games
     1/* Copyright (c) 2012 Wildfire Games
    22 *
    33 * Permission is hereby granted, free of charge, to any person obtaining
    44 * a copy of this software and associated documentation files (the
     
    159159
    160160extern const OsPath& wutil_SystemPath();
    161161extern const OsPath& wutil_ExecutablePath();
    162 extern const OsPath& wutil_AppdataPath();
     162extern const OsPath& wutil_LocalAppdataPath();
     163extern const OsPath& wutil_RoamingAppdataPath();
     164extern const OsPath& wutil_PersonalPath();
    163165
    164166
    165167//-----------------------------------------------------------------------------
  • source/ps/GameSetup/GameSetup.cpp

     
    441441    const size_t cacheSize = ChooseCacheSize();
    442442    g_VFS = CreateVfs(cacheSize);
    443443
    444     g_VFS->Mount(L"screenshots/", paths.Data()/"screenshots"/"");
    445     g_VFS->Mount(L"saves/", paths.Data()/"saves"/"");
     444    g_VFS->Mount(L"screenshots/", paths.UserData()/"screenshots"/"");
     445    g_VFS->Mount(L"saves/", paths.UserData()/"saves"/"");
    446446    const OsPath readonlyConfig = paths.RData()/"config"/"";
    447447    g_VFS->Mount(L"config/", readonlyConfig);
    448448    if(readonlyConfig != paths.Config())
  • source/ps/GameSetup/Paths.cpp

     
    1 /* Copyright (C) 2009 Wildfire Games.
     1/* Copyright (C) 2012 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    2222#include "lib/sysdep/sysdep.h"  // sys_get_executable_name
    2323#include "lib/sysdep/filesystem.h"  // wrealpath
    2424#if OS_WIN
    25 # include "lib/sysdep/os/win/wutil.h"   // wutil_AppdataPath
     25# include "lib/sysdep/os/win/wutil.h"   // wutil_*Path
     26#elif OS_MACOSX
     27# include "lib/sysdep/os/osx/osxpaths.h" // osx_*Path
    2628#endif
    2729
    2830
     
    3840
    3941    const char* subdirectoryName = args.Has("writableRoot")? 0 : "0ad";
    4042
    41     // everything is a subdirectory of the root
     43    // Note: Only if writableRoot option is passed to the game, then
     44    //  all the data is a subdirectory of the root
    4245    if(!subdirectoryName)
    4346    {
    44         m_data = m_rdata;
    45         m_config = m_data/"config"/"";
    46         m_cache = m_data/"cache"/"";
    47         m_logs = m_root/"logs"/"";
     47        m_gameData = m_rdata;
     48        m_userData = m_gameData;
     49        m_config = m_gameData / "config"/"";
     50        m_cache = m_gameData / "cache"/"";
     51        m_logs = m_root / "logs"/"";
    4852    }
    49     else
     53    else // OS-specific path handling
    5054    {
     55
    5156#if OS_WIN
    52         const OsPath appdata = wutil_AppdataPath() / subdirectoryName/"";
    53         m_data = appdata/"data"/"";
    54         m_config = appdata/"config"/"";
    55         m_cache = appdata/"cache"/"";
    56         m_logs = appdata/"logs"/"";
    57 #else
     57
     58        /* For reasoning behind our Windows paths, see the discussion here:
     59         * http://www.wildfiregames.com/forum/index.php?showtopic=14759
     60         *
     61         * Summary:
     62         *  1. Local appdata: for bulky unfriendly data like the cache,
     63         *      which can be recreated if deleted; doesn't need backing up.
     64         *  2. Roaming appdata: for slightly less unfriendly data like config
     65         *      files that might theoretically be shared between different
     66         *      machines on a domain.
     67         *  3. Personal / My Documents: for data explicitly created by the user,
     68         *      and which should be visible and easily accessed. We use a non-
     69         *      localized My Games subfolder for improved organization.
     70         */
     71
     72        // %localappdata%/0ad/
     73        const OsPath localAppdata = wutil_LocalAppdataPath() / subdirectoryName/"";
     74        // %appdata%/0ad/
     75        const OsPath roamingAppData = wutil_RoamingAppdataPath() / subdirectoryName/"";
     76        // My Documents/My Games/0ad/
     77        const OsPath personalData = wutil_PersonalPath() / "My Games" / subdirectoryName/"";
     78
     79        m_cache = localAppdata / "cache"/"";
     80        m_gameData = roamingAppData / "data"/"";
     81        m_userData = personalData/"";
     82        m_config = roamingAppData / "config"/"";
     83        m_logs = personalData / "logs"/"";
     84
     85#elif OS_MACOSX
     86
     87        /* For reasoning behind our OS X paths, see the discussion here:
     88         * http://www.wildfiregames.com/forum/index.php?showtopic=15511
     89         *
     90         * Summary:
     91         * 1. Application Support - This is where data created by the app
     92         *     is supposed to be stored, with a few exceptions. However,
     93         *     on Lion this folder is hidden in Finder, it doesn't make sense
     94         *     to store screenshots and saved games in a hidden location!
     95         * 2. Caches - This is where cached data for the app is stored,
     96         *     and unlike App Support it is not automatically backed up.
     97         * 3. Documents - This is a friendly easy-to-access folder that seems
     98         *     appropriate for files created on-demand by the user and which
     99         *     they want to access directory e.g. screenshots.
     100         *
     101         * Note: the paths returned by osx_*Path are not guaranteed to exist,
     102         *     but that's OK since we always create them on demand.
     103         * The returned CStringRef is our responsibility to release
     104         *     (so it's not automatically cleaned up when leaving Cocoa)
     105         */
     106
     107        // TODO: Use bundle name instead of 0ad?
     108        OsPath appSupportPath;  // ~/Library/Application Support/0ad
     109        OsPath cachePath;       // ~/Library/Caches/0ad
     110        OsPath documentsPath;   // ~/Documents/0ad
     111        {
     112            CFStringRef pathRef = osx_AppSupportPath();
     113            ENSURE(pathRef != NULL && CFStringGetLength(pathRef) > 0);
     114            // There's no PATH_MAX on OS X
     115            char path[CFStringGetMaximumSizeOfFileSystemRepresentation(pathRef)];
     116            // Convert path CFStringRef to char*
     117            CFStringGetFileSystemRepresentation(pathRef, path, sizeof(path));
     118            CFRelease(pathRef);
     119            appSupportPath = OsPath(path) / subdirectoryName;
     120        }
     121        {
     122            CFStringRef pathRef = osx_CachesPath();
     123            ENSURE(pathRef != NULL && CFStringGetLength(pathRef) > 0);
     124            char path[CFStringGetMaximumSizeOfFileSystemRepresentation(pathRef)];
     125            CFStringGetFileSystemRepresentation(pathRef, path, sizeof(path));
     126            CFRelease(pathRef);
     127            cachePath = OsPath(path) / subdirectoryName;
     128        }
     129        {
     130            CFStringRef pathRef = osx_DocumentsPath();
     131            ENSURE(pathRef != NULL && CFStringGetLength(pathRef) > 0);
     132            char path[CFStringGetMaximumSizeOfFileSystemRepresentation(pathRef)];
     133            CFStringGetFileSystemRepresentation(pathRef, path, sizeof(path));
     134            CFRelease(pathRef);
     135            documentsPath = OsPath(path) / subdirectoryName;
     136        }
     137
     138        m_gameData = appSupportPath / "data"/"";
     139        m_userData = documentsPath/"";
     140        m_cache = cachePath/"";
     141        m_config = appSupportPath / "config"/"";
     142        m_logs = appSupportPath / "logs"/"";
     143
     144#else // OS_UNIX
     145
    58146        const char* envHome = getenv("HOME");
    59147        ENSURE(envHome);
    60148        const OsPath home(envHome);
    61149        const OsPath xdgData   = XDG_Path("XDG_DATA_HOME",   home, home/".local/share/") / subdirectoryName;
    62150        const OsPath xdgConfig = XDG_Path("XDG_CONFIG_HOME", home, home/".config/"     ) / subdirectoryName;
    63151        const OsPath xdgCache  = XDG_Path("XDG_CACHE_HOME",  home, home/".cache/"      ) / subdirectoryName;
    64         m_data   = xdgData/"";
     152
     153        // We don't make the game vs. user data distinction on Unix
     154        m_gameData = xdgData/"";
     155        m_userData = m_gameData;
    65156        m_cache  = xdgCache/"";
    66         m_config = xdgConfig/"config"/"";
    67         m_logs   = xdgConfig/"logs"/"";
     157        m_config = xdgConfig / "config"/"";
     158        m_logs   = xdgConfig / "logs"/"";
    68159#endif
    69160    }
    70161}
  • source/ps/GameSetup/Paths.h

     
    1 /* Copyright (C) 2009 Wildfire Games.
     1/* Copyright (C) 2012 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    2121#include "lib/os_path.h"
    2222#include "CmdLineArgs.h"
    2323
     24/**
     25 * Wrapper class for OS paths used by the game
     26 */
    2427class Paths
    2528{
    2629public:
    2730    Paths(const CmdLineArgs& args);
    2831
     32    /**
     33     * Returns the game's root directory
     34     */
    2935    const OsPath& Root() const
    3036    {
    3137        return m_root;
    3238    }
    3339
     40    /**
     41     * Returns directory for read-only data installed with the game
     42     */
    3443    const OsPath& RData() const
    3544    {
    3645        return m_rdata;
    3746    }
    3847
    39     const OsPath& Data() const
     48    /**
     49     * Returns directory for game-managed data and mods
     50     */
     51    const OsPath& GameData() const
    4052    {
    41         return m_data;
     53        return m_gameData;
    4254    }
    4355
     56    /**
     57     * Returns directory for user-created data (e.g. screenshots)
     58     * Only things created in reponse to an explicit user action should go here.
     59     */
     60    const OsPath& UserData() const
     61    {
     62        return m_userData;
     63    }
     64
     65    /**
     66     * Returns config file directory
     67     */
    4468    const OsPath& Config() const
    4569    {
    4670        return m_config;
    4771    }
    4872
     73    /**
     74     * Returns cache directory
     75     */
    4976    const OsPath& Cache() const
    5077    {
    5178        return m_cache;
    5279    }
    5380
     81    /**
     82     * Returns logs directory
     83     */
    5484    const OsPath& Logs() const
    5585    {
    5686        return m_logs;
     
    6595    OsPath m_rdata;
    6696
    6797    // writable directories
    68     OsPath m_data;
     98    OsPath m_gameData;
     99    OsPath m_userData;
    69100    OsPath m_config;
    70101    OsPath m_cache;
    71102    OsPath m_logs;  // special-cased in single-root-folder installations