bitburner-src/electron/greenworks.js

297 lines
8.0 KiB
JavaScript
Raw Normal View History

2021-11-27 21:07:25 +01:00
// Copyright (c) 2015 Greenheart Games Pty. Ltd. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
// The source code can be found in https://github.com/greenheartgames/greenworks
2022-04-07 01:30:08 +02:00
var fs = require("fs");
2021-11-27 21:07:25 +01:00
var greenworks;
2023-10-05 01:46:08 +02:00
if (process.platform === "darwin") greenworks = require("./lib/greenworks-osx64");
else if (process.platform === "win32") greenworks = require("./lib/greenworks-win64");
else if (process.platform === "linux") greenworks = require("./lib/greenworks-linux64");
2021-11-27 21:07:25 +01:00
function error_process(err, error_callback) {
2022-04-07 01:30:08 +02:00
if (err && error_callback) error_callback(err);
2021-11-27 21:07:25 +01:00
}
if (greenworks) {
2022-04-07 10:23:26 +02:00
greenworks.ugcGetItems = function (options, ugc_matching_type, ugc_query_type, success_callback, error_callback) {
if (typeof options !== "object") {
error_callback = success_callback;
success_callback = ugc_query_type;
ugc_query_type = ugc_matching_type;
ugc_matching_type = options;
options = {
2022-04-07 10:23:26 +02:00
app_id: greenworks.getAppId(),
page_num: 1,
};
2021-11-27 21:07:25 +01:00
}
2022-04-07 10:23:26 +02:00
greenworks._ugcGetItems(options, ugc_matching_type, ugc_query_type, success_callback, error_callback);
};
2021-11-27 21:07:25 +01:00
2022-04-07 10:23:26 +02:00
greenworks.ugcGetUserItems = function (
options,
ugc_matching_type,
ugc_list_sort_order,
ugc_list,
success_callback,
error_callback,
) {
if (typeof options !== "object") {
error_callback = success_callback;
success_callback = ugc_list;
ugc_list = ugc_list_sort_order;
ugc_list_sort_order = ugc_matching_type;
ugc_matching_type = options;
options = {
2022-04-07 10:23:26 +02:00
app_id: greenworks.getAppId(),
page_num: 1,
};
2021-11-27 21:07:25 +01:00
}
2022-04-07 10:23:26 +02:00
greenworks._ugcGetUserItems(
options,
ugc_matching_type,
ugc_list_sort_order,
ugc_list,
success_callback,
error_callback,
);
};
2021-11-27 21:07:25 +01:00
2022-04-07 10:23:26 +02:00
greenworks.ugcSynchronizeItems = function (options, sync_dir, success_callback, error_callback) {
if (typeof options !== "object") {
error_callback = success_callback;
success_callback = sync_dir;
sync_dir = options;
options = {
2022-04-07 10:23:26 +02:00
app_id: greenworks.getAppId(),
page_num: 1,
};
2021-11-27 21:07:25 +01:00
}
2022-04-07 10:23:26 +02:00
greenworks._ugcSynchronizeItems(options, sync_dir, success_callback, error_callback);
};
2021-11-27 21:07:25 +01:00
2022-04-07 10:23:26 +02:00
greenworks.publishWorkshopFile = function (
options,
file_path,
image_path,
title,
description,
success_callback,
error_callback,
) {
if (typeof options !== "object") {
error_callback = success_callback;
success_callback = description;
description = title;
title = image_path;
image_path = file_path;
file_path = options;
options = {
2022-04-07 10:23:26 +02:00
app_id: greenworks.getAppId(),
tags: [],
};
2021-11-27 21:07:25 +01:00
}
2022-04-07 10:23:26 +02:00
greenworks._publishWorkshopFile(
options,
file_path,
image_path,
title,
description,
success_callback,
error_callback,
);
};
2021-11-27 21:07:25 +01:00
2022-04-07 10:23:26 +02:00
greenworks.updatePublishedWorkshopFile = function (
options,
published_file_handle,
file_path,
image_path,
title,
description,
success_callback,
error_callback,
) {
if (typeof options !== "object") {
error_callback = success_callback;
success_callback = description;
description = title;
title = image_path;
image_path = file_path;
file_path = published_file_handle;
published_file_handle = options;
options = {
2022-04-07 10:23:26 +02:00
tags: [], // No tags are set
};
2021-11-27 21:07:25 +01:00
}
2022-04-07 10:23:26 +02:00
greenworks._updatePublishedWorkshopFile(
options,
published_file_handle,
file_path,
image_path,
title,
description,
success_callback,
error_callback,
);
};
2021-11-27 21:07:25 +01:00
// An utility function for publish related APIs.
// It processes remains steps after saving files to Steam Cloud.
2022-04-07 10:23:26 +02:00
function file_share_process(file_name, image_name, next_process_func, error_callback, progress_callback) {
if (progress_callback) progress_callback("Completed on saving files on Steam Cloud.");
greenworks.fileShare(
file_name,
function () {
greenworks.fileShare(
image_name,
function () {
next_process_func();
},
function (err) {
error_process(err, error_callback);
},
);
},
function (err) {
error_process(err, error_callback);
},
);
}
2021-11-27 21:07:25 +01:00
// 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.
2022-04-07 10:23:26 +02:00
greenworks.ugcPublish = function (
file_name,
title,
description,
image_name,
success_callback,
error_callback,
progress_callback,
) {
var publish_file_process = function () {
2022-04-07 10:23:26 +02:00
if (progress_callback) progress_callback("Completed on sharing files.");
greenworks.publishWorkshopFile(
file_name,
image_name,
title,
description,
function (publish_file_id) {
success_callback(publish_file_id);
},
function (err) {
error_process(err, error_callback);
},
);
};
2022-04-07 10:23:26 +02:00
greenworks.saveFilesToCloud(
[file_name, image_name],
function () {
file_share_process(file_name, image_name, publish_file_process, error_callback, progress_callback);
},
function (err) {
error_process(err, error_callback);
},
);
};
2021-11-27 21:07:25 +01:00
// Update publish ugc steps:
// 1. Save new file and image to Steam Cloud.
// 2. Share file and images.
// 3. Update published file.
2022-04-07 10:23:26 +02:00
greenworks.ugcPublishUpdate = function (
published_file_id,
file_name,
2022-04-07 01:30:08 +02:00
title,
description,
2022-04-07 10:23:26 +02:00
image_name,
2022-04-07 01:30:08 +02:00
success_callback,
error_callback,
2022-04-07 10:23:26 +02:00
progress_callback,
) {
var update_published_file_process = function () {
if (progress_callback) progress_callback("Completed on sharing files.");
greenworks.updatePublishedWorkshopFile(
published_file_id,
file_name,
2022-04-07 01:30:08 +02:00
image_name,
2022-04-07 10:23:26 +02:00
title,
description,
2022-04-07 01:30:08 +02:00
function () {
2022-04-07 10:23:26 +02:00
success_callback();
2022-04-07 01:30:08 +02:00
},
function (err) {
error_process(err, error_callback);
},
);
2022-04-07 10:23:26 +02:00
};
2021-11-27 21:07:25 +01:00
2022-04-07 10:23:26 +02:00
greenworks.saveFilesToCloud(
[file_name, image_name],
2022-04-07 01:30:08 +02:00
function () {
2022-04-07 10:23:26 +02:00
file_share_process(file_name, image_name, update_published_file_process, error_callback, progress_callback);
2022-04-07 01:30:08 +02:00
},
function (err) {
error_process(err, error_callback);
},
);
2021-11-27 21:07:25 +01:00
};
2022-04-07 10:23:26 +02:00
// Greenworks Utils APIs implmentation.
greenworks.Utils.move = function (source_dir, target_dir, success_callback, error_callback) {
fs.rename(source_dir, target_dir, function (err) {
if (err) {
if (error_callback) error_callback(err);
return;
}
if (success_callback) success_callback();
});
};
2021-11-27 21:07:25 +01:00
2022-04-07 10:23:26 +02:00
greenworks.init = function () {
if (this.initAPI()) return true;
if (!this.isSteamRunning()) throw new Error("Steam initialization failed. Steam is not running.");
var appId;
try {
appId = fs.readFileSync("steam_appid.txt", "utf8");
} catch (e) {
throw new Error(
"Steam initialization failed. Steam is running," +
"but steam_appid.txt is missing. Expected to find it in: " +
require("path").resolve("steam_appid.txt"),
);
}
if (!/^\d+ *\r?\n?$/.test(appId)) {
throw new Error(
"Steam initialization failed. " +
"steam_appid.txt appears to be invalid; " +
"it should contain a numeric ID: " +
appId,
);
2021-11-27 21:07:25 +01:00
}
2022-04-07 01:30:08 +02:00
throw new Error(
2022-04-07 10:23:26 +02:00
"Steam initialization failed, but Steam is running, " +
"and steam_appid.txt is present and valid." +
"Maybe that's not really YOUR app ID? " +
appId.trim(),
2022-04-07 01:30:08 +02:00
);
2022-04-07 10:23:26 +02:00
};
2021-11-27 21:07:25 +01:00
2022-04-07 10:23:26 +02:00
var EventEmitter = require("events").EventEmitter;
greenworks.__proto__ = EventEmitter.prototype;
EventEmitter.call(greenworks);
2021-11-27 21:07:25 +01:00
greenworks._steam_events.on = function () {
greenworks.emit.apply(greenworks, arguments);
};
2021-11-27 21:07:25 +01:00
2022-04-07 10:23:26 +02:00
process.versions["greenworks"] = greenworks._version;
}
2021-11-27 21:07:25 +01:00
module.exports = greenworks;