修复mcpack制作过程中的问题

This commit is contained in:
EillesWan 2022-11-20 15:28:09 +08:00
parent 5929ae417d
commit be43e4a92a

View File

@ -30,7 +30,6 @@ Note! Except for this source file, all the files in this repository and this pro
limitations under the License. limitations under the License.
""" """
from operator import index
import os import os
import mido import mido
import brotli import brotli
@ -145,7 +144,7 @@ class midiConvert:
self.midi = mido.MidiFile(self.midiFile) self.midi = mido.MidiFile(self.midiFile)
"""MidiFile对象""" """MidiFile对象"""
except Exception as E: except Exception as E:
raise MidiDestroyedError(E) raise MidiDestroyedError(f"文件{self.midiFile}损坏:{E}")
self.outputPath = os.path.abspath(outputPath) self.outputPath = os.path.abspath(outputPath)
"""输出路径""" """输出路径"""
@ -1034,6 +1033,8 @@ class midiConvert:
:param speed: 速度注意这里的速度指的是播放倍率其原理为在播放音频的时候每个音符的播放时间除以 speed :param speed: 速度注意这里的速度指的是播放倍率其原理为在播放音频的时候每个音符的播放时间除以 speed
:return 成功与否成功返回(True,True)失败返回(False,str失败原因) :return 成功与否成功返回(True,True)失败返回(False,str失败原因)
""" """
# try: # try:
cmdlist, maxlen, maxscore = self.methods[method - 1]( cmdlist, maxlen, maxscore = self.methods[method - 1](
scoreboardname, volume, speed scoreboardname, volume, speed
@ -1041,15 +1042,15 @@ class midiConvert:
# except: # except:
# return (False, f"无法找到算法ID{method}对应的转换算法") # return (False, f"无法找到算法ID{method}对应的转换算法")
# 当文件f夹{self.outputPath}/temp/functions存在时清空其下所有项目若其不存在则创建 # 当文件f夹{self.outputPath}/temp/functions存在时清空其下所有项目然后创建
if os.path.exists(os.path.join(self.outputPath,"/temp/functions/")): if os.path.exists(f"{self.outputPath}/temp/functions/"):
shutil.rmtree(os.path.join(self.outputPath,"/temp/functions/")) shutil.rmtree(f"{self.outputPath}/temp/functions/")
os.makedirs(os.path.join(self.outputPath,"/temp/functions/mscplay")) os.makedirs(f"{self.outputPath}/temp/functions/mscplay")
# 写入manifest.json # 写入manifest.json
if not os.path.exists(os.path.join(self.outputPath,"/temp/manifest.json",)): if not os.path.exists(f"{self.outputPath}/temp/manifest.json"):
with open( with open(
os.path.join(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": "'
@ -1066,7 +1067,7 @@ class midiConvert:
) )
else: else:
with open( with open(
os.path.join(self.outputPath,"/temp/manifest.json",), "r", encoding="utf-8" f"{self.outputPath}/temp/manifest.json", "r", encoding="utf-8"
) as manifest: ) as manifest:
data = json.loads(manifest.read()) data = json.loads(manifest.read())
data["header"][ data["header"][
@ -1077,20 +1078,20 @@ 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(os.path.join(self.outputPath,"/temp/manifest.json",), "w", encoding="utf-8").write( open(f"{self.outputPath}/temp/manifest.json", "w", encoding="utf-8").write(
json.dumps(data) json.dumps(data)
) )
# 将命令列表写入文件 # 将命令列表写入文件
indexfile = open( indexfile = open(
os.path.join(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"
) )
with open( with open(
os.path.join(self.outputPath,f"/temp/functions/mscplay/track{cmdlist.index(track) + 1}.mcfunction",), f"{self.outputPath}/temp/functions/mscplay/track{cmdlist.index(track) + 1}.mcfunction",
"w", "w",
encoding="utf-8", encoding="utf-8",
) as f: ) as f:
@ -1119,7 +1120,7 @@ class midiConvert:
if progressbar: if progressbar:
if progressbar: if progressbar:
with open( with open(
os.path.join(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:
@ -1128,7 +1129,7 @@ class midiConvert:
) )
else: else:
with open( with open(
os.path.join(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:
@ -1143,14 +1144,14 @@ class midiConvert:
indexfile.close() indexfile.close()
makeZip( makeZip(
os.path.join(self.outputPath,"/temp/"), f"{self.outputPath}/temp/", self.outputPath + f"/{self.midFileName}.mcpack"
os.path.join(self.outputPath,f"/{self.midFileName}.mcpack"),
) )
shutil.rmtree(f"{self.outputPath}/temp/") shutil.rmtree(f"{self.outputPath}/temp/")
return (True, f"转换完成,总长度{maxlen}") return (True, f"转换完成,总长度{maxlen}")
def toBDXfile( def toBDXfile(
self, self,
method: int = 1, method: int = 1,