Skip to content

Commit b77a8ec

Browse files
authored
Merge pull request #1 from RimWorldMod/A17-patch
merge A17 patch into master
2 parents 43b1717 + 86e5593 commit b77a8ec

12 files changed

Lines changed: 551 additions & 442 deletions

Detour.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ namespace TechAdvancing
1010
{
1111
class Detour
1212
{
13-
public static void detour(MethodInfo source, MethodInfo target)
13+
public static void DoDetour(MethodInfo source, MethodInfo target)
1414
{
15-
//Log.Message("GHXX Techadvancing: Attempting detour: " + source.Name + " -- TO -> " + target.Name);
15+
if (LogOutput.DebugMode_TA_enabled) { LogOutput.WriteLogMessage(Errorlevel.Warning, "DEBUG MODE ACTIVATED"); }
16+
1617
MethodInfo methodToReplace = target;
17-
if (LogOutput.DebugMode_TA_enabled) { LogOutput.writeLogMessage(Errorlevel.Warning, "DEBUG MODE ACTIVATED"); }
18-
//typeof(Target).GetMethod("targetMethod" + funcNum, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
19-
MethodInfo methodToInject = source;
18+
MethodInfo methodToInject = source;
2019

21-
//typeof(Injection).GetMethod("injectionMethod" + funcNum, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
2220
RuntimeHelpers.PrepareMethod(methodToReplace.MethodHandle);
2321
RuntimeHelpers.PrepareMethod(methodToInject.MethodHandle);
2422

@@ -53,11 +51,11 @@ public static void detour(MethodInfo source, MethodInfo target)
5351
{
5452
// 32-bit systems use 32-bit relative offset and jump
5553
// 5 byte destructive
56-
54+
5755
// Get function pointers
5856
int Source_Base = source.MethodHandle.GetFunctionPointer().ToInt32();
5957
int Destination_Base = target.MethodHandle.GetFunctionPointer().ToInt32();
60-
58+
6159
// Native source address
6260
byte* Pointer_Raw_Source = (byte*)Source_Base;
6361

@@ -74,8 +72,6 @@ public static void detour(MethodInfo source, MethodInfo target)
7472

7573

7674
}
77-
//Log.Message("GHXX Techadvancing: Detour complete: " + source.Name + " -- TO -> " + target.Name);
78-
//return true;
7975
}
8076
}
8177
}

Extensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Verse;
77
using UnityEngine;
88
using TechAdvancing;
9-
using GHXXTechAdvancing;
109
using Harmony;
1110
using Verse.Sound;
1211
using System.Reflection;
@@ -17,10 +16,9 @@ public static class Extensions
1716
{
1817
public static string TranslateOrDefault(this string x,string fallback = null, string Prefix = null)
1918
{
20-
string retvar = "";
21-
if ((Prefix+x).TryTranslate(out retvar))
19+
if ((Prefix + x).TryTranslate(out string retvar))
2220
{
23-
return retvar;
21+
return retvar;
2422
}
2523
return fallback??(Prefix+x);
2624
}

HarmonyDetours.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Harmony;
2+
using RimWorld;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Text;
8+
using UnityEngine;
9+
using Verse;
10+
using Verse.Sound;
11+
12+
namespace TechAdvancing
13+
{
14+
/// <summary>
15+
/// Class storing all the detours and the detour call.
16+
/// </summary>
17+
class HarmonyDetours
18+
{
19+
/// <summary>
20+
/// Method for performing all the detours via Harmony.
21+
/// </summary>
22+
public static void Setup()
23+
{
24+
var harmony = HarmonyInstance.Create("com.ghxx.rimworld.techadvancing");
25+
harmony.PatchAll(Assembly.GetExecutingAssembly());
26+
}
27+
}
28+
29+
/// <summary>
30+
/// Prefix for adding the button below the progressbar of the research window. The button is used for opening the config screen.
31+
/// </summary>
32+
[HarmonyPatch(typeof(RimWorld.MainTabWindow_Research))]
33+
[HarmonyPatch("DrawLeftRect")]
34+
[HarmonyPatch(new Type[] { typeof(Rect) })]
35+
class TA_Research_Menu_Patch
36+
{
37+
static void Prefix(Rect leftOutRect)
38+
{
39+
Rect TA_Cfgrect = new Rect(0f, 0f, 180f, 20f);
40+
TA_Cfgrect.x = (leftOutRect.width - TA_Cfgrect.width) / 2f;
41+
TA_Cfgrect.y = leftOutRect.height - 20f;
42+
43+
if (Widgets.ButtonText(TA_Cfgrect, "TAcfgmenulabel".Translate(), true, false, true))
44+
{
45+
SoundDef.Named("ResearchStart").PlayOneShotOnCamera();
46+
Find.WindowStack.Add((Window)new TechAdvancing_Config_Tab());
47+
}
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Patch for having a method called when a pawn dies.
53+
/// </summary>
54+
[HarmonyPatch(typeof(Verse.Pawn))]
55+
[HarmonyPatch("Kill")]
56+
[HarmonyPatch(new Type[] { typeof(DamageInfo?) })]
57+
class TA_OnKill_Event
58+
{
59+
static void Postfix(Pawn __instance, DamageInfo? dinfo)
60+
{
61+
TechAdvancing.Event.OnKill(__instance);
62+
}
63+
}
64+
65+
/// <summary>
66+
/// Patch for getting notified about faction changes. E.g.: when a pawn joins the colony.
67+
/// </summary>
68+
[HarmonyPatch(typeof(Verse.Pawn))]
69+
[HarmonyPatch("SetFaction")]
70+
[HarmonyPatch(new Type[] { typeof(Faction), typeof(Pawn) })]
71+
class TA_OnNewPawn_Event
72+
{
73+
static void Prefix(Pawn __instance, Faction newFaction, Pawn recruiter = null)
74+
{
75+
TechAdvancing.Event.OnNewPawn(__instance);
76+
}
77+
}
78+
79+
/// <summary>
80+
/// Postfix Patch for getting to know the new faction.
81+
/// </summary>
82+
[HarmonyPatch(typeof(Verse.Pawn))]
83+
[HarmonyPatch("SetFaction")]
84+
[HarmonyPatch(new Type[] { typeof(Faction), typeof(Pawn) })]
85+
class TA_PostOnNewPawn_Event
86+
{
87+
static void Postfix(Faction newFaction, Pawn recruiter = null)
88+
{
89+
TechAdvancing.Event.PostOnNewPawn();
90+
}
91+
}
92+
93+
}

LogOutput.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88

99
namespace TechAdvancing
1010
{
11+
/// <summary>
12+
/// Class for writing log messages easily.
13+
/// </summary>
1114
class LogOutput
1215
{
13-
16+
#if DEBUG
17+
public static readonly bool DebugMode_TA_enabled = true;
18+
#else
1419
public static readonly bool DebugMode_TA_enabled = false;
15-
16-
public static void writeLogMessage(Errorlevel level, string message)
20+
#endif
21+
/// <summary>
22+
/// Sends a new colored log message.
23+
/// </summary>
24+
/// <param name="level">The severity level.</param>
25+
/// <param name="message">The message to write.</param>
26+
public static void WriteLogMessage(Errorlevel level, string message)
1727
{
18-
if((level == Errorlevel.Debug && DebugMode_TA_enabled)|| level == Errorlevel.Information)
28+
if ((level == Errorlevel.Debug && DebugMode_TA_enabled) || level == Errorlevel.Information)
1929
{
2030
Log.Message("[Tech Advancing] [" + level.ToString() + "] " + message);
2131
}
@@ -25,13 +35,16 @@ public static void writeLogMessage(Errorlevel level, string message)
2535
}
2636
else if (level == Errorlevel.Error || level == Errorlevel.Critical)
2737
{
28-
Log.Warning("[Tech Advancing] [" + level.ToString() + "] " + message);
38+
Log.Error("[Tech Advancing] [" + level.ToString() + "] " + message);
2939
}
3040
}
3141

3242

3343
}
3444

45+
/// <summary>
46+
/// Represents the available severity levels.
47+
/// </summary>
3548
public enum Errorlevel
3649
{
3750
Debug,

0 commit comments

Comments
 (0)