From 3295afc44771a773be74e31ca8decdcf93aab552 Mon Sep 17 00:00:00 2001 From: pas2704 Date: Mon, 12 May 2025 16:34:31 -0400 Subject: [PATCH] Add syntax rewriter to handle missing usings we haven't added Added Microsoft.VisualBasic to missing namespace patch --- .../Patches/DarkTardMissingNamespacePatch.cs | 1 + CringeLauncher/Patches/ModRewriterPatch.cs | 18 ++++++++++ .../SyntaxRewriters/MissingUsingRewriter.cs | 33 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 CringeLauncher/Patches/ModRewriterPatch.cs create mode 100644 CringeLauncher/SyntaxRewriters/MissingUsingRewriter.cs diff --git a/CringeLauncher/Patches/DarkTardMissingNamespacePatch.cs b/CringeLauncher/Patches/DarkTardMissingNamespacePatch.cs index d1c8d65..93ad4e9 100644 --- a/CringeLauncher/Patches/DarkTardMissingNamespacePatch.cs +++ b/CringeLauncher/Patches/DarkTardMissingNamespacePatch.cs @@ -16,5 +16,6 @@ public static class DarkTardMissingNamespacePatch ___m_compatibilityChanges["using System.Runtime.Remoting.Lifetime;"] = ""; ___m_compatibilityChanges["using System.Net.Configuration;"] = ""; ___m_compatibilityChanges["using System.Reflection.Metadata.Ecma335;"] = ""; + ___m_compatibilityChanges["using Microsoft.VisualBasic;"] = ""; } } \ No newline at end of file diff --git a/CringeLauncher/Patches/ModRewriterPatch.cs b/CringeLauncher/Patches/ModRewriterPatch.cs new file mode 100644 index 0000000..b69ef1e --- /dev/null +++ b/CringeLauncher/Patches/ModRewriterPatch.cs @@ -0,0 +1,18 @@ +using HarmonyLib; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis; +using VRage.Scripting; +using CringeLauncher.SyntaxRewriters; + +namespace CringeLauncher.Patches; + +[HarmonyPatch(typeof(MyScriptCompiler), "InjectMod")] +internal static class ModRewriterPatch +{ + public static void Prefix(ref CSharpCompilation compilation, ref SyntaxTree syntaxTree) + { + var fixedSyntaxTree = MissingUsingRewriter.Rewrite(compilation, syntaxTree); + compilation = compilation.ReplaceSyntaxTree(syntaxTree, fixedSyntaxTree); + syntaxTree = fixedSyntaxTree; + } +} diff --git a/CringeLauncher/SyntaxRewriters/MissingUsingRewriter.cs b/CringeLauncher/SyntaxRewriters/MissingUsingRewriter.cs new file mode 100644 index 0000000..1c71f56 --- /dev/null +++ b/CringeLauncher/SyntaxRewriters/MissingUsingRewriter.cs @@ -0,0 +1,33 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using System.Diagnostics; + +namespace CringeLauncher.SyntaxRewriters; +internal sealed class MissingUsingRewriter : CSharpSyntaxRewriter +{ + private readonly SemanticModel _semanticModel; + private MissingUsingRewriter(CSharpCompilation compilation, SyntaxTree tree) => _semanticModel = compilation.GetSemanticModel(tree); + + public static SyntaxTree Rewrite(CSharpCompilation compilation, SyntaxTree tree) + { + SyntaxNode syntaxNode = new MissingUsingRewriter(compilation, tree).Visit(tree.GetRoot()); + return tree.WithRootAndOptions(syntaxNode, tree.Options); + } + + public override SyntaxNode? VisitUsingDirective(UsingDirectiveSyntax node) + { + var visited = base.VisitUsingDirective(node); + + if (visited is not UsingDirectiveSyntax usingDirective) + return visited; + + var symbolInfo = _semanticModel.GetSymbolInfo(node.NamespaceOrType); + + if (symbolInfo.Symbol is INamespaceOrTypeSymbol) + return usingDirective; + + Debug.WriteLine($"Missing using: {usingDirective}"); + return null; + } +} \ No newline at end of file