mirror of
https://github.com/pardeike/Harmony
synced 2026-06-08 16:40:38 +00:00
199 lines
7.7 KiB
HTML
199 lines
7.7 KiB
HTML
<!DOCTYPE html>
|
|
<!--[if IE]><![endif]-->
|
|
<html>
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
|
<title>Utilities </title>
|
|
<meta name="viewport" content="width=device-width">
|
|
<meta name="title" content="Utilities ">
|
|
<meta name="generator" content="docfx 2.48.1.0">
|
|
|
|
<link rel="shortcut icon" href="../favicon.ico">
|
|
<link rel="stylesheet" href="../styles/docfx.vendor.css">
|
|
<link rel="stylesheet" href="../styles/docfx.css">
|
|
<link rel="stylesheet" href="../styles/main.css">
|
|
<meta property="docfx:navrel" content="../toc.html">
|
|
<meta property="docfx:tocrel" content="toc.html">
|
|
|
|
|
|
|
|
</head>
|
|
<body data-spy="scroll" data-target="#affix" data-offset="120">
|
|
<div id="wrapper">
|
|
<header>
|
|
|
|
<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
|
|
<div class="container">
|
|
<div class="navbar-header">
|
|
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
|
|
<span class="sr-only">Toggle navigation</span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
</button>
|
|
|
|
<a class="navbar-brand" href="../index.html">
|
|
<img id="logo" class="svg" src="../logo.svg" alt="">
|
|
</a>
|
|
</div>
|
|
<div class="collapse navbar-collapse" id="navbar">
|
|
<form class="navbar-form navbar-right" role="search" id="search">
|
|
<div class="form-group">
|
|
<input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<div class="subnav navbar navbar-default">
|
|
<div class="container hide-when-search" id="breadcrumb">
|
|
<ul class="breadcrumb">
|
|
<li></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
<div role="main" class="container body-content hide-when-search">
|
|
|
|
<div class="sidenav hide-when-search">
|
|
<a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
|
|
<div class="sidetoggle collapse" id="sidetoggle">
|
|
<div id="sidetoc"></div>
|
|
</div>
|
|
</div>
|
|
<div class="article row grid-right">
|
|
<div class="col-md-10">
|
|
<article class="content wrap" id="_content" data-uid="">
|
|
<h1 id="utilities">Utilities</h1>
|
|
|
|
<h3 id="accesstools">AccessTools</h3>
|
|
<p>To simplify reflections, Harmony has a helper class called AccessTools. Here are the most commonly used methods:</p>
|
|
<pre><code class="lang-csharp">public static BindingFlags all = ....
|
|
public static Type TypeByName(string name)
|
|
public static FieldInfo Field(Type type, string name)
|
|
public static PropertyInfo Property(Type type, string name)
|
|
public static MethodInfo Method(Type type, string name, Type[] parameters = null, Type[] generics = null)
|
|
public static ConstructorInfo Constructor(Type type, Type[] parameters = null)
|
|
public static Type Inner(Type type, string name)
|
|
public static Type FirstInner(Type type, Func<Type, bool> predicate)
|
|
</code></pre>
|
|
<p>Any of these methods use the <strong>all</strong> BindingFlags definition and thus work on anything regardless if it is public, private, static or else.</p>
|
|
<h3 id="traverse">Traverse</h3>
|
|
<p>In order to access fields, properties and methods from classes via reflection, Harmony contains a utility called Traverse. Think of it as LINQ for classes. Here are the main methods:</p>
|
|
<pre><code class="lang-csharp">// starting from a type or instance
|
|
public static Traverse Create(Type type)
|
|
public static Traverse Create<T>()
|
|
public static Traverse CreateWithType(string name)
|
|
|
|
// digging deeper
|
|
public Traverse Type(string name)
|
|
public Traverse Field(string name)
|
|
public Traverse Property(string name, object[] index = null)
|
|
public Traverse Method(string name, params object[] arguments)
|
|
public Traverse Method(string name, Type[] paramTypes, object[] arguments = null)
|
|
|
|
// calling getter or method
|
|
public object GetValue()
|
|
public T GetValue<T>()
|
|
public object GetValue(params object[] arguments)
|
|
public T GetValue<T>(params object[] arguments)
|
|
public override string ToString()
|
|
|
|
// calling setter
|
|
public Traverse SetValue(object value)
|
|
|
|
// iterating
|
|
public static void IterateFields(object source, Action<Traverse> action)
|
|
public static void IterateFields(object source, object target, Action<Traverse, Traverse> action)
|
|
public static void IterateProperties(object source, Action<Traverse> action)
|
|
public static void IterateProperties(object source, object target, Action<Traverse, Traverse> action)
|
|
</code></pre>
|
|
<p>Example:</p>
|
|
<pre><code class="lang-csharp" name="example">class Foo
|
|
{
|
|
struct Bar
|
|
{
|
|
static string secret = "hello";
|
|
|
|
public string ModifiedSecret() => secret.ToUpper();
|
|
}
|
|
|
|
Bar MyBar
|
|
{
|
|
get
|
|
{
|
|
return new Bar();
|
|
}
|
|
}
|
|
|
|
public string GetSecret() => MyBar.ModifiedSecret();
|
|
|
|
Foo()
|
|
{
|
|
}
|
|
|
|
static Foo MakeFoo() => new();
|
|
}
|
|
|
|
void Test()
|
|
{
|
|
var foo = Traverse.Create<Foo>().Method("MakeFoo").GetValue<Foo>();
|
|
Traverse.Create(foo).Property("MyBar").Field("secret").SetValue("world");
|
|
Console.WriteLine(foo.GetSecret()); // outputs WORLD
|
|
}
|
|
</code></pre>
|
|
<p>Although most fields, properties and methods in that class hierarchy are private, Traverse can easily access anything. It has build-in null protection and propagates null as a result if any of the intermediates would encounter null. It works with static types and caches lookups which makes it pretty fast.</p>
|
|
<h3 id="filelog">FileLog</h3>
|
|
<p>For simple and quick logging, Harmony uses a tool class FileLog. It has three methods:</p>
|
|
<pre><code class="lang-csharp">public static void Log(string str)
|
|
// Creates a new log file called "harmony.log.txt" on the computers Desktop (if it not already exists) and appends *str* to it.
|
|
|
|
public static void Reset()
|
|
// Deletes the log file.
|
|
|
|
public static unsafe void LogBytes(long ptr, int len)
|
|
// Same as Log(string str) but logs a hex dump and md5 hash.
|
|
</code></pre>
|
|
</article>
|
|
</div>
|
|
|
|
<div class="hidden-sm col-md-2" role="complementary">
|
|
<div class="sideaffix">
|
|
<div class="contribution">
|
|
<ul class="nav">
|
|
<li>
|
|
<a href="https://github.com/pardeike/Harmony/blob/master/Documentation/articles/utilities.md/#L1" class="contribution-link">Improve this Doc</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
|
|
<!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<footer>
|
|
<div class="grad-bottom"></div>
|
|
<div class="footer">
|
|
<div class="container">
|
|
<span class="pull-right">
|
|
<a href="#top">Back to top</a>
|
|
</span>
|
|
|
|
<span>Generated by <strong>DocFX</strong></span>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
</div>
|
|
|
|
<script type="text/javascript" src="../styles/docfx.vendor.js"></script>
|
|
<script type="text/javascript" src="../styles/docfx.js"></script>
|
|
<script type="text/javascript" src="../styles/main.js"></script>
|
|
</body>
|
|
</html>
|