summaryrefslogtreecommitdiffstats
path: root/src/common/resources/resourcemanager.cpp
blob: 0741c70013162bf08103ce5dd290024add25ba43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// * This file is part of the COLOBOT source code
// * Copyright (C) 2014 Polish Portal of Colobot (PPC)
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.


#include "common/resources/resourcemanager.h"

#include "common/config.h"
#include "common/logger.h"

#include <physfs.h>


CResourceManager::CResourceManager(const char *argv0)
{
    if (!PHYSFS_init(argv0))
    {
        CLogger::GetInstancePointer()->Error("Error while initializing physfs\n");
    }
}


CResourceManager::~CResourceManager()
{
    if (PHYSFS_isInit())
    {
        if (!PHYSFS_deinit())
        {
            CLogger::GetInstancePointer()->Error("Error while deinitializing physfs\n");
        }
    }
}


bool CResourceManager::AddLocation(const std::string &location, bool prepend)
{
    if (PHYSFS_isInit())
    {
        if (!PHYSFS_mount(location.c_str(), nullptr, prepend ? 0 : 1))
        {
            CLogger::GetInstancePointer()->Error("Error while mounting \"%s\"\n", location.c_str());
        }
    }
    
    return false;
}


bool CResourceManager::RemoveLocation(const std::string &location)
{
    if (PHYSFS_isInit())
    {
        if (!PHYSFS_removeFromSearchPath(location.c_str()))
        {
            CLogger::GetInstancePointer()->Error("Error while unmounting \"%s\"\n", location.c_str());
        }
    }
    
    return false;
}


bool CResourceManager::SetSaveLocation(const std::string &location)
{
    if (PHYSFS_isInit())
    {
        if (!PHYSFS_setWriteDir(location.c_str()))
        {
            CLogger::GetInstancePointer()->Error("Error while setting save location to \"%s\"\n", location.c_str());
        }
    }
    
    return false;
}


SDL_RWops* CResourceManager::GetSDLFileHandler(const std::string &filename)
{
    SDL_RWops *handler = SDL_AllocRW();
    if (!handler)
    {
        CLogger::GetInstancePointer()->Error("Unable to allocate SDL_RWops for \"%s\"\n", filename.c_str());
        return nullptr;
    }
    
    if (!PHYSFS_isInit())
    {
        SDL_FreeRW(handler);
        return nullptr;
    }
    
    PHYSFS_File *file = PHYSFS_openRead(filename.c_str());
    if (!file)
    {
        SDL_FreeRW(handler);
        return nullptr;
    }

    handler->seek = SDLSeek;
    handler->read = SDLRead;
    handler->write = SDLWrite;
    handler->close = SDLClose;
    handler->type = 0xc010b04f;
    handler->hidden.unknown.data1 = file;
    
    return handler;
}


CSNDFile* CResourceManager::GetSNDFileHandler(const std::string &filename)
{   
    return new CSNDFile(filename);
}


bool CResourceManager::Exists(const std::string &filename)
{
    return PHYSFS_exists(filename.c_str());
}

std::vector<std::string> CResourceManager::ListFiles(const std::string &directory)
{
    std::vector<std::string> result;
    
    char **files = PHYSFS_enumerateFiles(directory.c_str());
    
    for (char **i = files; *i != nullptr; i++) {
        result.push_back(*i);
    }
    
    PHYSFS_freeList(files);
    
    return result;
}


int CResourceManager::SDLClose(SDL_RWops *context)
{
    if (CheckSDLContext(context))
    {
        PHYSFS_close(static_cast<PHYSFS_File *>(context->hidden.unknown.data1));
        SDL_FreeRW(context);
        
        return 0;
    }
    
    return 1;
}


int CResourceManager::SDLRead(SDL_RWops *context, void *ptr, int size, int maxnum)
{
    if (CheckSDLContext(context))
    {
        PHYSFS_File *file = static_cast<PHYSFS_File *>(context->hidden.unknown.data1);
        SDL_memset(ptr, 0, size * maxnum);
       
        return PHYSFS_read(file, ptr, size, maxnum);
    }
    
    return 0;
}


int CResourceManager::SDLWrite(SDL_RWops *context, const void *ptr, int size, int num)
{
    return 0;
}


int CResourceManager::SDLSeek(SDL_RWops *context, int offset, int whence)
{
    if (CheckSDLContext(context))
    {
        PHYSFS_File *file = static_cast<PHYSFS_File *>(context->hidden.unknown.data1);
        int position, result;
        
        switch (whence)
        {
            default:
            case RW_SEEK_SET:
                result = PHYSFS_seek(file, offset);
                return result > 0 ? offset : -1;
                
            case RW_SEEK_CUR:
                position = offset + PHYSFS_tell(file);
                result = PHYSFS_seek(file, position);
                return result > 0 ? position : -1;
                
            case RW_SEEK_END:
                position = PHYSFS_fileLength(file) - offset;
                result = PHYSFS_seek(file, position);
                return result > 0 ? position : -1; 
        }
    }
    
    return -1;
}


bool CResourceManager::CheckSDLContext(SDL_RWops *context)
{
    if (context->type != 0xc010b04f)
    {
        SDL_SetError("Wrong kind of RWops");
        return false;
    }
    
    return true;
}