dnSpyEx / dnSpy

Unofficial revival of the well known .NET debugger and assembly editor, dnSpy
GNU General Public License v3.0
6.97k stars 458 forks source link

Support modifying .dlls via CLI #318

Open lerarosalene opened 6 months ago

lerarosalene commented 6 months ago

Problem Description

I am currently able to modify .dlls via editing C# sources in GUI. But there seems to be no documented way to patch .dll via dnSpy.Console.exe.

I suspect that it should be possible to decompile .dll fully to source code, introduce changes and compile it back, but I am not sure how (and if contents of decompiled files are guaranteed to be the same to reliable apply diff patches). But if it's the intended solution, then I'm dumb, sorry.

Proposal

Add CLI flags to introduce changes to .dlls based on modified source code of methods.

Alternatives

No response

Additional Context

No response

ElektroKill commented 6 months ago

The CLI app is very primitive and is currently written in a way that allows only decompilation of a given file. It lacks all the necessary infrastructure to run the other components of dnSpy like assembly editing, debugging, etc. Extending the CLI app to add more functionality is something I want to do especially for debugging.

However, I'm not sure how text-based C# level assembly editing would be implemented in a CLI. If you want to apply C# level patches it is probably best to fully decompile the file using dnSpy or ILSpy to produce a C# project file/solution file, apply text-based patches onto the source code, and then recompile. It is not the ideal way of applying patches to files though. The C# editing is only meant to be a quick way of applying patches to files manually. Automated patching should always do what is described below.

Ideally, you'd want to use a lower-level library like dnlib and edit assemblies on the metadata and CIL code level. This is a lower level than C# and makes it so you do not have to recompile the code. Libraries like dnlib can open a .exe and traverse the type structure similar to dnSpy and edit methods and method bodies in a way similar to Edit Method dialog and Edit IL instructions option. This ensures that parts of code you are not patching do not undergo changes as a result of decompilation errors.

Hope this answers your questions!