Skip to content

Commit d0d79ef

Browse files
committed
Improve file resolutions, fixes #16
1 parent c659e19 commit d0d79ef

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

ModFramework.Modules.CSharp/CSharpLoader.cs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ public class CSharpLoader
5151
public static AssemblyLoadContext AssemblyContextDefault { get; set; } = AssemblyLoadContext.Default;
5252
public AssemblyLoadContext AssemblyContext { get; set; } = AssemblyContextDefault;
5353

54-
public static List<string> DefaultSearchPaths { get; set; } = new() { "bin" };
54+
public static List<string> DefaultSearchPaths { get; set; } = [
55+
Environment.CurrentDirectory,
56+
"bin",
57+
AppContext.BaseDirectory,
58+
Path.Combine(AppContext.BaseDirectory, "bin"),
59+
];
5560
public List<string> SearchPaths { get; set; } = DefaultSearchPaths;
5661

5762
public IFrameworkResolver FrameworkResolver { get; set; } = new DefaultFrameworkResolver();
@@ -128,10 +133,10 @@ IEnumerable<MetadataReference> LoadExternalRefs(string path)
128133

129134
var filename = Path.GetFileName(ref_file);
130135

131-
var full_path = ResolveFile(ref_file);
136+
var full_path = TryResolveFile(ref_file);
132137
var sys_path = Path.Combine(assemblyPath, ref_file);
133138

134-
if (File.Exists(full_path))
139+
if (full_path is not null && File.Exists(full_path))
135140
yield return MetadataReference.CreateFromFile(full_path);
136141

137142
else if (File.Exists(sys_path))
@@ -204,9 +209,8 @@ public IEnumerable<MetadataReference> GetAllSystemReferences()
204209
return systemRefs = files.Select(f => MetadataReference.CreateFromFile(Path.GetFullPath(f)));
205210
}
206211

207-
public string ResolveFile(string path)
212+
public string? TryResolveFile(string path)
208213
{
209-
var dir = Path.GetDirectoryName(path);
210214
var filename = Path.GetFileName(path);
211215

212216
foreach (var searchPath in SearchPaths)
@@ -218,35 +222,23 @@ public string ResolveFile(string path)
218222
}
219223

220224
var spmatches = Directory.GetFiles(searchPath, filename, SearchOption.AllDirectories);
221-
if (spmatches.Any())
225+
if (spmatches.Length != 0)
222226
{
223227
var match = spmatches.First();
224-
if (File.Exists(match))
225-
{
226-
return new FileInfo(match).FullName;
227-
}
228+
var fullPath = Path.GetFullPath(match);
229+
if (File.Exists(fullPath))
230+
return fullPath;
228231
}
229232
}
230233

231-
if (String.IsNullOrWhiteSpace(dir))
232-
{
233-
if (!File.Exists(path))
234-
path = Path.Combine(Environment.CurrentDirectory, filename);
235-
else if (!File.Exists(path))
236-
path = Path.Combine(Environment.CurrentDirectory, "bin", filename);
237-
else if (!File.Exists(path))
238-
path = Path.Combine(AppContext.BaseDirectory, filename);
239-
}
240-
else path = Path.Combine(dir, filename);
241-
242-
return new FileInfo(path).FullName;
234+
return null;
243235
}
244236

245237
public MetadataReference? TryCreateRefFromFile(string path)
246238
{
247-
path = ResolveFile(path);
248-
if (File.Exists(path))
249-
return MetadataReference.CreateFromFile(path);
239+
var resolved = TryResolveFile(path);
240+
if (resolved is not null && File.Exists(resolved))
241+
return MetadataReference.CreateFromFile(resolved);
250242
return null;
251243
}
252244

ModFramework/ModContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public class ModContext
6161
public delegate void OnContextCreated(ModContext context);
6262
public static event OnContextCreated? ContextCreated; // allows consumers to add extras into the pipeline (e.g. OTAPI Client Launcher, whom boots terraria, so it can inject cef)
6363

64-
public string BaseDirectory { get; set; } = Environment.CurrentDirectory;
64+
public static String DefaultBaseDirectory => AppContext.BaseDirectory;
65+
public string BaseDirectory { get; set; } = DefaultBaseDirectory;
6566

6667
public ModPluginLoader PluginLoader { get; set; }
6768

0 commit comments

Comments
 (0)