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