1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Merge branch 'development' into feature/workshop

This commit is contained in:
Robert 2023-08-27 11:48:07 +02:00
commit 23f80895b6
7 changed files with 176 additions and 96 deletions

View File

@ -74,7 +74,8 @@ public class ProfileConfigurationIcon : CorePropertyChanged, IStorageModel
_iconStream?.Dispose(); _iconStream?.Dispose();
_iconStream = new MemoryStream(); _iconStream = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin); if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(_iconStream); stream.CopyTo(_iconStream);
_iconStream.Seek(0, SeekOrigin.Begin); _iconStream.Seek(0, SeekOrigin.Begin);

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Timers; using System.Timers;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -31,8 +32,8 @@ internal class ModuleService : IModuleService
pluginManagementService.PluginFeatureEnabled += PluginManagementServiceOnPluginFeatureEnabled; pluginManagementService.PluginFeatureEnabled += PluginManagementServiceOnPluginFeatureEnabled;
pluginManagementService.PluginFeatureDisabled += PluginManagementServiceOnPluginFeatureDisabled; pluginManagementService.PluginFeatureDisabled += PluginManagementServiceOnPluginFeatureDisabled;
_modules = pluginManagementService.GetFeaturesOfType<Module>().ToList(); _modules = pluginManagementService.GetFeaturesOfType<Module>().ToList();
foreach (Module module in _modules)
ImportDefaultProfiles(module); Task.Run(ImportDefaultProfiles);
} }
protected virtual void OnModuleActivated(ModuleEventArgs e) protected virtual void OnModuleActivated(ModuleEventArgs e)
@ -96,7 +97,7 @@ internal class ModuleService : IModuleService
{ {
if (e.PluginFeature is Module module && !_modules.Contains(module)) if (e.PluginFeature is Module module && !_modules.Contains(module))
{ {
ImportDefaultProfiles(module); Task.Run(() => ImportDefaultProfiles(module));
_modules.Add(module); _modules.Add(module);
} }
} }
@ -111,24 +112,21 @@ internal class ModuleService : IModuleService
} }
} }
private void ImportDefaultProfiles(Module module) private async Task ImportDefaultProfiles()
{
foreach (Module module in _modules)
await ImportDefaultProfiles(module);
}
private async Task ImportDefaultProfiles(Module module)
{ {
try try
{ {
List<ProfileConfiguration> profileConfigurations = _profileService.ProfileCategories.SelectMany(c => c.ProfileConfigurations).ToList();
foreach ((DefaultCategoryName categoryName, string profilePath) in module.DefaultProfilePaths) foreach ((DefaultCategoryName categoryName, string profilePath) in module.DefaultProfilePaths)
{ {
ProfileConfigurationExportModel? profileConfigurationExportModel = ProfileCategory category = _profileService.ProfileCategories.FirstOrDefault(c => c.Name == categoryName.ToString()) ?? _profileService.CreateProfileCategory(categoryName.ToString());
JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(File.ReadAllText(profilePath), IProfileService.ExportSettings); await using FileStream fileStream = File.OpenRead(profilePath);
if (profileConfigurationExportModel?.ProfileEntity == null) await _profileService.ImportProfile(fileStream, category, false, true, null);
throw new ArtemisCoreException($"Default profile at path {profilePath} contains no valid profile data");
if (profileConfigurations.Any(p => p.Entity.ProfileId == profileConfigurationExportModel.ProfileEntity.Id))
continue;
ProfileCategory category = _profileService.ProfileCategories.FirstOrDefault(c => c.Name == categoryName.ToString()) ??
_profileService.CreateProfileCategory(categoryName.ToString());
_profileService.ImportProfile(category, profileConfigurationExportModel, false, true, null);
} }
} }
catch (Exception e) catch (Exception e)

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using SkiaSharp; using SkiaSharp;
@ -118,17 +120,17 @@ public interface IProfileService : IArtemisService
void SaveProfile(Profile profile, bool includeChildren); void SaveProfile(Profile profile, bool includeChildren);
/// <summary> /// <summary>
/// Exports the profile described in the given <see cref="ProfileConfiguration" /> into an export model. /// Exports the profile described in the given <see cref="ProfileConfiguration" /> into a zip archive.
/// </summary> /// </summary>
/// <param name="profileConfiguration">The profile configuration of the profile to export.</param> /// <param name="profileConfiguration">The profile configuration of the profile to export.</param>
/// <returns>The resulting export model.</returns> /// <returns>The resulting zip archive.</returns>
ProfileConfigurationExportModel ExportProfile(ProfileConfiguration profileConfiguration); Task<Stream> ExportProfile(ProfileConfiguration profileConfiguration);
/// <summary> /// <summary>
/// Imports the provided base64 encoded GZIPed JSON as a profile configuration. /// Imports the provided base64 encoded GZIPed JSON as a profile configuration.
/// </summary> /// </summary>
/// <param name="archiveStream">The zip archive containing the profile to import.</param>
/// <param name="category">The <see cref="ProfileCategory" /> in which to import the profile.</param> /// <param name="category">The <see cref="ProfileCategory" /> in which to import the profile.</param>
/// <param name="exportModel">The model containing the profile to import.</param>
/// <param name="makeUnique">Whether or not to give the profile a new GUID, making it unique.</param> /// <param name="makeUnique">Whether or not to give the profile a new GUID, making it unique.</param>
/// <param name="markAsFreshImport"> /// <param name="markAsFreshImport">
/// Whether or not to mark the profile as a fresh import, causing it to be adapted until /// Whether or not to mark the profile as a fresh import, causing it to be adapted until
@ -136,8 +138,7 @@ public interface IProfileService : IArtemisService
/// </param> /// </param>
/// <param name="nameAffix">Text to add after the name of the profile (separated by a dash).</param> /// <param name="nameAffix">Text to add after the name of the profile (separated by a dash).</param>
/// <returns>The resulting profile configuration.</returns> /// <returns>The resulting profile configuration.</returns>
ProfileConfiguration ImportProfile(ProfileCategory category, ProfileConfigurationExportModel exportModel, bool makeUnique = true, bool markAsFreshImport = true, Task<ProfileConfiguration> ImportProfile(Stream archiveStream, ProfileCategory category, bool makeUnique, bool markAsFreshImport, string? nameAffix = "imported");
string? nameAffix = "imported");
/// <summary> /// <summary>
/// Adapts a given profile to the currently active devices. /// Adapts a given profile to the currently active devices.

View File

@ -2,7 +2,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Artemis.Storage.Entities.Profile; using Artemis.Storage.Entities.Profile;
using Artemis.Storage.Repositories.Interfaces; using Artemis.Storage.Repositories.Interfaces;
@ -260,8 +264,7 @@ internal class ProfileService : IProfileService
OnProfileDeactivated(new ProfileConfigurationEventArgs(profileConfiguration)); OnProfileDeactivated(new ProfileConfigurationEventArgs(profileConfiguration));
} }
/// <inheritdoc /> private void RequestDeactivation(ProfileConfiguration profileConfiguration)
public void RequestDeactivation(ProfileConfiguration profileConfiguration)
{ {
if (FocusProfile == profileConfiguration) if (FocusProfile == profileConfiguration)
throw new ArtemisCoreException("Cannot disable a profile that is being edited, that's rude"); throw new ArtemisCoreException("Cannot disable a profile that is being edited, that's rude");
@ -395,35 +398,82 @@ internal class ProfileService : IProfileService
} }
/// <inheritdoc /> /// <inheritdoc />
public ProfileConfigurationExportModel ExportProfile(ProfileConfiguration profileConfiguration) public async Task<Stream> ExportProfile(ProfileConfiguration profileConfiguration)
{ {
// The profile may not be active and in that case lets activate it real quick ProfileEntity? profileEntity = _profileRepository.Get(profileConfiguration.Entity.ProfileId);
Profile profile = profileConfiguration.Profile ?? ActivateProfile(profileConfiguration); if (profileEntity == null)
throw new ArtemisCoreException("Could not locate profile entity");
return new ProfileConfigurationExportModel string configurationJson = JsonConvert.SerializeObject(profileConfiguration.Entity, IProfileService.ExportSettings);
string profileJson = JsonConvert.SerializeObject(profileEntity, IProfileService.ExportSettings);
MemoryStream archiveStream = new();
// Create a ZIP archive
using (ZipArchive archive = new(archiveStream, ZipArchiveMode.Create, true))
{ {
ProfileConfigurationEntity = profileConfiguration.Entity, ZipArchiveEntry configurationEntry = archive.CreateEntry("configuration.json");
ProfileEntity = profile.ProfileEntity, await using (Stream entryStream = configurationEntry.Open())
ProfileImage = profileConfiguration.Icon.GetIconStream() {
}; await entryStream.WriteAsync(Encoding.Default.GetBytes(configurationJson));
}
ZipArchiveEntry profileEntry = archive.CreateEntry("profile.json");
await using (Stream entryStream = profileEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(profileJson));
}
await using Stream? iconStream = profileConfiguration.Icon.GetIconStream();
if (iconStream != null)
{
ZipArchiveEntry iconEntry = archive.CreateEntry("icon.png");
await using Stream entryStream = iconEntry.Open();
await iconStream.CopyToAsync(entryStream);
}
}
archiveStream.Seek(0, SeekOrigin.Begin);
return archiveStream;
} }
/// <inheritdoc /> /// <inheritdoc />
public ProfileConfiguration ImportProfile(ProfileCategory category, ProfileConfigurationExportModel exportModel, public async Task<ProfileConfiguration> ImportProfile(Stream archiveStream, ProfileCategory category, bool makeUnique, bool markAsFreshImport, string? nameAffix)
bool makeUnique, bool markAsFreshImport, string? nameAffix)
{ {
if (exportModel.ProfileEntity == null) using ZipArchive archive = new(archiveStream, ZipArchiveMode.Read, true);
throw new ArtemisCoreException("Cannot import a profile without any data");
// Create a copy of the entity because we'll be using it from now on // There should be a configuration.json and profile.json
ProfileEntity profileEntity = JsonConvert.DeserializeObject<ProfileEntity>( ZipArchiveEntry? configurationEntry = archive.Entries.FirstOrDefault(e => e.Name.EndsWith("configuration.json"));
JsonConvert.SerializeObject(exportModel.ProfileEntity, IProfileService.ExportSettings), ZipArchiveEntry? profileEntry = archive.Entries.FirstOrDefault(e => e.Name.EndsWith("profile.json"));
IProfileService.ExportSettings ZipArchiveEntry? iconEntry = archive.Entries.FirstOrDefault(e => e.Name.EndsWith("icon.png"));
)!;
if (configurationEntry == null)
throw new ArtemisCoreException("Could not import profile, configuration.json missing");
if (profileEntry == null)
throw new ArtemisCoreException("Could not import profile, profile.json missing");
await using Stream configurationStream = configurationEntry.Open();
using StreamReader configurationReader = new(configurationStream);
ProfileConfigurationEntity? configurationEntity = JsonConvert.DeserializeObject<ProfileConfigurationEntity>(await configurationReader.ReadToEndAsync(), IProfileService.ExportSettings);
if (configurationEntity == null)
throw new ArtemisCoreException("Could not import profile, failed to deserialize configuration.json");
await using Stream profileStream = profileEntry.Open();
using StreamReader profileReader = new(profileStream);
ProfileEntity? profileEntity = JsonConvert.DeserializeObject<ProfileEntity>(await profileReader.ReadToEndAsync(), IProfileService.ExportSettings);
if (profileEntity == null)
throw new ArtemisCoreException("Could not import profile, failed to deserialize profile.json");
// Assign a new GUID to make sure it is unique in case of a previous import of the same content // Assign a new GUID to make sure it is unique in case of a previous import of the same content
if (makeUnique) if (makeUnique)
profileEntity.UpdateGuid(Guid.NewGuid()); profileEntity.UpdateGuid(Guid.NewGuid());
else
{
// If the profile already exists and this one is not to be made unique, return the existing profile
ProfileConfiguration? existing = ProfileCategories.SelectMany(c => c.ProfileConfigurations).FirstOrDefault(p => p.ProfileId == profileEntity.Id);
if (existing != null)
return existing;
}
if (nameAffix != null) if (nameAffix != null)
profileEntity.Name = $"{profileEntity.Name} - {nameAffix}"; profileEntity.Name = $"{profileEntity.Name} - {nameAffix}";
@ -435,21 +485,18 @@ internal class ProfileService : IProfileService
else else
throw new ArtemisCoreException($"Cannot import this profile without {nameof(makeUnique)} being true"); throw new ArtemisCoreException($"Cannot import this profile without {nameof(makeUnique)} being true");
ProfileConfiguration profileConfiguration; // A new GUID will be given on save
if (exportModel.ProfileConfigurationEntity != null) configurationEntity.FileIconId = Guid.Empty;
ProfileConfiguration profileConfiguration = new(category, configurationEntity);
if (nameAffix != null)
profileConfiguration.Name = $"{profileConfiguration.Name} - {nameAffix}";
// If an icon was provided, import that as well
if (iconEntry != null)
{ {
ProfileConfigurationEntity profileConfigurationEntity = JsonConvert.DeserializeObject<ProfileConfigurationEntity>( await using Stream iconStream = iconEntry.Open();
JsonConvert.SerializeObject(exportModel.ProfileConfigurationEntity, IProfileService.ExportSettings), IProfileService.ExportSettings profileConfiguration.Icon.SetIconByStream(iconStream);
)!; SaveProfileConfigurationIcon(profileConfiguration);
// A new GUID will be given on save
profileConfigurationEntity.FileIconId = Guid.Empty;
profileConfiguration = new ProfileConfiguration(category, profileConfigurationEntity);
if (nameAffix != null)
profileConfiguration.Name = $"{profileConfiguration.Name} - {nameAffix}";
}
else
{
profileConfiguration = new ProfileConfiguration(category, profileEntity.Name, "Import");
} }
if (exportModel.ProfileImage != null) if (exportModel.ProfileImage != null)

View File

@ -69,7 +69,7 @@ public class MenuBarViewModel : ActivatableViewModelBase
ToggleSuspended = ReactiveCommand.Create(ExecuteToggleSuspended, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null)); ToggleSuspended = ReactiveCommand.Create(ExecuteToggleSuspended, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null));
DeleteProfile = ReactiveCommand.CreateFromTask(ExecuteDeleteProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null)); DeleteProfile = ReactiveCommand.CreateFromTask(ExecuteDeleteProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null));
ExportProfile = ReactiveCommand.CreateFromTask(ExecuteExportProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null)); ExportProfile = ReactiveCommand.CreateFromTask(ExecuteExportProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null));
DuplicateProfile = ReactiveCommand.Create(ExecuteDuplicateProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null)); DuplicateProfile = ReactiveCommand.CreateFromTask(ExecuteDuplicateProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null));
ToggleSuspendedEditing = ReactiveCommand.Create(ExecuteToggleSuspendedEditing); ToggleSuspendedEditing = ReactiveCommand.Create(ExecuteToggleSuspendedEditing);
OpenUri = ReactiveCommand.Create<string>(s => Process.Start(new ProcessStartInfo(s) {UseShellExecute = true, Verb = "open"})); OpenUri = ReactiveCommand.Create<string>(s => Process.Start(new ProcessStartInfo(s) {UseShellExecute = true, Verb = "open"}));
ToggleBooleanSetting = ReactiveCommand.Create<PluginSetting<bool>>(ExecuteToggleBooleanSetting); ToggleBooleanSetting = ReactiveCommand.Create<PluginSetting<bool>>(ExecuteToggleBooleanSetting);
@ -194,32 +194,31 @@ public class MenuBarViewModel : ActivatableViewModelBase
// Might not cover everything but then the dialog will complain and that's good enough // Might not cover everything but then the dialog will complain and that's good enough
string fileName = Path.GetInvalidFileNameChars().Aggregate(ProfileConfiguration.Name, (current, c) => current.Replace(c, '-')); string fileName = Path.GetInvalidFileNameChars().Aggregate(ProfileConfiguration.Name, (current, c) => current.Replace(c, '-'));
string? result = await _windowService.CreateSaveFileDialog() string? result = await _windowService.CreateSaveFileDialog()
.HavingFilter(f => f.WithExtension("json").WithName("Artemis profile")) .HavingFilter(f => f.WithExtension("zip").WithName("Artemis profile"))
.WithInitialFileName(fileName) .WithInitialFileName(fileName)
.ShowAsync(); .ShowAsync();
if (result == null) if (result == null)
return; return;
ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration);
string json = JsonConvert.SerializeObject(export, IProfileService.ExportSettings);
try try
{ {
await File.WriteAllTextAsync(result, json); await using Stream stream = await _profileService.ExportProfile(ProfileConfiguration);
await using FileStream fileStream = File.OpenWrite(result);
await stream.CopyToAsync(fileStream);
} }
catch (Exception e) catch (Exception e)
{ {
_windowService.ShowExceptionDialog("Failed to export profile", e); _windowService.ShowExceptionDialog("Failed to export profile", e);
} }
} }
private void ExecuteDuplicateProfile() private async Task ExecuteDuplicateProfile()
{ {
if (ProfileConfiguration == null) if (ProfileConfiguration == null)
return; return;
await using Stream export = await _profileService.ExportProfile(ProfileConfiguration);
ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration); await _profileService.ImportProfile(export, ProfileConfiguration.Category, true, false, "copy");
_profileService.ImportProfile(ProfileConfiguration.Category, export, true, false, "copy");
} }
private void ExecuteToggleSuspendedEditing() private void ExecuteToggleSuspendedEditing()

