Skip to content

Commit

Permalink
feat: show dialog when versions do not match
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Sep 29, 2024
1 parent d528394 commit 4938dbe
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 101 deletions.
21 changes: 10 additions & 11 deletions src/Beutl.Core/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public sealed class Project : Hierarchical, IStorable
private readonly HierarchicalList<ProjectItem> _items;
private readonly Dictionary<string, string> _variables = [];

public const string DefaultMinAppVersion = "1.0.0-preview.9";

static Project()
{
AppVersionProperty = ConfigureProperty<string, Project>(nameof(AppVersion))
Expand All @@ -32,13 +34,13 @@ static Project()

MinAppVersionProperty = ConfigureProperty<string, Project>(nameof(MinAppVersion))
.Accessor(o => o.MinAppVersion)
.DefaultValue("1.0.0-preview1")
.DefaultValue(DefaultMinAppVersion)
.Register();
}

public Project()
{
MinAppVersion = "1.0.0-preview1";
MinAppVersion = DefaultMinAppVersion;
_items = new HierarchicalList<ProjectItem>(this);
_items.CollectionChanged += Items_CollectionChanged;
}
Expand Down Expand Up @@ -103,9 +105,6 @@ public override void Deserialize(ICoreSerializationContext context)
using Activity? activity = BeutlApplication.ActivitySource.StartActivity("Project.Deserialize");
base.Deserialize(context);

AppVersion = context.GetValue<string>("appVersion") ?? AppVersion;
MinAppVersion = context.GetValue<string>("minAppVersion") ?? MinAppVersion;

SyncronizeScenes(context.GetValue<string[]>("items")!);

if (context.GetValue<Dictionary<string, string>>("variables") is { } vars)
Expand All @@ -117,22 +116,22 @@ public override void Deserialize(ICoreSerializationContext context)
}
}

activity?.SetTag("appVersion", AppVersion);
activity?.SetTag("minAppVersion", MinAppVersion);
activity?.SetTag("appVersion", BeutlApplication.Version);
activity?.SetTag("minAppVersion", DefaultMinAppVersion);
activity?.SetTag("itemsCount", Items.Count);
}

