Skip to content

Commit

Permalink
Write descriptor files to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
RagingFlames committed Mar 30, 2024
1 parent 9bab75b commit d0f49c8
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 148 deletions.
4 changes: 4 additions & 0 deletions createBarotraumaPack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import util

def create_modpack():
print("hi")
31 changes: 10 additions & 21 deletions createStellarisPack.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import os
import shutil
import sqlite3


def copy_files(src_folder, dest_folder):
# Walk through the source folder
for root, dirs, files in os.walk(src_folder):
for file in files:
src_path = os.path.join(root, file)

# Preserve the relative path structure in the destination folder
relative_path = os.path.relpath(src_path, src_folder)
dest_path = os.path.join(dest_folder, relative_path)

# Create the necessary directories in the destination folder
os.makedirs(os.path.dirname(dest_path), exist_ok=True)

# Copy the file, overwriting if it already exists
shutil.copy2(src_path, dest_path)
print(f"Copied: {src_path} to {dest_path}")
import util
from util import copy_files


def create_modpack():
Expand Down Expand Up @@ -66,7 +50,8 @@ def create_modpack():
input("Press 'Enter' to continue")

# Make the mod folder
modPackName = input("What is the name for this new modpack?")
modPackName = input("What is the name for this new modpack?\n")
modPackVersion = input("What is the version for this new modpack? (IE 3.10.1)\n")
destination = os.path.join(destination, modPackName)
try:
os.mkdir(destination)
Expand All @@ -79,8 +64,8 @@ def create_modpack():
# Find the workshop mods
workshopPath = ""
while True:
workshopPath = input("Copy paste the path to your stellaris workshop folder")
if not workshopPath.rsplit("\\", 1)[-1] == "281990":
workshopPath = input("Copy paste the path to your stellaris workshop folder\n")
if not workshopPath.rsplit("\\", 1)[-1] == "281990": # A really stupid simple check for the right path
print("It looks like yu didn't paste the correct folder, the path should end at the '281990' folder")
else:
break
Expand All @@ -96,3 +81,7 @@ def create_modpack():
# Commit the changes and close the connection
connection.commit()
connection.close()

# Make the description files
util.make_mod_file(modPackName, modPackVersion)
util.make_descriptor_file(modPackName, modPackVersion, destination)
156 changes: 29 additions & 127 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
import shutil
import sqlite3
from util import yes_or_no, convert_path
import createStellarisPack
import createBarotraumaPack
import util


def is_admin():
Expand All @@ -9,28 +12,8 @@ def is_admin():
except:
return False


def yes_or_no():
while True:
user_input = input().lower()
if user_input == 'y':
return True
elif user_input == 'n':
return False
else:
print("Invalid input. Please enter 'y' or 'n'.")


def convert_path(clipboard_path):
# Replace backslashes with double backslashes
formatted_path = clipboard_path.replace("\\", "\\\\")

# If the path contains a drive letter, add 'r' prefix to make it a raw string
if ":" in formatted_path:
formatted_path = "r'" + formatted_path + "'"

return formatted_path

def sort_mods(e):
return e[3]

def main_help():
print("0: Rename mod folders")
Expand All @@ -39,11 +22,14 @@ def main_help():
"temporary directory. Then run the script from that directory, or point the script at that directory. This will make a copy of all your stellaris mod" +
" folders with relevant names. For example '1121692237' would be renamed to 'Gigastructural Engineering & More '")
print("1: Create modpack")
print("A guided process to create a new modpack using an existing playset from the stellaris launcher. Make sure the mods in the playlist are already in "+
"the proper mod load order.")
print("A guided process for creating modpacks for a variety of games")
print("9: Help")
print("This is help")

def create_modpack_help():
print("Stellaris:")
print("A guided process to create a new modpack using an existing playset from the stellaris launcher. Make sure the mods in the playlist are already in "+
"the proper mod load order.")

def rename_folders():
current_directory = os.getcwd()
Expand Down Expand Up @@ -80,125 +66,41 @@ def rename_folders():
print(f"Error processing folder {folder_path}: {e}")
input("Press enter to continue")

def sort_mods(e):
return e[3]


def copy_files(src_folder, dest_folder):
# Walk through the source folder
for root, dirs, files in os.walk(src_folder):
for file in files:
src_path = os.path.join(root, file)

# Preserve the relative path structure in the destination folder
relative_path = os.path.relpath(src_path, src_folder)
dest_path = os.path.join(dest_folder, relative_path)

# Create the necessary directories in the destination folder
os.makedirs(os.path.dirname(dest_path), exist_ok=True)

# Copy the file, overwriting if it already exists
shutil.copy2(src_path, dest_path)
print(f"Copied: {src_path} to {dest_path}")

def create_modpack():
# Connect to the SQLite database file (create a new file if it doesn't exist)
destination = os.getcwd()
db_file_path = os.path.join(os.path.expanduser("~"), "Documents", "Paradox Interactive", "Stellaris", "launcher-v2.sqlite")
connection = sqlite3.connect(db_file_path)