View File

@ -1,11 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Reactive; using System.Reactive;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
@ -77,7 +79,7 @@ public class SidebarCategoryViewModel : ActivatableViewModelBase
Observable.FromEventPattern<ProfileConfigurationEventArgs>(x => profileCategory.ProfileConfigurationRemoved += x, x => profileCategory.ProfileConfigurationRemoved -= x) Observable.FromEventPattern<ProfileConfigurationEventArgs>(x => profileCategory.ProfileConfigurationRemoved += x, x => profileCategory.ProfileConfigurationRemoved -= x)
.Subscribe(e => profileConfigurations.RemoveMany(profileConfigurations.Items.Where(c => c == e.EventArgs.ProfileConfiguration))) .Subscribe(e => profileConfigurations.RemoveMany(profileConfigurations.Items.Where(c => c == e.EventArgs.ProfileConfiguration)))
.DisposeWith(d); .DisposeWith(d);
profileConfigurations.Edit(updater => profileConfigurations.Edit(updater =>
{ {
updater.Clear(); updater.Clear();
@ -155,32 +157,32 @@ public class SidebarCategoryViewModel : ActivatableViewModelBase
private async Task ExecuteImportProfile() private async Task ExecuteImportProfile()
{ {
string[]? result = await _windowService.CreateOpenFileDialog() string[]? result = await _windowService.CreateOpenFileDialog()
.HavingFilter(f => f.WithExtension("json").WithName("Artemis profile")) .HavingFilter(f => f.WithExtension("zip").WithExtension("json").WithName("Artemis profile"))
.ShowAsync(); .ShowAsync();
if (result == null) if (result == null)
return; return;
string json = await File.ReadAllTextAsync(result[0]);
ProfileConfigurationExportModel? profileConfigurationExportModel = null;
try try
{ {
profileConfigurationExportModel = JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(json, IProfileService.ExportSettings); // Removing this at some point in the future
} if (result[0].EndsWith("json"))
catch (JsonException e) {
{ ProfileConfigurationExportModel? exportModel = JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(await File.ReadAllTextAsync(result[0]), IProfileService.ExportSettings);
_windowService.ShowExceptionDialog("Import profile failed", e); if (exportModel == null)
} {
await _windowService.ShowConfirmContentDialog("Import profile", "Failed to import this profile, make sure it is a valid Artemis profile.", "Confirm", null);
return;
}
if (profileConfigurationExportModel == null) await using Stream convertedFileStream = await ConvertLegacyExport(exportModel);
{ await _profileService.ImportProfile(convertedFileStream, ProfileCategory, true, true);
await _windowService.ShowConfirmContentDialog("Import profile", "Failed to import this profile, make sure it is a valid Artemis profile.", "Confirm", null); }
return; else
} {
await using FileStream fileStream = File.OpenRead(result[0]);
try await _profileService.ImportProfile(fileStream, ProfileCategory, true, true);
{ }
_profileService.ImportProfile(ProfileCategory, profileConfigurationExportModel);
} }
catch (Exception e) catch (Exception e)
{ {
@ -234,4 +236,38 @@ public class SidebarCategoryViewModel : ActivatableViewModelBase
_profileService.SaveProfileCategory(categories[i]); _profileService.SaveProfileCategory(categories[i]);
} }
} }
private async Task<Stream> ConvertLegacyExport(ProfileConfigurationExportModel exportModel)
{
MemoryStream archiveStream = new();
string configurationJson = JsonConvert.SerializeObject(exportModel.ProfileConfigurationEntity, IProfileService.ExportSettings);
string profileJson = JsonConvert.SerializeObject(exportModel.ProfileEntity, IProfileService.ExportSettings);
// Create a ZIP archive
using (ZipArchive archive = new(archiveStream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry configurationEntry = archive.CreateEntry("configuration.json");
await using (Stream entryStream = configurationEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(configurationJson));
}
ZipArchiveEntry profileEntry = archive.CreateEntry("profile.json");
await using (Stream entryStream = profileEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(profileJson));
}
if (exportModel.ProfileImage != null)
{
ZipArchiveEntry iconEntry = archive.CreateEntry("icon.png");
await using Stream entryStream = iconEntry.Open();
await exportModel.ProfileImage.CopyToAsync(entryStream);
}
}
archiveStream.Seek(0, SeekOrigin.Begin);
return archiveStream;
}
} }

View File

@ -10,8 +10,6 @@ using Artemis.Core.Services;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Artemis.UI.Shared.Routing; using Artemis.UI.Shared.Routing;
using Artemis.UI.Shared.Services; using Artemis.UI.Shared.Services;
using Artemis.UI.Shared.Services.ProfileEditor;
using Newtonsoft.Json;
using ReactiveUI; using ReactiveUI;
namespace Artemis.UI.Screens.Sidebar; namespace Artemis.UI.Screens.Sidebar;
@ -36,7 +34,7 @@ public class SidebarProfileConfigurationViewModel : ActivatableViewModelBase
SuspendAll = ReactiveCommand.Create<string>(ExecuteSuspendAll); SuspendAll = ReactiveCommand.Create<string>(ExecuteSuspendAll);
DeleteProfile = ReactiveCommand.CreateFromTask(ExecuteDeleteProfile); DeleteProfile = ReactiveCommand.CreateFromTask(ExecuteDeleteProfile);
ExportProfile = ReactiveCommand.CreateFromTask(ExecuteExportProfile); ExportProfile = ReactiveCommand.CreateFromTask(ExecuteExportProfile);
DuplicateProfile = ReactiveCommand.Create(ExecuteDuplicateProfile); DuplicateProfile = ReactiveCommand.CreateFromTask(ExecuteDuplicateProfile);
this.WhenActivated(d => _isDisabled = ProfileConfiguration.WhenAnyValue(c => c.Profile) this.WhenActivated(d => _isDisabled = ProfileConfiguration.WhenAnyValue(c => c.Profile)
.Select(p => p == null) .Select(p => p == null)
@ -116,18 +114,18 @@ public class SidebarProfileConfigurationViewModel : ActivatableViewModelBase
// Might not cover everything but then the dialog will complain and that's good enough // Might not cover everything but then the dialog will complain and that's good enough
string fileName = Path.GetInvalidFileNameChars().Aggregate(ProfileConfiguration.Name, (current, c) => current.Replace(c, '-')); string fileName = Path.GetInvalidFileNameChars().Aggregate(ProfileConfiguration.Name, (current, c) => current.Replace(c, '-'));
string? result = await _windowService.CreateSaveFileDialog() string? result = await _windowService.CreateSaveFileDialog()
.HavingFilter(f => f.WithExtension("json").WithName("Artemis profile")) .HavingFilter(f => f.WithExtension("zip").WithName("Artemis profile"))
.WithInitialFileName(fileName) .WithInitialFileName(fileName)
.ShowAsync(); .ShowAsync();
if (result == null) if (result == null)
return; return;
ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration);
string json = JsonConvert.SerializeObject(export, IProfileService.ExportSettings);
try try
{ {
await File.WriteAllTextAsync(result, json); await using Stream stream = await _profileService.ExportProfile(ProfileConfiguration);
await using FileStream fileStream = File.OpenWrite(result);
await stream.CopyToAsync(fileStream);
} }
catch (Exception e) catch (Exception e)
{ {
@ -135,10 +133,10 @@ public class SidebarProfileConfigurationViewModel : ActivatableViewModelBase
} }
} }
private void ExecuteDuplicateProfile() private async Task ExecuteDuplicateProfile()
{ {
ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration); await using Stream export = await _profileService.ExportProfile(ProfileConfiguration);
_profileService.ImportProfile(ProfileConfiguration.Category, export, true, false, "copy"); await _profileService.ImportProfile(export, ProfileConfiguration.Category, true, false, "copy");
} }
public bool Matches(string s) public bool Matches(string s)