-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMcKathlin_SaveSync.js
204 lines (183 loc) · 7.7 KB
/
McKathlin_SaveSync.js
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
//=============================================================================
// Save Sync
// by McKathlin
// McKathlin_SaveFinder.js
//=============================================================================
/*
* MIT License
*
* Copyright (c) 2024 Kathy Bunn and Scott Tyrus Washburn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
var Imported = Imported || {};
Imported.McKathlin_SaveSync = true;
var McKathlin = McKathlin || {};
McKathlin.SaveSync = {};
/*:
* @plugindesc MV 1.0.0 Restores missing global save info
* @author McKathlin
*
* @help Save Sync for RPG Maker MV
*
* Sometimes when Steam Cloud updates people's save files, or when a
* player pastes in a savefile from a backup, the save files actually present
* in their game may fall out of sync with the list in global.rpgsave that
* RPG Maker MV's runtime uses to populate the Load Menu. This can leave the
* player unable to access their existing saves.
*
* When this Save Sync plugin is active, it resolves any inconsistencies
* between which savefiles are actually present and which saves are recorded
* in global.rpgsave.
*
* ============================================================================
* Compatibility Note
*
* This plugin does not have any known conflicts with existing plugins.
* If another plugin were to modify DataManager.loadGlobalInfo, this would
* cause a conflict. Barring that, you may place McKathlin_SaveSync anywhere
* on your game's plugin list.
*
* ============================================================================
* MIT License
*
* Copyright (c) 2023 Kathy Bunn and Scott Tyrus Washburn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* ============================================================================
* Happy storytelling!
* -McKathlin
*/
// Alias method
McKathlin.SaveSync.DataManager_saveGlobalInfo = DataManager.saveGlobalInfo;
DataManager.saveGlobalInfo = function(info) {
this._globalInfo = info; // Update the cache.
McKathlin.SaveSync.DataManager_saveGlobalInfo.call(this, info);
};
// Replacement method
DataManager.loadGlobalInfo = function() {
if (this._globalInfo) {
// It's cached. No need to load from storage this time.
return this._globalInfo;
}
// Try loading it from storage.
let globalInfo;
try {
let json = StorageManager.load(0);
if (!json) {
throw new Error("Global info file not found");
}
globalInfo = JSON.parse(json);
} catch (e) {
console.error("Failed to load global save info:", e);
globalInfo = [];
}
// Update to match which savefiles are actually present.
let infoUpdated = false;
for (var i = 1; i <= this.maxSavefiles(); i++) {
if (StorageManager.exists(i)) {
// There *should* be save info here. If not, make one!
if (!globalInfo[i]) {
globalInfo[i] = McKathlin.SaveSync.makeMissingSaveInfo(i);
infoUpdated = true;
}
} else {
// There should *not* be save info here. If there is, delete it.
if (globalInfo[i]) {
delete globalInfo[i];
infoUpdated = true;
}
}
}
this._globalInfo = globalInfo; // This caches it for easier reference.
if (infoUpdated) {
this.saveGlobalInfo(this._globalInfo);
}
return this._globalInfo;
};
// New method
McKathlin.SaveSync.makeMissingSaveInfo = function(savefileId) {
console.warn(`File ${savefileId} has no global save info.\n Restoring the info...`);
// Start with a dummy info object.
let info = {
globalId: DataManager._globalId,
title: $dataSystem.gameTitle,
characters: [],
faces: [],
playtime: "??:??:??",
timestamp: Date.now(),
};
try {
// Load the game to generate its save info.
let json = StorageManager.load(savefileId);
let saveContents = JsonEx.parse(json);
// Extract save-specific data
info.characters = McKathlin.SaveSync.extractCharacters(saveContents);
info.faces = McKathlin.SaveSync.extractFaces(saveContents);
info.playtime = McKathlin.SaveSync.extractPlaytime(saveContents);
console.log(`File ${savefileId}'s info has been restored successfully.`);
return info;
} catch (err) {
console.error(`Failed to restore some of File ${savefileId}'s save info:`, err);
console.warn(`You can still try loading File ${savefileId}.`);
}
return info;
};
McKathlin.SaveSync.extractCharacters = function(saveContents) {
const members = this.extractPartyMembers(saveContents);
return members.map((actor) =>
[actor.characterName(), actor.characterIndex()]);
};
McKathlin.SaveSync.extractFaces = function(saveContents) {
const members = this.extractPartyMembers(saveContents);
return members.map((actor) => [actor.faceName(), actor.faceIndex()]);
};
McKathlin.SaveSync.extractPlaytime = function(saveContents) {
const frames = saveContents.system._framesOnSave;
const totalSeconds = Math.floor(frames / 60);
const hour = Math.floor(totalSeconds / 60 / 60);
const min = Math.floor(totalSeconds / 60) % 60;
const sec = totalSeconds % 60;
return `${hour.padZero(2)}:${min.padZero(2)}:${sec.padZero(2)}`;
};
McKathlin.SaveSync.extractPartyMembers = function(saveContents) {
// Get the IDs of the active party members.
const maxActive = saveContents.party.maxBattleMembers();
const memberIds = saveContents.party._actors.slice(0, maxActive);
// Return the actors with those IDs.
const gameActors = saveContents.actors;
const members = memberIds.map((id) => gameActors.actor(id));
return members;
};