Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize assembly loading for MAUI to use client storage #2412

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions Oqtane.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,41 @@ private static async Task LoadClientAssemblies(HttpClient http)
// get assemblies from server and load into client app domain
var zip = await http.GetByteArrayAsync($"/api/Installation/load");

var dlls = new Dictionary<string, byte[]>();
var pdbs = new Dictionary<string, byte[]>();

// asemblies and debug symbols are packaged in a zip file
using (ZipArchive archive = new ZipArchive(new MemoryStream(zip)))
{
var dlls = new Dictionary<string, byte[]>();
var pdbs = new Dictionary<string, byte[]>();

foreach (ZipArchiveEntry entry in archive.Entries)
{
if (!assemblies.Contains(Path.GetFileNameWithoutExtension(entry.FullName)))
using (var memoryStream = new MemoryStream())
{
using (var memoryStream = new MemoryStream())
entry.Open().CopyTo(memoryStream);
byte[] file = memoryStream.ToArray();
switch (Path.GetExtension(entry.FullName))
{
entry.Open().CopyTo(memoryStream);
byte[] file = memoryStream.ToArray();
switch (Path.GetExtension(entry.FullName))
{
case ".dll":
dlls.Add(entry.FullName, file);
break;
case ".pdb":
pdbs.Add(entry.FullName, file);
break;
}
case ".dll":
dlls.Add(entry.FullName, file);
break;
case ".pdb":
pdbs.Add(entry.FullName, file);
break;
}
}
}
}

foreach (var item in dlls)
// load assemblies into app domain
foreach (var item in dlls)
{
if (pdbs.ContainsKey(item.Key.Replace(".dll", ".pdb")))
{
if (pdbs.ContainsKey(item.Key))
{
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value), new MemoryStream(pdbs[item.Key]));
}
else
{
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value));
}
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value), new MemoryStream(pdbs[item.Key.Replace(".dll", ".pdb")]));
}
else
{
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value));
}
}
}
Expand Down
127 changes: 101 additions & 26 deletions Oqtane.Maui/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Diagnostics;
using Oqtane.Modules;
using Oqtane.Services;
using System.Globalization;
using System.Text.Json;

namespace Oqtane.Maui;

Expand Down Expand Up @@ -61,49 +63,116 @@ private static void LoadClientAssemblies(HttpClient http)
{
try
{
// get list of loaded assemblies on the client
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetName().Name).ToList();
// ensure local assembly folder exists
string folder = Path.Combine(FileSystem.Current.AppDataDirectory, "oqtane");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}

var dlls = new Dictionary<string, byte[]>();
var pdbs = new Dictionary<string, byte[]>();

var filter = new List<string>();
var files = new List<string>();
foreach (var file in Directory.EnumerateFiles(folder, "*.dll", SearchOption.AllDirectories))
{
files.Add(file.Substring(folder.Length + 1).Replace("\\", "/"));
}
if (files.Count() != 0)
{
// get list of assemblies from server
var json = Task.Run(() => http.GetStringAsync("/api/Installation/list")).GetAwaiter().GetResult();
var assemblies = JsonSerializer.Deserialize<List<string>>(json);

// get assemblies from server and load into client app domain
var zip = Task.Run(() => http.GetByteArrayAsync("/api/Installation/load")).GetAwaiter().GetResult();
// determine which assemblies need to be downloaded
foreach (var assembly in assemblies)
{
var file = files.FirstOrDefault(item => item.Contains(assembly));
if (file == null)
{
filter.Add(assembly);
}
else
{
// check if newer version available
if (GetFileDate(assembly) > GetFileDate(file))
{
filter.Add(assembly);
}
}
}

// get assemblies already downloaded
foreach (var file in files)
{
if (assemblies.Contains(file) && !filter.Contains(file))
{
dlls.Add(file, File.ReadAllBytes(Path.Combine(folder, file)));
var pdb = file.Replace(".dll", ".pdb");
if (File.Exists(Path.Combine(folder, pdb)))
{
pdbs.Add(pdb, File.ReadAllBytes(Path.Combine(folder, pdb)));
}
}
else // file is deprecated
{
File.Delete(Path.Combine(folder, file));
}
}
}
else
{
filter.Add("*");
}

// asemblies and debug symbols are packaged in a zip file
using (ZipArchive archive = new ZipArchive(new MemoryStream(zip)))
if (filter.Count != 0)
{
var dlls = new Dictionary<string, byte[]>();
var pdbs = new Dictionary<string, byte[]>();
// get assemblies from server
var zip = Task.Run(() => http.GetByteArrayAsync("/api/Installation/load?list=" + string.Join(",", filter))).GetAwaiter().GetResult();

foreach (ZipArchiveEntry entry in archive.Entries)
// asemblies and debug symbols are packaged in a zip file
using (ZipArchive archive = new ZipArchive(new MemoryStream(zip)))
{
if (!assemblies.Contains(Path.GetFileNameWithoutExtension(entry.FullName)))
foreach (ZipArchiveEntry entry in archive.Entries)
{
using (var memoryStream = new MemoryStream())
{
entry.Open().CopyTo(memoryStream);
byte[] file = memoryStream.ToArray();
switch (Path.GetExtension(entry.FullName))

// save assembly to local folder
int subfolder = entry.FullName.IndexOf('/');
if (subfolder != -1 && !Directory.Exists(Path.Combine(folder, entry.FullName.Substring(0, subfolder))))
{
case ".dll":
dlls.Add(entry.FullName, file);
break;
case ".pdb":
pdbs.Add(entry.FullName, file);
break;
Directory.CreateDirectory(Path.Combine(folder, entry.FullName.Substring(0, subfolder)));
}
using var stream = File.Create(Path.Combine(folder, entry.FullName));
stream.Write(file, 0, file.Length);

if (Path.GetExtension(entry.FullName) == ".dll")
{
dlls.Add(entry.FullName, file);
}
else
{
pdbs.Add(entry.FullName, file);
}
}
}
}
}

foreach (var item in dlls)
// load assemblies into app domain
foreach (var item in dlls)
{
if (pdbs.ContainsKey(item.Key.Replace(".dll", ".pdb")))
{
if (pdbs.ContainsKey(item.Key))
{
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value), new MemoryStream(pdbs[item.Key]));
}
else
{
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value));
}
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value), new MemoryStream(pdbs[item.Key.Replace(".dll", ".pdb")]));
}
else
{
AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(item.Value));
}
}
}
Expand All @@ -113,6 +182,12 @@ private static void LoadClientAssemblies(HttpClient http)
}
}

private static DateTime GetFileDate(string filepath)
{
var segments = filepath.Split('.');
return DateTime.ParseExact(segments[segments.Length - 2], "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
}

private static void RegisterModuleServices(Assembly assembly, IServiceCollection services)
{
// dynamically register module scoped services
Expand Down
Loading