Skip to content

Commit

Permalink
2.4.1: Minor feature
Browse files Browse the repository at this point in the history
- Added automatic logging of all detours
  • Loading branch information
UnlimitedHugs committed Feb 4, 2017
1 parent 1ef789a commit 9e7a1f0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Mods/HugsLib/About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<targetVersion>0.16.0</targetVersion>
<description>&lt;color=orange&gt;&lt;b&gt;Important: &lt;/b&gt; This mod should be one of the first to be loaded to work properly.&lt;/color&gt;\n
HugsLib is a library that provides shared functionality to other mods.
Version: 2.4.0
Version: 2.4.1
</description>
</ModMetaData>
2 changes: 1 addition & 1 deletion Mods/HugsLib/About/Version.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<VersionData>
<overrideVersion>2.4.0</overrideVersion>
<overrideVersion>2.4.1</overrideVersion>
<gitHubRepository>UnlimitedHugs/RimworldHugsLib</gitHubRepository>
</VersionData>
Binary file modified Mods/HugsLib/Assemblies/HugsLib.dll
Binary file not shown.
48 changes: 48 additions & 0 deletions Source/Detour/DetourProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace HugsLib.Source.Detour {
/**
Expand All @@ -12,6 +13,9 @@ public class DetourProvider {
*/
private static readonly Dictionary<MethodInfo, MethodInfo> detours = new Dictionary<MethodInfo, MethodInfo>();

private static readonly List<Helpers.DetourPair> detoursToLog = new List<Helpers.DetourPair>();
private static bool loggingDetourGroup;

/**
* Same as TryCompatibleDetour, but writes an error to the console on failure
*/
Expand Down Expand Up @@ -101,6 +105,8 @@ public static unsafe bool TryIndepentDetour(MethodInfo source, MethodInfo destin
*Pointer_Raw_Address = offset;
}

detoursToLog.Add(new Helpers.DetourPair(source, destination));
if(!loggingDetourGroup) LogNewDetours("Manual detour: ");
// done!
return true;
}
Expand All @@ -112,6 +118,48 @@ public static MethodInfo TryGetExistingDetourDestination(MethodInfo source) {
return existing;
}

public static void BeginDetourGroupLogging() {
loggingDetourGroup = true;
}

public static void LogNewDetours(string message) {
if (detoursToLog.Count > 0) {
var builder = new StringBuilder("(info) ");
builder.Append(message);
if (detoursToLog.Count > 1) {
builder.Append('\n');
}
for (int i = 0; i < detoursToLog.Count; i++) {
var pair = detoursToLog[i];
// source method
if (pair.source == null) {
builder.Append("[null]");
} else {
if (pair.source.DeclaringType != null) {
builder.Append(pair.source.DeclaringType.Name);
builder.Append('.');
builder.Append(pair.source.Name);
} else {
builder.Append(pair.source.FullName());
}
}
builder.Append(" >> ");
// destination method
if (pair.destination == null) {
builder.Append("[null]");
} else {
builder.Append(pair.destination.FullName());
}
if (i < detoursToLog.Count - 1) {
builder.Append('\n');
}
}
HugsLibController.Logger.Message(builder.ToString());
}
loggingDetourGroup = false;
detoursToLog.Clear();
}

internal static bool CompatibleDetourWithExceptions(MethodInfo source, MethodInfo destination) {
MethodInfo existingDestination;
if (detours.TryGetValue(source, out existingDestination)) {
Expand Down
2 changes: 1 addition & 1 deletion Source/Detour/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace HugsLib.Source.Detour {
public static class Helpers {
public const BindingFlags AllBindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

private struct DetourPair {
internal struct DetourPair {
public readonly MethodInfo source;
public readonly MethodInfo destination;

Expand Down
15 changes: 12 additions & 3 deletions Source/HugsLibController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using HugsLib.Restarter;
using HugsLib.Settings;
using HugsLib.Source.Attrib;
using HugsLib.Source.Detour;
using HugsLib.Utils;
using UnityEngine;
using UnityEngine.SceneManagement;
Expand Down Expand Up @@ -301,8 +302,9 @@ private void OnDefReloadDetected() {
private void LoadReloadInitialize() {
try {
EnumerateModAssemblies();
AttributeDetector.ProcessNewTypes(); // do detours and other attribute work for (newly) loaded mods
EnumerateChildMods();
ProcessAttibutes(); // do detours and other attribute work for (newly) loaded mods
EnumerateChildMods();
DetourProvider.BeginDetourGroupLogging();
var initializationsThisRun = new List<string>();
for (int i = 0; i < childMods.Count; i++) {
var childMod = childMods[i];
Expand All @@ -318,14 +320,21 @@ private void LoadReloadInitialize() {
initializationsThisRun.Add(modId);
}
if (initializationsThisRun.Count > 0) {
DetourProvider.LogNewDetours("Manual detours applied: ");
Logger.Message("v{0} initialized {1}", LibraryVersion, initializationsThisRun.ListElements());
}
OnDefsLoaded();
} catch (Exception e) {
Logger.ReportException(e);
}
}


private void ProcessAttibutes() {
DetourProvider.BeginDetourGroupLogging();
AttributeDetector.ProcessNewTypes();
DetourProvider.LogNewDetours("Attribute detours applied: ");
}

// will run on startup and on reload. On reload it will add newly loaded mods
private void EnumerateChildMods() {
foreach (var subclass in typeof (ModBase).InstantiableDescendantsAndSelf()) {
Expand Down

0 comments on commit 9e7a1f0

Please sign in to comment.