Documentation
¶
Overview ¶
Package dotnet reads compiled .NET assemblies without running them: ECMA-335 metadata tables, IL method bodies, portable PDB sequence points, and the custom attributes that Harmony, BepInEx 5 and MelonLoader define. It parses PE files with debug/pe, does no reflection and loads no code.
Every query takes a path. A crafted or truncated image yields an error wrapping ErrMalformed, never a panic, and a file over MaxAssemblyBytes yields ErrTooLarge without being read.
Index ¶
- Constants
- Variables
- func Identity(path string) (mvid, symbols string)
- func PatchKindByName(method string) string
- func SourceLine(path, method string, params, il int) string
- func Usages(path, target string) ([]string, error)
- func Writes(path, game, wrapper string) ([]string, error)
- type Declared
- type Diagnostic
- type Melon
- type Member
- type Patch
- type Plugin
- type Relation
- type Setting
Constants ¶
const ( // MaxAssemblyBytes is the largest assembly a query reads; a larger file is refused unread with ErrTooLarge. MaxAssemblyBytes = 256 << 20 // MaxPDBBytes bounds a PDB, read from a file or inflated from an embedded one. MaxPDBBytes = 64 << 20 )
const ( HardDependency = "hard" SoftDependency = "soft" Incompatible = "incompatible" )
Kinds of Relation.
const ( // PatchSkip is a prefix that returns bool, so it can skip the original method. PatchSkip = "prefix-skip" PatchPrefix = "prefix" PatchPostfix = "postfix" PatchTranspiler = "transpiler" PatchFinalizer = "finalizer" // PatchHook is a MonoMod HookGen On hook, which wraps the original and can skip it like a skipping prefix. PatchHook = "hook" )
Kinds of Patch.
const ( // SymbolsPortable is a portable PDB, embedded in the DLL or beside it, which Mono reads for source lines. SymbolsPortable = "portable" // SymbolsWindows is a Windows (MSF) PDB beside the DLL, which Mono cannot read. SymbolsWindows = "windows" )
What Identity finds of an assembly's debug symbols.
Variables ¶
var ( // ErrNotAssembly marks a file that is not a PE image with a CLI header. ErrNotAssembly = errors.New("not a .NET assembly") // ErrMalformed marks an assembly whose metadata is truncated, inconsistent or crafted to exhaust the reader. ErrMalformed = errors.New("malformed assembly") // ErrTooLarge marks a file over MaxAssemblyBytes or MaxPDBBytes. ErrTooLarge = errors.New("file exceeds the size cap") )
Functions ¶
func Identity ¶
Identity reads the module version id of the assembly at path, as the runtime prints it in a stack frame it has no symbols for ("<mvid>:IL_002E", Guid.ToString("N")), and which symbols it ships: SymbolsPortable, SymbolsWindows or "". A file that is no assembly has neither.
func PatchKindByName ¶
PatchKindByName is the kind Harmony gives a patch method by its name alone (Prefix, Postfix, Transpiler, Finalizer), or "".
func SourceLine ¶
SourceLine is the source file's base name and line ("Patch.cs:17") of IL offset il in method ("Namespace.Type::Method", taking params arguments, or -1 when unknown) of the assembly at path, read from its portable PDB, embedded or beside it. It is "" when the assembly has none, when the PDB is not the one built with this DLL (a weaver rewrote the DLL and left the PDB), when the name matches no method or several overloads, and when the method has no sequence point at or before il.
func Usages ¶
Usages lists the methods of the assembly at path whose code calls, constructs, loads or stores target, named "Namespace.Type::Member" as Members names it; each caller is "Type::Method", once, in table order.
func Writes ¶
Writes lists the members of types in namespace game (or beneath it) that the assembly's method bodies assign, as "Namespace.Type::Member" with any set_ prefix removed: property setter calls and field stores through member references. A set_Value call on a type in namespace wrapper (a field wrapper such as Netcode's NetInt) counts as assigning the game member that loaded the wrapper. Instance writes to an object the same method built (an object initializer, or a local only ever assigned a new object) configure the mod's own object and are left out.
Types ¶
type Diagnostic ¶ added in v0.3.0
type Diagnostic struct {
File string `json:"file"`
Line int `json:"line"`
Column int `json:"column"`
Severity string `json:"severity"`
Code string `json:"code"`
Message string `json:"message"`
}
Diagnostic is one compiler error or warning, at its file and line.
func Diagnostics ¶ added in v0.3.0
func Diagnostics(out string) []Diagnostic
Diagnostics reads MSBuild's output for errors and warnings, each once and in order.
type Melon ¶
type Melon struct {
// Assembly is the assembly's simple name, which other melons name as a dependency.
Assembly string
Name, Version, Author string
// Priority is MelonPriority; lower loads first.
Priority int
Refs, Additional, Optional []string
HasInfo bool
// Cycle is a melon MelonLoader leaves out because its dependencies depend on it.
Cycle bool
}
Melon is what a MelonLoader mod or plugin assembly says about itself: its [assembly: MelonInfo], [MelonPriority], [MelonAdditionalDependencies] and [MelonOptionalDependencies] attributes (MelonLoader/Attributes), and the assemblies it references, which MelonLoader treats as dependencies too.
func MelonOrder ¶
MelonOrder is the order MelonLoader registers melons of one kind (plugins, or mods) in (MelonBase.SortMelons): a topological sort in which a melon follows the melons whose assemblies it references or names in MelonAdditionalDependencies, the loadable melon with the smallest name going first, then a stable sort by priority. A melon in a dependency cycle is not loaded; the ones MelonLoader reports are returned last with Cycle set.
ponytail: names compare lower-cased ordinal where MelonLoader's SortedList uses the current culture.
type Member ¶
type Member struct {
Type string `json:"type"`
// Kind is "method" or "field".
Kind string `json:"kind"`
Method string `json:"method"`
Static bool `json:"static"`
// Void is set when the method returns nothing.
Void bool `json:"void"`
Params []string `json:"params"`
// Types are the parameters' types as C# writes them, in Params' order; Returns is the method's return type, or a
// field's type ("void", "int", "GameNetcodeStuff.PlayerControllerB", "List<Tile>").
Types []string `json:"types"`
Returns string `json:"returns"`
}
Member is one method or field an assembly defines: its type as a stack frame names it ("Namespace.Outer+Inner"), and a method's parameters' names in order.
type Patch ¶
type Patch struct{ Target, Kind, Method string }
Patch is one Harmony patch an assembly declares with attributes: the method it targets as "Namespace.Type::Method", its kind, and the patch method itself as a stack frame names it ("Namespace.Outer+Inner::Method"; empty for a HookGen hook, whose handler is usually a lambda).
func Patches ¶
Patches lists the Harmony patches the assembly at path declares with [HarmonyPatch] on a class, its methods or both, each method a [HarmonyPrefix]-style attribute or its name (Prefix, Postfix, Transpiler, Finalizer) marks, and the HookGen hooks it subscribes: On.Type.add_Method as a hook, IL.Type.add_Method as a transpiler. Patches a plugin applies by calling Harmony.Patch or new Hook(...) at run time name their target only through reflection and are not seen.
type Plugin ¶
type Plugin struct{ GUID, Name, Version, Namespace string }
Plugin is one [BepInPlugin(GUID, Name, Version)] an assembly declares. Namespace is that of the class carrying it, which is how an exception's stack names the plugin.
type Relation ¶
type Relation struct{ Plugin, GUID, MinVersion, Kind string }
Relation is a [BepInDependency] or [BepInIncompatibility] on a plugin class: Plugin is the GUID of the plugin whose class carries it, GUID the one it names. MinVersion is the dependency's minimum, empty for no minimum.
type Setting ¶ added in v0.2.0
type Setting struct {
Section string `json:"section"`
Key string `json:"key"`
// Type is "bool", "int", "uint", "long", "ulong", "short", "ushort", "byte", "sbyte", "float", "double", "string"
// or "enum".
Type string `json:"type"`
// Default is written as BepInEx writes it to the .cfg; empty when the reader cannot tell (an enum it cannot name).
Default string `json:"default"`
Description string `json:"description,omitempty"`
// Min and Max are an AcceptableValueRange's bounds, in the same form as Default.
Min string `json:"min,omitempty"`
Max string `json:"max,omitempty"`
// Values are an enum's names, in declaration order.
Values []string `json:"values,omitempty"`
// Sync is "host-enforced" or "host-recommended" when the entry is shared with the lobby by Mortar's ConfigSync or
// by CSync straight from the Bind call, else empty.
Sync string `json:"sync,omitempty"`
}
Setting is one BepInEx config entry a plugin binds with a constant section, key and default.
func Settings ¶ added in v0.2.0
Settings lists the config entries the assembly at path binds through BepInEx's ConfigFile.Bind (or CSync's BindSyncedEntry) where the section, key and default are constants in the call, in IL order, each section and key once. A Bind whose default is computed is left out, as is one through a helper that takes the section or key as a parameter.