Skip to content

Commit

Permalink
v1.0.3 adds settings
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jul 13, 2021
1 parent 27ed957 commit 460637b
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 155 deletions.
2 changes: 1 addition & 1 deletion About/Manifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manifest>
<identifier>net.pardeike.rimworld.mod.visualexceptions</identifier>
<version>1.0.2.0</version>
<version>1.0.3.0</version>
<targetVersions>
<li>1.2.0</li>
<li>1.3.0</li>
Expand Down
Binary file modified Current/Assemblies/VisualExceptions.dll
Binary file not shown.
1 change: 0 additions & 1 deletion Source/Assets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ static class Assets
internal static readonly Texture2D cancel = ContentFinder<Texture2D>.Get("UI/Designators/Cancel", true);

internal static readonly Texture2D harmonyTab = LoadTexture("HarmonyTab");
internal static readonly Texture2D[] debugToggle = LoadTextures("Debug0", "Debug1");
internal static readonly Texture2D restart = LoadTexture("Restart");
internal static readonly Texture2D exception = LoadTexture("Exception");
internal static readonly Texture2D location = LoadTexture("Location");
Expand Down
3 changes: 2 additions & 1 deletion Source/Columns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ internal static ColumnDrawer ExceptionColumn(this ExceptionInfo info, Func<strin
Tools.Button(Assets.copy, r, "Copy", true, () =>
{
GUIUtility.systemCopyBuffer = exception().ToString();
SoundDefOf.Tick_Low.PlayOneShotOnCamera(null);
if (ExceptionState.configuration.UseSound)
SoundDefOf.Tick_Low.PlayOneShotOnCamera(null);
});
}
};
Expand Down
3 changes: 2 additions & 1 deletion Source/ExceptionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ internal string GetStacktrace()
internal void Remove()
{
ExceptionState.Remove(this);
SoundDefOf.TabOpen.PlayOneShotOnCamera(null);
if (ExceptionState.configuration.UseSound)
SoundDefOf.TabOpen.PlayOneShotOnCamera(null);
}

internal void Ban()
Expand Down
16 changes: 3 additions & 13 deletions Source/ExceptionInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace VisualExceptions
{
public class ExceptionInspector : MainTabWindow
{
internal Vector2 customPosition = Vector2.zero;
internal float scrollViewHeight = 0;
internal Vector2 scrollPosition = Vector2.zero;

Expand Down Expand Up @@ -114,18 +113,9 @@ public override void SetInitialSizeAndPosition()
doCloseX = true;
draggable = true;
resizeable = true;
if (customPosition != Vector2.zero)
{
if (customPosition.x > 0)
windowRect.x = customPosition.x;
else
windowRect.x = UI.screenWidth - windowRect.width + customPosition.x;

if (customPosition.y > 0)
windowRect.y = customPosition.y;
else
windowRect.y = UI.screenHeight - windowRect.height + customPosition.y;
}
if (ExceptionState.configuration.TabToTheRight)
windowRect.x = UI.screenWidth - windowRect.width;
Log.Warning($"{windowRect}");
}

public override void Close(bool doCloseSound = true)
Expand Down
2 changes: 1 addition & 1 deletion Source/ExceptionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static void Load()
configuration = (Configuration)serializer.ReadObject(stream);
}
else
configuration = new Configuration { Debugging = true };
configuration = new Configuration { Debugging = true, TabToTheRight = false, UseSound = true };
}
catch
{
Expand Down
43 changes: 34 additions & 9 deletions Source/Main.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
using HarmonyLib;
using UnityEngine;
using Verse;

namespace VisualExceptions
{
[StaticConstructorOnStartup]
class HarmonyMain : Mod
{
internal static string harmony_id = "net.pardeike.rimworld.lib.harmony";

static HarmonyMain()
static HarmonyMain() // loads earliest
{
var harmony = new Harmony(harmony_id);
_ = new PatchClassProcessor(harmony, typeof(ShowHarmonyVersionOnMainScreen)).Patch();


ExceptionState.Load();
if (ExceptionState.configuration.Debugging)
Patcher.Apply(harmony);
Patcher.Apply();
}

public HarmonyMain(ModContentPack content) : base(content) { }

public override void DoSettingsWindowContents(Rect inRect)
{
var list = new Listing_Standard { ColumnWidth = inRect.width / 2f };
list.Begin(inRect);
list.Gap(12f);

var conf = ExceptionState.configuration;
var debugging = conf.Debugging;
var tabToTheRight = conf.TabToTheRight;
var useSound = conf.UseSound;

list.CheckboxLabeled("Enabled", ref debugging);
list.CheckboxLabeled("Show exceptions to the right", ref tabToTheRight);
list.CheckboxLabeled("Use sound", ref useSound);

if (debugging != conf.Debugging || tabToTheRight != conf.TabToTheRight || useSound != conf.UseSound)
{
conf.Debugging = debugging;
conf.TabToTheRight = tabToTheRight;
conf.UseSound = useSound;
ExceptionState.Save();
}

list.End();
}

public override string SettingsCategory()
{
return "Visual Exceptions";
}
}
}
2 changes: 2 additions & 0 deletions Source/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace VisualExceptions
internal class Configuration
{
[DataMember] internal bool Debugging { get; set; }
[DataMember] internal bool TabToTheRight { get; set; }
[DataMember] internal bool UseSound { get; set; }
}

[DataContract]
Expand Down
11 changes: 6 additions & 5 deletions Source/Mods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,34 @@ internal static void Unpatch(this ExceptionDetails.Mod mod)
.Do(id =>
{
new Harmony(id).UnpatchAll(id);
SoundDefOf.TabOpen.PlayOneShotOnCamera(null);
if (ExceptionState.configuration.UseSound)
SoundDefOf.TabOpen.PlayOneShotOnCamera(null);
});
_ = UnpatchedMods.Add(mod.meta.PackageId);
}

