-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autoupdater.js
325 lines (257 loc) · 11.9 KB
/
Autoupdater.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//Automatebound: Easy modded server setup
//the start of a server application that interfaces with the steam web API to pull mods from workshop collections, and should check if an item has updated on the workshop in the collection
//or wait a specified amount of time to update the mods folder with up-to-date mods.
//Orginally By Darkly SteamGear
//Powershell script is a modified version of Archemedes' workshop file script
//THE SETUP PROCESS:
//First make sure you install SteamCMD inside of the main directory for this automation script by running steamcmd.exe inside of the SteamCMD folder.
//A copy of it is included in this project, but feel free to grab your own copy on steam's website
//Install nodejs
//Install python 3
//Make sure you have powershell installed as well if you are using some other system (WARNING: UNTESTED IN LINUX, DON'T EVEN TRY TO USE A MAC)
//there was an issue that occured when microsoft word was not installed alongside powershel, but that issue is resolved.
//Open up AAA_STARTSERVER_AAA and edit the file to direct and run this file
//Open up Autoupdater.js, and edit everything below this line to what you want:
//_________EDIT AS NEEDED_____________
//The name of the server
var nameOfServer = "NAMEOFSERVER";
//your steam username and password
var user = "USERNAME";
var pass = "PASSWORD";
//the ID of the game
var gameId = 211820;
//the ID of the game's dedicated server files
var serverId = 533830;
//the path of your server
var serverDir = "D:\\SERVER\\DIRECTORY\\NOSPACES\\";
//the ID of the collection you wish to use for your server
var collectionID = 12345678;
//The ID of the first workshop item (Because of file encoding weirdness)
var firstWorkshopID = 12345678;
//This will automatically set everything up for you to get started with a completely new modded starbound server.
var installServer = true;
//The time the script will wait before autoupdating again (in ms)
WAIT_TIME = 30000;
//DO NOT EDIT ANYTHING BELOW THIS
//Globals
//The server childprocess import
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const steamcmd_child = require("child_process");
const server_child = require("child_process");
const powershell_child = require("child_process");
const timer_child = require("child_process");
const copypaste_child = require("child_process");
const kill_child = require("child_process");
var psCode = [];
var a = "a";
var b = "b";
var out = "";
//set the first run to true
var firstRun = true;
var serverNotInstalled = true;
//the current location
var dir = process.cwd();
//the current directory
var steamCmdDir = String(dir) + "\\SteamCMD\\";
//the directory of the cache for steam
var serverCacheDir = String(dir) + "\\ServerCache\\";
//some random multithreading js tomfoolery
//probably summons the magic bananas that make the code good
process.chdir(steamCmdDir);
var fs = require("fs");
//this function was a pain in the butt to make
//HECK this function
//reads files and puts them into an array by line.
function read_file(fileDir){
var array = [];
//I SAID THIS DAMMIT
this.array = fs.readFileSync(fileDir).toString().split("\n");
for(i in this.array) {
//console.log(this.array[i]);
}
return this.array;
}
//this function basically gets the server ready by going into the server cache and updating all of the subbed workshop mods and the client.
//getting them ready for an endless loop of auto updating everytime a new mod updates.
function server_setup(){
//if no server directory exists
if (!fs.existsSync(serverDir)){
console.log("NO SERVER DIRECTORY FOUND. MAKING ONE AT: " + String(serverDir));
fs.mkdirSync(serverDir);
}
//if the steamcmd exists
if (fs.existsSync(steamCmdDir)){
read_ps_script(overwrite_ps_script)
if (!fs.existsSync(serverCacheDir)){
fs.mkdirSync(serverCacheDir);
}
console.log("please note that if this is your first time, run steamCMD manually");
//you better run it manually you butt
}
//you heck, you don't have steamcmd. go get it
else{
throw new Error("NO STEAMCMD FOLDER DETECTED! Please install steamcmd inside a folder called SteamCMD inside the same folder as the autoupdater server\n (.../autoupdater/SteamCMD)");
}
}
//this function will start up the server if there is no server running in the server child
//ARGUMENTS: serverExecutable: The exact name and directory of the server file
function run_server(server_dir){
if (fs.existsSync(serverDir)){
//Check if this is the first time the server has been ran
if (firstRun == false){
start_server_again(String(serverDir));
timerFunc = timer_child.spawn(String(dir) + "\\timer.bat",{shell:true, detached:true});
timerFunc.on("exit", function() {
read_ps_script(overwrite_ps_script);
});
}
else{
//Run the server for the first time
start_server();
timerFunc = timer_child.spawn(String(dir) + "\\timer.bat",{shell:true, detached:true});
timerFunc.on("exit", function() {
read_ps_script(overwrite_ps_script);
});
}
}
//else make a directory and add all necissary server files to the directory
else{
console.log("NO SERVER FOUND. MAKING ONE AT: " + String(serverDir))
fs.mkdir(serverDir);
}
};
//this function starts up the very first instance of the server
function start_server(server_dir){
console.log("THIS IS THE SERVER DIR " + String(serverDir))
starboundServer = server_child.spawn(String(serverDir) + "\\run.bat",{shell:true, detached:true});
starboundServer.on("data", args =>{ process.stdout.write("--" + String(nameOfServer) + "--\n" + args[0])});
output_data(starboundServer);
process.stdin.pipe(starboundServer.stdin);
firstRun = false;
}
//this function starts up every other instance of the server.
function start_server_again(server_dir){
//if there is no server running in the child node. had to remove error thing.
//Run the server
starboundServer = server_child.spawn(String(serverDir)+ "\\run.bat",{shell:true, detached:true});
starboundServer.on("data", args =>{ process.stdout.write("--" + nameOfServer + "--\n" + args[0])});
output_data(starboundServer);
process.stdin.pipe(starboundServer.stdin);
};
//This function will kill the server inside the server child
function kill_server(){
//End it's miserable life
//it doesn't deserve to live
//steamCmd.kill('SIGINT');
serverKill = kill_child.spawn(String(dir) + "\\serverkiller.bat",{shell:true, detached:true});
};
//this function outputs server data to the console
function output_data(childpar){
// Echoes any command output
childpar.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
// Error output
childpar.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
// Process exit
childpar.on('close', function (code) {
console.log('child process exited with code ' + code);
});
}
//complete insanity, callback functions for powershell stuff
function read_mod_text(){
var fs = require("fs");
var tex = fs.readFileSync(String(serverDir) +"mods_list.txt").toString("utf-16LE");
var modIds = tex.split("\n");
//console.log("TEXT 1: " + String(modIds));
return modIds;
}
//complete insanity, callback functions for powershell stuff
function read_ps_script(callback){
psCode = read_file(String(dir) + "\\WorkshopSearcher.ps1");
setTimeout(_=>{callback();},5000);
}
//overwrites the original powershell script with new data
function overwrite_ps_script(){
setTimeout(_=>{psCode[16] = "$WorkshopCollectionURL = 'http://steamcommunity.com/sharedfiles/filedetails/?id=" + String(collectionID) +"'";
psCode[20] = "$ModList = '"+ String(serverDir) +"mods_list.txt'";
psCode[21] = "$ModDesc = '"+ String(serverDir) +"mods_desc.txt'";
//console.log("This is pscode: " + psCode[16]);
joinedPsCode = psCode.join("\n");
fs.writeFileSync(String(dir) + "\\WorkshopSearcher.ps1",this.joinedPsCode,{encoding:'utf8',flag:'w'});
}, 100);
execute_ps_script();
}
//this function executes the power shell script
function execute_ps_script(){
console.log("EXECUTING POWERSHELL SCRIPT")
psScript = powershell_child.spawn("powershell.exe", [" Set-ExecutionPolicy RemoteSigned; ", String(dir) + "\\WorkshopSearcher.ps1"],{shell:true, detached:true});
output_data(psScript);
psScript.on("exit", function() {
get_steamcmd_commands(execute_steamcmd_commands);
})
}
//This function downloads all of the workshop files found in the collection by the powershell script
function get_steamcmd_commands(callback){
modIdList = read_mod_text();
out += "login "+ user + " " + pass + "\n" + "force_install_dir " + serverCacheDir + "\napp_update " + String(serverId)
return new Promise((resolve, reject) => {
for (index = 0; index < (modIdList.length-1); index++) {
if (index == 0){
modder = modIdList[index].toString("");
out += " \nworkshop_download_item " + String(gameId)+ " " + firstWorkshopID+"\n";
}
else if (index == modIdList.length-2){
out += "workshop_download_item " + String(gameId)+ " " + modIdList[index] +"\nexit";
}
else{
out += "workshop_download_item " + String(gameId)+ " " + modIdList[index] +"\n";
}
}
fs.writeFileSync(String(dir) + "\\updatemods.txt",out,{encoding:'utf-8',flag:'w'});
//console.log("THIS IS INITIAL OUT: " + String(out))
resolve();
setTimeout(_=>{callback();},5000);
return out;
})
}
//Executes commands using a steam command line child process. When the steam command line child process is finished, run the mod copy paste function.
function execute_steamcmd_commands(){
out = "";
steamCmd = steamcmd_child.spawn(steamCmdDir + "steamcmd.exe +runscript " + String(dir) + "\\updatemods.txt",{shell:true, detached:true})
console.log("STEAM CMD CHILDTHREAD STARTED");
output_data(steamCmd);
steamCmd.on('exit', function() {
console.log("COPYING AND PASTING MODS FROM CACHE")
copy_paste_mods();
});
}
//This will run the python script to copy, paste, and update mods inside of the specified mods folder
function copy_paste_mods(){
if (firstRun != true){
kill_server()
}
if (installServer == false){
copyPaster = copypaste_child.spawn("py", [String(dir) + "\\copypaste.py", "-b=" + String(serverDir), "-c=" + String(serverCacheDir), "steamapps\\workshop\\content\\" + String(gameId) ,"-i=n", "-sid=" + String(serverId)],{shell:true, detached:true});
console.log("COPYING MODS FROM CACHE TO SERVER MODS FOLDER");
}
if (installServer == true){
copyPaster = copypaste_child.spawn("py", [String(dir) + "\\copypaste.py", "-b=" + String(serverDir), "-c=" + String(serverCacheDir) + "steamapps\\workshop\\content\\" + String(gameId) ,"-i=y", "-sid=" + String(serverId)],{shell:true, detached:true});
console.log("NO SERVER FILES FOUND. COPYING MODS AND MAIN SERVER FILES FROM CACHE TO SERVER FOLDER");
installServer = false;
}
copyPaster.on('exit', function(){
run_server(serverDir);
})
}
//oh wow Im tired
//I should probably do that actual project I was supposed to be doing for school. Hi past me, we passed school and now no more homework :D. If you are en employer I am looking for work
//In automation engineering for software or systems. I have a BS in computer engineering and experience in PLC logic.
//and the actual execution of the program
//read_ps_script(overwrite_ps_script);
server_setup();
console.log("Server started");
//yay