格式化

This commit is contained in:
bgArray 2022-09-08 22:36:01 +08:00
parent 3b53846ade
commit 2e11a9949b

View File

@ -223,12 +223,13 @@ class midiConvert:
126: ("note.basedrum", 7), # 打击乐器无音域 126: ("note.basedrum", 7), # 打击乐器无音域
127: ("note.snare", 7), # 打击乐器无音域 127: ("note.snare", 7), # 打击乐器无音域
}[instrumentID] }[instrumentID]
except: except BaseException:
a = ("note.harp", 6) a = ("note.harp", 6)
return a return a
def __score2time(self, score: int): def __score2time(self, score: int):
return str(int(int(score / 20) / 60)) + ":" + str(int(int(score / 20) % 60)) return str(int(int(score / 20) / 60)) + ":" + \
str(int(int(score / 20) % 60))
def __formProgressBar( def __formProgressBar(
self, self,
@ -256,8 +257,11 @@ class midiConvert:
""" """
def __replace( def __replace(
s: str, tobeReplaced: str, replaceWith: str, times: int, other: str s: str,
): tobeReplaced: str,
replaceWith: str,
times: int,
other: str):
if times == 0: if times == 0:
return s.replace(tobeReplaced, other) return s.replace(tobeReplaced, other)
if times == s.count(tobeReplaced): if times == s.count(tobeReplaced):
@ -306,11 +310,11 @@ class midiConvert:
for i in range(maxscore): for i in range(maxscore):
nowstr = pgsstyle nowstr = pgsstyle
if ids[r"%%s"] == True: if ids[r"%%s"]:
nowstr = nowstr.replace(r"%%s", str(i + 1)) nowstr = nowstr.replace(r"%%s", str(i + 1))
if ids[r"%%t"] == True: if ids[r"%%t"]:
nowstr = nowstr.replace(r"%%t", self.__score2time(i + 1)) nowstr = nowstr.replace(r"%%t", self.__score2time(i + 1))
if ids[r"%%%"] == True: if ids[r"%%%"]:
nowstr = nowstr.replace( nowstr = nowstr.replace(
r"%%%", str(int((i + 1) / maxscore * 10000) / 100) + "%" r"%%%", str(int((i + 1) / maxscore * 10000) / 100) + "%"
) )
@ -318,15 +322,19 @@ class midiConvert:
countof_s = int((i + 1) / maxscore * pgblength) countof_s = int((i + 1) / maxscore * pgblength)
finalprgsbar.append( finalprgsbar.append(
"title @a[scores={" "title @a[scores={" +
+ scoreboardname scoreboardname +
+ "=" "=" +
+ str(i + 1) str(
+ "}] actionbar " i +
+ __replace( 1) +
nowstr, "_", progressbar[1][0], countof_s, progressbar[1][1] "}] actionbar " +
) __replace(
) nowstr,
"_",
progressbar[1][0],
countof_s,
progressbar[1][1]))
return finalprgsbar return finalprgsbar
@ -386,7 +394,8 @@ class midiConvert:
:return:str :return:str
""" """
block = b"\x24" + particularValue.to_bytes(2, byteorder="big", signed=False) block = b"\x24" + \
particularValue.to_bytes(2, byteorder="big", signed=False)
for i in [ for i in [
impluse.to_bytes(4, byteorder="big", signed=False), impluse.to_bytes(4, byteorder="big", signed=False),
@ -403,8 +412,10 @@ class midiConvert:
return block return block
def _toCmdList_m1( def _toCmdList_m1(
self, scoreboardname: str = "mscplay", volume: float = 1.0, speed: float = 1.0 self,
) -> list: scoreboardname: str = "mscplay",
volume: float = 1.0,
speed: float = 1.0) -> list:
""" """
使用Dislink Sforza的转换思路将midi转换为我的世界命令列表 使用Dislink Sforza的转换思路将midi转换为我的世界命令列表
:param scoreboardname: 我的世界的计分板名称 :param scoreboardname: 我的世界的计分板名称
@ -445,23 +456,24 @@ class midiConvert:
maxscore = max(maxscore, nowscore) maxscore = max(maxscore, nowscore)
soundID, _X = self.__Inst2soundIDwithX(instrumentID) soundID, _X = self.__Inst2soundIDwithX(instrumentID)
singleTrack.append( singleTrack.append(
"execute @a[scores={" "execute @a[scores={" +
+ str(scoreboardname) str(scoreboardname) +
+ "=" "=" +
+ str(nowscore) str(nowscore) +
+ "}" "}" +
+ f"] ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ {msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}" f"] ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ {msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}")
)
commands += 1 commands += 1
if len(singleTrack) != 0: if len(singleTrack) != 0:
tracks.append(singleTrack) tracks.append(singleTrack)
return tracks, commands, maxscore return [tracks, commands, maxscore]
# 值得注意的是,我这里没有修改 # 值得注意的是,我这里没有修改
def _toCmdList_m2( def _toCmdList_m2(
self, scoreboardname: str = "mscplay", volume: float = 1.0, speed: float = 1.0 self,
) -> list: scoreboardname: str = "mscplay",
volume: float = 1.0,
speed: float = 1.0) -> list:
""" """
使用金羿的转换思路将midi转换为我的世界命令列表 使用金羿的转换思路将midi转换为我的世界命令列表
:param scoreboardname: 我的世界的计分板名称 :param scoreboardname: 我的世界的计分板名称
@ -502,19 +514,17 @@ class midiConvert:
maxscore = max(maxscore, nowscore) maxscore = max(maxscore, nowscore)
soundID, _X = self.__Inst2soundIDwithX(instrumentID) soundID, _X = self.__Inst2soundIDwithX(instrumentID)
singleTrack.append( singleTrack.append(
"execute @a[scores={" "execute @a[scores={" +
+ str(scoreboardname) str(scoreboardname) +
+ "=" "=" +
+ str(nowscore) str(nowscore) +
+ "}" "}" +
+ f"] ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ {msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}" f"] ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ {msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}")
)
commands += 1 commands += 1
if len(singleTrack) != 0: if len(singleTrack) != 0:
tracks.append(singleTrack) tracks.append(singleTrack)
return tracks, commands, maxscore return [tracks, commands, maxscore]
def _toCmdList_withDelay_m1( def _toCmdList_withDelay_m1(
self, self,
@ -538,7 +548,7 @@ class midiConvert:
if volume <= 0: if volume <= 0:
volume = 0.001 volume = 0.001
if isMixedWithPrograssBar == True: if isMixedWithPrograssBar:
isMixedWithPrograssBar = ( isMixedWithPrograssBar = (
r"%%N [ %%s/%^s %%% __________ %%t|%^t ]", r"%%N [ %%s/%^s %%% __________ %%t|%^t ]",
("§e=§r", "§7=§r"), ("§e=§r", "§7=§r"),
@ -567,10 +577,9 @@ class midiConvert:
tracks[nowtick].append( tracks[nowtick].append(
f"execute {player} ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ {msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}" f"execute {player} ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ {msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}"
) )
except: except BaseException:
tracks[nowtick] = [ tracks[nowtick] = [
f"execute {player} ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ {msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}", f"execute {player} ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ {msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}", ]
]
allticks = list(tracks.keys()) allticks = list(tracks.keys())
@ -592,8 +601,11 @@ class midiConvert:
""" """
def __replace( def __replace(
s: str, tobeReplaced: str, replaceWith: str, times: int, other: str s: str,
): tobeReplaced: str,
replaceWith: str,
times: int,
other: str):
if times == 0: if times == 0:
return s.replace(tobeReplaced, other) return s.replace(tobeReplaced, other)
if times == s.count(tobeReplaced): if times == s.count(tobeReplaced):
@ -656,15 +668,15 @@ class midiConvert:
if isMixedWithPrograssBar: if isMixedWithPrograssBar:
nowstr = pgsstyle nowstr = pgsstyle
if ids[r"%%s"] == True: if ids[r"%%s"]:
nowstr = nowstr.replace(r"%%s", str(allticks[i] + 1)) nowstr = nowstr.replace(r"%%s", str(allticks[i] + 1))
if ids[r"%%t"] == True: if ids[r"%%t"]:
nowstr = nowstr.replace(r"%%t", self.__score2time(allticks[i] + 1))
if ids[r"%%%"] == True:
nowstr = nowstr.replace( nowstr = nowstr.replace(
r"%%%", r"%%t", self.__score2time(
str(int((allticks[i] + 1) / allticks[-1] * 10000) / 100) + "%", allticks[i] + 1))
) if ids[r"%%%"]:
nowstr = nowstr.replace(r"%%%", str(
int((allticks[i] + 1) / allticks[-1] * 10000) / 100) + "%", )
countof_s = int((allticks[i] + 1) / allticks[-1] * pgblength) countof_s = int((allticks[i] + 1) / allticks[-1] * pgblength)
@ -712,7 +724,8 @@ class midiConvert:
:return 成功与否成功返回(True,True)失败返回(False,str失败原因) :return 成功与否成功返回(True,True)失败返回(False,str失败原因)
""" """
if method == 1: if method == 1:
cmdlist, _a, maxscore = self._toCmdList_m1(scoreboardname, volume, speed) cmdlist, _a, maxscore = self._toCmdList_m1(
scoreboardname, volume, speed)
else: else:
return (False, f"无法找到算法ID{method}对应的转换算法") return (False, f"无法找到算法ID{method}对应的转换算法")
del _a del _a
@ -727,19 +740,17 @@ class midiConvert:
with open( with open(
f"{self.outputPath}/temp/manifest.json", "w", encoding="utf-8" f"{self.outputPath}/temp/manifest.json", "w", encoding="utf-8"
) as f: ) as f:
f.write( f.write('{\n "format_version": 1,\n "header": {\n "description": "' +
'{\n "format_version": 1,\n "header": {\n "description": "' self.midFileName +
+ self.midFileName ' Pack : behavior pack",\n "version": [ 0, 0, 1 ],\n "name": "' +
+ ' Pack : behavior pack",\n "version": [ 0, 0, 1 ],\n "name": "' self.midFileName +
+ self.midFileName 'Pack",\n "uuid": "' +
+ 'Pack",\n "uuid": "' str(uuid.uuid4()) +
+ str(uuid.uuid4()) '"\n },\n "modules": [\n {\n "description": "' +
+ '"\n },\n "modules": [\n {\n "description": "' f"the Player of the Music {self.midFileName}" +
+ f"the Player of the Music {self.midFileName}" '",\n "type": "data",\n "version": [ 0, 0, 1 ],\n "uuid": "' +
+ '",\n "type": "data",\n "version": [ 0, 0, 1 ],\n "uuid": "' str(uuid.uuid4()) +
+ str(uuid.uuid4()) '"\n }\n ]\n}')
+ '"\n }\n ]\n}'
)
else: else:
with open( with open(
f"{self.outputPath}/temp/manifest.json", "r", encoding="utf-8" f"{self.outputPath}/temp/manifest.json", "r", encoding="utf-8"
@ -753,14 +764,14 @@ class midiConvert:
data["modules"][0]["description"] = "None" data["modules"][0]["description"] = "None"
data["modules"][0]["uuid"] = str(uuid.uuid4()) data["modules"][0]["uuid"] = str(uuid.uuid4())
manifest.close() manifest.close()
open(f"{self.outputPath}/temp/manifest.json", "w", encoding="utf-8").write( open(f"{self.outputPath}/temp/manifest.json", "w",
json.dumps(data) encoding="utf-8").write(json.dumps(data))
)
# 将命令列表写入文件 # 将命令列表写入文件
indexfile = open( indexfile = open(
f"{self.outputPath}/temp/functions/index.mcfunction", "w", encoding="utf-8" f"{self.outputPath}/temp/functions/index.mcfunction",
) "w",
encoding="utf-8")
for track in cmdlist: for track in cmdlist:
indexfile.write( indexfile.write(
"function mscplay/track" + str(cmdlist.index(track) + 1) + "\n" "function mscplay/track" + str(cmdlist.index(track) + 1) + "\n"
@ -793,15 +804,17 @@ class midiConvert:
) )
if progressbar: if progressbar:
if progressbar == True: if progressbar:
with open( with open(
f"{self.outputPath}/temp/functions/mscplay/progressShow.mcfunction", f"{self.outputPath}/temp/functions/mscplay/progressShow.mcfunction",
"w", "w",
encoding="utf-8", encoding="utf-8",
) as f: ) as f:
f.writelines( f.writelines(
"\n".join(self.__formProgressBar(maxscore, scoreboardname)) "\n".join(
) self.__formProgressBar(
maxscore,
scoreboardname)))
else: else:
with open( with open(
f"{self.outputPath}/temp/functions/mscplay/progressShow.mcfunction", f"{self.outputPath}/temp/functions/mscplay/progressShow.mcfunction",
@ -819,8 +832,9 @@ class midiConvert:
indexfile.close() indexfile.close()
makeZip( makeZip(
f"{self.outputPath}/temp/", self.outputPath + f"/{self.midFileName}.mcpack" f"{self.outputPath}/temp/",
) self.outputPath +
f"/{self.midFileName}.mcpack")
shutil.rmtree(f"{self.outputPath}/temp/") shutil.rmtree(f"{self.outputPath}/temp/")
@ -902,7 +916,7 @@ class midiConvert:
) )
if progressbar: if progressbar:
if progressbar == True: if progressbar:
commands += self.__formProgressBar(maxScore, scoreboardname) commands += self.__formProgressBar(maxScore, scoreboardname)
else: else:
commands += self.__formProgressBar( commands += self.__formProgressBar(
@ -934,7 +948,8 @@ class midiConvert:
nowy += 1 if yforward else -1 nowy += 1 if yforward else -1
if ((nowy > maxheight) and (yforward)) or ((nowy < 0) and (not yforward)): if ((nowy > maxheight) and (yforward)) or (
(nowy < 0) and (not yforward)):
nowy -= 1 if yforward else -1 nowy -= 1 if yforward else -1
yforward = not yforward yforward = not yforward
@ -984,7 +999,8 @@ class midiConvert:
""" """
if method == 1: if method == 1:
cmdlist = self._toCmdList_withDelay_m1(volume, speed, player, progressbar) cmdlist = self._toCmdList_withDelay_m1(
volume, speed, player, progressbar)
else: else:
return (False, f"无法找到算法ID {method} 对应的转换算法") return (False, f"无法找到算法ID {method} 对应的转换算法")
@ -1044,7 +1060,8 @@ class midiConvert:
nowy += 1 if yforward else -1 nowy += 1 if yforward else -1
if ((nowy > maxheight) and (yforward)) or ((nowy < 0) and (not yforward)): if ((nowy > maxheight) and (yforward)) or (
(nowy < 0) and (not yforward)):
nowy -= 1 if yforward else -1 nowy -= 1 if yforward else -1
yforward = not yforward yforward = not yforward