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

Mod changes #115

Merged
merged 6 commits into from
Jan 8, 2025
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
2 changes: 1 addition & 1 deletion Fuyu.Modding/Fuyu.Modding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
</ItemGroup>
<ItemGroup>
Expand Down
214 changes: 111 additions & 103 deletions Fuyu.Modding/ModManager.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Fuyu.Common.IO;
using Fuyu.DependencyInjection;
#if NET
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
#endif

namespace Fuyu.Modding
{
Expand Down Expand Up @@ -38,37 +39,6 @@ private string GetResourcePath(string resourceRootPath, string resourcePath)
.Replace("/", "."); // unix /
}

private CSharpCompilation CreateCompilation(
string assemblyName,
IEnumerable<SyntaxTree> syntaxTrees,
bool includeReferences)
{
// TODO: Simplify this later
IEnumerable<PortableExecutableReference> references;

if (includeReferences)
{
references =
// Mods can reference everything we have loaded
AppDomain.CurrentDomain.GetAssemblies()
// Where it is a disk on file
.Where(a => !string.IsNullOrEmpty(a.Location))
// Create a MetadataReference from it
.Select(a => MetadataReference.CreateFromFile(a.Location));
}
else
{
references = Array.Empty<PortableExecutableReference>();
}

return CSharpCompilation.Create(
assemblyName,
syntaxTrees,
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
}

public void AddMods(string directory)
{
if (!VFS.DirectoryExists(directory))
Expand All @@ -79,19 +49,22 @@ public void AddMods(string directory)

var subdirectories = VFS.GetDirectories(directory);

foreach (var modDirectory in subdirectories)
foreach (var subdirectory in subdirectories)
{
var modDirectory = Path.GetFullPath(subdirectory);
var modType = GetModType(modDirectory);

switch (modType)
{
case EModType.DLL:
ProcessDLLMod(modDirectory);
break;
#if NET
case EModType.Source:
ProcessSourceFiles(modDirectory);
break;
case EModType.Invalid:
#endif
default:
throw new Exception($"{modDirectory} does not contain a valid mod setup");
}
}
Expand Down Expand Up @@ -126,77 +99,9 @@ private void ProcessDLLMod(string directory)
}
}

private void ProcessSourceFiles(string directory)
{
var sourceFiles = VFS.GetFiles(Path.Combine(directory, "src"), "*.cs", SearchOption.AllDirectories);

if (sourceFiles.Length == 0)
{
throw new Exception($"{directory} was marked as a source mod yet no source files were found");
}

var assemblyName = Path.GetFileName(directory);
var syntaxTrees = new List<SyntaxTree>(sourceFiles.Length);

foreach (var file in sourceFiles)
{
var fileContents = VFS.ReadTextFile(file);
var syntaxTree = CSharpSyntaxTree.ParseText(
fileContents,
new CSharpParseOptions(
LanguageVersion.Latest,
DocumentationMode.None,
SourceCodeKind.Regular
),
file
);

syntaxTrees.Add(syntaxTree);
}

var resourceRootPath = Path.Combine(directory, "res");
var resourcePaths = VFS.GetFiles(resourceRootPath, "*.*", SearchOption.AllDirectories);
var resources = resourcePaths.Select(resourcePath =>
{
var fileName = $"{assemblyName}.Resources.{GetResourcePath(resourceRootPath, resourcePath)}";

return new ResourceDescription(
fileName,
() => VFS.OpenRead(resourcePath),
isPublic: true
);
}).ToArray();

var compilation = CreateCompilation(assemblyName, syntaxTrees, true);

Assembly assembly;

// Intentionally left open so that we can use it later
using (var ms = new MemoryStream())
{
var emitResult = compilation.Emit(ms, manifestResources: resources);
if (!emitResult.Success)
{
var errors = emitResult.Diagnostics
.Where(d => !d.IsSuppressed && d.Severity == DiagnosticSeverity.Error);

// Technically no cleanup is being done here but that's because this is considered a
// failed state and the ModManager should no longer live (and therefore the program)
// -- nexus4880, 2024-12-3

throw new Exception(string.Join(Environment.NewLine, errors));
}

ms.Position = 0;
assembly = Assembly.Load(ms.ToArray());
}

Resx.SetSource(assemblyName, assembly);
ProcessAssembly(assembly, EModType.Source);
}

private void ProcessAssembly(Assembly assembly, EModType assemblyModType)
{
#if NET
if (assemblyModType == EModType.DLL)
{
var resourceAssembly = GenerateResourceAssembly(assembly);
Expand All @@ -205,6 +110,7 @@ private void ProcessAssembly(Assembly assembly, EModType assemblyModType)
Resx.SetSource(assembly.GetName().Name, resourceAssembly);
}
}
#endif

// Get types where T inherits from Mod
var modTypes = assembly.GetExportedTypes()
Expand Down Expand Up @@ -289,6 +195,107 @@ private async Task UnloadMod(Mod mod)

_mods.Remove(mod);
}

