-
Notifications
You must be signed in to change notification settings - Fork 41
/
OrangeSteamCloud.js
185 lines (157 loc) · 5.41 KB
/
OrangeSteamCloud.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
/*=============================================================================
* Orange - SteamCloud
* By Hudell - www.hudell.com
* OrangeSteamCloud.js
* Version: 1.0
* Free for commercial and non commercial use.
*=============================================================================*/
/*:
* @plugindesc Steam Cloud Integration <OrangeSteamCloud>
* @author Hudell
*
* @help
* ============================================================================
* Hudell's Plugins
* ============================================================================
*
* Check out my website to learn how to use this plugin:
* http://hudell.com
*
*=============================================================================*/
var Imported = Imported || {};
var Hudell = Hudell || {};
Hudell.OrangeSteamCloud = Hudell.OrangeSteamCloud || {};
(function($) {
"use strict";
if (!Utils.isNwjs()) return;
if (Imported.OrangeGreenworks === undefined || isNaN(Imported.OrangeGreenworks) || Imported.OrangeGreenworks < 1.1) {
console.error('OrangeGreenworks not found.');
return;
}
if (!OrangeGreenworks.initialized) {
console.error('OrangeGreenworks not initialized');
return;
}
var g = OrangeGreenworks.greenworks;
if (!g) {
console.error('Unable to find OrangeGreenworks internal component.');
return;
}
$.syncAllSaves = function() {
if (!g.isSteamRunning()) return;
if (!g.isCloudEnabled()) return;
if (!g.isCloudEnabledForUser()) return;
g.readTextFromFile('globalInfo', function(json){
var localJson;
try {
localJson = StorageManager.load(0);
} catch (e) {
console.error(e);
localJson = '';
}
if (json == localJson) return;
var globalInfo;
var localGlobalInfo;
try {
globalInfo = JSON.parse(json);
}
catch(e) {
globalInfo = {};
}
try {
localGlobalInfo = JSON.parse(localJson);
}
catch(e) {
localGlobalInfo = {};
}
for (var i = 1; i <= DataManager.maxSavefiles(); i++) {
var remoteData = globalInfo[i];
var localData = localGlobalInfo[i];
if (!remoteData) continue;
if (!!localData) {
if (localData.timestamp >= remoteData.timestamp) continue;
}
$.downloadFile(i, remoteData);
}
}, function(msg){
console.log('Global Info download failed. Error message:', msg);
});
};
$.saveFile = function(fileName, fileContent) {
if (!g.isSteamRunning()) return;
if (!g.isCloudEnabled()) return;
if (!g.isCloudEnabledForUser()) return;
g.saveTextToFile(fileName, fileContent, function(){
console.log('Save file uploaded successfully', arguments);
}, function(){
console.error('Failed to upload save file:', arguments);
});
};
$.updateLocalGlobalInfo = function(savefileId, header) {
var globalInfo = DataManager.loadGlobalInfo() || [];
globalInfo[savefileId] = header;
$.saveLocally(0, JSON.stringify(globalInfo));
};
$.saveLocally = function(savefileId, json) {
if (StorageManager.isLocalMode()) {
StorageManager.saveToLocalFile(savefileId, json);
} else {
StorageManager.saveToWebStorage(savefileId, json);
}
};
$.downloadFile = function(savefileId, header) {
if (!g.isSteamRunning()) return;
if (!g.isCloudEnabled()) return;
if (!g.isCloudEnabledForUser()) return;
if (savefileId > 0) {
g.readTextFromFile('save_' + savefileId, function(fileContent){
try {
//Make sure the data is parse-able before saving it.
var data = JSON.parse(fileContent);
$.saveLocally(savefileId, fileContent);
$.updateLocalGlobalInfo(savefileId, header);
}
catch(e) {
console.error('Failed to save downloaded file ' + savefileId, e);
}
}, function(msg){
console.error('Failed to download save file ' + savefileId, msg);
});
} else {
if (savefileId == -1) { //jshint ignore:line
g.readTextFromFile('config', function(fileContent){
$.saveLocally(-1, fileContent);
ConfigManager.load();
}, function(msg){
console.error('Failed to download config from cloud.', msg);
});
}
}
};
$._oldDataManager_makeSavefileInfo = DataManager.makeSavefileInfo;
DataManager.makeSavefileInfo = function() {
var info = $._oldDataManager_makeSavefileInfo.call(this);
info.timestamp = new Date().getTime();
return info;
};
$._oldStorageManager_save = StorageManager.save;
StorageManager.save = function(savefileId, json) {
$._oldStorageManager_save.call(this, savefileId, json);
if (savefileId == 0) { //jshint ignore:line
$.saveFile('globalInfo', json);
} else if (savefileId == -1) {
$.saveFile('config', json);
} else {
$.saveFile('save_' + savefileId, json);
}
};
$._oldDataManager_saveGlobalInfo = DataManager.saveGlobalInfo;
DataManager.saveGlobalInfo = function(info) {
info.timestamp = new Date().getTime();
$._oldDataManager_saveGlobalInfo.call(this, info);
};
$.downloadFile(-1);
$.syncAllSaves();
})(Hudell.OrangeSteamCloud);
OrangeSteamCloud = Hudell.OrangeSteamCloud;
Imported.OrangeSteamCloud = 1.0;