Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare Update #112

Merged
merged 8 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 73 additions & 54 deletions build/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,41 @@

def parse_args():
parser = argparse.ArgumentParser(prog="build", description=__doc__)
parser.add_argument("--sha", action="store_true", help="append git hash to zips")
parser.add_argument("--sha", action="store_true",
help="append git hash to zips")
parser.add_argument("--name", type=str, help="append name to zips")
parser.add_argument("--retries", type=int, default=3, help="download attempts before failure")
parser.add_argument("--clean", action="store_true", help="clean output dirs")
parser.add_argument("--retries", type=int, default=3,
help="download attempts before failure")
parser.add_argument("--clean", action="store_true",
help="clean output dirs")
parser.add_argument("--dev_build", action="store_true",
help="makes a folder with all the files symlinked for development. probally only works on linux")
parser.add_argument("-c", "--client", action="store_true",
help="only builds the client pack")
return parser.parse_args()


def build(args):
modlist = []
basePath = os.path.normpath(os.path.realpath(__file__)[:-7] + "..")
copyDirs = ["/groovy", "/config", "/mods", "/structures"]
serverCopyDirs = ["/groovy", "/config", "/mods", "/structures"]
copyDirs = ["/config", "/mods", "/groovy"]
serverCopyDirs = ["/config", "/mods", "/groovy"]
modURLlist = []
modClientOnly = []
# remove the old build files

shutil.rmtree(basePath + "/buildOut/client/overrides",
ignore_errors=True)
shutil.rmtree(basePath + "/buildOut/server", ignore_errors=True)

if args.clean:
shutil.rmtree(basePath + "/buildOut/client/overrides", ignore_errors=True)
shutil.rmtree(basePath + "/buildOut/server", ignore_errors=True)
shutil.rmtree(basePath + "/mods", ignore_errors=True)
sys.exit(0)
sha = ""
if args.sha:
try:
p = subprocess.run(["git", "rev-parse", "--short", "HEAD"], capture_output=True, cwd=basePath)
p = subprocess.run(
["git", "rev-parse", "--short", "HEAD"], capture_output=True, cwd=basePath)
sha = p.stdout.strip().decode("utf-8")
except Exception as e:
print("could not determine git sha, skipping")
Expand Down Expand Up @@ -78,54 +88,58 @@ def mkdirs(path):

r = requests.get(mod["url"])

hash = hashlib.sha256(jar.read()).hexdigest()
hash = hashlib.sha256(r.content).hexdigest()
if str(hash) == mod["hash"]:
jar.write(r.content)
modlist.append(mod["name"])
print("hash succsessful")
print("hash succsessful for {}".format(mod["name"]))
break
else:
print("hash unsuccsessful")
print("hash unsuccsessful for {}".format(mod["name"]))
print("use", str(hash), "this if it is consistant across runs")
pass

for dir in copyDirs:
try:
shutil.copytree(basePath + dir, basePath + "/buildOut/client/overrides" + dir)
shutil.copytree(basePath + dir, basePath +
"/buildOut/client/overrides" + dir)
except Exception as e:
print("Directory exists, skipping")

print("directories copied to buildOut/client")
archive = "buildOut/client"
if sha:
archive = "%s-%s" % (archive, sha)
shutil.copy(basePath + "/manifest.json", basePath + "/buildOut/client/manifest.json")
shutil.copy(basePath + "/manifest.json", basePath +
"/buildOut/client/manifest.json")
shutil.make_archive(archive, "zip", basePath + "/buildOut/client")
print('client zip "%s.zip" made' % (archive))

if (args.client):
return

cringe = []
headers = {'Accept': 'application/json', 'x-api-key': os.getenv("CFAPIKEY")}
headers = {'Accept': 'application/json',
'x-api-key': os.getenv("CFAPIKEY")}
for mod in manifest["files"]:
clientOnly = False
try:
clientOnly = mod["clientOnly"]
# clean up the distributed file
del mod["clientOnly"]
except:
pass
modClientOnly.append(clientOnly)

