icsharpcode / ILSpy

.NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform!
20.97k stars 3.31k forks source link

Allow a program driving ILSpy.Decompilation to provide variable names during decompilation #3155

Open Velociraptor115 opened 6 months ago

Velociraptor115 commented 6 months ago

Is your feature request related to a problem? Please describe. Some assemblies have huge methods with complex logic. ILSpy decompiles them with generic variable names. It takes time to understand the meaning of the variables. It would be helpful for programs that specialize the decompiling capability provided by ILSpy for their specific use cases, if ILSpy would provide a way for the driver program to hook into the variable name generation part

Describe the solution you'd like An interface or hook for the driver program to provide suggested names to ILVariables

Additional context In the C# game modding context, we would like to track changes to the main assembly and possibly share our own local name mapping with fellow modders (it would be problematic to share the entire decompiled source). I have modified ILSpy a tiny bit here to help provide a map of local names with the driver program I believe this is a common problem for modders in general, so it would be beneficial to have a common, better way of doing this

dgrunwald commented 6 months ago

There's already an interface IDebugInfoProvider intended for proving pluggable support for debug information (e.g. different .pdb file readers). This includes the functionality to provide variable names for local slots via the TryGetName method.

Velociraptor115 commented 6 months ago

That is what I used initially However, it doesn't cover StackSlot variables - the intermediate variables introduced by ILSpy for the sake of readability What I'm describing is a way to extend or hook into the name generation part, rather than a mapping of locals to variable names

Velociraptor115 commented 6 months ago

I have noticed a related issue The variable name generation in ILSpy.Decompilation performs name collision checks on the entire method, I assume for performance and simplicity reasons.

For the reasons stated initially in this thread, long methods result in loop index variables, loop enumerator variables, and similar limited scope variables having unnecessary number suffixes appended to them.

Would modifying the logic to perform scope-based name collision detection be a good enhancement to ILSpy.Decompilation in general?