From 7512c87425b8b3c0063779b3b8242d58c29ce1f5 Mon Sep 17 00:00:00 2001 From: KadeDeveloper Date: Tue, 20 Jul 2021 02:08:39 -0700 Subject: [PATCH] Fix SM replays --- source/LoadReplayState.hx | 57 +++++++++++++++++++++++++++++++++++++-- source/Note.hx | 4 +++ source/Replay.hx | 20 +++++++++++--- source/ResultsScreen.hx | 44 +++++++++++++++++++++++++++--- 4 files changed, 116 insertions(+), 9 deletions(-) diff --git a/source/LoadReplayState.hx b/source/LoadReplayState.hx index 8bbb255..135c5ae 100644 --- a/source/LoadReplayState.hx +++ b/source/LoadReplayState.hx @@ -1,5 +1,12 @@ package; +import haxe.Exception; +import lime.app.Application; + +#if sys +import smTools.SMFile; +import sys.FileSystem; +#end import Controls.KeyboardScheme; import Controls.Control; import flash.text.TextField; @@ -164,9 +171,55 @@ class LoadReplayState extends MusicBeatState case 'philly-nice': songFormat = 'Philly'; } - var poop:String = Highscore.formatSong(songFormat, PlayState.rep.replay.songDiff); + var poop = ""; + + #if sys + if (PlayState.rep.replay.sm) + if (!FileSystem.exists(StringTools.replace(PlayState.rep.replay.chartPath,"converted.json",""))) + { + Application.current.window.alert("The SM file in this replay does not exist!","SM Replays"); + return; + } + #end - PlayState.SONG = Song.loadFromJson(poop, PlayState.rep.replay.songName); + PlayState.isSM = PlayState.rep.replay.sm; + #if sys + if (PlayState.isSM) + PlayState.pathToSm = StringTools.replace(PlayState.rep.replay.chartPath,"converted.json",""); + #end + + #if sys + if (PlayState.isSM) + { + poop = File.getContent(PlayState.rep.replay.chartPath); + try + { + PlayState.sm = SMFile.loadFile(PlayState.pathToSm + "/" + StringTools.replace(PlayState.rep.replay.songName," ", "_") + ".sm"); + } + catch(e:Exception) + { + Application.current.window.alert("Make sure that the SM file is called " + PlayState.pathToSm + "/" + StringTools.replace(PlayState.rep.replay.songName," ", "_") + ".sm!\nAs I couldn't read it.","SM Replays"); + return; + } + } + else + poop = Highscore.formatSong(songFormat, PlayState.rep.replay.songDiff); + #else + poop = Highscore.formatSong(songFormat, PlayState.rep.replay.songDiff); + #end + + try + { + if (PlayState.isSM) + PlayState.SONG = Song.loadFromJsonRAW(poop); + else + PlayState.SONG = Song.loadFromJson(poop, PlayState.rep.replay.songName); + } + catch(e:Exception) + { + Application.current.window.alert("Failed to load the song! Does the JSON exist?","Replays"); + return; + } PlayState.isStoryMode = false; PlayState.storyDifficulty = PlayState.rep.replay.songDiff; PlayState.storyWeek = getWeekNumbFromSong(PlayState.rep.replay.songName); diff --git a/source/Note.hx b/source/Note.hx index e1650ad..0d3607a 100644 --- a/source/Note.hx +++ b/source/Note.hx @@ -84,6 +84,10 @@ class Note extends FlxSprite { this.strumTime = strumTime; rStrumTime = strumTime - (FlxG.save.data.offset + PlayState.songOffset); + #if sys + if (PlayState.isSM) + rStrumTime = rStrumTime - Std.parseFloat(PlayState.sm.header.OFFSET); + #end } diff --git a/source/Replay.hx b/source/Replay.hx index 18ce656..5d88f32 100644 --- a/source/Replay.hx +++ b/source/Replay.hx @@ -45,8 +45,10 @@ typedef ReplayJSON = public var songNotes:Array; public var songJudgements:Array; public var noteSpeed:Float; + public var chartPath:String; public var isDownscroll:Bool; public var sf:Int; + public var sm:Bool; public var ana:Analysis; } @@ -66,6 +68,8 @@ class Replay isDownscroll: false, songNotes: [], replayGameVer: version, + chartPath: "", + sm: false, timestamp: Date.now(), sf: Conductor.safeFrames, ana: new Analysis(), @@ -86,20 +90,28 @@ class Replay public function SaveReplay(notearray:Array, judge:Array, ana:Analysis) { + #if sys + var chartPath = PlayState.isSM ? PlayState.pathToSm + "/converted.json" : ""; + #else + var chartPath = ""; + #end + var json = { "songName": PlayState.SONG.song, "songDiff": PlayState.storyDifficulty, + "chartPath": chartPath, + "sm": PlayState.isSM, + "timestamp": Date.now(), + "replayGameVer": version, + "sf": Conductor.safeFrames, "noteSpeed": (FlxG.save.data.scrollSpeed > 1 ? FlxG.save.data.scrollSpeed : PlayState.SONG.speed), "isDownscroll": FlxG.save.data.downscroll, "songNotes": notearray, "songJudgements": judge, - "timestamp": Date.now(), - "replayGameVer": version, - "sf": Conductor.safeFrames, "ana": ana }; - var data:String = Json.stringify(json); + var data:String = Json.stringify(json, null, ""); var time = Date.now().getTime(); diff --git a/source/ResultsScreen.hx b/source/ResultsScreen.hx index 79ee271..10d5a60 100644 --- a/source/ResultsScreen.hx +++ b/source/ResultsScreen.hx @@ -1,5 +1,10 @@ package; - +import haxe.Exception; +#if sys +import smTools.SMFile; +import sys.FileSystem; +import sys.io.File; +#end import openfl.geom.Matrix; import openfl.display.BitmapData; import flixel.system.FlxSound; @@ -210,6 +215,7 @@ class ResultsScreen extends FlxSubState PlayState.rep = Replay.LoadReplay(PlayState.rep.path); PlayState.loadRep = true; + PlayState.isSM = PlayState.rep.replay.sm; var songFormat = StringTools.replace(PlayState.rep.replay.songName, " ", "-"); switch (songFormat) { @@ -231,11 +237,43 @@ class ResultsScreen extends FlxSubState Highscore.saveCombo(songHighscore, Ratings.GenerateLetterRank(PlayState.instance.accuracy),PlayState.storyDifficulty); #end - var poop:String = Highscore.formatSong(songFormat, PlayState.rep.replay.songDiff); + #if sys + if (PlayState.rep.replay.sm) + if (!FileSystem.exists(StringTools.replace(PlayState.rep.replay.chartPath,"converted.json",""))) + { + Application.current.window.alert("The SM file in this replay does not exist!","SM Replays"); + return; + } + #end + + var poop = ""; + + #if sys + if (PlayState.isSM) + { + poop = File.getContent(PlayState.rep.replay.chartPath); + try + { + PlayState.sm = SMFile.loadFile(PlayState.pathToSm + "/" + StringTools.replace(PlayState.rep.replay.songName," ", "_") + ".sm"); + } + catch(e:Exception) + { + Application.current.window.alert("Make sure that the SM file is called " + PlayState.pathToSm + "/" + StringTools.replace(PlayState.rep.replay.songName," ", "_") + ".sm!\nAs I couldn't read it.","SM Replays"); + return; + } + } + else + poop = Highscore.formatSong(songFormat, PlayState.rep.replay.songDiff); + #else + poop = Highscore.formatSong(PlayState.rep.replay.songName, PlayState.rep.replay.songDiff); + #end music.fadeOut(0.3); - PlayState.SONG = Song.loadFromJson(poop, PlayState.rep.replay.songName); + if (PlayState.isSM) + PlayState.SONG = Song.loadFromJsonRAW(poop); + else + PlayState.SONG = Song.loadFromJson(poop, PlayState.rep.replay.songName); PlayState.isStoryMode = false; PlayState.storyDifficulty = PlayState.rep.replay.songDiff; LoadingState.loadAndSwitchState(new PlayState());