r = requests.get(
'https://api.curseforge.com/v1/mods/{0}/files/{1}/download-url'.format(mod["projectID"], mod["fileID"]),
'https://api.curseforge.com/v1/mods/{0}/files/{1}/download-url'.format(
mod["projectID"], mod["fileID"]),
headers=headers)
try:
metadata = json.loads(r.text)
except:
print(
'https://api.curseforge.com/v1/mods/{0}/files/{1}/download-url'.format(mod["projectID"], mod["fileID"]))
cringe_r = requests.get('https://api.curseforge.com/v1/mods/{0}'.format(mod["projectID"]), headers=headers)
cringe_r = requests.get(
'https://api.curseforge.com/v1/mods/{0}'.format(mod["projectID"]), headers=headers)
try:
data = cringe_r.json()["data"]
cringe.append("https://www.curseforge.com/minecraft/mc-mods/{0}/files/{1}".format(data["slug"], mod["fileID"]))
cringe.append(
"https://www.curseforge.com/minecraft/mc-mods/{0}/files/{1}".format(data["slug"], mod["fileID"]))
except:
cringe.append('This is the raw mod id and file id, the cf api was being mega fucked: `{0}`, `{1}`'.format(mod["projectID"], mod["fileID"]))
cringe.append('This is the raw mod id and file id, the cf api was being odd: `{0}`, `{1}`'.format(
mod["projectID"], mod["fileID"]))

continue

Expand All @@ -136,33 +150,35 @@ def mkdirs(path):
modlist.append(name)
else:
modlist.append(metadata["data"].split("/")[-1])
modURLlist.append(metadata["data"])

# write the json without "clientOnly" to the file
with open(basePath + "/manifest.json", mode='w') as file:
json.dump(manifest, file, indent=4);

modURLlist.append(metadata["data"])
try:
modClientOnly.append(mod["clientOnly"])
except:
modClientOnly.append(False)
print("modlist compiled")

with open(basePath + "/buildOut/modlist.html", "w") as file:
data = "<html><body><h1>Modlist</h1><ul>"
for mod in modlist:
data += "<li>" + mod.split(".jar")[0] + "</li>"
data += "</ul></body></html>"
file.write(data)
print("modlist.html done")
shutil.copy(basePath + "/manifest.json", basePath + "/buildOut/server/manifest.json")
shutil.copy(basePath + "/manifest.json", basePath +
"/buildOut/server/manifest.json")
shutil.copy(basePath + "/LICENSE", basePath + "/buildOut/server/LICENSE")
shutil.copy(basePath + "/launch.sh", basePath + "/buildOut/server/launch.sh")
shutil.copy(basePath + "/launch.sh", basePath +
"/buildOut/server/launch.sh")
for dir in serverCopyDirs:
try:
shutil.copytree(basePath + dir, basePath + "/buildOut/server" + dir)
shutil.copytree(basePath + dir, basePath +
"/buildOut/server" + dir)
except Exception as e:
print("Directory exists, skipping")
print("directories copied to buildOut/server")
for i, mod in enumerate(modURLlist):
jarname = mod.split("/")[-1]
if (modClientOnly[i]):
if (modClientOnly[i] == True):
continue