public override void Serialize(ICoreSerializationContext context)
{
using Activity? activity = BeutlApplication.ActivitySource.StartActivity("Project.Serialize");
activity?.SetTag("appVersion", AppVersion);
activity?.SetTag("minAppVersion", MinAppVersion);
activity?.SetTag("appVersion", BeutlApplication.Version);
activity?.SetTag("minAppVersion", DefaultMinAppVersion);
activity?.SetTag("itemsCount", Items.Count);

base.Serialize(context);

context.SetValue("appVersion", AppVersion);
context.SetValue("minAppVersion", MinAppVersion);
context.SetValue("appVersion", BeutlApplication.Version);
context.SetValue("minAppVersion", DefaultMinAppVersion);

context.SetValue("items", Items
.Select(item => Path.GetRelativePath(RootDirectory, item.FileName).Replace('\\', '/')));
Expand Down
24 changes: 24 additions & 0 deletions src/Beutl.Language/Message.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 40 additions & 28 deletions src/Beutl.Language/Message.ja.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -309,4 +309,16 @@
<data name="File_is_not_selected" xml:space="preserve">
<value>ファイルが選択されていません</value>
</data>
</root>
<data name="CheckDifferentVersion_Title" xml:space="preserve">
<value>前回と異なるバージョンで起動しています</value>
</data>
<data name="CheckDifferentVersion_Content" xml:space="preserve">
<value>前回のバージョンで開いたファイルを開くと、ファイルが破損する可能性があります。</value>
</data>
<data name="ProjectVersionMismatch_Content" xml:space="preserve">
<value>プロジェクトはBeutlの新しいバージョン(&gt;= {0})で作成されました。プロジェクトを開くには、Beutlをアップデートしてください。</value>
</data>
<data name="ProjectVersionMismatch_Title" xml:space="preserve">
<value>バージョンの不一致</value>
</data>
</root>
68 changes: 40 additions & 28 deletions src/Beutl.Language/Message.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -309,4 +309,16 @@ Please wait a moment.</value>
<data name="File_is_not_selected" xml:space="preserve">
<value>File is not selected</value>
</data>
</root>
<data name="CheckDifferentVersion_Title" xml:space="preserve">
<value>Opening a file that was opened in a previous version may result in file corruption.</value>
</data>
<data name="CheckDifferentVersion_Content" xml:space="preserve">
<value>You are running on a different version than last time.</value>
</data>
<data name="ProjectVersionMismatch_Title" xml:space="preserve">
<value>Version Mismatch</value>
</data>
<data name="ProjectVersionMismatch_Content" xml:space="preserve">
<value>The project was created with a newer version of Beutl (&gt;= {0}). Please update Beutl to open the project.</value>
</data>
</root>
53 changes: 39 additions & 14 deletions src/Beutl/Services/ProjectService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Reactive.Subjects;

using System.Text.Json;
using System.Text.Json.Nodes;
using Beutl.Configuration;
using Beutl.Logging;
using Beutl.Models;
using Beutl.ProjectSystem;

using FluentAvalonia.UI.Controls;
using Microsoft.Extensions.Logging;

using NuGet.Versioning;
using OpenTelemetry.Trace;

using Reactive.Bindings;

namespace Beutl.Services;
Expand All @@ -35,13 +35,43 @@ public ProjectService()

public IReadOnlyReactiveProperty<bool> IsOpened => _isOpened;

public Project? OpenProject(string file)
private static async Task<(NuGetVersion AppVersion, NuGetVersion MinVersion)> GetProjectVersion(string file)
{
using Activity? activity = Telemetry.StartActivity("OpenProject");
await using var stream = File.OpenRead(file);
var node = await JsonNode.ParseAsync(stream);
string? appVersion = (string?)node?["appVersion"];
string? minAppVersion = (string?)node?["minAppVersion"];
if (appVersion == null || minAppVersion == null)
{
throw new InvalidOperationException("The project file does not contain version information.");
}

return (NuGetVersion.Parse(appVersion), NuGetVersion.Parse(minAppVersion));
}

public async Task OpenProject(string file)
{
using Activity? activity = Telemetry.StartActivity();
try
{
CloseProject();

(NuGetVersion appVersion, NuGetVersion minVersion) = await GetProjectVersion(file);
activity?.SetTag(nameof(appVersion), appVersion.ToString());
activity?.SetTag(nameof(minVersion), minVersion.ToString());
if (minVersion > NuGetVersion.Parse(BeutlApplication.Version) &&
!Preferences.Default.Get("ProjectService.SkipVersionCheck", false))
{
var dialog = new ContentDialog
{
Title = Message.ProjectVersionMismatch_Title,
Content = string.Format(Message.ProjectVersionMismatch_Content, minVersion),
PrimaryButtonText = Strings.Close
};
await dialog.ShowAsync();
return;
}

var project = new Project();
project.Restore(file);

Expand All @@ -50,14 +80,12 @@ public ProjectService()
_projectObservable.OnNext((New: project, null));

AddToRecentProjects(file);

return project;
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error);
_logger.LogError(ex, "Unable to open the project.");
return null;
NotificationService.ShowInformation("", Message.CouldNotOpenProject);
}
}

Expand All @@ -74,7 +102,7 @@ public void CloseProject()

public Project? CreateProject(int width, int height, int framerate, int samplerate, string name, string location)
{
using Activity? activity = Telemetry.StartActivity("CreateProject");
using Activity? activity = Telemetry.StartActivity();
activity?.SetTag(nameof(width), width);
activity?.SetTag(nameof(height), height);
activity?.SetTag(nameof(framerate), framerate);
Expand All @@ -88,10 +116,7 @@ public void CloseProject()
ProjectItemContainer.Current.Add(scene);
var project = new Project()
{
Items =
{
scene
},
Items = { scene },
Variables =
{
[ProjectVariableKeys.FrameRate] = framerate.ToString(),
Expand Down
Loading

0 comments on commit 4938dbe

Please sign in to comment.