-
Notifications
You must be signed in to change notification settings - Fork 0
/
copypaste.py
156 lines (150 loc) · 8.4 KB
/
copypaste.py
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
#No comments in the code, F. Let's change that!
#By Darkly SteamGear
#This python script is for copying and pasting mods from the main cache to the mods folder of your server, to reduce downtime through splitting functionality
#between several threads based in different languages
#Unfortunetly, this does increase the complexity, which requires more debugging
import shutil, os
import filecmp
import argparse
import datetime
import time
import sys
#All of our global variables
#-b D:\starboundservertest -c D:\CodingStuff\Javascript\Starbound_serverstuff\ServerCaches\steamapps\workshop\content\211820 -i y -sid 533830
start = time.time()
#The parser for the main python script
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--baseDir", help = "The directory of your server's folder.")
parser.add_argument("-c", "--cacheDir", help = "The directory of the SteamCMD mod cache.")
parser.add_argument("-i", "--installServer", help = "Installs the server.")
parser.add_argument("-sid", "--serverId", help = "The server steam ID.")
args = parser.parse_args()
SID = args.serverId
BASEDIR = args.baseDir
MODDIR = BASEDIR + "\\mods"
MOVEDIR = args.cacheDir
installServer = True if "y" in str(args.installServer) else False
print(MODDIR)
#Checks the mod description files generated by the power shell scripts, and adds the generated text into the mods folders to create
#log files with the names of each mods for easier mod finding for manual edits
try:
descriptionFile = open(BASEDIR + "\\mods_desc.txt", encoding = "utf-16")
descriptions = descriptionFile.readlines()
idFile = open(BASEDIR + "\\mods_list.txt", encoding = "utf-16")
ids = idFile.readlines()
i = 0
#removes all characters that will mess with naming the text files for easier location
for id in ids:
ids[i] = id.replace('\n', '')
i = i +1
i = 0
#removes all characters that will mess with naming the text files for easier location
for description in descriptions:
descriptions[i] = descriptions[i].replace('\n', '')
descriptions[i] = descriptions[i].replace('.', '')
descriptions[i] = descriptions[i].replace('#', '')
descriptions[i] = descriptions[i].replace('%', '')
descriptions[i] = descriptions[i].replace('&', '')
descriptions[i] = descriptions[i].replace('{', '')
descriptions[i] = descriptions[i].replace('}', '')
descriptions[i] = descriptions[i].replace('\\', '')
descriptions[i] = descriptions[i].replace('<', '')
descriptions[i] = descriptions[i].replace('>', '')
descriptions[i] = descriptions[i].replace('*', '')
descriptions[i] = descriptions[i].replace('?', '')
descriptions[i] = descriptions[i].replace('/', '')
descriptions[i] = descriptions[i].replace('$', '')
descriptions[i] = descriptions[i].replace('!', '')
descriptions[i] = descriptions[i].replace("'", '')
descriptions[i] = descriptions[i].replace('"', '')
descriptions[i] = descriptions[i].replace(':', '')
descriptions[i] = descriptions[i].replace('@', '')
descriptions[i] = descriptions[i].replace('+', '')
descriptions[i] = descriptions[i].replace('`', '')
descriptions[i] = descriptions[i].replace('|', '')
descriptions[i] = descriptions[i].replace('=', '')
i = i +1
except Exception:
descriptions = []
ids = []
#Function to build and install a server in a given directory if it does not exist
if installServer:
print("Creating new server in " + BASEDIR + ". You will still need to set up the server configuration file!")
cacheDir = MOVEDIR[0:MOVEDIR.rfind("\\steam")]
#Looks through the cache directory for the appropriate installed server files.
for filesS in os.listdir(cacheDir):
if os.path.isdir(cacheDir + "\\" + filesS):
if not "steamapps" in filesS:
if not os.path.isdir(BASEDIR+ "\\" +filesS):
shutil.copytree(cacheDir + "\\" +filesS, BASEDIR+ "\\" +filesS)
print("COPIED " + filesS+ " TO " + BASEDIR)
for cacheFile in os.listdir((cacheDir+ "\\" + filesS)):
if "server.exe" in cacheFile:
#builds the main batch file for automatebound to run
if not os.path.exists(BASEDIR + "\\run.bat"):
runner = open(BASEDIR + "\\run.bat", "w+")
runner.write("@ECHO OFF\n")
runner.write("CD " + BASEDIR + "\\" +filesS +"\n")
runner.write("START " + BASEDIR + "\\" + filesS + "\\starbound_server.exe\n")
runner.close()
if not os.path.exists(os.path.join(MODDIR)):
print(os.path.join(MODDIR), " not found, creating new directory")
os.mkdir(MODDIR)
# Walk through all files in the directory that contains the files to copy
d = 0
#For all directories in the cache. Since steam downloads each mod in it's own seperate folder,
#We will need a nested for loop to detect and add each .pak mod file inside of each downloaded workshop file directory id
#to download/update check all mods in the cache to our mods folder.
for root, dirs, files in os.walk(MOVEDIR):
for filename in files:
if ".pak" not in filename:
continue
lastSlash = root.rfind("\\")
id = root[lastSlash+1:]
#totally not stolen basic parsing code
#print(id)
# I use absolute path, case you want to move several dirs.
old_name = os.path.join( os.path.abspath(root), filename )
# Separate base from extension
base, extension = os.path.splitext(filename)
newFileName = id + ".pak"
# Initial new name
new_name = os.path.join(MODDIR, id +".pak")
#print(new_name)
# Something is very wrong if you somehow flag this boolean
if not os.path.exists(os.path.join(BASEDIR)):
print(os.path.join(BASEDIR), " not found, Something is very wrong.")
os.mkdir(BASEDIR)
#if the mods folder exists, and the mod id file does not exist but is still in the collection cache, download the mod to the server
#and create a new log file entry for that given mod
elif not os.path.exists(new_name): # folder exists, file does not
shutil.copy(old_name, new_name)
print("Added mod with ID " + str(id) + " to the mods folder")
if not os.path.isdir(BASEDIR + "\\mods\\" + str.rstrip(descriptions[ids.index(str(id))]) + ".txt"):
modName = open(BASEDIR + "\\mods\\" + str(id) + " - " + str.rstrip(descriptions[ids.index(str(id))]) + ".txt", "a")
now = datetime.datetime.now()
modName.write("[INFO] This mod was downloaded to server. [" + str(now) + "]\n")
modName.close()
else: # folder exists, file exists as well
# if when attempting to update the mod, no change was found in the cache, log the fact that it was checked, but don't make any
# changes to the mods in the server folder
if filecmp.cmp(old_name, new_name):
print("mod with id " + id + " does not have any new updates")
modName = open(BASEDIR + "\\mods\\" + str(id) + " - " + str.rstrip(descriptions[ids.index(str(id))]) + ".txt", "a")
now = datetime.datetime.now()
modName.write("[INFO] Update check did not find any updates available. [" + str(now) + "]\n")
modName.close()
i = 1
# if when attempting to update the mod, a change was found in the cache, log the fact that it was checked, and update
# the mod on the server.
else:
print("removing mod with id " + id + " And updating with new version")
modName = open(BASEDIR + "\\mods\\" + str(id) + " - " + str.rstrip(descriptions[ids.index(str(id))]) + ".txt", "a")
now = datetime.datetime.now()
modName.write("[INFO] Update found. Removing old version of mod and adding new version. [" + str(now) + "]\n")
modName.close()
os.remove(new_name)
shutil.copy(old_name, new_name)
d = d + 1
print("Took " + str(time.time() - start) + "seconds To check " + str(d) + " Mods for updates.")
os.system("exit")