internal static IEnumerable<ModInfo> GetPrefixes(Patches info)
{
if (info == null) return new List<ModInfo>().AsEnumerable();
return AddMetadata(info.Prefixes.Where(t => t.owner != HarmonyMain.harmony_id).OrderBy(t => t.priority).Select(t => t.PatchMethod));
return AddMetadata(info.Prefixes.Where(t => t.owner != Patcher.harmony_id).OrderBy(t => t.priority).Select(t => t.PatchMethod));
}

internal static IEnumerable<ModInfo> GetPostfixes(Patches info)
{
if (info == null) return new List<ModInfo>().AsEnumerable();
return AddMetadata(info.Postfixes.Where(t => t.owner != HarmonyMain.harmony_id).OrderBy(t => t.priority).Select(t => t.PatchMethod));
return AddMetadata(info.Postfixes.Where(t => t.owner != Patcher.harmony_id).OrderBy(t => t.priority).Select(t => t.PatchMethod));
}

internal static IEnumerable<ModInfo> GetTranspilers(Patches info)
{
if (info == null) return new List<ModInfo>().AsEnumerable();
return AddMetadata(info.Transpilers.Where(t => t.owner != HarmonyMain.harmony_id).OrderBy(t => t.priority).Select(t => t.PatchMethod));
return AddMetadata(info.Transpilers.Where(t => t.owner != Patcher.harmony_id).OrderBy(t => t.priority).Select(t => t.PatchMethod));
}

internal static IEnumerable<ModInfo> GetFinalizers(Patches info)
{
if (info == null) return new List<ModInfo>().AsEnumerable();
return AddMetadata(info.Finalizers.Where(t => t.owner != HarmonyMain.harmony_id).OrderBy(t => t.priority).Select(t => t.PatchMethod));
return AddMetadata(info.Finalizers.Where(t => t.owner != Patcher.harmony_id).OrderBy(t => t.priority).Select(t => t.PatchMethod));
}

static IEnumerable<ModInfo> AddMetadata(IEnumerable<MethodInfo> methods)
Expand Down
61 changes: 0 additions & 61 deletions Source/PatchPersistence.cs

This file was deleted.

22 changes: 4 additions & 18 deletions Source/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ namespace VisualExceptions
static class Patcher
{
internal static bool patchesApplied = false;
internal static string harmony_id = "net.pardeike.rimworld.lib.harmony";

internal static void Apply(Harmony harmony)
internal static void Apply()
{
var harmony = new Harmony(harmony_id);
_ = new PatchClassProcessor(harmony, typeof(RunloopExceptionHandler)).Patch();
_ = new PatchClassProcessor(harmony, typeof(ShowLoadingExceptions)).Patch();
_ = new PatchClassProcessor(harmony, typeof(AddHarmonyTabWhenNecessary)).Patch();
Expand All @@ -24,19 +26,6 @@ internal static void Apply(Harmony harmony)
}
}

// draw the harmony lib version at the start screen
// Note: always patched from HarmonyMain()
//
[HarmonyPatch(typeof(VersionControl))]
[HarmonyPatch(nameof(VersionControl.DrawInfoInCorner))]
static class ShowHarmonyVersionOnMainScreen
{
internal static void Postfix()
{
Tools.DrawInfoSection();
}
}

// adds exception handlers
//
[HarmonyPatch]
Expand Down Expand Up @@ -79,13 +68,10 @@ internal static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruct

