Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
NickeManarin committed Feb 12, 2022
2 parents 9cfdf56 + e4b3e58 commit 1044044
Show file tree
Hide file tree
Showing 41 changed files with 785 additions and 316 deletions.
30 changes: 30 additions & 0 deletions ScreenToGif.Model/Enums/ApplicationTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace ScreenToGif.Domain.Enums;

public enum ApplicationTypes
{
Unidentified = 0,

/// <summary>
/// Light package (.NET 6 desktop runtime download and installation required).
/// Distributted as a single file, as an EXE.
/// </summary>
DependantSingle = 1,

/// <summary>
/// Full package (.NET 6 desktop runtime included).
/// Distributted as a single file, as an EXE.
/// </summary>
FullSingle = 2,

/// <summary>
/// Full package (.NET 6 desktop runtime included).
/// Distributted as multiple files, as a MSIX for the outside the Store.
/// </summary>
FullMultiMsix = 3,

/// <summary>
/// Full package (.NET 6 desktop runtime included).
/// Distributted as multiple files, as a MSIX for the Store.
/// </summary>
FullMultiMsixStore = 4,
}
8 changes: 8 additions & 0 deletions ScreenToGif.Model/Enums/OverwriteModes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ScreenToGif.Domain.Enums;

public enum OverwriteModes
{
Allow,
Warn,
Prompt
}
2 changes: 1 addition & 1 deletion ScreenToGif.Model/Interfaces/IExportPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IExportPreset : IPreset
string DescriptionKey { get; set; }
ExportFormats Type { get; set; }
bool PickLocation { get; set; }
bool OverwriteOnSave { get; set; }
OverwriteModes OverwriteMode { get; set; }
bool ExportAsProjectToo { get; set; }
bool UploadFile { get; set; }
string UploadService { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion ScreenToGif.Model/ScreenToGif.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<DebugType>embedded</DebugType>
<UseWPF>True</UseWPF>
<Platforms>AnyCPU;ARM64;x64;x86</Platforms>
<Version>2.35.4</Version>
<Version>2.36.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
2 changes: 1 addition & 1 deletion ScreenToGif.Native/ScreenToGif.Native.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<UseWindowsForms>True</UseWindowsForms>
<DebugType>embedded</DebugType>
<Platforms>AnyCPU;ARM64;x64;x86</Platforms>
<Version>2.35.4</Version>
<Version>2.36.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
2 changes: 1 addition & 1 deletion ScreenToGif.Util/Codification/Psd/LayerRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public byte[] Content
stream.WriteByte(0); //Flags, Visible = true, 1 byte. (For invisible, try using 10)
stream.WriteByte(0); //Filler, 1 byte

var name = StreamHelpers.GetPascalStringAsBytes(Encoding.GetEncoding(1252).GetBytes(Name));
var name = StreamHelpers.GetPascalStringAsBytes(Encoding.Unicode.GetBytes(Name));
var aditionalLayerInfo = AditionalInfo.SelectMany(s => s.Content).ToArray();

stream.WriteUInt32(BitHelper.ConvertEndian((uint)(4 + 4 + name.Length + aditionalLayerInfo.Length))); //Extra data length, 4 bytes.
Expand Down
46 changes: 46 additions & 0 deletions ScreenToGif.Util/IdentityHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using ScreenToGif.Domain.Enums;

namespace ScreenToGif.Util;

public static class IdentityHelper
{
public static ApplicationTypes ApplicationType
{
get
{
#if DEPENDANT_SINGLE
//Dependant, Single File
return ApplicationTypes.DependantSingle;
#elif FULL_SINGLE
//Full, Single File
return ApplicationTypes.FullSingle;
#elif FULL_MULTI_MSIX
//Full, Multiple Files, MSIX
return ApplicationTypes.FullMultiMsix;
#elif FULL_MULTI_MSIX_STORE
//Full, Multiple Files, MSIX, Store
return ApplicationTypes.FullMultiMsixStore;
#endif

return ApplicationTypes.Unidentified;
}
}

public static string ApplicationTypeDescription
{
get
{
#if DEPENDANT_SINGLE
return "Framework Dependant, Single File";
#elif FULL_SINGLE
return "Full, Single File";
#elif FULL_MULTI_MSIX
return "Full, Multiple Files, MSIX";
#elif FULL_MULTI_MSIX_STORE
return "Full, Multiple Files, MSIX, Store";
#endif

return "Unidentified";
}
}
}
30 changes: 29 additions & 1 deletion ScreenToGif.Util/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ public static string GetEntryAssemblyPath()
}
}

public static async Task<string> Start(string arguments, bool runWithPowershell = true)
{
var info = new ProcessStartInfo(runWithPowershell ? "Powershell.exe" : "cmd.exe")
{
Arguments = (!runWithPowershell ? "/c " : "") + arguments,
RedirectStandardOutput = true,
CreateNoWindow = true
};

try
{
using var process = new Process();
process.StartInfo = info;
process.Start();

var message = await process.StandardOutput.ReadToEndAsync();

await process.WaitForExitAsync();

return message;
}
catch (Exception e)
{
LogWriter.Log(e, "It was not possible to run the command");
return "";
}
}

public static void StartWithShell(string filename)
{
var info = new ProcessStartInfo
Expand All @@ -31,7 +59,7 @@ public static void StartWithShell(string filename)

Process.Start(info);
}

