convert Pilz to c# & add AppVersion to there

This commit is contained in:
Pilzinsel64
2024-08-30 11:43:32 +02:00
parent 54fadfbe8d
commit f230fb823b
14 changed files with 372 additions and 170 deletions

View File

@@ -29,6 +29,6 @@
<Import Include="System.Threading.Tasks" /> <Import Include="System.Threading.Tasks" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Pilz\Pilz.vbproj" /> <ProjectReference Include="..\Pilz\Pilz.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -91,6 +91,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Pilz.Win32\Pilz.Win32.vbproj" /> <ProjectReference Include="..\Pilz.Win32\Pilz.Win32.vbproj" />
<ProjectReference Include="..\Pilz\Pilz.vbproj" /> <ProjectReference Include="..\Pilz\Pilz.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.7.34018.315 VisualStudioVersion = 17.7.34018.315
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Pilz", "Pilz\Pilz.vbproj", "{277D2B83-7613-4C49-9CAB-E080195A6E0C}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pilz", "Pilz\Pilz.csproj", "{9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}"
EndProject EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Pilz.Drawing", "Pilz.Drawing\Pilz.Drawing.vbproj", "{1A0B8106-2449-4908-B5E1-A00D8E9CF8F6}" Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Pilz.Drawing", "Pilz.Drawing\Pilz.Drawing.vbproj", "{1A0B8106-2449-4908-B5E1-A00D8E9CF8F6}"
EndProject EndProject
@@ -53,14 +53,14 @@ Global
Release|x86 = Release|x86 Release|x86 = Release|x86
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{277D2B83-7613-4C49-9CAB-E080195A6E0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{277D2B83-7613-4C49-9CAB-E080195A6E0C}.Debug|Any CPU.Build.0 = Debug|Any CPU {9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{277D2B83-7613-4C49-9CAB-E080195A6E0C}.Debug|x86.ActiveCfg = Debug|Any CPU {9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}.Debug|x86.ActiveCfg = Debug|Any CPU
{277D2B83-7613-4C49-9CAB-E080195A6E0C}.Debug|x86.Build.0 = Debug|Any CPU {9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}.Debug|x86.Build.0 = Debug|Any CPU
{277D2B83-7613-4C49-9CAB-E080195A6E0C}.Release|Any CPU.ActiveCfg = Release|Any CPU {9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{277D2B83-7613-4C49-9CAB-E080195A6E0C}.Release|Any CPU.Build.0 = Release|Any CPU {9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}.Release|Any CPU.Build.0 = Release|Any CPU
{277D2B83-7613-4C49-9CAB-E080195A6E0C}.Release|x86.ActiveCfg = Release|Any CPU {9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}.Release|x86.ActiveCfg = Release|Any CPU
{277D2B83-7613-4C49-9CAB-E080195A6E0C}.Release|x86.Build.0 = Release|Any CPU {9559AAE8-BA4B-03B8-1EF3-2AFE7BCD5AAC}.Release|x86.Build.0 = Release|Any CPU
{1A0B8106-2449-4908-B5E1-A00D8E9CF8F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1A0B8106-2449-4908-B5E1-A00D8E9CF8F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A0B8106-2449-4908-B5E1-A00D8E9CF8F6}.Debug|Any CPU.Build.0 = Debug|Any CPU {1A0B8106-2449-4908-B5E1-A00D8E9CF8F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A0B8106-2449-4908-B5E1-A00D8E9CF8F6}.Debug|x86.ActiveCfg = Debug|Any CPU {1A0B8106-2449-4908-B5E1-A00D8E9CF8F6}.Debug|x86.ActiveCfg = Debug|Any CPU

9
Pilz/AppChannel.cs Normal file
View File

@@ -0,0 +1,9 @@
namespace Pilz;
public enum AppChannel
{
Stable,
PreRelease,
Beta,
Alpha
}

152
Pilz/AppVersion.cs Normal file
View File

@@ -0,0 +1,152 @@
using Newtonsoft.Json;
namespace Pilz;
[method: JsonConstructor]
public class AppVersion(Version version, int build, AppChannel channel) : IComparable, IComparable<AppVersion>
{
// P r o p e r t i e s
[JsonConverter(typeof(Newtonsoft.Json.Converters.VersionConverter))]
public Version Version { get; set; } = version;
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public AppChannel Channel { get; set; } = channel;
public int Build { get; set; } = build;
// C o n s t r u c t o r s
public AppVersion() : this(new Version("1.0.0.0"))
{
}
public AppVersion(Version version) : this(version, 1, AppChannel.Stable)
{
}
// F e a t u r e s
public override string ToString()
{
if (Channel == AppChannel.Stable && Build == 1)
return Version.ToString();
return $"{Version} {Channel} {Build}";
}
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
return true;
if (obj is not AppVersion applicationVersion)
return false;
return applicationVersion == this;
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public static bool TryParse(string input, out AppVersion version)
{
try
{
version = Parse(input);
}
catch (Exception)
{
version = null;
}
return version != null;
}
public static AppVersion Parse(string input)
{
if (string.IsNullOrWhiteSpace(input))
throw new FormatException();
if (input.StartsWith("version", StringComparison.InvariantCultureIgnoreCase))
input = input[7..];
var splitted = input.Trim().Split(' ');
if (splitted.Length < 1 || !Version.TryParse(splitted[0], out Version? version) || version == null)
throw new FormatException();
if (splitted.Length < 2 || !Enum.TryParse(splitted[1], out AppChannel channel))
channel = AppChannel.Stable;
if (splitted.Length < 3 || !int.TryParse(splitted[2], out int build))
build = 1;
return new AppVersion(version, build, channel);
}
// C o m p a r e
public int CompareTo(object appVersion)
{
return CompareTo(appVersion as AppVersion);
}
public int CompareTo(AppVersion appVersion)
{
if (appVersion is null)
return 1;
if (Version != appVersion.Version)
{
if (Version > appVersion.Version)
return 1;
return -1;
}
if (Channel != appVersion.Channel)
{
if (Channel < appVersion.Channel)
return 1;
return -1;
}
if (Build != appVersion.Build)
{
if (Build > appVersion.Build)
return 1;
return -1;
}
return 0;
}
// O p e r a t o r s
public static bool operator >(AppVersion a, AppVersion b)
{
return a.Version > b.Version || a.Version == b.Version && (a.Channel < b.Channel || (a.Channel == b.Channel && a.Build > b.Build));
}
public static bool operator <(AppVersion a, AppVersion b)
{
return a.Version < b.Version || a.Version == b.Version && (a.Channel > b.Channel || (a.Channel == b.Channel && a.Build < b.Build));
}
public static bool operator ==(AppVersion a, AppVersion b)
{
return a.Version == b.Version && a.Channel == b.Channel && a.Build == b.Build;
}
public static bool operator !=(AppVersion a, AppVersion b)
{
return a.Version != b.Version || a.Channel != b.Channel || a.Build != b.Build;
}
public static bool operator >=(AppVersion a, AppVersion b)
{
return a == b || a > b;
}
public static bool operator <=(AppVersion a, AppVersion b)
{
return a == b || a < b;
}
}

View File

@@ -0,0 +1,18 @@
namespace Pilz.GeneralEventArgs;
public class GetValueEventArgs<T> : EventArgs
{
public T? Value { get; set; }
public GetValueEventArgs() : base()
{
}
public GetValueEventArgs(T? value) : base()
{
Value = value;
}
}

View File

@@ -1,18 +0,0 @@
Namespace GeneralEventArgs
Public Class GetValueEventArgs(Of T)
Inherits EventArgs
Public Property Value As T
Public Sub New()
MyBase.New
End Sub
Public Sub New(value As T)
MyBase.New
End Sub
End Class
End Namespace

View File

@@ -0,0 +1,80 @@
using Newtonsoft.Json;
namespace Pilz.Json;
public class AppVersionStringJsonConverter(bool serializeAsObject) : JsonConverter
{
public AppVersionStringJsonConverter() : this(false)
{
}
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
if (serializeAsObject || value is not AppVersion appVersion)
writer.WriteValue(value);
else
writer.WriteValue(appVersion.ToString());
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
try
{
if (reader.TokenType == JsonToken.String)
{
var strValue = reader.Value?.ToString();
if (string.IsNullOrWhiteSpace(strValue))
return null;
return AppVersion.Parse(strValue);
}
if (reader.TokenType == JsonToken.StartObject)
return serializer.Deserialize<AppVersion>(reader);
}
catch (Exception ex)
{
throw new JsonSerializationException("Error converting value {0} to type '{1}'.", ex);
}
// we don't actually expect to get here.
throw new JsonSerializationException("Unexpected token {0} when parsing app version.");
}
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(AppVersion);
}
}

14
Pilz/Pilz.csproj Normal file
View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@@ -1,50 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<DocumentationFile>Pilz.xml</DocumentationFile>
<DefineTrace>true</DefineTrace>
<LangVersion>latest</LangVersion>
<AssemblyTitle>Pilz</AssemblyTitle>
<Company>Pilzinsel64</Company>
<Product>Pilz</Product>
<Copyright>Copyright © Pilzinsel64 2019 - 2020</Copyright>
<DocumentationFile>Pilz.xml</DocumentationFile>
<DefineTrace>true</DefineTrace>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DefineDebug>true</DefineDebug>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DefineDebug>false</DefineDebug>
</PropertyGroup>
<PropertyGroup>
<OptionExplicit>On</OptionExplicit>
</PropertyGroup>
<PropertyGroup>
<OptionCompare>Binary</OptionCompare>
</PropertyGroup>
<PropertyGroup>
<OptionStrict>Off</OptionStrict>
<OptionInfer>On</OptionInfer>
</PropertyGroup>
<PropertyGroup>
<Version>2.0.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualBasic" Version="10.3.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
<ItemGroup>
<Import Include="Microsoft.VisualBasic" />
<Import Include="System" />
<Import Include="System.Collections" />
<Import Include="System.Collections.Generic" />
<Import Include="System.Data" />
<Import Include="System.Diagnostics" />
<Import Include="System.Linq" />
<Import Include="System.Xml.Linq" />
<Import Include="System.Threading.Tasks" />
</ItemGroup>
</Project>

9
Pilz/Runtime/OSType.cs Normal file
View File

@@ -0,0 +1,9 @@
namespace Pilz.Runtime;
public enum OSType
{
Unknown,
Windows,
Linux,
OSX
}

View File

@@ -1,10 +0,0 @@
Namespace Runtime
Public Enum OSType
Unknown
Windows
Linux
OSX
End Enum
End Namespace

View File

@@ -0,0 +1,79 @@
using System.Runtime.InteropServices;
namespace Pilz.Runtime;
public static class RuntimeInformationsEx
{
private static OSType? osType;
private static OSType? osTypeReal;
public static OSType OSType => osType ??= GetOSType();
public static OSType RealOSType => osTypeReal ??= GetRealOSType();
public static OSType GetOSType()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return OSType.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return OSType.Linux;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return OSType.OSX;
return OSType.Unknown;
}
public static OSType GetRealOSType()
{
var windir = Environment.GetEnvironmentVariable("windir");
const string ostypeDirWine = @"Z:\proc\sys\kernel\ostype";
const string ostypeDirNative = "/proc/sys/kernel/ostype";
const string systemVersionWine = @"Z:\System\Library\CoreServices\SystemVersion.plist";
const string systemVersionNative = "/System/Library/CoreServices/SystemVersion.plist";
// Linux using wine
if (File.Exists(ostypeDirWine))
{
var osTypeString = File.ReadAllText(ostypeDirWine);
if (osTypeString.StartsWith("Linux", StringComparison.OrdinalIgnoreCase))
// Note: Android gets here too
return OSType.Linux;
else
return OSType.Unknown;
}
// Linux native
else if (File.Exists(ostypeDirNative))
{
var osTypeString = File.ReadAllText(ostypeDirNative);
if (osTypeString.StartsWith("Linux", StringComparison.OrdinalIgnoreCase))
// Note: Android gets here too
return OSType.Linux;
else
return OSType.Unknown;
}
// OSX using wine
else if (File.Exists(systemVersionWine))
// Note: iOS gets here too
return OSType.OSX;
// OSX native
else if (File.Exists(systemVersionNative))
// Note: iOS gets here too
return OSType.OSX;
// Windows
else if (!string.IsNullOrEmpty(windir) && Directory.Exists(windir) && Path.DirectorySeparatorChar == '\\')
return OSType.Windows;
return OSType.Unknown;
}
public static bool IsOSPlatform(OSType os, bool checkRealOS)
{
return (checkRealOS ? RealOSType : OSType) == os;
}
}