internal static IEnumerable<MethodBase> TargetMethods()
{
var methods = PatchPersistence.Methods;
if (methods.Any()) return methods;
methods = typeof(Pawn).Assembly.GetTypes()
var methods = typeof(Pawn).Assembly.GetTypes()
.Where(t => t.IsGenericType == false && (t.FullName.StartsWith("Verse.") || t.FullName.StartsWith("RimWorld.") || t.FullName.StartsWith("RuntimeAudioClipLoader.")))
.SelectMany(t => AccessTools.GetDeclaredMethods(t))
.Where(m => m.IsGenericMethod == false && HasCatch(m));
PatchPersistence.Methods = methods;
return methods;
}

Expand Down
13 changes: 5 additions & 8 deletions Source/Tab.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using RimWorld;
using UnityEngine;
using Verse;

namespace VisualExceptions
Expand All @@ -13,7 +12,6 @@ public Tab() : base()
tabWindowClass = typeof(ExceptionInspector);
defName = "harmony";
description = "Harmony";
order = -99999;
validWithoutMap = true;
minimized = true;
}
Expand Down Expand Up @@ -42,7 +40,10 @@ internal static void AddExceptions_Play(UIRoot_Play rootPlay)
if (allTabs.Contains(instance) == false)
{
instance.icon = Assets.harmonyTab;
allTabs.Insert(0, instance);
if (ExceptionState.configuration.TabToTheRight)
allTabs.Add(instance);
else
allTabs.Insert(0, instance);
Tools.PlayErrorSound();
}
}
Expand All @@ -52,11 +53,7 @@ internal static void AddExceptions_Entry()
if (Find.WindowStack.IsOpen<ExceptionInspector>() == false)
{
instance.icon = Assets.harmonyTab;
Find.WindowStack.Add(new ExceptionInspector
{
def = instance,
customPosition = new Vector2(10, 10)
});
Find.WindowStack.Add(new ExceptionInspector { def = instance });
Tools.PlayErrorSound();
}
}
Expand Down
37 changes: 2 additions & 35 deletions Source/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ struct ColumnInfo { internal Action draw; internal float dim; }

static class Tools
{
static readonly Rect debugLabelRect = new Rect(28f, 16f, 212f, 20f);
static readonly Rect debugImageRect = new Rect(0f, 20f, 23f, 12f);
static readonly Rect debugButtonRect = new Rect(0f, 16f, 240f, 36f);
internal static readonly Assembly HarmonyModAssembly = typeof(Tools).Assembly;
internal static readonly string RimworldAssemblyName = typeof(Pawn).Assembly.GetName().Name;
internal static AudioSource audioSource = null;
Expand All @@ -46,7 +43,8 @@ internal static AudioSource GetAudioSource()

internal static void PlayErrorSound()
{
GetAudioSource().PlayOneShot(Assets.error);
if (ExceptionState.configuration.UseSound)
GetAudioSource().PlayOneShot(Assets.error);
}

internal static void Iterate<T>(this IEnumerable<T> collection, Action<T, int> action)
Expand Down Expand Up @@ -168,36 +166,5 @@ internal static void Button(Texture2D texture, Rect rect, string tipKey, bool hi
TooltipHandler.TipRegionByKey(rect, tipKey);
if (Widgets.ButtonInvisible(rect)) action();
}

internal static void DrawInfoSection()
{
var oldFont = Text.Font;
var oldColor = GUI.color;
Text.Font = GameFont.Small;

GUI.BeginGroup(new Rect(10f, 74f, 240f, 40f));
GUI.color = new Color(1f, 1f, 1f, 0.5f);
var devText = "Visual Exceptions Enabled";
var devTextLen = devText.Size().x;
Widgets.Label(debugLabelRect, devText);
GUI.color = Color.white;
GUI.DrawTexture(debugImageRect, Assets.debugToggle[ExceptionState.configuration.Debugging ? 1 : 0]);
if (Patcher.patchesApplied != ExceptionState.configuration.Debugging && DateTime.Now.Second % 2 == 0)
{
var rect = new Rect(debugImageRect.xMax + devTextLen + 10, debugImageRect.y - 2, debugImageRect.height + 4, debugImageRect.height + 4);
GUI.DrawTexture(rect, Assets.restart);
}
if (Widgets.ButtonInvisible(debugButtonRect, false))
{
ExceptionState.configuration.Debugging = !ExceptionState.configuration.Debugging;
if (ExceptionState.configuration.Debugging)
PatchPersistence.ClearMethods();
ExceptionState.Save();
}
GUI.EndGroup();

GUI.color = oldColor;
Text.Font = oldFont;
}
}
}
2 changes: 1 addition & 1 deletion Source/VisualExceptions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<OutputPath>..\Current\Assemblies\</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Version>1.0.2.0</Version>
<Version>1.0.3.0</Version>
<Copyright>Copyright Andreas Pardeike</Copyright>
</PropertyGroup>

Expand Down
Binary file removed Textures/Debug0.png
Binary file not shown.
Binary file removed Textures/Debug1.png
Binary file not shown.

0 comments on commit 460637b

Please sign in to comment.