Skip to content

Commit cae5746

Browse files
authored
Add AOT Compatibility (#44)
* add optional native aot support * update to dotnet 8 for lts and aot support
1 parent 0db8f0f commit cae5746

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

examples/DesktopApp/DesktopApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ApplicationIcon>icon.ico</ApplicationIcon>
77
</PropertyGroup>
88

examples/Minimal/Minimal.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ApplicationIcon>icon.ico</ApplicationIcon>
77
</PropertyGroup>
88

src/SharpWebview/Loopback.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private List<FirewallAppContainer> GetAllAppContainers()
4040
{
4141
var size = 0u;
4242
var arrayValue = IntPtr.Zero;
43-
var structSize = Marshal.SizeOf(typeof(FirewallAppContainer));
43+
var structSize = Marshal.SizeOf<FirewallAppContainer>();
4444

4545
var handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned);
4646
var handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned);
@@ -50,7 +50,7 @@ private List<FirewallAppContainer> GetAllAppContainers()
5050
var firewallApps = new List<FirewallAppContainer>();
5151
for (var i = 0; i < size; i++)
5252
{
53-
var cur = (FirewallAppContainer) Marshal.PtrToStructure(arrayValue, typeof(FirewallAppContainer));
53+
var cur = (FirewallAppContainer)Marshal.PtrToStructure<FirewallAppContainer>(arrayValue);
5454
firewallApps.Add(cur);
5555
arrayValue = new IntPtr((long)arrayValue + structSize);
5656
}
@@ -65,7 +65,7 @@ private List<SidAndAttributes> GetAllAppContainerConfigs()
6565
{
6666
var size = 0u;
6767
var arrayValue = IntPtr.Zero;
68-
var structSize = Marshal.SizeOf(typeof(SidAndAttributes));
68+
var structSize = Marshal.SizeOf<SidAndAttributes>();
6969

7070
var handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned);
7171
var handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned);
@@ -75,7 +75,7 @@ private List<SidAndAttributes> GetAllAppContainerConfigs()
7575
var firewallAppConfigs = new List<SidAndAttributes>();
7676
for (var i = 0; i < size; i++)
7777
{
78-
var currentConfig = (SidAndAttributes)Marshal.PtrToStructure(arrayValue, typeof(SidAndAttributes));
78+
var currentConfig = (SidAndAttributes)Marshal.PtrToStructure<SidAndAttributes>(arrayValue);
7979
firewallAppConfigs.Add(currentConfig);
8080
arrayValue = new IntPtr((long)arrayValue + structSize);
8181
}

src/SharpWebview/SharpWebview.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
4+
<TargetFrameworks>net7.0;net8.0;net9.0</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<AssemblyName>SharpWebview</AssemblyName>
77
<Authors>Gerrit 'Geaz' Gazic</Authors>
@@ -35,7 +35,6 @@
3535
<ItemGroup>
3636
<FrameworkReference Include="Microsoft.AspNetCore.App" />
3737
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
38-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3938
</ItemGroup>
4039

4140
</Project>

src/SharpWebview/Webview.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using Newtonsoft.Json;
32
using SharpWebview.Content;
43
using System.Diagnostics;
54
using System.Collections.Generic;
@@ -206,10 +205,11 @@ private void InterceptExternalLinks()
206205
// This method opens the url parameter in the system browser
207206
Bind("openExternalLink", (id, req) =>
208207
{
209-
dynamic args = JsonConvert.DeserializeObject(req);
208+
string url = ExtractUrlArgument(req);
209+
210210
ProcessStartInfo psi = new ProcessStartInfo
211211
{
212-
FileName = args[0],
212+
FileName = url,
213213
UseShellExecute = true
214214
};
215215
Process.Start (psi);
@@ -248,6 +248,26 @@ function interceptClickEvent(e) {
248248
");
249249
}
250250

251+
private string ExtractUrlArgument(string jsonArray)
252+
{
253+
if (string.IsNullOrWhiteSpace(jsonArray))
254+
return string.Empty;
255+
256+
jsonArray = jsonArray.Trim();
257+
258+
// Expected format: ["http..."]
259+
if (!jsonArray.StartsWith("[\"http") || !jsonArray.Contains("\"]"))
260+
return string.Empty;
261+
262+
int startIndex = 2; // After ["
263+
int endIndex = jsonArray.IndexOf("\"]", startIndex);
264+
265+
if (endIndex == -1)
266+
return string.Empty;
267+
268+
return jsonArray.Substring(startIndex, endIndex - startIndex);
269+
}
270+
251271
private bool? CheckLoopbackException(string url)
252272
{
253273
// https://docs.microsoft.com/de-de/windows/win32/sysinfo/operating-system-version

0 commit comments

Comments
 (0)