Skip to content

Commit

Permalink
In the middle of something...
Browse files Browse the repository at this point in the history
  • Loading branch information
steviegt6 committed Jan 26, 2023
1 parent ae37351 commit 8bd212a
Show file tree
Hide file tree
Showing 24 changed files with 435 additions and 54 deletions.
Binary file not shown.
Binary file added lib/Mono.Cecil/Mono.Cecil.Mdb.dll
Binary file not shown.
Binary file added lib/Mono.Cecil/Mono.Cecil.Mdb.pdb
Binary file not shown.
Binary file added lib/Mono.Cecil/Mono.Cecil.Pdb.dll
Binary file not shown.
Binary file added lib/Mono.Cecil/Mono.Cecil.Pdb.pdb
Binary file not shown.
Binary file added lib/Mono.Cecil/Mono.Cecil.Rocks.dll
Binary file not shown.
Binary file added lib/Mono.Cecil/Mono.Cecil.Rocks.pdb
Binary file not shown.
Binary file added lib/Mono.Cecil/Mono.Cecil.dll
Binary file not shown.
Binary file added lib/Mono.Cecil/Mono.Cecil.pdb
Binary file not shown.
10 changes: 9 additions & 1 deletion lib/README.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Mono.Cecil
version 0.11.4.0
framework netstandard2.0

MonoMod.RuntimeDetour
version 22.07.31.01
framework netstandard2.0
framework netstandard2.0

MMHOOK_Microsoft.CodeAnalysis.CSharp
version 4.4.0
framework todo
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
<PackageReference Include="NUnit.Analyzers" Version="3.3.0"/>
<PackageReference Include="coverlet.collector" Version="3.1.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CleElum.Bootstrapper.Analyzer\CleElum.Bootstrapper.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true"/>
<ProjectReference Include="..\CleElum.Bootstrapper.Analyzer\CleElum.Bootstrapper.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<ProjectReference Include="..\CleElum.UnrestrictedNameOf.Analyzer\CleElum.UnrestrictedNameOf.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
</ItemGroup>

</Project>
11 changes: 0 additions & 11 deletions src/CleElum.Bootstrapper.Analyzer.Tests/UnitTest1.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace CleElum.Bootstrapper.Analyzer.Tests.UnrestrictedNameOf;

[TestFixture]
public static class UnrestrictedNameOfTests {
private class A {
private int B;
}

[TestCase(nameof(A), "A")]
//[TestCase(nameof(A.B), "B")]
public static void TestNameOf(string nameOf, string actual) {
Assert.Equals(nameOf, actual);
}
}
135 changes: 110 additions & 25 deletions src/CleElum.Bootstrapper.Analyzer/BootstrapAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using MonoMod.RuntimeDetour.HookGen;
using MonoMod.Utils;

namespace CleElum.Bootstrapper.Analyzer;

Expand All @@ -25,7 +28,11 @@ public sealed class BootstrapAnalyzer : DiagnosticAnalyzer {
isEnabledByDefault: true,
description: INITIALIZED_FAILED_DESCRIPTION,
helpLinkUri: null,
customTags: "CompilationEnd"
customTags: new[] {
Build,
Compiler,
CompilationEnd,
}
);

