Skip to content

Commit c822559

Browse files
committed
feat: search dirs and reference resolution
1 parent fb1ca4c commit c822559

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/SharpWebserver/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ partial class ListenServer
2525
public static string BaseDir { get; private set; } = string.Empty;
2626
public static string WebRoot { get; private set; } = string.Empty;
2727
public static string ConfigDir { get; private set; } = string.Empty;
28-
public static string IncludesDir { get; private set; } = string.Empty;
28+
public static string ReferenceDir { get; private set; } = string.Empty;
2929
public static readonly IEvaluator ScriptRunner = CSScript.Evaluator.ReferenceAssembly(typeof(ListenServer).Assembly);
3030
public static bool SafeMode => GlobalConfig.SafeMode;
3131
public static string Version { get; private set; } = string.Empty;
@@ -82,13 +82,13 @@ static void Main(string[] args)
8282

8383
WebRoot = Path.Combine(BaseDir, "www");
8484
ConfigDir = Path.Combine(BaseDir, "config");
85-
IncludesDir = Path.Combine(BaseDir, "includes");
85+
ReferenceDir = Path.Combine(BaseDir, "references");
8686
Version = $"v{ver.Major}.{ver.Minor}";
8787

8888
Utilities.EnsureDirectory(BaseDir);
8989
Utilities.EnsureDirectory(WebRoot);
9090
Utilities.EnsureDirectory(ConfigDir);
91-
Utilities.EnsureDirectory(IncludesDir);
91+
Utilities.EnsureDirectory(ReferenceDir);
9292

9393
//Load global config
9494
if (ConfigManager.LoadConfig<SharpConfig>("SharpConfig.toml") is not SharpConfig gc)
@@ -97,6 +97,8 @@ static void Main(string[] args)
9797
GlobalConfig = gc;
9898

9999
Utilities.SecurityPolicy.LoadBlockList();
100+
CSScript.GlobalSettings.AddSearchDir(ReferenceDir);
101+
AppDomain.CurrentDomain.AssemblyResolve += Utilities.ResolveAssembly;
100102

101103
Logger.LogInfo("Startup", [
102104
("BaseDir", BaseDir),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright (C) 2025 Robyn (robyn@mamallama.dev)
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
*/
9+
10+
using System;
11+
using System.Collections.Generic;
12+
using System.IO;
13+
using System.Reflection;
14+
15+
namespace SharpWebserver;
16+
public static partial class Utilities
17+
{
18+
private static readonly List<string> RefAssemblies = [];
19+
public static string[] ReferenceAssemblies => [.. RefAssemblies];
20+
public static Assembly? ResolveAssembly(object? sender, ResolveEventArgs args)
21+
{
22+
23+
var assemblyName = new AssemblyName(args.Name);
24+
25+
if (assemblyName.Name is null)
26+
return null;
27+
28+
FileInfo load = new(Path.Combine(ListenServer.ReferenceDir, assemblyName.Name + ".dll"));
29+
var LoadFrom = load.FullName.Replace(ListenServer.BaseDir, string.Empty);
30+
31+
Logger.LogInfo("Resolving an assembly", [
32+
("Assembly", assemblyName.Name),
33+
("Load From", LoadFrom),
34+
("Located?", load.Exists)
35+
]);
36+
37+
if (load.Exists)
38+
{
39+
RefAssemblies.Add(LoadFrom);
40+
return Assembly.LoadFrom(load.FullName);
41+
}
42+
43+
44+
return null;
45+
}
46+
}

0 commit comments

Comments
 (0)