|
| 1 | +// Copyright (c) 2015 Greenheart Games Pty. Ltd. All rights reserved. |
| 2 | +// Use of this source code is governed by the MIT license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// The source code can be found in https://github.com/greenheartgames/greenworks |
| 6 | +var fs = require('fs'); |
| 7 | + |
| 8 | +var greenworks; |
| 9 | + |
| 10 | +if (process.platform == 'darwin') { |
| 11 | + if (process.arch == 'x64') |
| 12 | + greenworks = require('./lib/greenworks-osx64'); |
| 13 | + else if (process.arch == 'ia32') |
| 14 | + greenworks = require('./lib/greenworks-osx32'); |
| 15 | +} else if (process.platform == 'win32') { |
| 16 | + if (process.arch == 'x64') |
| 17 | + greenworks = require('./lib/greenworks-win64'); |
| 18 | + else if (process.arch == 'ia32') |
| 19 | + greenworks = require('./lib/greenworks-win32'); |
| 20 | +} else if (process.platform == 'linux') { |
| 21 | + if (process.arch == 'x64') |
| 22 | + greenworks = require('./lib/greenworks-linux64'); |
| 23 | + else if (process.arch == 'ia32') |
| 24 | + greenworks = require('./lib/greenworks-linux32'); |
| 25 | +} |
| 26 | + |
| 27 | +function error_process(err, error_callback) { |
| 28 | + if (err && error_callback) |
| 29 | + error_callback(err); |
| 30 | +} |
| 31 | + |
| 32 | +greenworks.ugcGetItems = function(options, ugc_matching_type, ugc_query_type, |
| 33 | + success_callback, error_callback) { |
| 34 | + if (typeof options !== 'object') { |
| 35 | + error_callback = success_callback; |
| 36 | + success_callback = ugc_query_type; |
| 37 | + ugc_query_type = ugc_matching_type; |
| 38 | + ugc_matching_type = options; |
| 39 | + options = { |
| 40 | + 'app_id': greenworks.getAppId(), |
| 41 | + 'page_num': 1 |
| 42 | + } |
| 43 | + } |
| 44 | + greenworks._ugcGetItems(options, ugc_matching_type, ugc_query_type, |
| 45 | + success_callback, error_callback); |
| 46 | +} |
| 47 | + |
| 48 | +greenworks.ugcGetUserItems = function(options, ugc_matching_type, |
| 49 | + ugc_list_sort_order, ugc_list, success_callback, error_callback) { |
| 50 | + if (typeof options !== 'object') { |
| 51 | + error_callback = success_callback; |
| 52 | + success_callback = ugc_list; |
| 53 | + ugc_list = ugc_list_sort_order; |
| 54 | + ugc_list_sort_order = ugc_matching_type; |
| 55 | + ugc_matching_type = options; |
| 56 | + options = { |
| 57 | + 'app_id': greenworks.getAppId(), |
| 58 | + 'page_num': 1 |
| 59 | + } |
| 60 | + } |
| 61 | + greenworks._ugcGetUserItems(options, ugc_matching_type, ugc_list_sort_order, |
| 62 | + ugc_list, success_callback, error_callback); |
| 63 | +} |
| 64 | + |
| 65 | +greenworks.ugcSynchronizeItems = function (options, sync_dir, success_callback, |
| 66 | + error_callback) { |
| 67 | + if (typeof options !== 'object') { |
| 68 | + error_callback = success_callback; |
| 69 | + success_callback = sync_dir; |
| 70 | + sync_dir = options; |
| 71 | + options = { |
| 72 | + 'app_id': greenworks.getAppId(), |
| 73 | + 'page_num': 1 |
| 74 | + } |
| 75 | + } |
| 76 | + greenworks._ugcSynchronizeItems(options, sync_dir, success_callback, |
| 77 | + error_callback); |
| 78 | +} |
| 79 | + |
| 80 | +greenworks.publishWorkshopFile = function(options, file_path, image_path, title, |
| 81 | + description, success_callback, error_callback) { |
| 82 | + if (typeof options !== 'object') { |
| 83 | + error_callback = success_callback; |
| 84 | + success_callback = description; |
| 85 | + description = title; |
| 86 | + title = image_path; |
| 87 | + image_path = file_path; |
| 88 | + file_path = options; |
| 89 | + options = { |
| 90 | + 'app_id': greenworks.getAppId(), |
| 91 | + 'tags': [] |
| 92 | + } |
| 93 | + } |
| 94 | + greenworks._publishWorkshopFile(options, file_path, image_path, title, |
| 95 | + description, success_callback, error_callback); |
| 96 | +} |
| 97 | + |
| 98 | +greenworks.updatePublishedWorkshopFile = function(options, |
| 99 | + published_file_handle, file_path, image_path, title, description, |
| 100 | + success_callback, error_callback) { |
| 101 | + if (typeof options !== 'object') { |
| 102 | + error_callback = success_callback; |
| 103 | + success_callback = description; |
| 104 | + description = title; |
| 105 | + title = image_path; |
| 106 | + image_path = file_path; |
| 107 | + file_path = published_file_handle; |
| 108 | + published_file_handle = options; |
| 109 | + options = { |
| 110 | + 'tags': [] // No tags are set |
| 111 | + } |
| 112 | + } |
| 113 | + greenworks._updatePublishedWorkshopFile(options, published_file_handle, |
| 114 | + file_path, image_path, title, description, success_callback, |
| 115 | + error_callback); |
| 116 | +} |
| 117 | + |
| 118 | +// An utility function for publish related APIs. |
| 119 | +// It processes remains steps after saving files to Steam Cloud. |
| 120 | +function file_share_process(file_name, image_name, next_process_func, |
| 121 | + error_callback, progress_callback) { |
| 122 | + if (progress_callback) |
| 123 | + progress_callback("Completed on saving files on Steam Cloud."); |
| 124 | + greenworks.fileShare(file_name, function() { |
| 125 | + greenworks.fileShare(image_name, function() { |
| 126 | + next_process_func(); |
| 127 | + }, function(err) { error_process(err, error_callback); }); |
| 128 | + }, function(err) { error_process(err, error_callback); }); |
| 129 | +} |
| 130 | + |
| 131 | +// Publishing user generated content(ugc) to Steam contains following steps: |
| 132 | +// 1. Save file and image to Steam Cloud. |
| 133 | +// 2. Share the file and image. |
| 134 | +// 3. publish the file to workshop. |
| 135 | +greenworks.ugcPublish = function(file_name, title, description, image_name, |
| 136 | + success_callback, error_callback, progress_callback) { |
| 137 | + var publish_file_process = function() { |
| 138 | + if (progress_callback) |
| 139 | + progress_callback("Completed on sharing files."); |
| 140 | + greenworks.publishWorkshopFile(file_name, image_name, title, description, |
| 141 | + function(publish_file_id) { success_callback(publish_file_id); }, |
| 142 | + function(err) { error_process(err, error_callback); }); |
| 143 | + }; |
| 144 | + greenworks.saveFilesToCloud([file_name, image_name], function() { |
| 145 | + file_share_process(file_name, image_name, publish_file_process, |
| 146 | + error_callback, progress_callback); |
| 147 | + }, function(err) { error_process(err, error_callback); }); |
| 148 | +} |
| 149 | + |
| 150 | +// Update publish ugc steps: |
| 151 | +// 1. Save new file and image to Steam Cloud. |
| 152 | +// 2. Share file and images. |
| 153 | +// 3. Update published file. |
| 154 | +greenworks.ugcPublishUpdate = function(published_file_id, file_name, title, |
| 155 | + description, image_name, success_callback, error_callback, |
| 156 | + progress_callback) { |
| 157 | + var update_published_file_process = function() { |
| 158 | + if (progress_callback) |
| 159 | + progress_callback("Completed on sharing files."); |
| 160 | + greenworks.updatePublishedWorkshopFile(published_file_id, |
| 161 | + file_name, image_name, title, description, |
| 162 | + function() { success_callback(); }, |
| 163 | + function(err) { error_process(err, error_callback); }); |
| 164 | + }; |
| 165 | + |
| 166 | + greenworks.saveFilesToCloud([file_name, image_name], function() { |
| 167 | + file_share_process(file_name, image_name, update_published_file_process, |
| 168 | + error_callback, progress_callback); |
| 169 | + }, function(err) { error_process(err, error_callback); }); |
| 170 | +} |
| 171 | + |
| 172 | +// Greenworks Utils APIs implmentation. |
| 173 | +greenworks.Utils.move = function(source_dir, target_dir, success_callback, |
| 174 | + error_callback) { |
| 175 | + fs.rename(source_dir, target_dir, function(err) { |
| 176 | + if (err) { |
| 177 | + if (error_callback) error_callback(err); |
| 178 | + return; |
| 179 | + } |
| 180 | + if (success_callback) |
| 181 | + success_callback(); |
| 182 | + }); |
| 183 | +} |
| 184 | + |
| 185 | +greenworks.init = function() { |
| 186 | + if (this.initAPI()) return true; |
| 187 | + if (!this.isSteamRunning()) |
| 188 | + throw new Error("Steam initialization failed. Steam is not running."); |
| 189 | + var appId; |
| 190 | + try { |
| 191 | + appId = fs.readFileSync('steam_appid.txt', 'utf8'); |
| 192 | + } catch (e) { |
| 193 | + throw new Error("Steam initialization failed. Steam is running," + |
| 194 | + "but steam_appid.txt is missing. Expected to find it in: " + |
| 195 | + require('path').resolve('steam_appid.txt')); |
| 196 | + } |
| 197 | + if (!/^\d+ *\r?\n?$/.test(appId)) { |
| 198 | + throw new Error("Steam initialization failed. " + |
| 199 | + "steam_appid.txt appears to be invalid; " + |
| 200 | + "it should contain a numeric ID: " + appId); |
| 201 | + } |
| 202 | + throw new Error("Steam initialization failed, but Steam is running, " + |
| 203 | + "and steam_appid.txt is present and valid." + |
| 204 | + "Maybe that's not really YOUR app ID? " + appId.trim()); |
| 205 | +} |
| 206 | + |
| 207 | +var EventEmitter = require('events').EventEmitter; |
| 208 | +greenworks.__proto__ = EventEmitter.prototype; |
| 209 | +EventEmitter.call(greenworks); |
| 210 | + |
| 211 | +greenworks._steam_events.on = function () { |
| 212 | + greenworks.emit.apply(greenworks, arguments); |
| 213 | +}; |
| 214 | + |
| 215 | +process.versions['greenworks'] = greenworks._version; |
| 216 | + |
| 217 | +module.exports = greenworks; |
0 commit comments