CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/GameInfoCache.h
Views: 1401
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
// Guessed from GameID, not necessarily accurate
42
enum GameRegion {
43
GAMEREGION_JAPAN,
44
GAMEREGION_USA,
45
GAMEREGION_EUROPE,
46
GAMEREGION_HONGKONG,
47
GAMEREGION_ASIA,
48
GAMEREGION_KOREA,
49
GAMEREGION_OTHER,
50
GAMEREGION_MAX,
51
};
52
53
enum class GameInfoFlags {
54
FILE_TYPE = 0x01, // Don't need to specify this, always included.
55
PARAM_SFO = 0x02,
56
ICON = 0x04,
57
BG = 0x08,
58
SND = 0x10,
59
SIZE = 0x20,
60
UNCOMPRESSED_SIZE = 0x40,
61
};
62
ENUM_CLASS_BITOPS(GameInfoFlags);
63
64
class FileLoader;
65
enum class IdentifiedFileType;
66
67
struct GameInfoTex {
68
std::string data;
69
Draw::Texture *texture = nullptr;
70
// The time at which the Icon and the BG were loaded.
71
// Can be useful to fade them in smoothly once they appear.
72
// Also, timeLoaded != 0 && texture == nullptr means that the load failed.
73
double timeLoaded = 0.0;
74
std::atomic<bool> dataLoaded{};
75
76
// Can ONLY be called from the main thread!
77
void Clear();
78
bool Failed() const {
79
return timeLoaded != 0.0 && !texture;
80
}
81
};
82
83
class GameInfo {
84
public:
85
GameInfo(const Path &gamePath);
86
~GameInfo();
87
88
bool Delete(); // Better be sure what you're doing when calling this.
89
bool DeleteAllSaveData();
90
bool CreateLoader();
91
92
bool HasFileLoader() const {
93
return fileLoader.get() != nullptr;
94
}
95
96
std::shared_ptr<FileLoader> GetFileLoader();
97
void DisposeFileLoader();
98
99
u64 GetSizeUncompressedInBytes(); // NOTE: More expensive than GetGameSizeOnDiskInBytes().
100
u64 GetSizeOnDiskInBytes();
101
u64 GetGameSavedataSizeInBytes(); // For games
102
u64 GetInstallDataSizeInBytes();
103
104
// For various kinds of savedata, mainly.
105
// NOTE: This one actually performs I/O directly, not cached.
106
std::string GetMTime() const;
107
108
void ParseParamSFO();
109
const ParamSFOData &GetParamSFO() const {
110
_dbg_assert_(hasFlags & GameInfoFlags::PARAM_SFO);
111
return paramSFO;
112
}
113
void FinishPendingTextureLoads(Draw::DrawContext *draw);
114
115
std::vector<Path> GetSaveDataDirectories();
116
117
std::string GetTitle();
118
void SetTitle(const std::string &newTitle);
119
120
const Path &GetFilePath() const {
121
return filePath_;
122
}
123
124
bool Ready(GameInfoFlags flags) {
125
std::unique_lock<std::mutex> guard(lock);
126
// Avoid the operator, we want to check all the bits.
127
return ((int)hasFlags & (int)flags) == (int)flags;
128
}
129
130
void MarkReadyNoLock(GameInfoFlags flags) {
131
hasFlags |= flags;
132
pendingFlags &= ~flags;
133
}
134
135
GameInfoTex *GetBGPic() {
136
if (pic1.texture)
137
return &pic1;
138
if (pic0.texture)
139
return &pic0;
140
return nullptr;
141
}
142
143
// Hold this when reading or writing from the GameInfo.
144
// Don't need to hold it when just passing around the pointer,
145
// and obviously also not when creating it and holding the only pointer
146
// to it.
147
std::mutex lock;
148
149
// Controls access to the fileLoader pointer.
150
std::mutex loaderLock;
151
152
// Keep track of what we have, or what we're processing.
153
// These are protected by the mutex. While pendingFlags != 0, something is being loaded.
154
GameInfoFlags hasFlags{};
155
GameInfoFlags pendingFlags{};
156
157
std::string id;
158
std::string id_version;
159
int disc_total = 0;
160
int disc_number = 0;
161
int region = -1;
162
IdentifiedFileType fileType;
163
bool hasConfig = false;
164
165
// Pre read the data, create a texture the next time (GL thread..)
166
GameInfoTex icon;
167
GameInfoTex pic0;
168
GameInfoTex pic1;
169
170
std::string sndFileData;
171
std::atomic<bool> sndDataLoaded{};
172
173
double lastAccessedTime = 0.0;
174
175
u64 gameSizeUncompressed = 0;
176
u64 gameSizeOnDisk = 0; // compressed size, in case of CSO
177
u64 saveDataSize = 0;
178
u64 installDataSize = 0;
179
180
protected:
181
ParamSFOData paramSFO;
182
// Note: this can change while loading, use GetTitle().
183
std::string title;
184
185
// TODO: Get rid of this shared_ptr and managae lifetime better instead.
186
std::shared_ptr<FileLoader> fileLoader;
187
Path filePath_;
188
189
void SetupTexture(Draw::DrawContext *draw, GameInfoTex &tex);
190
191
private:
192
DISALLOW_COPY_AND_ASSIGN(GameInfo);
193
friend class GameInfoWorkItem;
194
};
195
196
class GameInfoCache {
197
public:
198
GameInfoCache();
199
~GameInfoCache();
200
201
// This creates a background worker thread!
202
void Clear();
203
void PurgeType(IdentifiedFileType fileType);
204
205
// All data in GameInfo including icon.texture may be zero the first time you call this
206
// but filled in later asynchronously in the background. So keep calling this,
207
// redrawing the UI often. Only set flags to GAMEINFO_WANTBG or WANTSND if you really want them
208
// because they're big. bgTextures and sound may be discarded over time as well.
209
// NOTE: This never returns null, so you don't need to check for that. Do check Ready() flags though.
210
std::shared_ptr<GameInfo> GetInfo(Draw::DrawContext *draw, const Path &gamePath, GameInfoFlags wantFlags);
211
void FlushBGs(); // Gets rid of all BG textures. Also gets rid of bg sounds.
212
213
void CancelAll();
214
void WaitUntilDone(std::shared_ptr<GameInfo> &info);
215
216
private:
217
void Init();
218
void Shutdown();
219
220
// Maps ISO path to info. Need to use shared_ptr as we can return these pointers -
221
// and if they get destructed while being in use, that's bad.
222
std::map<std::string, std::shared_ptr<GameInfo> > info_;
223
std::mutex mapLock_;
224
};
225
226
// This one can be global, no good reason not to.
227
extern GameInfoCache *g_gameInfoCache;
228
229