#if NET
private CSharpCompilation CreateCompilation(
string assemblyName,
IEnumerable<SyntaxTree> syntaxTrees,
bool includeReferences)
{
// TODO: Simplify this later
IEnumerable<PortableExecutableReference> references;

if (includeReferences)
{
references =
// Mods can reference everything we have loaded
AppDomain.CurrentDomain.GetAssemblies()
// Where it is a disk on file
.Where(a => !string.IsNullOrEmpty(a.Location))
// Create a MetadataReference from it
.Select(a => MetadataReference.CreateFromFile(a.Location));
}
else
{
references = Array.Empty<PortableExecutableReference>();
}

return CSharpCompilation.Create(
assemblyName,
syntaxTrees,
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
}

private void ProcessSourceFiles(string directory)
{
var sourceFiles = VFS.GetFiles(Path.Combine(directory, "src"), "*.cs", SearchOption.AllDirectories);

if (sourceFiles.Length == 0)
{
throw new Exception($"{directory} was marked as a source mod yet no source files were found");
}

var assemblyName = Path.GetFileName(directory);
var syntaxTrees = new List<SyntaxTree>(sourceFiles.Length);

foreach (var file in sourceFiles)
{
var fileContents = VFS.ReadTextFile(file);
var syntaxTree = CSharpSyntaxTree.ParseText(
fileContents,
new CSharpParseOptions(
LanguageVersion.Latest,
DocumentationMode.None,
SourceCodeKind.Regular
),
file
);

syntaxTrees.Add(syntaxTree);
}

var resourceRootPath = Path.Combine(directory, "res");
var resourcePaths = VFS.GetFiles(resourceRootPath, "*.*", SearchOption.AllDirectories);
var resources = resourcePaths.Select(resourcePath =>
{
var fileName = $"{assemblyName}.Resources.{GetResourcePath(resourceRootPath, resourcePath)}";

return new ResourceDescription(
fileName,
() => VFS.OpenRead(resourcePath),
isPublic: true
);
}).ToArray();

var compilation = CreateCompilation(assemblyName, syntaxTrees, true);

Assembly assembly;

// Intentionally left open so that we can use it later
using (var ms = new MemoryStream())
{
var emitResult = compilation.Emit(ms, manifestResources: resources);
if (!emitResult.Success)
{
var errors = emitResult.Diagnostics
.Where(d => !d.IsSuppressed && d.Severity == DiagnosticSeverity.Error);

// Technically no cleanup is being done here but that's because this is considered a
// failed state and the ModManager should no longer live (and therefore the program)
// -- nexus4880, 2024-12-3

throw new Exception(string.Join(Environment.NewLine, errors));
}

ms.Position = 0;
assembly = Assembly.Load(ms.ToArray());
}

Resx.SetSource(assemblyName, assembly);
ProcessAssembly(assembly, EModType.Source);
}

private Assembly GenerateResourceAssembly(Assembly sourceAssembly)
{
Expand Down Expand Up @@ -344,5 +351,6 @@ private Assembly GenerateResourceAssembly(Assembly sourceAssembly)
return resourceAssembly;
}
}
#endif
}
}
29 changes: 18 additions & 11 deletions Fuyu.Plugin.Arena/Plugin.cs → Fuyu.Plugin.Arena/ArenaMod.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
using BepInEx;
using System.Threading.Tasks;
using Fuyu.Common.IO;
using Fuyu.DependencyInjection;
using Fuyu.Modding;
using Fuyu.Plugin.Arena.Patches;
using Fuyu.Plugin.Arena.Utils;
using Fuyu.Plugin.Common.Reflection;
using Fuyu.Plugin.Common.Utils;

namespace Fuyu.Plugin.Arena
{
[BepInPlugin("com.fuyu.plugin.arena", "Fuyu.Plugin.Arena", "1.0.0")]
public class Plugin : BaseUnityPlugin
public class ArenaMod : Mod
{
private readonly APatch[] _patches;

public Plugin()
public override string Id { get; } = "com.fuyu.plugin.arena";

public override string Name { get; } = "Fuyu.Plugin.Arena";

public ArenaMod()
{
_patches = new APatch[]
{
Expand All @@ -20,11 +25,9 @@ public Plugin()
};
}

protected void Awake()
public override Task OnLoad(DependencyContainer container)
{
LogWriter.Initialize(Logger, GetType().Assembly);

LogWriter.WriteLine("Patching...");
Terminal.WriteLine("Patching...");

// TODO: disable when running on HTTPS
// -- seionmoya, 2024-11-19
Expand All @@ -34,16 +37,20 @@ protected void Awake()
{
patch.Enable();
}

return Task.CompletedTask;
}

protected void OnApplicationQuit()
public override Task OnShutdown()
{
LogWriter.WriteLine("Unpatching...");
Terminal.WriteLine("Unpatching...");

foreach (var patch in _patches)
{
patch.Disable();
}

return Task.CompletedTask;
}
}
}
4 changes: 0 additions & 4 deletions Fuyu.Plugin.Arena/Fuyu.Plugin.Arena.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BepInEx.Core" Version="5.4.21" IncludeAssets="compile" />
<PackageReference Include="HarmonyX" Version="2.7.0" IncludeAssets="compile" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" IncludeAssets="compile" />
<PackageReference Include="UnityEngine.Modules" Version="2022.3.43" IncludeAssets="compile" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Fuyu.Plugin.Arena/Utils/ProtocolUtil.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Fuyu.Common.IO;
using HarmonyLib;
using Fuyu.Plugin.Arena.Reflection;
using Fuyu.Plugin.Common.Utils;

namespace Fuyu.Plugin.Arena.Utils
{
Expand All @@ -14,7 +14,7 @@ public static class ProtocolUtil
// -- seionmoya, 2024/08/xx
public static void RemoveTransportPrefixes()
{
LogWriter.WriteLine("Removing transport prefixes...");
Terminal.WriteLine("Removing transport prefixes...");

var target = "TransportPrefixes";
var type = PatchHelper.Types.Single(t => t.GetField(target) != null);
Expand Down
Loading