private static readonly DiagnosticDescriptor already_initialized = new(
Expand All @@ -37,27 +44,31 @@ public sealed class BootstrapAnalyzer : DiagnosticAnalyzer {
isEnabledByDefault: true,
description: ALREADY_INITIALIZED_DESCRIPTION,
helpLinkUri: null,
customTags: "CompilationEnd"
customTags: CompilationEnd
);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(initialization_failed, already_initialized);

private static readonly List<Action<bool>> actions = new();
// private static readonly List<Action<bool>> actions = new();
private static bool initialized;
private static bool initializedTwice;

// private static bool initializedTwice;
private static Exception? exception;

public override void Initialize(AnalysisContext context) {
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

context.RegisterCompilationStartAction(ctx => {
ctx.RegisterSymbolStartAction(_ => { }, default);
});
context.RegisterCompilationAction(ctx => {
if (initializedTwice) {
/*if (initializedTwice) {
ctx.ReportDiagnostic(
Diagnostic.Create(already_initialized, Location.None)
);
}
}*/

if (exception is null)
return;
Expand All @@ -71,64 +82,79 @@ public override void Initialize(AnalysisContext context) {
);
});

EnsureInitialized();
}

public static void EnsureInitialized() {
if (initialized) {
initializedTwice = true;
// initializedTwice = true;
return;
}

try {
EnsureAssemblies();
InitializeMonoMod();
}
catch (Exception e) {
exception = e;
}
finally {
// Don't attempt to initialize again, even if it failed.
initialized = true;

// Run post-initialization actions.
foreach (var action in actions)
action(exception is not null);
actions.Clear();
}

Patch();
}

private const string ce_ns = "CleElum.Bootstrapper.Analyzer.";
private const string ext = ".dll";

private static readonly string[] expected_assemblies = {
"CleElum.Bootstrapper.Analyzer.MonoMod.Common.dll",
"CleElum.Bootstrapper.Analyzer.MonoMod.Utils.dll",
"CleElum.Bootstrapper.Analyzer.MonoMod.dll",
"CleElum.Bootstrapper.Analyzer.MonoMod.RuntimeDetour.dll",
"Mono.Cecil",
"Mono.Cecil.Rocks",
"Mono.Cecil.Pdb",
"Mono.Cecil.Mdb",
"MonoMod.Common",
"MonoMod.Utils",
"MonoMod",
"MonoMod.RuntimeDetour",
// "MMHOOK_Microsoft.CodeAnalysis.CSharp",
};

private static readonly Dictionary<string, Assembly> assembly_map = new();

private static void EnsureAssemblies() {
var asm = typeof(BootstrapAnalyzer).Assembly;

foreach (var assembly in expected_assemblies) {
var stream = asm.GetManifestResourceStream(assembly);
var fileName = ce_ns + assembly + ext;
var stream = asm.GetManifestResourceStream(fileName);
if (stream is null)
throw new InvalidOperationException(
$"Could not find embedded resource {assembly}"
$"Could not find embedded resource {fileName}"
);

var bytes = new byte[stream.Length];
var read = stream.Read(bytes, 0, bytes.Length);
if (read != bytes.Length)
throw new InvalidOperationException(
$"Could not read embedded resource {assembly}"
$"Could not read embedded resource {fileName}"
);

#pragma warning disable RS1035
Assembly.Load(bytes);
assembly_map[assembly] = Assembly.Load(bytes);
#pragma warning restore RS1035
}
}

private static void InitializeMonoMod() {
// TODO: Anything important to do here?
AppDomain.CurrentDomain.AssemblyResolve += (_, args) => {
var name = new AssemblyName(args.Name);

return assembly_map.ContainsKey(name.Name)
? assembly_map[name.Name]
: null;
};
}

/// <summary>
/*/// <summary>
/// Executes the given <paramref name="action"/> post-initialization.
/// </summary>
/// <param name="action">The action to execute.</param>
Expand All @@ -141,5 +167,64 @@ public static void ExecuteAfterInitialization(Action<bool> action) {
action(exception is not null);
else
actions.Add(action);
}*/

private static void Patch() {
var asm = typeof(LanguageVersion).Assembly;

var ac = asm.GetType("Microsoft.CodeAnalysis.CSharp.AccessCheck");
var isac = ac.GetMethod(
"IsSymbolAccessibleCore",
BindingFlags.Static | BindingFlags.NonPublic
);

// On_AccessCheck.IsSymbolAccessibleCore += MakeAccessible;
HookEndpointManager.Add(isac, MakeAccessible);

/*var type = asm.GetType("Microsoft.CodeAnalysis.CSharp.NameofBinder");
var meth = type?.GetMethod(
"LookupSymbolsInSingleBinder",
BindingFlags.NonPublic | BindingFlags.Instance
);
if (type is null || meth is null)
return;
HookEndpointManager.Add(
meth,
Test
);*/
}

private delegate void OrigIsSymbolAccessibleCore(
object symbol,
object within,
object throughTypeOpt,
out bool failedThroughTypeCheck,
CSharpCompilation compilation,
IntPtr useSiteInfo,
object basesBeingResolved
);

private static bool MakeAccessible(
OrigIsSymbolAccessibleCore orig,
object symbol,
object within,
object throughTypeOpt,
out bool failedThroughTypeCheck,
CSharpCompilation compilation,
IntPtr useSiteInfo,
object basesBeingResolved
) {
orig(
symbol,
within,
throughTypeOpt,
out failedThroughTypeCheck,
compilation,
useSiteInfo,
basesBeingResolved
);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="../version.targets" />
<Import Project="../version.targets"/>

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!--<IncludeBuildOutput>false</IncludeBuildOutput>-->
<IncludeBuildOutput>false</IncludeBuildOutput>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
Expand All @@ -13,33 +13,44 @@
<FileVersion>$(SharedVersion)</FileVersion>
<AssemblyVersion>$(SharedVersion)</AssemblyVersion>

<developmentDependency>true</developmentDependency>
<NoWarn>$(SuppressWarnings);MSB3277</NoWarn>
</PropertyGroup>

<ItemGroup>
<None Include="$(OutputPath)\netstandard2.0\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\netstandard2.0\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="AnalyzerReleases.Shipped.md" />
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />
<AdditionalFiles Include="AnalyzerReleases.Shipped.md"/>
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md"/>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="../../lib/MonoMod.RuntimeDetour/*.dll" />
<EmbeddedResource Include="../../lib/Mono.Cecil/*.dll"/>
<EmbeddedResource Include="../../lib/MonoMod.RuntimeDetour/*.dll"/>
<!-- <EmbeddedResource Include="../../lib/\MMHOOK_Microsoft.CodeAnalysis.CSharp/*.dll"/> -->
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1"/>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0"/>
</ItemGroup>

<ItemGroup>
<!-- <Reference Include="MMHOOK_Microsoft.CodeAnalysis.CSharp">
<Private>false</Private>
<HintPath>..\..\lib\MMHOOK_Microsoft.CodeAnalysis.CSharp\MMHOOK_Microsoft.CodeAnalysis.CSharp.dll</HintPath>
</Reference> -->
<Reference Include="MonoMod.Utils">
<Private>false</Private>
<HintPath>..\..\lib\MonoMod.RuntimeDetour\MonoMod.Utils.dll</HintPath>
</Reference>
<Reference Include="MonoMod.RuntimeDetour">
<Private>false</Private>
<HintPath>..\..\lib\MonoMod.RuntimeDetour\MonoMod.RuntimeDetour.dll</HintPath>
</Reference>
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/CleElum.Bootstrapper.Analyzer/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal static class Constants {

#region Categories
public const string CATEGORY_BOOTSTRAP = "Bootstrap";
public const string CATEGORY_PATCH = "Patch";
#endregion

#region Titles
Expand Down
3 changes: 2 additions & 1 deletion src/CleElum.Bootstrapper.Analyzer/Usings.cs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
global using static CleElum.Bootstrapper.Analyzer.Constants;
global using static CleElum.Bootstrapper.Analyzer.Constants;
global using static Microsoft.CodeAnalysis.WellKnownDiagnosticTags;
14 changes: 14 additions & 0 deletions src/CleElum.HookGen/CleElum.HookGen.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="22.7.31.1"/>
</ItemGroup>

</Project>
Loading

0 comments on commit 8bd212a

Please sign in to comment.