From 957dabc498c76d951c00d1c61354345b631871c2 Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Fri, 16 Dec 2022 18:38:15 +0100 Subject: [PATCH] Update screen (using a temp repo) --- Project.xml | 8 +- buildnumber.txt | 2 +- source/funkin/menus/TitleState.hx | 13 +- source/funkin/system/UpdateUtil.hx | 29 ---- .../funkin/updating/UpdateAvailableScreen.hx | 163 ++++++++++++++++++ source/funkin/updating/UpdateScreen.hx | 17 ++ source/funkin/updating/UpdateUtil.hx | 98 +++++++++++ source/funkin/updating/Updater.hx | 52 ++++++ update.bat | 3 +- 9 files changed, 346 insertions(+), 39 deletions(-) delete mode 100644 source/funkin/system/UpdateUtil.hx create mode 100644 source/funkin/updating/UpdateAvailableScreen.hx create mode 100644 source/funkin/updating/UpdateScreen.hx create mode 100644 source/funkin/updating/UpdateUtil.hx create mode 100644 source/funkin/updating/Updater.hx diff --git a/Project.xml b/Project.xml index 9d68851b..143b6b18 100644 --- a/Project.xml +++ b/Project.xml @@ -95,6 +95,9 @@ + + + @@ -117,9 +120,10 @@ + + - @@ -129,6 +133,8 @@ + + diff --git a/buildnumber.txt b/buildnumber.txt index 029aeb48..ae6f1163 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -1474 \ No newline at end of file +1507 \ No newline at end of file diff --git a/source/funkin/menus/TitleState.hx b/source/funkin/menus/TitleState.hx index b4734867..58b685b5 100644 --- a/source/funkin/menus/TitleState.hx +++ b/source/funkin/menus/TitleState.hx @@ -1,6 +1,5 @@ package funkin.menus; -import funkin.system.UpdateUtil; import funkin.github.GitHub; import funkin.system.MusicBeatGroup; import funkin.system.XMLUtil; @@ -176,12 +175,12 @@ class TitleState extends MusicBeatState new FlxTimer().start(2, function(tmr:FlxTimer) { - /* - UpdateUtil.checkForUpdates(function(newUpdate) { - - }); - */ - FlxG.switchState(new MainMenuState()); + var report = funkin.updating.UpdateUtil.checkForUpdates(); + if (report.newUpdate) { + FlxG.switchState(new funkin.updating.UpdateScreen(report)); + } else { + FlxG.switchState(new MainMenuState()); + } }); } diff --git a/source/funkin/system/UpdateUtil.hx b/source/funkin/system/UpdateUtil.hx deleted file mode 100644 index c808b8ff..00000000 --- a/source/funkin/system/UpdateUtil.hx +++ /dev/null @@ -1,29 +0,0 @@ -package funkin.system; - -import funkin.github.GitHub; -import lime.app.Application; - -class UpdateUtil { - - function checkForUpdates(callback:Bool->Void) { - var curTag = 'v${Application.current.meta.get('version')}'; - trace(curTag); - - var error = false; - var releases = GitHub.getReleases("YoshiCrafter29", "YoshiCrafterEngine", function(e) { - error = true; - callback(false); - }); - if (error) return; - } -} - -typedef UpdateCheckCallback = { - var success:Bool; - - var newUpdate:Bool; - - var currentVersion:String; - - var newVersion:String; -} \ No newline at end of file diff --git a/source/funkin/updating/UpdateAvailableScreen.hx b/source/funkin/updating/UpdateAvailableScreen.hx new file mode 100644 index 00000000..feb1e4c2 --- /dev/null +++ b/source/funkin/updating/UpdateAvailableScreen.hx @@ -0,0 +1,163 @@ +package funkin.updating; + +import funkin.menus.MainMenuState; +import flixel.math.FlxPoint; +import flixel.math.FlxMath; +import flixel.FlxSprite; +import funkin.updating.UpdateUtil.UpdateCheckCallback; +import funkin.ui.FunkinText; +import flixel.text.FlxText; +import flixel.tweens.FlxEase; +import funkin.ui.Alphabet; +import flixel.addons.transition.FlxTransitionableState; +import flixel.tweens.FlxTween; +import flixel.FlxG; + +class UpdateAvailableScreen extends MusicBeatState { + public var bg:FlxSprite; + + public var versionCheckBG:FlxSprite; + public var title:Alphabet; + public var versionDifferenceLabel:FunkinText; + public var changeLogText:FunkinText; + public var check:UpdateCheckCallback; + + public var optionsBG:FlxSprite; + public var installButton:FunkinText; + public var skipButton:FunkinText; + + public var installSelected:Bool = true; + + public function new(check:UpdateCheckCallback) { + super(); + this.check = check; + } + + public override function create() { + super.create(); + FlxTransitionableState.skipNextTransIn = true; + FlxG.mouse.visible = true; + + FlxG.camera.flash(0xFF000000, 0.25); + + bg = new FlxSprite().loadAnimatedGraphic(Paths.image("menus/menuDesat")); + bg.color = 0xFF181818; + bg.antialiasing = true; + bg.scrollFactor.set(); + bg.screenCenter(); + add(bg); + + title = new Alphabet(0, 10, "NEW UPDATE", true); + title.screenCenter(X); + title.scrollFactor.set(); + + versionDifferenceLabel = new FunkinText(0, title.y + title.height + 10, FlxG.width, '${check.currentVersionTag} < ${check.newVersionTag}', 28, false); + versionDifferenceLabel.color = 0xFFFFFFFF; + versionDifferenceLabel.alignment = CENTER; + versionDifferenceLabel.scrollFactor.set(); + + versionCheckBG = new FlxSprite(-1, -1).makeGraphic(1, 1, 0xFFFFFFFF); + versionCheckBG.alpha = 0.75; + versionCheckBG.color = 0xFF000000; + versionCheckBG.scale.set(FlxG.width + 2, versionDifferenceLabel.y + versionDifferenceLabel.height + 14); + versionCheckBG.updateHitbox(); + versionCheckBG.scrollFactor.set(); + + changeLogText = new FunkinText(0, versionCheckBG.y + versionCheckBG.height + 10, FlxG.width, "", 20, false); + changeLogText.textField.htmlText = Markdown.markdownToHtml(check.updates.last().body.replace("\t", " ")); + changeLogText.applyMarkup( + UpdateUtil.parseWarnings(changeLogText.textField.text), + [ + new FlxTextFormatMarkerPair(new FlxTextFormat(0xFFFFFF00, false, true), ""), + new FlxTextFormatMarkerPair(new FlxTextFormat(0xFFFF0000, false, true), ""), + new FlxTextFormatMarkerPair(new FlxTextFormat(0xFF00FF08, false, true), ""), + new FlxTextFormatMarkerPair(new FlxTextFormat(0xFF7D7D7D, false, true), ""), + ] + ); + + installButton = new FunkinText(0, FlxG.height - 25, Std.int(FlxG.width / 2), "> INSTALL <", 32); + skipButton = new FunkinText(Std.int(FlxG.width / 2), FlxG.height - 25, Std.int(FlxG.width / 2), "SKIP", 32); + + skipButton.y -= skipButton.height; + installButton.y -= installButton.height; + + installButton.alignment = skipButton.alignment = CENTER; + + optionsBG = new FlxSprite(-1, installButton.y - 25).makeGraphic(1, 1, 0xFFFFFFFF); + optionsBG.alpha = 0.75; + optionsBG.color = 0xFF000000; + optionsBG.scale.set(FlxG.width + 2, Std.int(FlxG.height - optionsBG.y)); + optionsBG.updateHitbox(); + + installButton.scrollFactor.set(); + skipButton.scrollFactor.set(); + optionsBG.scrollFactor.set(); + + add(changeLogText); + + add(versionCheckBG); + add(title); + add(versionDifferenceLabel); + + add(optionsBG); + add(installButton); + add(skipButton); + + oldPos = FlxG.mouse.getScreenPosition(); + } + + var destY:Float = 0; + var oldPos:FlxPoint; + + public override function update(elapsed:Float) { + super.update(elapsed); + + destY = FlxMath.bound(destY - (FlxG.mouse.wheel * 40), 0, Math.max(0, changeLogText.height - FlxG.height + versionCheckBG.height + 20 + optionsBG.height)); + FlxG.camera.scroll.y = lerp(FlxG.camera.scroll.y, destY, 1/3); + + if (controls.LEFT_P || controls.RIGHT_P) { + installSelected = !installSelected; + changeSelection(); + } + + var newPos = FlxG.mouse.getScreenPosition(); + if (oldPos.x != newPos.x || oldPos.y != newPos.y) { + if (newPos.y >= optionsBG.y) { + if (installSelected != (installSelected = (newPos.x < (FlxG.width / 2)))) { + changeSelection(); + } + } + oldPos = newPos; + } + + if (controls.ACCEPT || (newPos.y >= optionsBG.y && FlxG.mouse.justPressed)) + select(); + } + + public function select() { + if (installSelected) { + CoolUtil.playMenuSFX(1); + FlxG.switchState(new UpdateScreen(check)); + } else { + CoolUtil.playMenuSFX(2); + FlxG.switchState(new MainMenuState()); + } + } + + + public function changeSelection() { + CoolUtil.playMenuSFX(); + if (installSelected) { + installButton.text = "> INSTALL <"; + skipButton.text = "SKIP"; + } else { + installButton.text = "INSTALL"; + skipButton.text = "> SKIP <"; + } + } + + public override function destroy() { + super.destroy(); + FlxG.mouse.visible = false; + } +} \ No newline at end of file diff --git a/source/funkin/updating/UpdateScreen.hx b/source/funkin/updating/UpdateScreen.hx new file mode 100644 index 00000000..9e3a2112 --- /dev/null +++ b/source/funkin/updating/UpdateScreen.hx @@ -0,0 +1,17 @@ +package funkin.updating; + +import funkin.updating.UpdateUtil.UpdateCheckCallback; + +class UpdateScreen extends MusicBeatState { + public var updater:Updater; + + public function new(check:UpdateCheckCallback) { + super(false); + updater = new AsyncUpdater(check); + } + + public override function create() { + super.create(); + Main.execAsync(installUpdates); + } +} \ No newline at end of file diff --git a/source/funkin/updating/UpdateUtil.hx b/source/funkin/updating/UpdateUtil.hx new file mode 100644 index 00000000..67745101 --- /dev/null +++ b/source/funkin/updating/UpdateUtil.hx @@ -0,0 +1,98 @@ +package funkin.updating; + +import funkin.github.GitHubRelease; +import funkin.github.GitHub; +import lime.app.Application; + +using funkin.github.GitHub; + +class UpdateUtil { + public static function checkForUpdates():UpdateCheckCallback { + var curTag = 'v${Application.current.meta.get('version')}'; + trace(curTag); + + var error = false; + + // TODO: replace "CodenameTestRepo" by "CodenameEngine" once done. + + var newUpdates = __doReleaseFiltering(GitHub.getReleases("YoshiCrafter29", "CodenameTestRepo", function(e) { + error = true; + }), curTag); + + if (error) return { + success: false, + newUpdate: false + }; + + if (newUpdates.length <= 0) { + return { + success: true, + newUpdate: false + }; + } + + return { + success: true, + newUpdate: true, + currentVersionTag: curTag, + newVersionTag: newUpdates.last().tag_name, + updates: newUpdates + }; + } + + static var __curVersionPos = 0; + static function __doReleaseFiltering(releases:Array, currentVersionTag:String) { + releases = releases.filterReleases(false, false); + if (releases.length <= 0) + return releases; + + var newArray:Array = []; + + for(index in 0...releases.length) { + var i = releases.length - 1 - index; + + var release = releases[i]; + if (release.tag_name == currentVersionTag) { + __curVersionPos = index; + } + newArray.push(release); + } + + return newArray.length <= 0 ? newArray : newArray.splice(__curVersionPos+1, newArray.length-(__curVersionPos+1)); + } + + /** + * Replaces all `⚠ TEXT` into `/!\ TEXT` + * @param text + */ + public static function parseWarnings(text:String) { + text = parseEmote(text, "⚠", "(!)", ""); + text = parseEmote(text, "❌", "(X)", ""); + text = parseEmote(text, "✔", "(+)", ""); + text = parseEmote(text, "\n> ", "\n| ", ""); + return text; + } + + public static function parseEmote(text:String, emote:String, beginning:String, end:String) { + var index:Int; + while((index = text.indexOf(emote)) >= 0) { + var nextIndex = text.indexOf("\n", index + emote.length); + if (nextIndex < 0) + nextIndex = text.length; + text = text.substr(0, index) + beginning + text.substring(index + emote.length, nextIndex) + end + text.substring(nextIndex); + } + return text; + } +} + +typedef UpdateCheckCallback = { + var success:Bool; + + var newUpdate:Bool; + + @:optional var currentVersionTag:String; + + @:optional var newVersionTag:String; + + @:optional var updates:Array; +} \ No newline at end of file diff --git a/source/funkin/updating/Updater.hx b/source/funkin/updating/Updater.hx new file mode 100644 index 00000000..7d45e04f --- /dev/null +++ b/source/funkin/updating/Updater.hx @@ -0,0 +1,52 @@ +package funkin.updating; + +import sys.FileSystem; +import funkin.github.GitHubRelease; + +class AsyncUpdater { + // NON ASYNC STUFF + #if REGION + public function new(releases:Array) { + this.releases = releases; + } + + public function execute() { + Main.execAsync(installUpdates); + } + #end + + public var releases:Array; + public var progress:UpdaterProgress = new UpdaterProgress(); + public var path:String; + + public function installUpdates() { + prepareInstallationEnvironment(); + + } + + public function prepareInstallationEnvironment() { + progress.step = PREPARING; + + #if windows + path = '${Sys.getEnv("TEMP")}\\Codename Engine\\Updater\\'; + #else + path = '.\\.temp\\'; + #end + + FileSystem.createDirectory(path); + } +} + +class UpdaterProgress { + public var step:UpdaterStep = PREPARING; + + public function new() {} +} + +@:enum +abstract UpdaterStep(Int) { + var PREPARING = 0; + var DOWNLOADING_ASSETS = 1; + var DOWNLOADING_EXECUTABLE = 2; + var INSTALLING = 3; +} \ No newline at end of file diff --git a/update.bat b/update.bat index c336a351..a74344ed 100644 --- a/update.bat +++ b/update.bat @@ -2,4 +2,5 @@ haxelib git linc_discord-rpc https://github.com/Aidan63/linc_discord-rpc haxelib git flixel-yc29 https://www.github.com/YoshiCrafter29/flixel haxelib git hscript-improved https://www.github.com/YoshiCrafter29/hscript-improved haxelib install away3d -haxelib install hxCodec \ No newline at end of file +haxelib install hxCodec +haxelib install markdown \ No newline at end of file