-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgreenworks.js
140 lines (122 loc) · 4.87 KB
/
greenworks.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
var fs = require("fs");
var archiver = require("archiver");
var unzip = require("unzip");
var path = require("path");
var greenworks;
if (process.platform === "darwin") {
if (process.arch === "x64") {
greenworks = require("./lib/greenworks-osx64");
} else if (process.arch === "ia32") {
greenworks = require("./lib/greenworks-osx32");
}
} else if (process.platform === "win32") {
if (process.arch === "x64") {
greenworks = require("./lib/greenworks-win64");
} else if (process.arch === "ia32") {
greenworks = require("./lib/greenworks-win32");
}
} else if (process.platform === "linux") {
if (process.arch === "x64") {
greenworks = require("./lib/greenworks-linux64");
} else if (process.arch === "ia32") {
greenworks = require("./lib/greenworks-linux32");
}
}
function error_process(err, errorCallback) {
if (err && errorCallback) {
errorCallback(err);
}
}
// An utility function for publish related APIs.
// It processes remains steps after saving files to Steam Cloud.
function file_share_process(fileName, imageName, nextProcessFunc, errorCallback, progressCallback) {
if (progressCallback) {
progressCallback("Completed on saving files on Steam Cloud.");
}
greenworks.fileShare(fileName, function() {
greenworks.fileShare(imageName, function() {
nextProcessFunc();
}, function(err) { error_process(err, errorCallback); });
}, function(err) { error_process(err, errorCallback); });
}
// Publishing user generated content(ugc) to Steam contains following steps:
// 1. Save file and image to Steam Cloud.
// 2. Share the file and image.
// 3. publish the file to workshop.
greenworks.ugcPublish = function(fileName, title, description, imageName, successCallback, errorCallback, progressCallback) {
var publishFileProcess = function() {
if (progressCallback) {
progressCallback("Completed on sharing files.");
}
greenworks.publishWorkshopFile(fileName, imageName, title, description,
function(publishFileID) { successCallback(publishFileID); },
function(err) { error_process(err, errorCallback); });
};
greenworks.saveFilesToCloud([fileName, imageName], function() {
file_share_process(fileName, imageName, publishFileProcess,
errorCallback, progressCallback);
}, function(err) { error_process(err, errorCallback); });
};
// Update publish ugc steps:
// 1. Save new file and image to Steam Cloud.
// 2. Share file and images.
// 3. Update published file.
greenworks.ugcPublishUpdate = function(publishedFileID, fileName, title, description, imageName, successCallback, errorCallback, progressCallback) {
var updatePublishedFileProcess = function() {
if (progressCallback) {
progressCallback("Completed on sharing files.");
}
greenworks.updatePublishedWorkshopFile(publishedFileID,
fileName, imageName, title, description,
function() { successCallback(); },
function(err) { error_process(err, errorCallback); });
};
greenworks.saveFilesToCloud([fileName, imageName], function() {
file_share_process(fileName, imageName, updatePublishedFileProcess,
errorCallback, progressCallback);
}, function(err) { error_process(err, errorCallback); });
};
// Greenworks Utils APIs implmentation.
greenworks.Utils.move = function(sourceDir, targetDir, successCallback, errorCallback) {
fs.rename(sourceDir, targetDir, function(err) {
if (err) {
if (errorCallback) {
errorCallback(err);
}
return;
}
if (successCallback) {
successCallback();
}
});
};
greenworks.Utils.createArchive = function(zipPath, sourceDir, successCallback, errorCallback) {
var output = fs.createWriteStream(zipPath);
var archive = archiver("zip");
output.on("close", successCallback);
if (errorCallback) {
archive.on("error", errorCallback);
}
archive.pipe(output);
// Replace '\' with '/'
sourceDir = sourceDir.replace(/\\/g, "/");
// Remove last '/' character.
if (sourceDir[sourceDir.length - 1] === "/") {
sourceDir = sourceDir.slice(0, -1);
}
archive.bulk([{ expand: true, cwd: path.normalize(sourceDir + "/../"), src: [path.basename(sourceDir) + "/**"], dot: true }]);
archive.finalize();
};
greenworks.Utils.extractArchive = function(zipFilePath, extractDir, successCallback, errorCallback) {
var unzipExtractor = unzip.Extract({ path: extractDir });
if (errorCallback) {
unzipExtractor.on("error", errorCallback);
}
unzipExtractor.on("close", successCallback);
fs.createReadStream(zipFilePath).pipe(unzipExtractor);
};
// run steam callbacks every 50ms
setInterval(function() {
greenworks.runCallbacks();
}, 50);
module.exports = greenworks;