-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionExamplePlugin.cs
More file actions
38 lines (35 loc) · 1.47 KB
/
Copy pathReflectionExamplePlugin.cs
File metadata and controls
38 lines (35 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Reflection;
using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using SDG.Unturned;
namespace UnturnedRedistExample.Reflection
{
/// <summary>
/// Reads a NON-public Unturned field WITHOUT the .Publicized package — so the
/// only option is reflection. Compare with <c>../Publicized</c>, which reads
/// the same field as a plain, compile-checked field access.
/// </summary>
public class ReflectionExamplePlugin : RocketPlugin
{
protected override void Load()
{
// isDedicatedUGCInstalled is NON-public: a direct
// `Provider.isDedicatedUGCInstalled` does not even compile against the
// plain redist (CS0117). Without the .Publicized package, reflection
// is the only way in:
FieldInfo field = typeof(Provider).GetField(
"isDedicatedUGCInstalled",
BindingFlags.NonPublic | BindingFlags.Static);
bool ugcInstalled = (bool)field.GetValue(null);
Logger.Log(
$"[UnturnedRedistExample.Reflection] Loaded. Read non-public " +
$"isDedicatedUGCInstalled = {ugcInstalled} via REFLECTION — the name " +
$"is a string (a rename breaks at runtime, not build) and every read " +
$"pays a reflection + boxing cost.");
}
protected override void Unload()
{
Logger.Log("[UnturnedRedistExample.Reflection] Unloaded.");
}
}
}