CrabUI
Loading...
Searching...
No Matches
Ghost Detector.cs
1using System;
2using System.Reflection;
3using System.Runtime.CompilerServices;
4using System.Collections.Generic;
5using System.Collections.Immutable;
6using System.Linq;
7
8using Barotrauma;
9using HarmonyLib;
10using Microsoft.Xna.Framework;
11using System.IO;
12
13namespace CrabUI
14{
15 public class GhostDetector
16 {
17 public static bool Dead { get; set; }
18 public static Action<string> OnDetect;
19
20 /// <summary>
21 /// Call this in your harmony patches
22 /// </summary>
23 public static bool Check(
24 [CallerMemberName] string memberName = "",
25 [CallerFilePath] string source = "",
26 [CallerLineNumber] int lineNumber = 0
27 )
28 {
29 if (Dead)
30 {
31 string at = $"{CutFilePath(source)}:{lineNumber} {memberName}";
32
33 if (ShouldReport(at)) OnDetect?.Invoke(at);
34 }
35
36 return Dead;
37 }
38
39 public static string CutFilePath(string path)
40 {
41 int i = path.IndexOf("CSharp");
42 if (i == -1) i = path.IndexOf("SharedSource");
43 if (i == -1) i = path.IndexOf("ClientSource");
44 if (i == -1) i = path.IndexOf("ServerSource");
45
46 if (i == -1) return path;
47 return path.Substring(i);
48 }
49
50
51 public static int MinDetections = 3;
52 public static int MaxDetections = 6;
53 public static Dictionary<string, int> Detections = new();
54 public static bool ShouldReport(string at)
55 {
56 if (!Detections.ContainsKey(at)) Detections[at] = 1;
57 else Detections[at]++;
58
59 return MinDetections < Detections[at] && Detections[at] <= MaxDetections;
60 }
61
62 /// <summary>
63 /// Call this in IAssemblyPlugin.Initialize
64 /// </summary>
65 public static void Activate()
66 {
67 OnDetect ??= (at) =>
68 {
69 Log($"{CUI.HookIdentifier} GUI just died, It seems that there is a mod conflict");
70 Log($"At {at}", Color.Orange);
71 };
72 }
73
74 public static void Deactivate()
75 {
76 Dead = true;
77 }
78
79 public static void Log(object msg, Color? color = null)
80 {
81 color ??= Color.Yellow;
82 LuaCsLogger.LogMessage($"{msg ?? "null"}", color * 0.8f, color);
83 }
84 }
85
86}