1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 14:12:54 +08:00
osu-lazer/osu.Desktop/Overlays/VersionManager.cs

266 lines
9.6 KiB
C#
Raw Normal View History

2017-02-14 15:13:25 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Diagnostics;
2017-02-14 15:13:25 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
2017-02-21 12:52:30 +08:00
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using Squirrel;
using osu.Framework.Graphics.Shapes;
2017-02-13 19:06:51 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Game.Graphics;
using OpenTK;
using OpenTK.Graphics;
2017-02-18 18:46:47 +08:00
using System.Net.Http;
2017-03-03 17:09:00 +08:00
using osu.Framework.Logging;
using osu.Game;
using osu.Game.Configuration;
namespace osu.Desktop.Overlays
{
public class VersionManager : OverlayContainer
{
private UpdateManager updateManager;
private NotificationOverlay notificationOverlay;
private OsuConfigManager config;
private OsuGameBase game;
2017-02-13 19:06:51 +08:00
public override bool HandleInput => false;
[BackgroundDependencyLoader]
private void load(NotificationOverlay notification, OsuColour colours, TextureStore textures, OsuGameBase game, OsuConfigManager config)
{
notificationOverlay = notification;
this.config = config;
this.game = game;
AutoSizeAxes = Axes.Both;
Anchor = Anchor.BottomCentre;
Origin = Anchor.BottomCentre;
2017-02-13 19:06:51 +08:00
Alpha = 0;
2017-02-13 19:06:51 +08:00
Children = new Drawable[]
{
2017-03-02 02:33:01 +08:00
new FillFlowContainer
2017-02-13 19:06:51 +08:00
{
AutoSizeAxes = Axes.Both,
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Vertical,
2017-02-13 19:06:51 +08:00
Children = new Drawable[]
{
2017-03-02 02:33:01 +08:00
new FillFlowContainer
2017-02-13 19:06:51 +08:00
{
AutoSizeAxes = Axes.Both,
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Horizontal,
2017-03-02 02:33:01 +08:00
Spacing = new Vector2(5),
2017-02-13 19:06:51 +08:00
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Children = new Drawable[]
{
new OsuSpriteText
{
Font = @"Exo2.0-Bold",
Text = game.Name
2017-02-13 19:06:51 +08:00
},
new OsuSpriteText
{
Colour = game.IsDebug ? colours.Red : Color4.White,
Text = game.Version
2017-02-13 19:06:51 +08:00
},
}
},
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
TextSize = 12,
Colour = colours.Yellow,
Font = @"Venera",
2017-03-07 09:59:19 +08:00
Text = @"Development Build"
2017-02-13 19:06:51 +08:00
},
new Sprite
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
2017-02-13 19:06:51 +08:00
Texture = textures.Get(@"Menu/dev-build-footer"),
},
}
}
};
if (game.IsDeployedBuild)
checkForUpdateAsync();
}
protected override void LoadComplete()
{
base.LoadComplete();
var version = game.Version;
var lastVersion = config.Get<string>(OsuSetting.Version);
2017-08-22 21:48:50 +08:00
if (game.IsDeployedBuild && version != lastVersion)
{
config.Set(OsuSetting.Version, version);
// only show a notification if we've previously saved a version to the config file (ie. not the first run).
if (!string.IsNullOrEmpty(lastVersion))
Scheduler.AddDelayed(() => notificationOverlay.Post(new UpdateCompleteNotification(version)), 5000);
}
}
private class UpdateCompleteNotification : SimpleNotification
{
public UpdateCompleteNotification(string version)
{
Text = $"You are now running osu!lazer {version}.\nClick to see what's new!";
Icon = FontAwesome.fa_check_square;
Activated = delegate
{
Process.Start($"https://github.com/ppy/osu/releases/tag/v{version}");
return true;
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
IconBackgound.Colour = colours.BlueDark;
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
updateManager?.Dispose();
}
private async void checkForUpdateAsync(bool useDeltaPatching = true, UpdateProgressNotification notification = null)
{
//should we schedule a retry on completion of this check?
bool scheduleRetry = true;
2017-02-18 02:07:59 +08:00
try
{
if (updateManager == null) updateManager = await UpdateManager.GitHubUpdateManager(@"https://github.com/ppy/osu", @"osulazer", null, null, true);
var info = await updateManager.CheckForUpdate(!useDeltaPatching);
if (info.ReleasesToApply.Count == 0)
//no updates available. bail and retry later.
return;
if (notification == null)
{
notification = new UpdateProgressNotification { State = ProgressNotificationState.Active };
Schedule(() => notificationOverlay.Post(notification));
}
Schedule(() =>
{
notification.Progress = 0;
notification.Text = @"Downloading update...";
});
try
{
await updateManager.DownloadReleases(info.ReleasesToApply, p => Schedule(() => notification.Progress = p / 100f));
Schedule(() =>
{
notification.Progress = 0;
notification.Text = @"Installing update...";
});
await updateManager.ApplyReleases(info, p => Schedule(() => notification.Progress = p / 100f));
Schedule(() => notification.State = ProgressNotificationState.Completed);
}
2017-03-03 17:09:00 +08:00
catch (Exception e)
{
if (useDeltaPatching)
{
2017-03-03 17:09:00 +08:00
Logger.Error(e, @"delta patching failed!");
//could fail if deltas are unavailable for full update path (https://github.com/Squirrel/Squirrel.Windows/issues/959)
//try again without deltas.
checkForUpdateAsync(false, notification);
scheduleRetry = false;
}
2017-03-03 17:09:00 +08:00
else
{
Logger.Error(e, @"update failed!");
}
}
}
catch (HttpRequestException)
{
//likely have no internet connection.
//we'll ignore this and retry later.
}
finally
{
if (scheduleRetry)
{
//check again in 30 minutes.
Scheduler.AddDelayed(() => checkForUpdateAsync(), 60000 * 30);
if (notification != null)
notification.State = ProgressNotificationState.Cancelled;
}
}
}
protected override void PopIn()
{
this.FadeIn(1000);
}
protected override void PopOut()
{
}
2017-03-07 09:59:19 +08:00
private class UpdateProgressNotification : ProgressNotification
{
private OsuGame game;
protected override Notification CreateCompletionNotification() => new ProgressCompletionNotification
{
Text = @"Update ready to install. Click to restart!",
Activated = () =>
{
UpdateManager.RestartAppWhenExited();
game.GracefullyExit();
return true;
}
};
2017-02-21 12:52:30 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours, OsuGame game)
2017-02-21 12:52:30 +08:00
{
this.game = game;
2017-07-11 21:58:06 +08:00
IconContent.AddRange(new Drawable[]
2017-02-21 12:52:30 +08:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
2017-07-23 13:30:50 +08:00
Colour = ColourInfo.GradientVertical(colours.YellowDark, colours.Yellow)
2017-02-21 12:52:30 +08:00
},
new SpriteIcon
2017-02-21 12:52:30 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2017-02-21 12:52:30 +08:00
Icon = FontAwesome.fa_upload,
Colour = Color4.White,
Size = new Vector2(20),
2017-02-21 12:52:30 +08:00
}
});
}
}
}
}