# Create a cursor object to execute SQL queries
cursor = connection.cursor()
# Get everything in the playset table
cursor.execute("SELECT * FROM playsets")
rows = cursor.fetchall()

print("Found the following playsets, which playset will we use?")
# Print the rows found
for i in range(len(rows)):
print("\033[1m" + str(i) + "\033[0m" + " : " + rows[i][1])
selection = input()
playset = rows[int(selection)][0]
print("Using playset with ID: " + playset)

# Get all relavent mods
cursor.execute("SELECT * FROM playsets_mods")
rows = cursor.fetchall()
modIDList = []
for row in rows:
if(row[0] == playset):
modIDList.append(row)
# Sort the list by load order
modIDList = sorted(modIDList, key=lambda x:x[3])

cursor.execute("SELECT * FROM mods")
rows = cursor.fetchall()
modWorkshopIDList = []
for mod in modIDList: # For every mod in the list
for row in rows: # For every row in the mods table
if row[0] == mod[1]:
modWorkshopIDList.append(row)

# Print sorted mod list
print("The following mods will be used in this order for the modpack")
for mod in modWorkshopIDList:
print(mod[5])
input("Press 'Enter' to continue")



# Make the mod folder
modPackName = input("What is the name for this new modpack?")
destination = os.path.join(destination, modPackName)
try:
os.mkdir(destination)
print(f"Directory '{destination}' created successfully.")
except FileExistsError:
print(f"Directory '{destination}' already exists.")
except Exception as e:
print(f"An error occurred: {e}")


# Find the workshop mods
workshopPath = ""
def createModpack():
print("Which game is this modpack for?")
print("0: Barotrauma")
selection = input("9: Stellaris\n")
while True:
workshopPath = input("Copy paste the path to your stellaris workshop folder")
if not workshopPath.rsplit("\\", 1)[-1] == "281990":
print("It looks like yu didn't paste the correct folder, the path should end at the '281990' folder")
else:
break

#workshopPath = convert_path(workshopPath)
workshopMods = [f for f in os.listdir(workshopPath) if os.path.isdir(os.path.join(workshopPath, f))] #List of every folder in the workshop path
# Start copying the files
for mod in modWorkshopIDList:
workshopModFolder = os.path.join(workshopPath, mod[2])
copy_files(workshopModFolder, destination)




# Close the cursor
cursor.close()
# Commit the changes and close the connection
connection.commit()
connection.close()

match selection:
case "0":
rename_folders()
break
case "1": #Barotrauma
createBarotraumaPack.create_modpack()
break
case "9": #Stellaris
createStellarisPack.create_modpack()
break
case _:
print("Please retype answer")

if __name__ == '__main__':

print("This is a multi tool script, Please type the number for what you would like to do:")
print("0: Rename mod folders")
print("1: Create modpack")
selection = input("9: help")
selection = input("9: help\n")
while True:

match selection:
case "0":
rename_folders()
break
case "1":
create_modpack()
createModpack()
break
case "9":
main_help()
Expand Down
77 changes: 77 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import os
import shutil


def copy_files(src_folder, dest_folder):
# Walk through the source folder
for root, dirs, files in os.walk(src_folder):
for file in files:
src_path = os.path.join(root, file)

# Preserve the relative path structure in the destination folder
relative_path = os.path.relpath(src_path, src_folder)
dest_path = os.path.join(dest_folder, relative_path)

# Create the necessary directories in the destination folder
os.makedirs(os.path.dirname(dest_path), exist_ok=True)

# Copy the file, overwriting if it already exists
shutil.copy2(src_path, dest_path)
print(f"Copied: {src_path} to {dest_path}")


def yes_or_no():
while True:
user_input = input().lower()
if user_input == 'y':
return True
elif user_input == 'n':
return False
else:
print("Invalid input. Please enter 'y' or 'n'.")


def convert_path(clipboard_path):
# Replace backslashes with double backslashes
formatted_path = clipboard_path.replace("\\", "\\\\")

# If the path contains a drive letter, add 'r' prefix to make it a raw string
if ":" in formatted_path:
formatted_path = "r'" + formatted_path + "'"

return formatted_path

def make_mod_file(name, version):
## The mod file template
content = f'''name="{name}"
version="{version}"
tags={{
"Gameplay"
}}
picture="thumbnail.png"
supported_version="{version}"
path="mod/{name}"'''
## Writing to disk
with open(name+".mod", "w") as file: # Name the file the same name as the folder plus the .mod extension
file.write(content)
print("Content has been written to mod_info.txt")

def make_descriptor_file(name, version, path):
# Define the content string using the provided template
content = f'''name="{name}"
version="{version}"
tags={{
"Gameplay"
}}
picture="thumbnail.png"
supported_version="{version}"'''

# Specify the file path
file_path = os.path.join(path, "descriptor.mod")

# Write content to the specified file path
with open(file_path, "w") as file:
file.write(content)

print(f"Content has been written to {file_path}")

0 comments on commit d0f49c8

Please sign in to comment.