Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/GameInfoCache.h
5675 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <string>
21
#include <map>
22
#include <memory>
23
#include <mutex>
24
#include <atomic>
25
26
#include "Common/Thread/Event.h"
27
#include "Core/ELF/ParamSFO.h"
28
#include "Common/File/Path.h"
29
30
namespace Draw {
31
class DrawContext;
32
class Texture;
33
}
34
35
// A GameInfo holds information about a game, and also lets you do things that the VSH
36
// does on the PSP, namely checking for and deleting savedata, and similar things.
37
// Only cares about games that are installed on the current device.
38
39
// A GameInfo object can also represent a piece of savedata.
40
41
enum class GameInfoFlags {
42
EMPTY = 0x00,
43
FILE_TYPE = 0x01, // Don't need to specify this, always included.
44
PARAM_SFO = 0x02,
45
ICON = 0x04,
46
PIC1 = 0x08,
47
PIC0 = 0x10,
48
SND = 0x20,
49
SIZE = 0x40,
50
UNCOMPRESSED_SIZE = 0x80,
51
};
52
ENUM_CLASS_BITOPS(GameInfoFlags);
53
54
class FileLoader;
55
enum class IdentifiedFileType;
56
57
struct GameInfoTex {
58
std::string data;
59
Draw::Texture *texture = nullptr;
60
// The time at which the Icon and the BG were loaded.
61
// Can be useful to fade them in smoothly once they appear.
62
// Also, timeLoaded != 0 && texture == nullptr means that the load failed.
63
double timeLoaded = 0.0;
64
std::atomic<bool> dataLoaded{};
65
66
// Can ONLY be called from the main thread!
67
void Clear();
68
bool Failed() const {
69
return timeLoaded != 0.0 && !texture;
70
}
71
};
72
73
class GameInfo {
74
public:
75
GameInfo(const Path &gamePath);
76
~GameInfo();
77
78
bool Delete(); // Better be sure what you're doing when calling this. Will move to trash if available on the system, though.
79
bool DeleteAllSaveData();
80
bool CreateLoader();
81
82
bool HasFileLoader() const {
83
return fileLoader.get() != nullptr;
84
}
85
86
std::shared_ptr<FileLoader> GetFileLoader();
87
void DisposeFileLoader();
88
89
u64 GetSizeUncompressedInBytes(); // NOTE: More expensive than GetGameSizeOnDiskInBytes().
90
u64 GetSizeOnDiskInBytes();
91
u64 GetGameSavedataSizeInBytes(); // For games
92
u64 GetInstallDataSizeInBytes();
93
94
// For various kinds of savedata, mainly.
95
// NOTE: This one actually performs I/O directly, not cached.
96
std::string GetMTime() const;
97
98
void ParseParamSFO(IdentifiedFileType type);
99
const ParamSFOData &GetParamSFO() const {
100
_dbg_assert_(hasFlags & GameInfoFlags::PARAM_SFO);
101
return paramSFO;
102
}
103
void FinishPendingTextureLoads(Draw::DrawContext *draw);
104
105
std::vector<Path> GetSaveDataDirectories();
106
107
std::string GetTitle();
108
std::string GetDBTitle(); // Falls back to GetTitle if not in the DB.
109
110
void SetTitle(const std::string &newTitle);
111
112
const Path &GetFilePath() const {
113
return filePath_;
114
}
115
116
bool Ready(GameInfoFlags flags) {
117
std::unique_lock<std::mutex> guard(lock);
118
// Avoid the operator, we want to check all the bits.
119
return ((int)hasFlags & (int)flags) == (int)flags;
120
}
121
122
void MarkReadyNoLock(GameInfoFlags flags) {
123
hasFlags |= flags;
124
pendingFlags &= ~flags;
125
}
126
127
GameInfoTex *GetPIC1() {
128
if (pic1.texture)
129
return &pic1;
130
return nullptr;
131
}
132
133
// Hold this when reading or writing from the GameInfo.
134
// Don't need to hold it when just passing around the pointer,
135
// and obviously also not when creating it and holding the only pointer
136
// to it.
137
std::mutex lock;
138
139
// Controls access to the fileLoader pointer.
140
std::mutex loaderLock;
141
142
// Keep track of what we have, or what we're processing.
143
// These are protected by the mutex. While pendingFlags != 0, something is being loaded.
144
GameInfoFlags hasFlags{};
145
GameInfoFlags pendingFlags{};
146
147
std::string id;
148
std::string id_version;
149
int disc_total = 0;
150
int disc_number = 0;
151
GameRegion region = GameRegion::UNKNOWN;
152
IdentifiedFileType fileType;
153
bool hasConfig = false;
154
155
// Pre read the data, create a texture the next time
156
GameInfoTex icon;
157
GameInfoTex pic0;
158
GameInfoTex pic1;
159
160
std::string sndFileData;
161
std::atomic<bool> sndDataLoaded{};
162
163
double lastAccessedTime = 0.0;
164
165
u64 gameSizeUncompressed = 0;
166
u64 gameSizeOnDisk = 0; // compressed size, in case of CSO
167
u64 saveDataSize = 0;
168
u64 installDataSize = 0;
169
170
std::string errorString;
171
172
protected:
173
ParamSFOData paramSFO;
174
// Note: this can change while loading, use GetTitle().
175
std::string title;
176
177
// TODO: Get rid of this shared_ptr and managae lifetime better instead.
178
std::shared_ptr<FileLoader> fileLoader;
179
Path filePath_;
180
181
void SetupTexture(Draw::DrawContext *draw, GameInfoTex &tex);
182
183
private:
184
DISALLOW_COPY_AND_ASSIGN(GameInfo);
185
friend class GameInfoWorkItem;
186
};
187
188
class GameInfoCache {
189
public:
190
GameInfoCache();
191
~GameInfoCache();
192
193
// This creates a background worker thread!
194
void Clear();
195
void PurgeType(IdentifiedFileType fileType);
196
197
// All data in GameInfo including icon.texture may be zero the first time you call this
198
// but filled in later asynchronously in the background. So keep calling this,
199
// redrawing the UI often. Only set flags to GAMEINFO_WANTBG or WANTSND if you really want them
200
// because they're big. bgTextures and sound may be discarded over time as well.
201
// NOTE: This never returns null, so you don't need to check for that. Do check Ready() flags though.
202
// It's OK to pass in nullptr for draw if you don't need the actual texture right now.
203
std::shared_ptr<GameInfo> GetInfo(Draw::DrawContext *draw, const Path &gamePath, GameInfoFlags wantFlags, GameInfoFlags *outHasFlags = nullptr);
204
void FlushBGs(); // Gets rid of all BG textures. Also gets rid of bg sounds.
205
206
void CancelAll();
207
208
private:
209
void Shutdown();
210
211
// Maps ISO path to info. Need to use shared_ptr as we can return these pointers -
212
// and if they get destructed while being in use, that's bad.
213
std::map<std::string, std::shared_ptr<GameInfo> > info_;
214
std::mutex mapLock_;
215
};
216
217
// This one can be global, no good reason not to.
218
extern GameInfoCache *g_gameInfoCache;
219
220