-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial experimental support for running injectors from build tasks.
- Loading branch information
Showing
21 changed files
with
197 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\CommonNetStandard.props" /> | ||
|
||
<Target Name="CopyFilesToGame" AfterTargets="CopyFilesToOutputDirectory"> | ||
<ItemGroup> | ||
<LibFiles Include="$(OutDir)*.dll" /> | ||
</ItemGroup> | ||
<Copy SourceFiles="@(LibFiles)" DestinationFolder="$(ModTekLibDir)" /> | ||
</Target> | ||
|
||
<PropertyGroup> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="ModTek" /> | ||
<InternalsVisibleTo Include="ModTek.Preloader" /> | ||
<InternalsVisibleTo Include="ModTek.InjectorRunner" /> | ||
<InternalsVisibleTo Include="ModTek.Injectors" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 9 additions & 1 deletion
10
...jectorRunner/ModTek.InjectorRunner.csproj → ModTek.Injectors/ModTek.Injectors.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.Build.Framework; | ||
|
||
namespace ModTek.InjectorsTask; | ||
|
||
// from Krafs.Publicizer | ||
internal static class TaskItemExtensions | ||
{ | ||
internal static string FileName(this ITaskItem item) | ||
{ | ||
return item.GetMetadata("Filename"); | ||
} | ||
|
||
internal static string FullPath(this ITaskItem item) | ||
{ | ||
return item.GetMetadata("Fullpath"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\CommonNetStandard.props" /> | ||
|
||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<Target Name="CopyFilesToGame" AfterTargets="CopyFilesToOutputDirectory"> | ||
<ItemGroup> | ||
<LibFiles Include="$(OutDir)*.dll;*.props;*.targets" /> | ||
</ItemGroup> | ||
<Copy SourceFiles="@(LibFiles)" DestinationFolder="$(ModTekLibDir)" /> | ||
</Target> | ||
|
||
<ItemGroup> | ||
<!-- TOOD transitive dependency to Common gets included even if Private=false --> | ||
<ProjectReference Include="..\ModTek.Injectors\ModTek.Injectors.csproj" Private="false" ExcludeAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Build.Utilities.Core"> | ||
<PrivateAssets>All</PrivateAssets> | ||
<ExcludeAssets>runtime</ExcludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project> | ||
<UsingTask | ||
TaskName="ModTek.InjectorsTask.ModTekInjectorsRunner" | ||
AssemblyFile="$(BattleTechGameDir)\Mods\ModTek\lib\ModTek.InjectorsTask.dll" | ||
/> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project> | ||
<!-- PublicizeAssemblies is from Krafs.Publicizer --> | ||
<Target | ||
Name="ModTekInjectorsRunner" | ||
DependsOnTargets="ResolveAssemblyReferences" | ||
BeforeTargets="PublicizeAssemblies;ResolveReferences" | ||
> | ||
<ModTekInjectorsRunner BattleTechGameDir="$(BattleTechGameDir)" ReferencePaths="@(ReferencePath)"> | ||
<Output TaskParameter="ReferencePathsToDelete" ItemName="_ModTekReferencePathsToDelete" /> | ||
<Output TaskParameter="ReferencePathsToAdd" ItemName="_ModTekReferencePathsToAdd" /> | ||
</ModTekInjectorsRunner> | ||
|
||
<ItemGroup> | ||
<ReferencePath Remove="@(_ModTekReferencePathsToDelete)" /> | ||
<ReferencePath Include="@(_ModTekReferencePathsToAdd)" /> | ||
</ItemGroup> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
using ModTek.Injectors; | ||
|
||
namespace ModTek.InjectorsTask; | ||
|
||
/* | ||
* see https://learn.microsoft.com/en-us/visualstudio/msbuild/tutorial-custom-task-code-generation?view=vs-2022#generate-a-console-app-and-use-the-custom-task | ||
* see https://github.com/krafs/Publicizer/blob/main/src/Publicizer/Krafs.Publicizer.targets and props | ||
* - run before Rafs.Publicizer | ||
* - needs to modify the references? or just push them ahead so they are found? | ||
* - see Rafs ReferencePathsToDelete and ReferencePathsToAdd and ReferencePaths etc.... | ||
*/ | ||
public class ModTekInjectorsRunner : Task | ||
{ | ||
[Required] | ||
public string BattleTechGameDir { get; set; } = null!; | ||
|
||
[Required] | ||
public ITaskItem[] ReferencePaths { get; set; } = null!; | ||
|
||
[Output] | ||
public ITaskItem[] ReferencePathsToDelete { get; set; } = []; | ||
|
||
[Output] | ||
public ITaskItem[] ReferencePathsToAdd { get; set; } = []; | ||
|
||
public override bool Execute() | ||
{ | ||
var executingAssembly = Assembly.GetExecutingAssembly(); | ||
var applicationDirectory = Path.GetDirectoryName(executingAssembly.Location)!; | ||
|
||
var currentDomain = AppDomain.CurrentDomain; | ||
currentDomain.AssemblyResolve += (_, args) => | ||
{ | ||
var resolvingName = new AssemblyName(args.Name); | ||
var path = Path.Combine(applicationDirectory, resolvingName.Name + ".dll"); | ||
Log.LogWarning($"Loading assembly {args.Name} from {path}"); | ||
return Assembly.LoadFrom(path); | ||
}; | ||
|
||
Runner.Run(); | ||
|
||
var referencePathsToAdd = new List<ITaskItem>(); | ||
var referencePathsToDelete = new List<ITaskItem>(); | ||
foreach (var path in Runner.GetInjectedPaths()) | ||
{ | ||
var filename = Path.GetFileNameWithoutExtension(path); | ||
foreach (var referencePath in ReferencePaths) | ||
{ | ||
if (filename == referencePath.FileName()) | ||
{ | ||
var newReference = new TaskItem(path); | ||
referencePathsToAdd.Add(newReference); | ||
referencePathsToDelete.Add(referencePath); | ||
break; | ||
} | ||
} | ||
} | ||
ReferencePathsToDelete = referencePathsToDelete.ToArray(); | ||
ReferencePathsToAdd = referencePathsToAdd.ToArray(); | ||
return !Log.HasLoggedErrors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.