From cd73a0a1232e474db8805498c774553e2494fb0a Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Fri, 16 Dec 2022 13:31:16 +0100 Subject: [PATCH] working on update checking --- Project.xml | 4 +- buildnumber.txt | 2 +- source/funkin/github/GitHub.hx | 60 ++++++++++- source/funkin/github/GitHubAsset.hx | 19 ++++ source/funkin/github/GitHubException.hx | 15 +++ source/funkin/github/GitHubReactions.hx | 13 +++ source/funkin/github/GitHubRelease.hx | 62 +++++++++++ source/funkin/github/GitHubUser.hx | 132 ++++++++++++++++++++++++ source/funkin/menus/TitleState.hx | 35 ++++--- source/funkin/system/UpdateUtil.hx | 29 ++++++ 10 files changed, 356 insertions(+), 15 deletions(-) create mode 100644 source/funkin/github/GitHubAsset.hx create mode 100644 source/funkin/github/GitHubException.hx create mode 100644 source/funkin/github/GitHubReactions.hx create mode 100644 source/funkin/github/GitHubRelease.hx create mode 100644 source/funkin/github/GitHubUser.hx create mode 100644 source/funkin/system/UpdateUtil.hx diff --git a/Project.xml b/Project.xml index 24300d4a..9d68851b 100644 --- a/Project.xml +++ b/Project.xml @@ -114,11 +114,13 @@ - + + + diff --git a/buildnumber.txt b/buildnumber.txt index 3e09715f..029aeb48 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -1472 \ No newline at end of file +1474 \ No newline at end of file diff --git a/source/funkin/github/GitHub.hx b/source/funkin/github/GitHub.hx index 6871092b..d8067cdc 100644 --- a/source/funkin/github/GitHub.hx +++ b/source/funkin/github/GitHub.hx @@ -1,5 +1,63 @@ package funkin.github; -class GitHub { +import haxe.Json; +import haxe.Exception; +import haxe.Http; +// TODO: Document further and perhaps make this a Haxelib. +class GitHub { + /** + * Gets all the releases from a specific GitHub repository using the GitHub API. + * @param user + * @param repository + * @return Releases + */ + public static function getReleases(user:String, repository:String, ?onError:Exception->Void):Array { + try { + var url = 'https://api.github.com/repos/${user}/${repository}/releases'; + + var data = Json.parse(__requestOnGitHubServers(url)); + if (!(data is Array)) + throw __parseGitHubException(data); + + return data; + } catch(e) { + if (onError != null) + onError(e); + } + return []; + } + + /** + * Filters all releases gotten by `getReleases` + * @param releases Releases + * @param keepPrereleases Whenever to keep Pre-Releases. + * @param keepDrafts Whenever to keep Drafts. + * @return Filtered releases. + */ + public static inline function filterReleases(releases:Array, keepPrereleases:Bool = true, keepDrafts:Bool = false) + return [for(release in releases) if (release != null && (!release.prerelease || (release.prerelease && keepPrereleases)) && (!release.draft || (release.draft && keepDrafts))) release]; + + private static function __requestOnGitHubServers(url:String) { + var h = new Http(url); + h.setHeader("User-Agent", "request"); + var r = null; + h.onData = function(d) { + r = d; + } + h.onError = function(e) { + throw e; + } + h.request(false); + return r; + } + private static function __parseGitHubException(obj:Dynamic):GitHubException { + var msg:String = "(No message)"; + var url:String = "(No API url)"; + if (Reflect.hasField(obj, "message")) + msg = Reflect.field(obj, "message"); + if (Reflect.hasField(obj, "documentation_url")) + url = Reflect.field(obj, "documentation_url"); + return new GitHubException(msg, url); + } } \ No newline at end of file diff --git a/source/funkin/github/GitHubAsset.hx b/source/funkin/github/GitHubAsset.hx new file mode 100644 index 00000000..4f85d427 --- /dev/null +++ b/source/funkin/github/GitHubAsset.hx @@ -0,0 +1,19 @@ +package funkin.github; + +import funkin.github.GitHubUser; + +typedef GitHubAsset = { + var url:String; + var id:Int; + var node_id:String; + var name:String; + var label:String; + var uploader:GitHubUser; + var content_type:String; + var state:String; + var size:UInt; + var download_count:Int; + var created_at:String; + var updated_at:String; + var browser_download_url:String; +} \ No newline at end of file diff --git a/source/funkin/github/GitHubException.hx b/source/funkin/github/GitHubException.hx new file mode 100644 index 00000000..5a514260 --- /dev/null +++ b/source/funkin/github/GitHubException.hx @@ -0,0 +1,15 @@ +package funkin.github; + +import haxe.Exception; + +class GitHubException extends Exception { + public var apiMessage:String; + + public var documentationUrl:String; + + public function new(apiMessage:String, documentationUrl:String) { + super('[GitHubException] ${apiMessage} (Check ${documentationUrl})'); + this.apiMessage = apiMessage; + this.documentationUrl = documentationUrl; + } +} \ No newline at end of file diff --git a/source/funkin/github/GitHubReactions.hx b/source/funkin/github/GitHubReactions.hx new file mode 100644 index 00000000..cdcb3dfe --- /dev/null +++ b/source/funkin/github/GitHubReactions.hx @@ -0,0 +1,13 @@ +package funkin.github; + +typedef GitHubReactions = { + var url:String; + var total_count:Int; + // +1 and -1 cant be added, you need to use Reflect.field + var laugh:Int; + var hooray:Int; + var confused:Int; + var heart:Int; + var rocket:Int; + var eyes:Int; +} \ No newline at end of file diff --git a/source/funkin/github/GitHubRelease.hx b/source/funkin/github/GitHubRelease.hx new file mode 100644 index 00000000..ea687403 --- /dev/null +++ b/source/funkin/github/GitHubRelease.hx @@ -0,0 +1,62 @@ +package funkin.github; + +typedef GitHubRelease = { + /** + * Body of the GitHub request (Markdown) + */ + var body:String; + + /** + * Url of the release (GitHub API) + */ + var url:String; + + /** + * Url for the assets JSON. Also accessible via `GitHubRelease.assets` + */ + var assets_url:String; + + /** + * Template URL for asset download link. + */ + var upload_url:String; + + /** + * Link to the release on the GitHub website. + */ + var html_url:String; + + /** + * ID of the release. + */ + var id:Int; + + /** + * Author of the release + */ + var author:GitHubUser; + + var node_id:String; + + var tag_name:String; + + var target_commitish:String; + + var name:String; + + var draft:Bool; + + var prerelease:Bool; + + var created_at:String; + + var published_at:String; + + var assets:Array; + + var tarball_url:String; + + var zipball_url:String; + + var reactions:GitHubReactions; +} \ No newline at end of file diff --git a/source/funkin/github/GitHubUser.hx b/source/funkin/github/GitHubUser.hx new file mode 100644 index 00000000..5705af83 --- /dev/null +++ b/source/funkin/github/GitHubUser.hx @@ -0,0 +1,132 @@ +package funkin.github; + + +typedef GitHubUser = { + /** + * Username of the user. + */ + var login:String; + + /** + * ID of the user. + */ + var id:Int; + + /** + * ID of the current node on the GitHub database. + */ + var node_id:String; + + /** + * Link to the avatar (profile picture). + */ + var avatar_url:String; + + /** + * Unknown + */ + var gravatar_id:String; + + /** + * URL to the user on GitHub's servers. + */ + var url:String; + + /** + * URL to the user on GitHub's website. + */ + var html_url:String; + + /** + * URL on GitHub's API to access this user's followers. + */ + var followers_url:String; + + /** + * URL on GitHub's API to access the accounts this user is following. + */ + var following_url:String; + + /** + * URL on GitHub's API to access this user's gists. + */ + var gists_url:String; + + /** + * URL on GitHub's API to access this user's starred repositories. + */ + var starred_url:String; + + // NOT COMPLETE: MISSING repos_url, organizations_url, subscriptions_url, events_url, received_events_url. + + /** + * Type of the user. + */ + var type:GitHubUserType; + + /** + * Whenever the user is a GitHub administrator. + */ + var site_admin:Bool; + + /** + * Name of the user. + */ + var name:String; + + /** + * The company this user belongs to. Can be `null`. + */ + var company:String; + + var blog:String; + + var location:String; + + var email:String; + + var hireable:Null; + + var bio:String; + + /** + * Twitter username of the user. Can be null. + */ + var twitter_username:String; + + /** + * Number of public repos this user own. + */ + var public_repos:Int; + + /** + * Number of public gists this user own. + */ + var public_gists:Int; + + /** + * Number of followers this user have + */ + var followers:Int; + + /** + * Number of accounts this user follows. + */ + var following:Int; + + /** + * Date of creation of the account + */ + var created_at:String; + + /** + * Date of last account update. + */ + var updated_at:String; +} + +@:enum +abstract GitHubUserType(String) { + var USER = "User"; + var ORGANIZATION = "Organization"; +} \ No newline at end of file diff --git a/source/funkin/menus/TitleState.hx b/source/funkin/menus/TitleState.hx index 822380ec..b4734867 100644 --- a/source/funkin/menus/TitleState.hx +++ b/source/funkin/menus/TitleState.hx @@ -1,5 +1,7 @@ package funkin.menus; +import funkin.system.UpdateUtil; +import funkin.github.GitHub; import funkin.system.MusicBeatGroup; import funkin.system.XMLUtil; import flixel.util.typeLimit.OneOfThree; @@ -154,18 +156,7 @@ class TitleState extends MusicBeatState if (pressedEnter && !transitioning && skippedIntro) { - titleText.animation.play('press'); - - FlxG.camera.flash(FlxColor.WHITE, 1); - CoolUtil.playMenuSFX(1, 0.7); - - transitioning = true; - // FlxG.sound.music.stop(); - - new FlxTimer().start(2, function(tmr:FlxTimer) - { - FlxG.switchState(new MainMenuState()); - }); + pressEnter(); } if (pressedEnter && !skippedIntro) @@ -174,6 +165,26 @@ class TitleState extends MusicBeatState super.update(elapsed); } + public function pressEnter() { + titleText.animation.play('press'); + + FlxG.camera.flash(FlxColor.WHITE, 1); + CoolUtil.playMenuSFX(1, 0.7); + + transitioning = true; + // FlxG.sound.music.stop(); + + new FlxTimer().start(2, function(tmr:FlxTimer) + { + /* + UpdateUtil.checkForUpdates(function(newUpdate) { + + }); + */ + FlxG.switchState(new MainMenuState()); + }); + } + public function createCoolText(textArray:Array) { for (i=>text in textArray) diff --git a/source/funkin/system/UpdateUtil.hx b/source/funkin/system/UpdateUtil.hx new file mode 100644 index 00000000..c808b8ff --- /dev/null +++ b/source/funkin/system/UpdateUtil.hx @@ -0,0 +1,29 @@ +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