public static async Task<bool> RestartAsAdmin(string arguments = "", bool waitToClose = false)
{
try
Expand Down
10 changes: 9 additions & 1 deletion ScreenToGif.Util/ScreenToGif.Util.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>True</UseWPF>
<DebugType>embedded</DebugType>
<Platforms>AnyCPU;ARM64;x64;x86</Platforms>
<Version>2.35.4</Version>
<Version>2.36.0</Version>
<!--<UseWindowsForms>True</UseWindowsForms>-->
</PropertyGroup>

Expand All @@ -19,41 +19,49 @@
<PlatformTarget>AnyCPU</PlatformTarget>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<PlatformTarget>x86</PlatformTarget>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<PlatformTarget>x86</PlatformTarget>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<PlatformTarget>ARM64</PlatformTarget>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<PlatformTarget>ARM64</PlatformTarget>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 10 additions & 1 deletion ScreenToGif.Util/Settings/Migrations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ScreenToGif.Domain.Models;
using ScreenToGif.Settings.Migrations;
using ScreenToGif.Util.Settings.Migrations;

namespace ScreenToGif.Util.Settings;

Expand Down Expand Up @@ -28,7 +29,7 @@ public static bool Migrate(List<Property> properties, string version)

case "2.31": //To 2.32
Migration2_31_0To2_32_0.Up(properties);
goto default;
goto case "2.32";

case "2.32": //To 2.35
case "2.32.1":
Expand All @@ -37,6 +38,14 @@ public static bool Migrate(List<Property> properties, string version)
case "2.34":
case "2.34.1":
Migration2_32_0To2_35_0.Up(properties);
goto case "2.35";

case "2.35": //To 2.36
case "2.35.1":
case "2.35.2":
case "2.35.3":
case "2.35.4":
Migration2_35_0To2_36_0.Up(properties);
goto default;

default:
Expand Down
33 changes: 33 additions & 0 deletions ScreenToGif.Util/Settings/Migrations/Migration2_35_0To2_36_0.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using ScreenToGif.Domain.Models;

namespace ScreenToGif.Util.Settings.Migrations;

internal class Migration2_35_0To2_36_0
{
internal static bool Up(List<Property> properties)
{
//Rename a property.
var presets = properties.FirstOrDefault(f => f.Key == "ExportPresets");

if (presets != null)
{
foreach (var child in presets.Children)
{
foreach (var attribute in child.Attributes)
{
switch (attribute.Key)
{
case "OverwriteOnSave":
{
attribute.Key = "OverwriteMode";
attribute.Value = attribute.Value == "True" ? "Allow" : "Prompt";
break;
}
}
}
}
}

return true;
}
}
33 changes: 18 additions & 15 deletions ScreenToGif.Util/Settings/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ private static object ParseProperty(Property property)
return Enum.Parse(typeof(ColorQuantizationTypes), property.Value);
case "SizeUnits":
return Enum.Parse(typeof(SizeUnits), property.Value);
case "OverwriteModes":
return Enum.Parse(typeof(OverwriteModes), property.Value);

case "FontWeight":
return new FontWeightConverter().ConvertFrom(property.Value);
Expand Down Expand Up @@ -494,7 +496,7 @@ private static object DeserializeProperty(Property property)
}


public static void Save(bool canForce = false)
public static void Save(bool canForce = false, bool saveToAppData = false)
{
//Only writes if non-default values were created. Should not write the default dictionary.
if (_local == null && _appData == null)
Expand All @@ -503,17 +505,14 @@ public static void Save(bool canForce = false)
try
{
//Filename (Local or AppData).
var folder = _local != null ? AppDomain.CurrentDomain.BaseDirectory : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ScreenToGif");
var folder = !saveToAppData && _local != null ? AppDomain.CurrentDomain.BaseDirectory : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ScreenToGif");
var filename = Path.Combine(folder, "Settings.xaml");
var backup = filename + ".bak";


//Create folder.
if (!string.IsNullOrWhiteSpace(folder) && !Directory.Exists(folder))
Directory.CreateDirectory(folder);

//Create the backup, in case the save operation fails.
if (File.Exists(filename))
File.Copy(filename, backup, true);
var backup = File.Exists(filename) ? File.ReadAllText(filename) : null;

var settings = new XmlWriterSettings
{
Expand All @@ -532,17 +531,21 @@ public static void Save(bool canForce = false)
XamlWriter.Save(_local ?? _appData, writer);

CheckIfSavedCorrectly(filename, backup, true);

File.Delete(backup);
}
catch (UnauthorizedAccessException u)
{
LogWriter.Log(u, "Unauthorized to save the settings.");

if (canForce)
//Try saving to AppData first, then try harder.
if (!saveToAppData)
{
Save(canForce, true);
}
else if (canForce)
{
LogWriter.Log(u, "Unauthorized to save the settings.");
throw new SettingsPersistenceException(_local ?? _appData, _local != null);
}
}
catch (Exception e)
catch (Exception e) when (e is not SettingsPersistenceException)
{
LogWriter.Log(e, "Impossible to save the settings.");
}
Expand Down Expand Up @@ -589,15 +592,15 @@ private static void CheckIfSavedCorrectly(string filename, string backup, bool t
if (content.All(x => x == '\0'))
{
LogWriter.Log("Settings disk persistence failed.", content);
File.Copy(backup, filename, true);
File.WriteAllText(filename, backup);

if (throwException)
throw new UnauthorizedAccessException("The file had garbage inside it.");
}
}
catch (Exception e)
{
LogWriter.Log(e, "Impossible to check if the settings file was saved correctly.");
LogWriter.Log(e, "Impossible to check if the settings file was saved correctly or impossible to restore backup.");
}
}

Expand Down
Loading

0 comments on commit 1044044

Please sign in to comment.