Skip to content

Commit

Permalink
Clean up ChartTools, temporary Legacy Events fix
Browse files Browse the repository at this point in the history
Yeah!
  • Loading branch information
BombasticTom committed May 26, 2024
1 parent 5fdbe09 commit 5925634
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Weekend1/
vs-skippa-psychengine/

## Files
**/test-noteconv.py
CodeTest.py
fnf-porter.code-workspace
Thumbs.db
Expand Down
12 changes: 11 additions & 1 deletion psychtobase/src/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import time
from src import Constants
from re import sub

def getRuntime(start:float) -> float:
return start - time.time()
Expand Down Expand Up @@ -68,4 +69,13 @@ def coolText(text:str) -> str:
length += len(text) % 2

text = " " * ((length - len(text)) // 2) + text
return "\n" + "=" * length + f"\n{text}\n" + "=" * length
return "\n" + "=" * length + f"\n{text}\n" + "=" * length

def formatToSongPath(name:str) -> str:
invalidChars = r'[~&\\;:<>#]'
hideChars = r'[.,\'"%?!]'

name = name.replace(" ", "-").lower()
name = sub(invalidChars, '-', name)

return sub(hideChars, '', name).strip("-")
84 changes: 47 additions & 37 deletions psychtobase/src/tools/ChartTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,34 @@
import logging
from pathlib import Path
from src.Paths import Paths
from copy import deepcopy

class ChartObject:
"""
A convenient way to store chart metadata.
Args:
path (str): The path where the song's chart data is stored.
output (str): The path where you want to save the song.
"""
def __init__(self, path: str, output:str) -> None:
self.songPath = Path(path)
self.songNameRaw:str = self.songPath.name
self.songNamePath = self.songNameRaw.replace(' ', '-').lower()
self.outputpath = Path(output)
self.savePath = Path(output)

self.startingBpm = 0
self.songFile = self.songPath.name
self.songName = self.songFile.replace("-", " ")

self.startingBpm = 0
self.sections = []

self.difficulties:list = []
self.metadata:dict = Constants.BASE_CHART_METADATA.copy()
self.metadata:dict = deepcopy(Constants.BASE_CHART_METADATA)
self.charts:dict = {}
self.difficulties:list = []

self.chart:dict = Constants.BASE_CHART.copy()
self.chart:dict = deepcopy(Constants.BASE_CHART)

self.initCharts()

try:
self.setMetadata()
except:
Expand All @@ -35,25 +38,24 @@ def __init__(self, path: str, output:str) -> None:
logging.info(f"Chart for {self.metadata.get('songName')} was created!")

def initCharts(self):
logging.info(f"Initialising charts for {self.songNameRaw}...")
logging.info(f"Initialising charts for {self.songName}...")

charts = self.charts

difficulties = self.difficulties
unorderedDiffs = set()

dirFiles = list(self.songPath.iterdir())
chartFiles = []
for _f in dirFiles:
if not _f.suffix == ".json":
continue
if _f.name.endswith("events.json"):
logging.warn(f'[{self.songNameRaw}] events.json not supported yet! Sorry!')
continue
for file in self.songPath.iterdir():

chartFiles.append(_f)
if file.suffix == ".json":
if file.stem == "events":
# If file is events.json: skip
logging.warn(f'[{self.songFile}] events.json not supported yet! Sorry!')
continue
else:
# If file isn't json: skip
continue

for file in chartFiles:
fileName = file.stem
nameSplit = fileName.split("-")
nameLength = len(nameSplit)
Expand All @@ -62,7 +64,7 @@ def initCharts(self):

if nameLength > 2:
difficulty = nameSplit[-1]
elif nameLength > 1 and fileName != self.songNameRaw:
elif nameLength > 1 and fileName != self.songFile:
difficulty = nameSplit[1]

filePath = self.songPath / fileName
Expand Down Expand Up @@ -96,7 +98,7 @@ def setMetadata(self):
playData = metadata["playData"]
characters = playData["characters"]

metadata["songName"] = sampleChart.get("song").replace("-", " ").title()
self.songName = metadata["songName"] = sampleChart.get("song").replace("-", " ").title()
metadata["artist"] = 'Unknown Artist'

logging.info(f"Initialising metadata for {self.metadata.get('songName')}...")
Expand All @@ -113,19 +115,18 @@ def setMetadata(self):

def convert(self):
logging.info(f"Chart conversion for {self.metadata.get('songName')} started!")

prevMustHit = self.sampleChart["notes"][0].get("mustHitSection", True)
prevTime = 0
self.chart["events"] = [Utils.focusCamera(0, prevMustHit)]
events = self.chart["events"]

firstChart = True
events = self.chart["events"]
events.append(Utils.focusCamera(0, prevMustHit))

for diff, cChart in self.charts.items():
for i, (diff, cChart) in enumerate(self.charts.items()):
# cChart - convert Chart
self.chart["scrollSpeed"][diff] = cChart.get("speed")
self.chart["notes"][diff] = []
notes = self.chart["notes"][diff] = []

notes = self.chart["notes"][diff]
steps = 0

prev_notes = set()
Expand All @@ -140,6 +141,10 @@ def convert(self):
noteData = note[1]
length = note[2]

if noteData < 0: # Event notes (not yet supported, simply skipping them to keep the chart valid)
logging.warn(f'Tried converting legacy event "{length}". Legacy events are currently not supported. Sorry!')
continue

if not mustHit:
noteData = (noteData + 4) % 8 # We're shifting the notes! Basic arithmetic operations 🤓

Expand All @@ -155,13 +160,16 @@ def convert(self):
if is_duplicate:
total_duplicates += 1
continue

prev_notes.add((strumTime, noteData))

notes.append(Utils.note(noteData, length, strumTime))

if firstChart:
if i == 0:
# Genius
lengthInSteps = section.get("lengthInSteps", section.get("sectionBeats", 4) * 4)
sectionBeats = section.get("sectionBeats", lengthInSteps / 4)

bpm = section.get('bpm', self.startingBpm)
changeBPM = section.get('changeBPM', False)

Expand All @@ -184,22 +192,24 @@ def convert(self):
self.metadata["timeChanges"].append(Utils.timeChange(prevTime, bpm, sectionBeats, sectionBeats, 0, [sectionBeats]*4))
self.stepCrochet = 15000 / bpm
steps = 0

firstChart = False

if total_duplicates > 0:
logging.warn(f"{total_duplicates} duplicate notes detected and removed from '{diff}' chart!")
logging.warn(f"We found {total_duplicates} duplicate notes in '{diff}' difficulty data! Notes were successfully removed.")

logging.info(f"Chart conversion for {self.metadata.get('songName')} was completed!")

def save(self):
folder = Path(Constants.FILE_LOCS.get('CHARTFOLDER')[1]) / self.songNamePath
saveDir = f'{self.outputpath}{folder}'
# In case there were issues in how the Song was previously named, we save it under a new name!
newSongFile = Utils.formatToSongPath(self.songName)

folder = Path(Constants.FILE_LOCS.get('CHARTFOLDER')[1]) / newSongFile
saveDir = f'{self.savePath}{folder}'
files.folderMake(saveDir)

savePath = Paths.join(saveDir, f'{self.songNamePath}-metadata')
Paths.writeJson(savePath, self.metadata, 2)
output = Paths.join(saveDir, f'{self.songFile}-metadata')
Paths.writeJson(output, self.metadata, 2)

savePath = Paths.join(saveDir, f'{self.songNamePath}-chart')
Paths.writeJson(savePath, self.chart, 2)
output = Paths.join(saveDir, f'{newSongFile}-chart')
Paths.writeJson(output, self.chart, 2)

logging.info(f"[{self.songNamePath}] Saving {self.metadata.get('songName')} to {saveDir}")
logging.info(f"[{newSongFile}] Saving {self.songName} to {saveDir}")

0 comments on commit 5925634

Please sign in to comment.