Skip to content

Commit

Permalink
v1.0.1 fixes bug with labels not showing
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Feb 19, 2017
1 parent a280145 commit 6aaeaf8
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 42 deletions.
Binary file modified About/Preview.PNG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Assemblies/CameraPlus.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
A RimWorld mod to improve the zoom factor for the build-in camera.

Powered by [Harmony](https://github.com/pardeike/Harmony)
The runtime patch library for .NET
The runtime patch library for Unity

![Harmony](https://s24.postimg.org/58bl1rz39/logo.png)
180 changes: 141 additions & 39 deletions Source/Main.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,142 @@
using Verse;
using Harmony;
using UnityEngine;
using System.Reflection;

namespace CameraPlus
{
[StaticConstructorOnStartup]
class Main
{
static Main()
{
var harmony = HarmonyInstance.Create("net.pardeike.rimworld.mod.camera+");
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
}

[HarmonyPatch(typeof(CameraDriver))]
[HarmonyPatch("ApplyPositionToGameObject")]
class CameraDriverPatch
{
static void Postfix(CameraDriver __instance)
{
var trv = Traverse.Create(__instance);
var p_MyCamera = trv.Property("MyCamera").GetValue<Camera>();
var pos = p_MyCamera.transform.position;
var y = p_MyCamera.transform.position.y;
var o = p_MyCamera.orthographicSize;

var y2 = GenMath.LerpDouble(65, 15, 65, 32, y);
var o2 = GenMath.LerpDouble(60, 11, 60, 0.5f, o);
var dolly = GenMath.LerpDouble(65, 15, 1, 3, y);
dolly = 85 - dolly * dolly * dolly * dolly;

p_MyCamera.transform.position = new Vector3(pos.x, y2, pos.z);
p_MyCamera.orthographicSize = o2;
__instance.config.dollyRateKeys = dolly;
}
}
using Verse;
using Harmony;
using UnityEngine;
using System.Reflection;
using Harmony.ILCopying;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;

namespace CameraPlus
{
[StaticConstructorOnStartup]
class Main
{
static Main()
{
var harmony = HarmonyInstance.Create("net.pardeike.rimworld.mod.camera+");
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
}

[HarmonyPatch(typeof(GenMapUI))]
[HarmonyPatch("DrawThingLabel")]
[HarmonyPatch(new Type[] { typeof(Vector2), typeof(string), typeof(Color) })]

static class GenMapUI_DrawThingLabel_Patch
{
static GameFont GetAdaptedGameFont()
{
var rootSize = Traverse.Create(Find.CameraDriver).Field("rootSize").GetValue<float>();
if (rootSize < 14f) return GameFont.Medium;
if (rootSize < 19f) return GameFont.Small;
return GameFont.Tiny;
}

class AdaptedGameFontReplacer : IILProcessor
{
bool firstInstruction = true;

public List<ILInstruction> Start(ILGenerator generator, MethodBase original)
{
return new List<ILInstruction>();
}

// we replace the first "GameFont.Tiny" with our "GetAdaptedGameFont()"
//
public List<ILInstruction> Process(ILInstruction instruction)
{
var result = new List<ILInstruction>();
if (firstInstruction && instruction.opcode == OpCodes.Ldc_I4_0)
{
var method = AccessTools.Method(typeof(GenMapUI_DrawThingLabel_Patch), "GetAdaptedGameFont");
var call = new ILInstruction(OpCodes.Call);
call.operand = method;
call.argument = method;
result.Add(call);
}
else
result.Add(instruction);

firstInstruction = false;
return result;
}

public List<ILInstruction> End(ILGenerator generator, MethodBase original)
{
return new List<ILInstruction>();
}
}

[HarmonyProcessors]
static HarmonyProcessor AdaptedGameFontReplacerPatch(MethodBase original)
{
var processor = new HarmonyProcessor();
processor.AddILProcessor(new AdaptedGameFontReplacer());
return processor;
}
}

[HarmonyPatch(typeof(CameraDriver))]
[HarmonyPatch("get_CurrentZoom")]
static class CameraDriver_get_CurrentZoom_Patch
{
class ZoomLerper : IILProcessor
{
public List<ILInstruction> Start(ILGenerator generator, MethodBase original)
{
return new List<ILInstruction>();
}

// Normal values: 12, 13.8, 42, 57
//
public List<ILInstruction> Process(ILInstruction instruction)
{
if (instruction.opcode == OpCodes.Ldc_R4)
{
var f = (float)instruction.operand;
f = GenMath.LerpDouble(12, 57, 30, 60, f);
instruction.operand = f;
instruction.argument = f;
}
return new List<ILInstruction>() { instruction };
}

public List<ILInstruction> End(ILGenerator generator, MethodBase original)
{
return new List<ILInstruction>();
}
}

[HarmonyProcessors]
static HarmonyProcessor LerpCurrentZoom(MethodBase original)
{
var processor = new HarmonyProcessor();
processor.AddILProcessor(new ZoomLerper());
return processor;
}
}

[HarmonyPatch(typeof(CameraDriver))]
[HarmonyPatch("ApplyPositionToGameObject")]
static class CameraDriver_ApplyPositionToGameObject_Patch
{
static void Postfix(CameraDriver __instance)
{
var trv = Traverse.Create(__instance);
var p_MyCamera = trv.Property("MyCamera").GetValue<Camera>();
var pos = p_MyCamera.transform.position;
var y = p_MyCamera.transform.position.y;
var o = p_MyCamera.orthographicSize;

var y2 = GenMath.LerpDouble(65, 15, 65, 32, y);
var o2 = GenMath.LerpDouble(60, 11, 60, 0.5f, o);
var dolly = GenMath.LerpDouble(65, 15, 1, 3, y);
dolly = 85 - dolly * dolly * dolly * dolly;

p_MyCamera.transform.position = new Vector3(pos.x, y2, pos.z);
p_MyCamera.orthographicSize = o2;
__instance.config.dollyRateKeys = dolly;
}
}
}
4 changes: 2 additions & 2 deletions Source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.4")]
[assembly: AssemblyFileVersion("1.0.0.4")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
Binary file added Source/animation.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6aaeaf8

Please sign in to comment.