forked from donzgold/JustMC_compilator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjmcc.py
More file actions
263 lines (238 loc) · 11.7 KB
/
jmcc.py
File metadata and controls
263 lines (238 loc) · 11.7 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import shutil
import sys
import zipfile
import requests
import os
import socket
def is_connected():
try:
socket.create_connection(("1.1.1.1", 53))
return True
except:
pass
return False
color_codes = {"0": "#000000", "1": "#0000AA", "2": "#00AA00", "3": "#00AAAA", "4": "#AA0000", "5": "#AA00AA",
"6": "#FFAA00", "7": "#AAAAAA", "8": "#555555", "9": "#5555FF", "a": "#55FF55", "b": "#55FFFF",
"c": "#FF5555", "d": "#FF55FF", "e": "#FFFF55", "f": "#FFFFFF"}
codes = {"r": "\x1b[0m", "l": "\x1b[1m", "o": "\x1b[3m", "n": "\x1b[4m", "k": "\x1b[40m"}
for k, v in color_codes.items():
r, g, b = [int(v[i:i + 2], 16) for i in range(1, len(v), 2)]
codes[k] = f"\x1b[38;2;{r};{g};{b}m"
allowed_symbols = "0123456789abcdefABCDEF"
def minecraft_based_text(text1, ignore_last_symbol=False):
if ignore_last_symbol is False:
text1 += "&r"
def next_symbol(afsd, fdsa):
if fdsa >= len(afsd):
return fdsa + 1, None
return fdsa + 1, afsd[fdsa]
pos = 0
msg = ""
while (s := next_symbol(text1, pos))[1] is not None:
pos, symbol = s[0], s[1]
if symbol == "&" and (s := next_symbol(text1, pos))[1] is not None:
pos, symbol = s[0], s[1]
if symbol in codes:
msg += codes[symbol]
continue
elif symbol == "#":
thing = "#"
if (s := next_symbol(text1, pos))[1] is not None:
pos, symbol = s[0], s[1]
thing += symbol
if not symbol in allowed_symbols:
msg += "&" + thing
continue
if (s := next_symbol(text1, pos))[1] is not None:
pos, symbol = s[0], s[1]
thing += symbol
if not symbol in allowed_symbols:
msg += "&" + thing
continue
if (s := next_symbol(text1, pos))[1] is not None:
pos, symbol = s[0], s[1]
thing += symbol
if not symbol in allowed_symbols:
msg += "&" + thing
continue
if (s := next_symbol(text1, pos))[1] is not None:
pos, symbol = s[0], s[1]
thing += symbol
if not symbol in allowed_symbols:
msg += "&" + thing
continue
if (s := next_symbol(text1, pos))[1] is not None:
pos, symbol = s[0], s[1]
thing += symbol
if not symbol in allowed_symbols:
msg += "&" + thing
continue
if (s := next_symbol(text1, pos))[1] is not None:
pos, symbol = s[0], s[1]
thing += symbol
if not symbol in allowed_symbols:
msg += "&" + thing
continue
a_as, gds, bdsa = [int(thing[i:i + 2], 16) for i in range(1, len(thing), 2)]
msg += f"\x1b[38;2;{a_as};{gds};{bdsa}m"
continue
msg += "&" + symbol
continue
msg += symbol
continue
return msg
def fix_type(thing):
try:
thing = int(thing)
except:
try:
thing = float(thing)
except:
if thing == "False":
thing = False
elif thing == "True":
thing = True
return thing
def download_latest_release(reload=False):
global data, an_data
url = requests.get("https://api.github.com/repos/donzgold/JustMC_compilator/releases/latest").json()["assets"][-1][
"browser_download_url"]
filereq = requests.get(url, stream=True)
open("release.zip", "wb").write(filereq.content)
if os.path.isfile("jmcc.properties"):
with zipfile.ZipFile(f"release.zip", "a") as z:
for zip_info in z.infolist():
if zip_info.filename != 'jmcc.properties':
z.extract(zip_info)
else:
z.getinfo(zip_info.filename).filename = "jmcc2.properties"
z.extract("jmcc.properties")
an_data = {i[:i.find("=")].strip(): fix_type(
i[i.find("=") + 1:].encode('raw_unicode_escape').decode('unicode_escape').strip()) for i in
open("jmcc2.properties", "r+")}
data["release_version"] = an_data["release_version"]
data["data_version"] = an_data["data_version"]
data["current_version"] = an_data["current_version"]
open("jmcc.properties", "w+").write("\n".join([f"{k}={v}" for k, v in data.items()]))
os.remove("jmcc2.properties")
else:
shutil.unpack_archive("release.zip")
os.remove("release.zip")
del filereq
print(minecraft_based_text("&fУстановлен последний релиз jmcc"))
if reload:
print(minecraft_based_text("&cК сожалению запуск прервался из-за обновления."))
exit()
def download_latest_version(reload=False):
global data, an_data
filereq = requests.get('https://raw.githubusercontent.com/donzgold/JustMC_compilator/master/release.zip')
open("release.zip", "wb").write(filereq.content)
if os.path.isfile("jmcc.properties"):
with zipfile.ZipFile(f"release.zip", "a") as z:
for zip_info in z.infolist():
if zip_info.filename != 'jmcc.properties':
z.extract(zip_info)
else:
z.getinfo(zip_info.filename).filename = "jmcc2.properties"
z.extract("jmcc.properties")
an_data = {i[:i.find("=")].strip(): fix_type(
i[i.find("=") + 1:].encode('raw_unicode_escape').decode('unicode_escape').strip()) for i in open("jmcc2.properties", "r+")}
data["release_version"] = an_data["release_version"]
data["data_version"] = an_data["data_version"]
data["current_version"] = an_data["current_version"]
open("jmcc.properties", "w+").write("\n".join([f"{k}={v}" for k, v in data.items()]))
os.remove("jmcc2.properties")
else:
shutil.unpack_archive("release.zip")
os.remove("release.zip")
del filereq
print(minecraft_based_text("&fУстановлена последняя версия jmcc"))
if reload:
print(minecraft_based_text("&cК сожалению запуск прервался из-за обновления."))
exit()
def download_latest_data():
global data, an_data
if not os.path.isdir("data"):
os.mkdir("data")
open("data/actions.json", "w+").write(
requests.get('https://raw.githubusercontent.com/donzgold/JustMC_compilator/master/data/actions.json').text)
open("data/events.json", "w+").write(
requests.get('https://raw.githubusercontent.com/donzgold/JustMC_compilator/master/data/events.json').text)
open("data/values.json", "w+").write(
requests.get('https://raw.githubusercontent.com/donzgold/JustMC_compilator/master/data/values.json').text)
data["data_version"] = an_data["data_version"]
open("jmcc.properties").write(f"{k}={v}" for k, v in data.items())
print(minecraft_based_text("&fУстановлена последняя версия базы"))
def check_updates():
global an_data
an_data = {i[:i.find("=")].strip(): fix_type(
i[i.find("=") + 1:].encode('raw_unicode_escape').decode('unicode_escape').strip()) for i in requests.get(
'https://raw.githubusercontent.com/donzgold/JustMC_compilator/master/jmcc.properties').text.split("\n")}
if data["check_beta_versions"] and (data["release_version"] < an_data["release_version"]):
print(minecraft_based_text(
f"&6Релизная версия jmcc отстаёт от последней на {an_data['release_version'] - data['release_version']}"))
if data["auto_update"]:
print(minecraft_based_text("&fИсправляем..."))
download_latest_release()
return
elif data["check_beta_versions"] and (data["current_version"] < an_data["current_version"]):
print(minecraft_based_text(
f"&6Бета версия jmcc отстаёт от последней на {an_data['current_version'] - data['current_version']}"))
if data["auto_update"]:
print(minecraft_based_text("&fИсправляем..."))
download_latest_version()
return
if data["data_version"] < an_data["data_version"]:
print(minecraft_based_text(
f"&6База кода jmcc отстаёт от последней на {an_data['data_version'] - data['data_version']}"))
print(minecraft_based_text("&fИсправляем..."))
download_latest_data()
path = os.path.realpath(__file__)
path = path[:path.rfind("\\")]
os.chdir(path)
an_data = {}
if not os.path.isfile("jmcc.properties"):
download_latest_release()
exit()
else:
data = {i[:i.find("=")].strip(): fix_type(
i[i.find("=") + 1:].encode('raw_unicode_escape').decode('unicode_escape').strip()) for i in
open("jmcc.properties", "r").readlines()}
a = is_connected()
if a:
check_updates()
if __name__ == "__main__":
additional = sys.orig_argv[2:]
if len(additional) == 0:
additional = ["about"]
if len(additional) >= 1:
if additional[0] == "compile" and len(additional) > 1:
from compilator import compile_file
compile_file(additional[1], upload=additional[-1] == "-u")
elif additional[0] == "decompile" and len(additional) > 1:
from decompilator import decompile_file
decompile_file(additional[1])
elif additional[0] == "update" and len(additional) > 1:
if additional[1] == "data":
download_latest_data()
elif additional[1] == "to_release":
download_latest_release(reload=False)
elif additional[1] == "to_version":
download_latest_version(reload=False)
else:
print(minecraft_based_text("&cНеизвестная команда, попробуйте команду &4help"))
elif additional[0] == "help":
print(minecraft_based_text("&fПомощь по &eJMCC &fdonzgold edition:\n"
"Команды:\n"
" about - выести сведения о программе\n"
" help - вывести это сообщение\n"
" compile <file_path> [-u] &7- &fскомпилировать файл, -u для загрузки на сервер.\n"
" decompile <file_path> &7- &fдекомпилировать json в код\n"
" update data &7- &fобновить базу кода\n"
" update to_release &7- &fобновиться до последнего релиза\n"
" update to_version &7- &fобновиться до последней версии"))
elif additional[0] == "about":
print(minecraft_based_text(
"&eJMCC &fdonzgold edition:\nЭто джей эм си си, он компилирует код в json, который используется на сервере justmc.ru\nПомощь по командам доступна с помощью команды &ehelp"))
else:
print(minecraft_based_text("&cНеизвестная команда, попробуйте команду &4help"))