Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HW/MemoryStick.cpp
3186 views
1
// Copyright (c) 2012- 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
#include <algorithm>
19
#include <string>
20
#include <vector>
21
#include <string_view>
22
#include "Common/Serialize/Serializer.h"
23
#include "Common/Serialize/SerializeFuncs.h"
24
#include "Common/File/DiskFree.h"
25
#include "Common/File/FileUtil.h"
26
#include "Common/File/DirListing.h"
27
#include "Core/Config.h"
28
#include "Core/CoreTiming.h"
29
#include "Core/Compatibility.h"
30
#include "Core/FileSystems/MetaFileSystem.h"
31
#include "Core/HW/MemoryStick.h"
32
#include "Core/System.h"
33
#include "Common/CommonTypes.h"
34
#include "Common/TimeUtil.h"
35
#include "Common/Thread/Promise.h"
36
37
// MS and FatMS states.
38
static MemStickState memStickState;
39
static MemStickFatState memStickFatState;
40
static bool memStickNeedsAssign = false;
41
static uint64_t memStickInsertedAt = 0;
42
static uint64_t memstickInitialFree = 0;
43
static uint64_t memstickCurrentUse = 0;
44
static bool memstickCurrentUseValid = false;
45
46
enum FreeCalcStatus {
47
NONE,
48
RUNNING,
49
DONE,
50
CLEANED_UP,
51
};
52
53
static const uint64_t normalMemstickSize = 9ULL * 1024 * 1024 * 1024;
54
static const uint64_t smallMemstickSize = 1ULL * 1024 * 1024 * 1024;
55
56
static Promise<uint64_t> *g_initialMemstickSizePromise = nullptr;
57
58
void MemoryStick_DoState(PointerWrap &p) {
59
auto s = p.Section("MemoryStick", 1, 5);
60
if (!s)
61
return;
62
63
Do(p, memStickState);
64
Do(p, memStickFatState);
65
if (s >= 4) {
66
// Do nothing.
67
} else if (s >= 2) {
68
// Really no point in storing the memstick size.
69
u64 memStickSize = normalMemstickSize;
70
Do(p, memStickSize);
71
}
72
if (s >= 5) {
73
Do(p, memstickInitialFree);
74
}
75
76
if (s >= 3) {
77
Do(p, memStickNeedsAssign);
78
Do(p, memStickInsertedAt);
79
}
80
}
81
82
MemStickState MemoryStick_State() {
83
return memStickState;
84
}
85
86
MemStickFatState MemoryStick_FatState() {
87
if (memStickNeedsAssign && CoreTiming::GetTicks() > memStickInsertedAt + msToCycles(500)) {
88
// It's been long enough for us to be done mounting the memory stick.
89
memStickFatState = PSP_FAT_MEMORYSTICK_STATE_ASSIGNED;
90
memStickNeedsAssign = false;
91
}
92
return memStickFatState;
93
}
94
95
u64 MemoryStick_SectorSize() {
96
return 32 * 1024; // 32KB
97
}
98
99
static uint64_t ComputeSizeOfSavedataForGame(const Path &saveFolder, const std::string_view gameID) {
100
uint64_t space = 0;
101
std::vector<File::FileInfo> subDirs;
102
File::GetFilesInDir(saveFolder, &subDirs, nullptr, 0, gameID); // gameID as directory prefix.
103
for (auto &dir : subDirs) {
104
if (!dir.isDirectory)
105
continue;
106
space += File::ComputeRecursiveDirectorySize(saveFolder / dir.name);
107
}
108
return space;
109
}
110
111
u64 MemoryStick_FreeSpace(std::string gameID) {
112
double start = time_now_d();
113
INFO_LOG(Log::IO, "Calculating free disk space (%s)", gameID.c_str());
114
115
const CompatFlags &flags = PSP_CoreParameter().compat.flags();
116
u64 realFreeSpace = pspFileSystem.FreeDiskSpace("ms0:/");
117
118
// Cap the memory stick size to avoid math errors when old games get sizes that were
119
// not planned for back then (even though 2GB cards were available.)
120
// We have a compat setting to make it even smaller for Harry Potter : Goblet of Fire, see #13266.
121
const u64 memStickSize = flags.ReportSmallMemstick ? smallMemstickSize : (u64)g_Config.iMemStickSizeGB * 1024 * 1024 * 1024;
122
123
// Assume the memory stick is only used to store savedata, for the current game only.
124
if (!memstickCurrentUseValid) {
125
Path saveFolder = GetSysDirectory(DIRECTORY_SAVEDATA);
126
memstickCurrentUse = ComputeSizeOfSavedataForGame(saveFolder, gameID);
127
memstickCurrentUseValid = true;
128
}
129
130
u64 simulatedFreeSpace = 0;
131
if (memstickCurrentUse < memStickSize) {
132
simulatedFreeSpace = memStickSize - memstickCurrentUse;
133
} else if (flags.ReportSmallMemstick) {
134
// There's more stuff in the memstick than the size we report.
135
// This doesn't work, so we'll just have to lie. Not sure what the best way is.
136
simulatedFreeSpace = smallMemstickSize / 2; // just pick a value.
137
}
138
if (flags.MemstickFixedFree) {
139
const u64 memstickInitialFree = g_initialMemstickSizePromise->BlockUntilReady();
140
_dbg_assert_(g_initialMemstickSizePromise);
141
// Assassin's Creed: Bloodlines fails to save if free space changes incorrectly during game.
142
// See issue #12761
143
realFreeSpace = 0;
144
if (memstickCurrentUse <= memstickInitialFree) {
145
realFreeSpace = memstickInitialFree - memstickCurrentUse;
146
}
147
}
148
INFO_LOG(Log::IO, "Done calculating free disk space (%0.3f s)", time_now_d() - start);
149
150
return std::min(simulatedFreeSpace, realFreeSpace);
151
}
152
153
void MemoryStick_NotifyWrite() {
154
memstickCurrentUseValid = false;
155
}
156
157
void MemoryStick_SetFatState(MemStickFatState state) {
158
memStickFatState = state;
159
memStickNeedsAssign = false;
160
}
161
162
void MemoryStick_SetState(MemStickState state) {
163
if (memStickState == state) {
164
return;
165
}
166
167
memStickState = state;
168
169
// If removed, we unmount. Otherwise, mounting is delayed.
170
if (state == PSP_MEMORYSTICK_STATE_NOT_INSERTED) {
171
MemoryStick_SetFatState(PSP_FAT_MEMORYSTICK_STATE_UNASSIGNED);
172
} else {
173
memStickInsertedAt = CoreTiming::GetTicks();
174
memStickNeedsAssign = true;
175
}
176
}
177
178
void MemoryStick_NotifyGameName(std::string gameID) {
179
const CompatFlags &flags = PSP_CoreParameter().compat.flags();
180
if (!flags.MemstickFixedFree) {
181
return;
182
}
183
184
// See issue #12761
185
if (!g_initialMemstickSizePromise) {
186
g_initialMemstickSizePromise = Promise<uint64_t>::Spawn(&g_threadManager, [gameID]() -> uint64_t {
187
INFO_LOG(Log::System, "Calculating initial savedata size for %s...", gameID.c_str());
188
// NOTE: We only really need to sum up the diskspace for subfolders related to this game, and add it to the actual free space,
189
// to obtain the free space with the save data removed.
190
// We previously went through the meta file system here, but the memstick is always a directory so no need.
191
Path saveFolder = GetSysDirectory(DIRECTORY_SAVEDATA);
192
int64_t freeSpace = 0;
193
free_disk_space(saveFolder, freeSpace);
194
195
// Only add the space of the folders that the game itself touches.
196
freeSpace += ComputeSizeOfSavedataForGame(saveFolder, gameID);
197
return freeSpace;
198
}, TaskType::IO_BLOCKING);
199
}
200
}
201
202
void MemoryStick_Init() {
203
if (g_Config.bMemStickInserted) {
204
memStickState = PSP_MEMORYSTICK_STATE_INSERTED;
205
memStickFatState = PSP_FAT_MEMORYSTICK_STATE_ASSIGNED;
206
} else {
207
memStickState = PSP_MEMORYSTICK_STATE_NOT_INSERTED;
208
memStickFatState = PSP_FAT_MEMORYSTICK_STATE_UNASSIGNED;
209
}
210
211
memStickNeedsAssign = false;
212
}
213
214
void MemoryStick_Shutdown() {
215
if (g_initialMemstickSizePromise) {
216
g_initialMemstickSizePromise->BlockUntilReady();
217
}
218
delete g_initialMemstickSizePromise;
219
g_initialMemstickSizePromise = nullptr;
220
}
221
222