Skip to content

Commit

Permalink
Create compile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lightumcc committed May 6, 2024
1 parent c0d6a7e commit 9e2a665
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions compile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import os
import json
import hashlib
import shutil

base = os.path.dirname(os.path.abspath(__file__))

# config
manifest_path = os.path.join(base, 'server-manifest.json')
override_path = os.path.join(base, 'overrides')

detect_base = os.path.abspath('/Users/jessiezhu/Library/Application Support/minecraft/versions/CMIP')
override_contents = [
'mods',
'config',
'ModTranslations',
'resourcepacks',
'log4j2.xml'
'hmclversion.cfg'
]

override_ignore = [
'.DS_Store',
'desktop.ini',
'System Volume Information',
'Thumbs.db',
'$RECYCLE.BIN',
'.hmcl.json',
'.config.ini',
'autorun.inf'
]

manifest = {
"name": "Createch Mod Integration Pack",
"author": "CreatechStudio Shanghai Inc.",
"version": "1.20.4b4e",
"description": "",
"fileApi": "https://mci.createchstudio.com",
"files": {},
"addons": [
{
"id": "game",
"version": "1.20.4"
},
{
"id": "fabric",
"version": "0.15.6"
}
]
}

# funcs
def get_hash(p):
sha1 = hashlib.sha1()
with open(p, 'rb') as f:
while True:
data_bytes = f.read(128000)
sha1.update(data_bytes)
if not data_bytes:
break
return sha1.hexdigest()

def copy_file(original_path, p):
shutil.copyfile(original_path, os.path.join(override_path, p))

def scan_dir(content_path, p):
files = []

current_override_path = os.path.join(override_path, p)

if not os.path.exists(current_override_path):
os.mkdir(current_override_path)

for _p, dir_list, file_list in os.walk(content_path):
for d in dir_list:
if d in override_ignore:
continue
files += scan_dir(os.path.join(content_path, d), os.path.join(p, d))
for f in file_list:
if f in override_ignore:
continue
files.append({
'path': os.path.join(p, f),
'hash': get_hash(os.path.join(_p, f))
})
copy_file(os.path.join(content_path, f), os.path.join(p, f))

return files

def new_files():
files = []

if not os.path.exists(override_path):
os.mkdir(override_path)

for content in override_contents:
if content in override_ignore:
continue

content_path = os.path.join(detect_base, content)
if os.path.exists(content_path):
if os.path.isdir(content_path):
files += scan_dir(content_path, content)
elif os.path.isfile(content_path):
files.append({
'path': content,
'hash': get_hash(content_path)
})
parent_dir = os.path.dirname(os.path.join(override_path, content))
if not os.path.exists(parent_dir):
os.mkdir(parent_dir)
copy_file(content_path, content)

return files

# main
old_manifest = {}
if os.path.exists(manifest_path):
with open(manifest_path, 'r') as f:
old_manifest = json.loads(f.read())

if 'version' in old_manifest:
if old_manifest['version'] == manifest['version']:
print('[WARNING] Version does not change!')

manifest['files'] = new_files()

with open(manifest_path, 'w') as f:
f.write(json.dumps(manifest, indent=2))

print(f'Current Version: {manifest["version"]}')

0 comments on commit 9e2a665

Please sign in to comment.