View File

@@ -1,81 +0,0 @@
Imports System.IO
Imports System.Runtime.InteropServices
Namespace Runtime
Public Module RuntimeInformationsEx
Public ReadOnly Property OSType As OSType
Get
Static t As OSType? = Nothing
If t Is Nothing Then
Select Case True
Case RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
t = OSType.Windows
Case RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
t = OSType.Linux
Case RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
t = OSType.OSX
Case Else
t = OSType.Unknown
End Select
End If
Return t
End Get
End Property
Public ReadOnly Property RealOSType As OSType
Get
Static t As OSType? = Nothing
If t Is Nothing Then
Dim windir = Environment.GetEnvironmentVariable("windir")
Const ostypeDirWine = "Z:\proc\sys\kernel\ostype"
Const ostypeDirNative = "/proc/sys/kernel/ostype"
Const systemVersionWine = "Z:\System\Library\CoreServices\SystemVersion.plist"
Const systemVersionNative = "/System/Library/CoreServices/SystemVersion.plist"
If File.Exists(ostypeDirWine) Then ' Linux using wine
Dim osTypeString As String = File.ReadAllText(ostypeDirWine)
If osTypeString.StartsWith("Linux", StringComparison.OrdinalIgnoreCase) Then
' Note: Android gets here too
t = OSType.Linux
Else
t = OSType.Unknown
End If
ElseIf File.Exists(ostypeDirNative) Then ' Linux native
Dim osTypeString As String = File.ReadAllText(ostypeDirNative)
If osTypeString.StartsWith("Linux", StringComparison.OrdinalIgnoreCase) Then
' Note: Android gets here too
t = OSType.Linux
Else
t = OSType.Unknown
End If
ElseIf File.Exists(systemVersionWine) Then ' OSX using wine
' Note: iOS gets here too
t = OSType.OSX
ElseIf File.Exists(systemVersionNative) Then ' OSX native
' Note: iOS gets here too
t = OSType.OSX
ElseIf Not String.IsNullOrEmpty(windir) AndAlso Directory.Exists(windir) AndAlso Path.DirectorySeparatorChar = "\"c Then ' Windows
t = OSType.Windows
Else
t = OSType.Unknown
End If
End If
Return t
End Get
End Property
Public Function IsOSPlatform(os As OSType, checkRealOS As Boolean) As Boolean
Return If(checkRealOS, RealOSType, OSType) = os
End Function
End Module
End Namespace