Skip to content

Commit

Permalink
Merge pull request #106 from nexus4880/mod-manager-changes
Browse files Browse the repository at this point in the history
Mod manager changes
  • Loading branch information
Lacyway authored Dec 13, 2024
2 parents 24f5fa8 + a0f04ae commit 6619084
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 141 deletions.
17 changes: 13 additions & 4 deletions Fuyu.Backend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Fuyu.DependencyInjection;
using Fuyu.Modding;
using System.Threading.Tasks;
using Fuyu.Common.Networking;

namespace Fuyu.Backend
{
Expand All @@ -22,15 +23,23 @@ static async Task Main()
ItemFactoryService.Load();

var coreServer = new CoreServer();
coreServer.RegisterServices();
container.RegisterSingleton<HttpServer, CoreServer>(coreServer);

coreServer.RegisterServices();
coreServer.Start();

var eftMainServer = new EftMainServer();
eftMainServer.RegisterServices();
container.RegisterSingleton<HttpServer, EftMainServer>(eftMainServer);

eftMainServer.RegisterServices();
eftMainServer.Start();

await ModManager.Instance.Load(container);
Terminal.WaitForInput();
Terminal.WriteLine("Loading mods...");
ModManager.Instance.AddMods("./Fuyu/Mods");
await ModManager.Instance.Load(container);
Terminal.WriteLine("Finished loading mods");

Terminal.WaitForInput();
await ModManager.Instance.UnloadAll();
}
}
Expand Down
61 changes: 32 additions & 29 deletions Fuyu.Common/IO/Resx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,46 @@ public static void SetSource(string id, Assembly assembly)
_assemblies.Add(id, assembly);
_fullpaths.Add(id, paths);
}
}
}

public static Stream GetStream(string id, string path)
{
// validate assembly
var hasAssembly = _assemblies.TryGetValue(id, out var assembly);
public static Stream GetStream(string id, string path)
{
// validate assembly
var hasAssembly = _assemblies.TryGetValue(id, out var assembly);

if (!hasAssembly)
{
throw new ArgumentException($"Source {id} is not registered for assemblies");
}
if (!hasAssembly)
{
throw new ArgumentException($"Source {id} is not registered for assemblies");
}

// validate fullpaths
var hasFullpath = _fullpaths.TryGetValue(id, out var fullpaths);
// validate fullpaths
var hasFullpath = _fullpaths.TryGetValue(id, out var fullpaths);

if (!hasFullpath)
{
throw new ArgumentException($"Source {id} is not registered for paths");
}
if (!hasFullpath)
{
throw new ArgumentException($"Source {id} is not registered for paths");
}

// find target
var target = $"{assembly.GetName().Name}.embedded.{path}";
// find target

// NOTE: replacing ".Resources" is, ideally, a temporary solution
// -- nexus4880, 2024-12-11
var target = $"{assembly.GetName().Name.Replace(".Resources", string.Empty)}.embedded.{path}";

foreach (var fullpath in fullpaths)
{
if (fullpath == target)
{
// target found
return assembly.GetManifestResourceStream(target);
}
}
foreach (var fullpath in fullpaths)
{
if (fullpath == target)
{
// target found
return assembly.GetManifestResourceStream(target);
}
}

// target not found
throw new FileNotFoundException($"Cannot find resource {path}");
}
// target not found
throw new FileNotFoundException($"Cannot find resource {path}");
}

public static string GetText(string id, string path)
public static string GetText(string id, string path)
{
using (var rs = GetStream(id, path))
{
Expand Down
70 changes: 53 additions & 17 deletions Fuyu.Common/IO/VFS.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.IO;

Expand Down Expand Up @@ -32,35 +33,53 @@ public static string[] GetFiles(string path)
return Directory.GetFiles(path);
}

public static void CreateDirectory(string path)
public static string[] GetFiles(string path, string pattern)
{
Directory.CreateDirectory(path);
if (!Directory.Exists(path))
{
throw new DirectoryNotFoundException($"Directory {path} doesn't exist.");
}

return Directory.GetFiles(path, pattern);
}

public static string ReadTextFile(string filepath)
public static string[] GetFiles(string path, string pattern, SearchOption search)
{
var path = Path.GetDirectoryName(filepath);

if (!DirectoryExists(path))
if (!Directory.Exists(path))
{
throw new DirectoryNotFoundException($"Directory {path} doesn't exist.");
}

if (!File.Exists(filepath))
{
throw new FileNotFoundException($"File {filepath} doesn't exist.");
}
return Directory.GetFiles(path, pattern, search);
}

using (var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.None))
public static string[] GetDirectories(string path)
{
if (!Directory.Exists(path))
{
using (var sr = new StreamReader(fs))
{
var text = sr.ReadToEnd();
return text;
}
throw new DirectoryNotFoundException($"Directory {path} doesn't exist.");
}

return Directory.GetDirectories(path);
}

public static void CreateDirectory(string path)
{
Directory.CreateDirectory(path);
}

public static string ReadTextFile(string filepath)
{
using (var fs = OpenRead(filepath))
{
using (var sr = new StreamReader(fs))
{
var text = sr.ReadToEnd();
return text;
}
}
}

// NOTE: we must prevent threads from accessing the same file at the
// same time. This way we can prevent data corruption when
// writing to the same file.
Expand Down Expand Up @@ -104,5 +123,22 @@ public static void WriteTextFile(string filepath, string text, bool append = fal
// release thread lock
_writeLock.TryRemove(filepath, out _);
}
}

public static Stream OpenRead(string filepath)
{
var path = Path.GetDirectoryName(filepath);

if (!DirectoryExists(path))
{
throw new DirectoryNotFoundException($"Directory {path} doesn't exist.");
}

if (!File.Exists(filepath))
{
throw new FileNotFoundException($"File {filepath} doesn't exist.");
}

return new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.None);
}
}
}
9 changes: 9 additions & 0 deletions Fuyu.Modding/EModType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Fuyu.Modding
{
public enum EModType
{
Invalid,
DLL,
Source
}
}
3 changes: 3 additions & 0 deletions Fuyu.Modding/Fuyu.Modding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<PropertyGroup>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fuyu.Common\Fuyu.Common.csproj" />
<ProjectReference Include="..\Fuyu.DependencyInjection\Fuyu.DependencyInjection.csproj" />
Expand Down
Loading

0 comments on commit 6619084

Please sign in to comment.