forked from DS-Homebrew/twlmenu-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
executable file
·224 lines (197 loc) · 7.75 KB
/
generate.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
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
#!/usr/bin/env python3
import datetime
import glob
import io
import json
import os
from PIL import Image, ImageDraw
import py7zr
import sys
import urllib.parse
import yaml
# No py 2
if(sys.version_info.major != 3):
print("This is Python %d!\nPlease use Python 3!" % sys.version_info.major)
exit()
# Convert names to lowercase alphanumeric + underscore and hyphen
def webName(name):
name = name.lower()
out = ""
for letter in name:
if letter in "abcdefghijklmnopqrstuvwxyz0123456789-_.":
out += letter
elif letter == " ":
out += "-"
return out
def getTheme(path):
if "3dsmenu/" in path:
return "Nintendo 3DS"
elif "akmenu/" in path:
return "Wood UI"
elif "dsimenu/" in path:
return "Nintendo DSi"
elif "r4menu/" in path:
return "R4 Original"
return ""
def getDefaultIcon(path):
if "3dsmenu/" in path:
return 0
elif "akmenu/" in path:
return -1
elif "dsimenu/" in path:
return 1
elif "r4menu/" in path:
return 2
return -1
def lastUpdated(sevenZip):
latest = None
for item in sevenZip.list():
if latest == None or item.creationtime > latest:
latest = item.creationtime
return latest
# Read version from old unistore
unistoreOld = {}
if os.path.exists("twlmenu-skins.unistore"):
with open("twlmenu-skins.unistore", "r", encoding="utf8") as file:
unistoreOld = json.load(file)
# Create UniStore base
unistore = {
"storeInfo": {
"title": "TWiLight Menu++ Skins",
"author": "DS-Homebrew",
"url": "https://raw.githubusercontent.com/DS-Homebrew/twlmenu-extras/master/unistore/twlmenu-skins.unistore",
"file": "twlmenu-skins.unistore",
"sheetURL": "https://raw.githubusercontent.com/DS-Homebrew/twlmenu-extras/master/unistore/twlmenu-skins.t3x",
"sheet": "twlmenu-skins.t3x",
"description": "A collection of skins for TWiLight Menu++\nfrom DS-Homebrew/twlmenu-extras on GitHub\n\n(The 'Console' is the theme in TWiLight)",
"version": 3,
"revision": 0 if ("storeInfo" not in unistoreOld or "revision" not in unistoreOld["storeInfo"]) else unistoreOld["storeInfo"]["revision"]
},
"storeContent": [],
}
# Icons array
icons = []
iconIndex = 0
# Make 3DS, DSi, R4 icons
for file in ["3ds.png", "dsi.png", "r4.png"]:
with Image.open(open(os.path.join("unistore", "icons", file), "rb")) as icon:
if not os.path.exists(os.path.join("unistore", "temp")):
os.mkdir(os.path.join("unistore", "temp"))
icon.thumbnail((48, 48))
icon.save(os.path.join("unistore", "temp", str(iconIndex) + ".png"))
icons.append(str(iconIndex) + ".png")
iconIndex += 1
# Auth header
header = None
if len(sys.argv) > 1:
header = {"Authorization": "token " + sys.argv[1]}
# Get skin files
files = [f for f in glob.glob("_nds/TWiLightMenu/*menu/themes/*.7z")]
# Generate UniStore entries
for skin in files:
# Skip Wood UI for now
if(getTheme(skin) == "Wood UI"):
continue
info = {}
updated = datetime.datetime.utcfromtimestamp(0)
skinName = skin[skin.rfind("/")+1:-3]
with py7zr.SevenZipFile(skin) as a:
updated = lastUpdated(a)
if os.path.exists(os.path.join(skin[:skin.rfind("/")], "meta", skinName, "info.json")):
with open(os.path.join(skin[:skin.rfind("/")], "meta", skinName, "info.json")) as file:
info = json.load(file)
if not "unistore_exclude" in info or info["unistore_exclude"] == False:
# Make icon for UniStore
iconExists = False
if os.path.exists(os.path.join(skin[:skin.rfind("/")], "meta", skinName, "icon.png")):
with Image.open(open(os.path.join(skin[:skin.rfind("/")], "meta", skinName, "icon.png"), "rb")) as icon:
iconExists = True
if not os.path.exists(os.path.join("unistore", "temp")):
os.mkdir(os.path.join("unistore", "temp"))
icon.thumbnail((48, 48))
icon.save(os.path.join("unistore", "temp", str(iconIndex) + ".png"))
icons.append(str(iconIndex) + ".png")
iconIndex += 1
screenshots = []
if os.path.exists(os.path.join(skin[:skin.rfind("/")], "meta", skinName, "screenshots")):
dirlist = os.listdir((os.path.join(skin[:skin.rfind("/")], "meta", skinName, "screenshots")))
dirlist.sort()
for screenshot in dirlist:
if screenshot[-3:] == "png":
screenshots.append({
"url": "https://raw.githubusercontent.com/DS-Homebrew/twlmenu-extras/master/" + skin[:skin.rfind("/")] + "/meta/" + urllib.parse.quote(skinName) + "/screenshots/" + screenshot,
"description": screenshot[:screenshot.rfind(".")].capitalize().replace("-", " ")
})
# Add entry to UniStore
unistore["storeContent"].append({
"info": {
"title": info["title"] if "title" in info else skinName,
"version": info["version"] if "version" in info else "v1.0.0",
"author": info["author"] if "author" in info else "",
"category": info["categories"] if "categories" in info else [],
"console": getTheme(skin),
"icon_index": len(icons) - 1 if iconExists else getDefaultIcon(skin),
"description": info["description"] if "description" in info else "",
"screenshots": screenshots,
"license": info["license"] if "license" in info else "",
"last_updated": updated.strftime("%Y-%m-%d at %H:%M (UTC)")
},
info["title"] if "title" in info else skinName: [
{
"type": "downloadFile",
"file": "https://raw.githubusercontent.com/DS-Homebrew/twlmenu-extras/master/" + urllib.parse.quote(skin),
"output": "sdmc:/" + skinName + ".7z",
"message": "Downloading " + info["title"] if "title" in info else skinName + "..."
},
{
"type": "extractFile",
"file": "sdmc:/" + skinName + ".7z",
"input": skinName + "/",
"output": "sdmc:/" + skin[:-3] + "/",
"message": "Extracting " + info["title"] if "title" in info else skinName + "..."
},
{
"type": "deleteFile",
"file": "sdmc:/" + skinName + ".7z",
"message": "Deleting " + skinName + ".7z..."
}
]
})
# Website file
web = unistore["storeContent"][-1]["info"].copy()
web["layout"] = "app"
web["updated"] = updated.strftime("%Y-%m-%dT%H:%M:%SZ")
web["systems"] = [web["console"]]
web["downloads"] = {skinName: {
"url": "https://raw.githubusercontent.com/DS-Homebrew/twlmenu-extras/master/" + skin,
"size": os.path.getsize(skin)
}}
if web["icon_index"] != -1:
if web["icon_index"] < 3:
web["icon"] = "https://raw.githubusercontent.com/DS-Homebrew/twlmenu-extras/master/unistore/icons/" + ["3ds", "dsi", "r4", "ak"][web["icon_index"]] + ".png"
else:
web["icon"] = "https://raw.githubusercontent.com/DS-Homebrew/twlmenu-extras/master/" + skin[:skin.rfind("/")] + "/meta/" + urllib.parse.quote(skinName) + "/icon.png"
web["image"] = web["icon"]
web.pop("icon_index")
if "title" in web:
if not os.path.exists(os.path.join("docs", "_" + webName(web["console"]))):
os.mkdir(os.path.join("docs", "_" + webName(web["console"])))
with open(os.path.join("docs", "_" + webName(web["console"]), webName(web["title"]) + ".md"), "w", encoding="utf8") as file:
file.write("---\n" + yaml.dump(web) + "---\n")
for category in web["category"]:
if not os.path.exists(os.path.join("docs", webName(web["console"]))):
os.mkdir(os.path.join("docs", webName(web["console"])))
with open(os.path.join("docs", webName(web["console"]), category + ".md"), "w", encoding="utf8") as file:
file.write(f"---\nlayout: cards\ntitle: {getTheme(skin)} - {category}\nsystem: {webName(web['console'])}\ncategory: {category}\n---")
# Make t3x
with open(os.path.join("unistore", "temp", "icons.t3s"), "w", encoding="utf8") as file:
file.write("--atlas -f rgba -z auto\n\n")
for icon in icons:
file.write(icon + "\n")
os.system("tex3ds -i " + os.path.join("unistore", "temp", "icons.t3s") + " -o " + os.path.join("unistore", "twlmenu-skins.t3x"))
# Increment revision if not the same
if unistore != unistoreOld:
unistore["storeInfo"]["revision"] += 1
# Write unistore to file
with open(os.path.join("unistore", "twlmenu-skins.unistore"), "w", encoding="utf8") as file:
file.write(json.dumps(unistore, sort_keys=True))