cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_sysfilesystem.cpp (5124B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      4
      5  This software is provided 'as-is', without any express or implied
      6  warranty.  In no event will the authors be held liable for any damages
      7  arising from the use of this software.
      8
      9  Permission is granted to anyone to use this software for any purpose,
     10  including commercial applications, and to alter it and redistribute it
     11  freely, subject to the following restrictions:
     12
     13  1. The origin of this software must not be misrepresented; you must not
     14     claim that you wrote the original software. If you use this software
     15     in a product, an acknowledgment in the product documentation would be
     16     appreciated but is not required.
     17  2. Altered source versions must be plainly marked as such, and must not be
     18     misrepresented as being the original software.
     19  3. This notice may not be removed or altered from any source distribution.
     20*/
     21#include "../../SDL_internal.h"
     22
     23/* TODO, WinRT: include copyright info in SDL_winrtpaths.cpp
     24   TODO, WinRT: remove the need to compile this with C++/CX (/ZW) extensions, and if possible, without C++ at all
     25*/
     26
     27#ifdef __WINRT__
     28
     29extern "C" {
     30#include "SDL_filesystem.h"
     31#include "SDL_error.h"
     32#include "SDL_stdinc.h"
     33#include "SDL_system.h"
     34#include "../../core/windows/SDL_windows.h"
     35}
     36
     37#include <string>
     38#include <unordered_map>
     39
     40using namespace std;
     41using namespace Windows::Storage;
     42
     43extern "C" const wchar_t *
     44SDL_WinRTGetFSPathUNICODE(SDL_WinRT_Path pathType)
     45{
     46    switch (pathType) {
     47        case SDL_WINRT_PATH_INSTALLED_LOCATION:
     48        {
     49            static wstring path;
     50            if (path.empty()) {
     51                path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
     52            }
     53            return path.c_str();
     54        }
     55
     56        case SDL_WINRT_PATH_LOCAL_FOLDER:
     57        {
     58            static wstring path;
     59            if (path.empty()) {
     60                path = ApplicationData::Current->LocalFolder->Path->Data();
     61            }
     62            return path.c_str();
     63        }
     64
     65#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
     66        case SDL_WINRT_PATH_ROAMING_FOLDER:
     67        {
     68            static wstring path;
     69            if (path.empty()) {
     70                path = ApplicationData::Current->RoamingFolder->Path->Data();
     71            }
     72            return path.c_str();
     73        }
     74
     75        case SDL_WINRT_PATH_TEMP_FOLDER:
     76        {
     77            static wstring path;
     78            if (path.empty()) {
     79                path = ApplicationData::Current->TemporaryFolder->Path->Data();
     80            }
     81            return path.c_str();
     82        }
     83#endif
     84
     85        default:
     86            break;
     87    }
     88
     89    SDL_Unsupported();
     90    return NULL;
     91}
     92
     93extern "C" const char *
     94SDL_WinRTGetFSPathUTF8(SDL_WinRT_Path pathType)
     95{
     96    typedef unordered_map<SDL_WinRT_Path, string> UTF8PathMap;
     97    static UTF8PathMap utf8Paths;
     98
     99    UTF8PathMap::iterator searchResult = utf8Paths.find(pathType);
    100    if (searchResult != utf8Paths.end()) {
    101        return searchResult->second.c_str();
    102    }
    103
    104    const wchar_t * ucs2Path = SDL_WinRTGetFSPathUNICODE(pathType);
    105    if (!ucs2Path) {
    106        return NULL;
    107    }
    108
    109    char * utf8Path = WIN_StringToUTF8(ucs2Path);
    110    utf8Paths[pathType] = utf8Path;
    111    SDL_free(utf8Path);
    112    return utf8Paths[pathType].c_str();
    113}
    114
    115extern "C" char *
    116SDL_GetBasePath(void)
    117{
    118    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_INSTALLED_LOCATION);
    119    size_t destPathLen;
    120    char * destPath = NULL;
    121
    122    if (!srcPath) {
    123        SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
    124        return NULL;
    125    }
    126
    127    destPathLen = SDL_strlen(srcPath) + 2;
    128    destPath = (char *) SDL_malloc(destPathLen);
    129    if (!destPath) {
    130        SDL_OutOfMemory();
    131        return NULL;
    132    }
    133
    134    SDL_snprintf(destPath, destPathLen, "%s\\", srcPath);
    135    return destPath;
    136}
    137
    138extern "C" char *
    139SDL_GetPrefPath(const char *org, const char *app)
    140{
    141    /* WinRT note: The 'SHGetFolderPath' API that is used in Windows 7 and
    142     * earlier is not available on WinRT or Windows Phone.  WinRT provides
    143     * a similar API, but SHGetFolderPath can't be called, at least not
    144     * without violating Microsoft's app-store requirements.
    145     */
    146
    147#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
    148    /* A 'Roaming' folder is not available in Windows Phone 8, however a 'Local' folder is. */
    149    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_LOCAL_FOLDER);
    150#else
    151    /* A 'Roaming' folder is available on Windows 8 and 8.1.  Use that. */
    152    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_ROAMING_FOLDER);
    153#endif
    154
    155    size_t destPathLen;
    156    char * destPath = NULL;
    157
    158    if (!srcPath) {
    159        SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
    160        return NULL;
    161    }
    162
    163    destPathLen = SDL_strlen(srcPath) + SDL_strlen(org) + SDL_strlen(app) + 4;
    164    destPath = (char *) SDL_malloc(destPathLen);
    165    if (!destPath) {
    166        SDL_OutOfMemory();
    167        return NULL;
    168    }
    169
    170    SDL_snprintf(destPath, destPathLen, "%s\\%s\\%s\\", srcPath, org, app);
    171    return destPath;
    172}
    173
    174#endif /* __WINRT__ */