if os.path.exists(os.path.join(cachepath, jarname)):
Expand All @@ -180,15 +196,15 @@ def mkdirs(path):
forgeVer = manifest["minecraft"]["modLoaders"][0]["id"].split("-")[-1]
mcVer = manifest["minecraft"]["version"]
url = (
"https://maven.minecraftforge.net/net/minecraftforge/forge/"
+ mcVer
+ "-"
+ forgeVer
+ "/forge-"
+ mcVer
+ "-"
+ forgeVer
+ "-installer.jar"
"https://maven.minecraftforge.net/net/minecraftforge/forge/"
+ mcVer
+ "-"
+ forgeVer
+ "/forge-"
+ mcVer
+ "-"
+ forgeVer
+ "-installer.jar"
)
r = requests.get(url)
jar.write(r.content)
Expand All @@ -201,7 +217,8 @@ def mkdirs(path):
r = requests.get(url)
jar.write(r.content)
print("Vanilla Downloaded")
subprocess.run(["java", "-jar", "forge-installer.jar", "--installServer"], cwd=basePath + "/buildOut/server/")
subprocess.run(["java", "-jar", "forge-installer.jar",
"--installServer"], cwd=basePath + "/buildOut/server/")
print("Forge Installed")
if len(cringe) != 0 or os.path.exists(basePath + "/README_SERVER.md"):
with open(basePath + "/buildOut/server/README_SERVER.md", "w") as f:
Expand Down Expand Up @@ -232,28 +249,30 @@ def mkdirs(path):
shutil.rmtree(basePath + "/buildOut/mmc/minecraft/mods/")
except:
pass
shutil.copytree(basePath + "/buildOut/server/mods/", basePath + "/buildOut/mmc/minecraft/mods/")
shutil.copytree(basePath + "/buildOut/server/mods/",
basePath + "/buildOut/mmc/minecraft/mods/")
for dir in copyDirs:
try:
os.symlink(basePath + dir, basePath + "/buildOut/mmc/minecraft/" + dir)
os.copytree(basePath + dir, basePath +
"/buildOut/mmc/minecraft/" + dir)
except Exception as e:
print("Directory exists, skipping")
print("directories copied to buildOut/mmc/minecraft")

for i, mod in enumerate(modURLlist):
jarname = mod.split("/")[-1]
if (not modClientOnly[i]):
break

with open(basePath + "/buildOut/mmc/minecraft/mods/" + jarname, "w+b") as jar:
r = requests.get(mod)
jar.write(r.content)
print(mod + " Downloaded")

shutil.copy(basePath + "/mmc-instance-data.json", basePath + "/buildOut/mmc/mmc-pack.json")
shutil.copy(basePath + "/mmc-instance-data.json",
basePath + "/buildOut/mmc/mmc-pack.json")
instanceFolder = input("What is your MultiMC instance folder:")
instanceName = input("What do you want to call the instance:")
os.symlink(basePath + "/buildOut/mmc/", instanceFolder + "/" + instanceName)
os.symlink(basePath + "/buildOut/mmc/",
instanceFolder + "/" + instanceName)
print("you might need to add an instance.cfg for mmc to reconise it")
print("done")

Expand Down
37 changes: 37 additions & 0 deletions build/updateMods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import json
import requests
from datetime import datetime

def is112(file):
True in ["1.12" in i for i in file["gameVersions"]]

headers = {
"Accept": "application/json",
"x-api-key": os.getenv("CFAPIKEY")
}

basePath = os.path.normpath(os.path.realpath(__file__)[:-13] + "..")
with open(f"{basePath}/manifest.json") as f:
manifest = json.load(f)

ver_id = requests.get("https://api.curseforge.com/v1/minecraft/version/1.12.2", headers = headers).json()["data"]["gameVersionTypeId"]

for entry in manifest["files"]:
project_id = entry["projectID"]
mod = requests.get(f"https://api.curseforge.com/v1/mods/{project_id}", headers = headers).json()["data"]

old_file = requests.get(f"https://api.curseforge.com/v1/mods/{project_id}/files/" + str(entry["fileID"]), headers = headers).json()["data"]

latest_files = requests.get(f"https://api.curseforge.com/v1/mods/{project_id}/files?gameVersionTypeId={ver_id}", headers = headers).json()["data"]
new_file = sorted(latest_files, key=lambda file: datetime.fromisoformat(file["fileDate"]))

new_file = new_file[-1]

if datetime.fromisoformat(new_file["fileDate"]) > datetime.fromisoformat(old_file["fileDate"]):
if input(mod["name"] + " (" + old_file["fileName"] + ") -> " + new_file["fileName"] + "?") == "y":
entry["fileID"] = new_file["id"]


with open(f"{basePath}/manifest.json", "w") as f:
json.dump(manifest, f, indent=4)
Loading