cu-trainer/Ultra Paradise Application/src/Debug.cs
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Globalization; |
| 4 | using System.Reflection; |
| 5 | using System.Text; |
| 6 | using System.Text.RegularExpressions; |
| 7 | using HarmonyLib; |
| 8 | using UnityEngine; |
| 9 | using UnityEngine.SceneManagement; |
| 10 | using Object = UnityEngine.Object; |
| 11 | |
| 12 | namespace UltraParadise |
| 13 | { |
| 14 | internal static partial class Plugin |
| 15 | { |
| 16 | internal static void DescribeCheats(StringBuilder sb) |
| 17 | { |
| 18 | Index(); |
| 19 | List<string> on = new List<string>(); |
| 20 | foreach (KeyValuePair<string, Flag> kv in flags) if (kv.Value.Value) on.Add(kv.Key); |
| 21 | on.Sort(StringComparer.Ordinal); |
| 22 | sb.Append(on.Count == 0 ? "D" : "G").Append("On (").Append(on.Count).Append("): ").Append(on.Count == 0 ? "nothing" : string.Join(", ", on.ToArray())).Append('\n'); |
| 23 | List<string> vals = new List<string>(); |
| 24 | foreach (KeyValuePair<string, Num> kv in nums) vals.Add(kv.Key + " " + kv.Value.Value.ToString("0.##", CultureInfo.InvariantCulture)); |
| 25 | vals.Sort(StringComparer.Ordinal); |
| 26 | sb.Append("NGame speed x").Append(GameSpeed.ToString("0.##", CultureInfo.InvariantCulture)).Append(" | camera zoom x").Append(CameraZoom.ToString("0.##", CultureInfo.InvariantCulture)) |
| 27 | .Append(" | mood lock ").Append(float.IsNaN(MoodTarget) ? "-" : MoodTarget.ToString("0", CultureInfo.InvariantCulture)).Append('\n'); |
| 28 | sb.Append("DSliders: ").Append(string.Join(", ", vals.ToArray())).Append('\n'); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | internal static class DebugFeed |
| 33 | { |
| 34 | public const char NL = '\u001e'; |
| 35 | public const char REC = '\u001d'; |
| 36 | public const char SEP = '\u001c'; |
| 37 | |
| 38 | private struct Entry { public string Kind, Text; } |
| 39 | |
| 40 | private static volatile bool on; |
| 41 | private static bool hooked; |
| 42 | private static readonly object gate = new object(); |
| 43 | private static readonly Queue<Entry> pending = new Queue<Entry>(); |
| 44 | private static int dropped; |
| 45 | private static float nextSnap, nextWatch, nextSys; |
| 46 | private static float fps = 60f, worst, worstShown, sampleStart; |
| 47 | private static int frames; |
| 48 | private static int restoreBase, restoresPerSec; |
| 49 | private static readonly Regex tags = new Regex("<[^>]+>"); |
| 50 | |
| 51 | public static bool On { get { return on; } } |
| 52 | |
| 53 | public static void SetOn(bool v) |
| 54 | { |
| 55 | if (v) Hook(); |
| 56 | if (v == on) return; |
| 57 | lock (gate) { pending.Clear(); dropped = 0; } |
| 58 | on = v; |
| 59 | if (v) |
| 60 | { |
| 61 | nextSnap = 0f; |
| 62 | nextSys = 0f; |
| 63 | ResetWatch(); |
| 64 | Add("TRAINER", "Debug feed started"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private static void Hook() |
| 69 | { |
| 70 | if (hooked) return; |
| 71 | hooked = true; |
| 72 | Application.logMessageReceivedThreaded += OnUnityLog; |
| 73 | SceneManager.sceneLoaded += delegate (Scene s, LoadSceneMode m) |
| 74 | { |
| 75 | Add("SCENE", "Loaded '" + s.name + "' (" + m + ", " + s.rootCount + " root objects)"); |
| 76 | }; |
| 77 | SceneManager.sceneUnloaded += delegate (Scene s) { Add("SCENE", "Unloaded '" + s.name + "'"); }; |
| 78 | Application.lowMemory += delegate { Add("ERROR", "Low memory warning from the system"); }; |
| 79 | Application.focusChanged += delegate (bool f) { Add("SYSTEM", f ? "Game window focused" : "Game window lost focus"); }; |
| 80 | } |
| 81 | |
| 82 | private static Type FindType(string name) |
| 83 | { |
| 84 | foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) |
| 85 | { |
| 86 | try { Type t = a.GetType(name, false); if (t != null) return t; } catch (Exception) { } |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | private static object Prop(object o, string name) |
| 92 | { |
| 93 | if (o == null) return null; |
| 94 | try |
| 95 | { |
| 96 | PropertyInfo p = o.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
| 97 | if (p != null) return p.GetValue(o, null); |
| 98 | FieldInfo f = o.GetType().GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
| 99 | return f != null ? f.GetValue(o) : null; |
| 100 | } |
| 101 | catch (Exception) { return null; } |
| 102 | } |
| 103 | |
| 104 | internal static System.Collections.IDictionary BepPlugins() |
| 105 | { |
| 106 | Type t = FindType("BepInEx.Bootstrap.Chainloader"); |
| 107 | if (t == null) return null; |
| 108 | try |
| 109 | { |
| 110 | PropertyInfo p = t.GetProperty("PluginInfos", BindingFlags.Public | BindingFlags.Static); |
| 111 | return p != null ? p.GetValue(null, null) as System.Collections.IDictionary : null; |
| 112 | } |
| 113 | catch (Exception) { return null; } |
| 114 | } |
| 115 | |
| 116 | private static void DescribeBep(StringBuilder sb) |
| 117 | { |
| 118 | System.Collections.IDictionary d = BepPlugins(); |
| 119 | Type paths = FindType("BepInEx.Paths"); |
| 120 | sb.Append("HBEPINEX\n"); |
| 121 | if (d == null || paths == null) { sb.Append("DNot loaded (the trainer doesn't need it; my other mods do)\n"); return; } |
| 122 | sb.Append("NBepInEx ").Append(paths.Assembly.GetName().Version).Append(", ").Append(d.Count).Append(" plugins\n"); |
| 123 | foreach (System.Collections.DictionaryEntry kv in d) |
| 124 | { |
| 125 | object meta = Prop(kv.Value, "Metadata"); |
| 126 | string f = ""; |
| 127 | try { f = System.IO.Path.GetFileName(Prop(kv.Value, "Location") as string); } catch (Exception) { } |
| 128 | sb.Append("N").Append(Prop(meta, "Name")).Append(' ').Append(Prop(meta, "Version")).Append(" ").Append(f).Append('\n'); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private static void OnUnityLog(string msg, string stack, LogType type) |
| 133 | { |
| 134 | if (!on) return; |
| 135 | string text = msg ?? "null"; |
| 136 | string kind = "GAME"; |
| 137 | if (type == LogType.Warning) kind = "WARNING"; |
| 138 | else if (type == LogType.Error || type == LogType.Exception || type == LogType.Assert) |
| 139 | { |
| 140 | kind = "ERROR"; |
| 141 | if (!string.IsNullOrEmpty(stack)) text += "\n" + stack.TrimEnd(); |
| 142 | } |
| 143 | if (text.StartsWith("[Ultra Paradise]", StringComparison.Ordinal) && kind == "GAME") kind = "TRAINER"; |
| 144 | Add(kind, text); |
| 145 | } |
| 146 | |
| 147 | public static void Add(string kind, string text) |
| 148 | { |
| 149 | if (!on || text == null) return; |
| 150 | if (text.Length > 6000) text = text.Substring(0, 6000) + "..."; |
| 151 | Entry e; |
| 152 | e.Kind = kind; |
| 153 | e.Text = text.Replace("\r", "").Replace('\n', NL); |
| 154 | lock (gate) |
| 155 | { |
| 156 | if (pending.Count >= 3000) { dropped++; return; } |
| 157 | pending.Enqueue(e); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | public static void Feed(string tag, string color, string text) |
| 162 | { |
| 163 | string kind = "TRAINER"; |
| 164 | if (tag == "WARNING") kind = color == "#9cff8a" ? "GOOD" : "ALERT"; |
| 165 | else if (tag == "EXPLOSION") kind = "EVENT"; |
| 166 | Add(kind, (tag == "MOD" ? "" : tag + ": ") + text); |
| 167 | } |
| 168 | |
| 169 | public static void Tick() |
| 170 | { |
| 171 | if (!on) return; |
| 172 | float now = Time.unscaledTime; |
| 173 | float dt = Time.unscaledDeltaTime; |
| 174 | frames++; |
| 175 | if (dt > worst) worst = dt; |
| 176 | if (now - sampleStart >= 1f) |
| 177 | { |
| 178 | fps = frames / Mathf.Max(0.001f, now - sampleStart); |
| 179 | worstShown = worst; |
| 180 | worst = 0f; |
| 181 | frames = 0; |
| 182 | sampleStart = now; |
| 183 | restoresPerSec = Stats.Restores - restoreBase; |
| 184 | restoreBase = Stats.Restores; |
| 185 | } |
| 186 | try { MoodWatch.Tick(); } catch (Exception) { } |
| 187 | if (now >= nextWatch) |
| 188 | { |
| 189 | nextWatch = now + 0.25f; |
| 190 | try { Watch(); } catch (Exception) { } |
| 191 | } |
| 192 | Flush(); |
| 193 | if (now >= nextSys) |
| 194 | { |
| 195 | nextSys = now + 60f; |
| 196 | try { Link.Post("DSYS " + Encode(SystemText())); } catch (Exception e) { Add("TRAINER", "Couldn't read the system info: " + e.Message); } |
| 197 | } |
| 198 | if (now >= nextSnap) |
| 199 | { |
| 200 | nextSnap = now + 1f; |
| 201 | try { Link.Post("DSNAP " + Encode(Snapshot())); } |
| 202 | catch (Exception e) { Add("TRAINER", "Status failed: " + e.GetType().Name + " " + e.Message); } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private static void Flush() |
| 207 | { |
| 208 | StringBuilder sb = null; |
| 209 | int n = 0, drop; |
| 210 | lock (gate) |
| 211 | { |
| 212 | drop = dropped; |
| 213 | dropped = 0; |
| 214 | while (pending.Count > 0 && n < 400) |
| 215 | { |
| 216 | Entry e = pending.Dequeue(); |
| 217 | if (sb == null) sb = new StringBuilder("DLOG "); |
| 218 | else sb.Append(REC); |
| 219 | sb.Append(e.Kind).Append(SEP).Append(e.Text); |
| 220 | n++; |
| 221 | } |
| 222 | } |
| 223 | if (drop > 0) |
| 224 | { |
| 225 | if (sb == null) sb = new StringBuilder("DLOG "); |
| 226 | else sb.Append(REC); |
| 227 | sb.Append("ERROR").Append(SEP).Append(drop).Append(" lines skipped (the game logged faster than the trainer could keep up)"); |
| 228 | } |
| 229 | if (sb != null) Link.Post(sb.ToString()); |
| 230 | } |
| 231 | |
| 232 | private static string Encode(string s) |
| 233 | { |
| 234 | return s.TrimEnd('\n').Replace("\r", "").Replace('\n', NL); |
| 235 | } |
| 236 | |
| 237 | private static string F(float v) { return v.ToString("0.0", CultureInfo.InvariantCulture); } |
| 238 | |
| 239 | private static int Count<T>() where T : Object |
| 240 | { |
| 241 | try { return Object.FindObjectsOfType<T>().Length; } catch (Exception) { return 0; } |
| 242 | } |
| 243 | |
| 244 | private static string SystemText() |
| 245 | { |
| 246 | StringBuilder sb = new StringBuilder(); |
| 247 | sb.Append("HGAME\n"); |
| 248 | sb.Append("N").Append(Application.productName).Append(' ').Append(Application.version).Append(" | Unity ").Append(Application.unityVersion).Append('\n'); |
| 249 | sb.Append("NTrainer code ").Append(Link.Version) |
| 250 | #if LINUXPLUGIN |
| 251 | .Append(" | Linux code ").Append(LinuxHost.Version) |
| 252 | #endif |
| 253 | .Append(" | UltraPara ").Append(Environment.GetEnvironmentVariable("ULTRAPARA_VERSION") ?? "not loaded") |
| 254 | .Append(" | Harmony ").Append(typeof(Harmony).Assembly.GetName().Version).Append('\n'); |
| 255 | sb.Append("NGame folder: ").Append(Scrub(System.IO.Path.GetDirectoryName(Application.dataPath))).Append('\n'); |
| 256 | sb.Append("HSYSTEM\n"); |
| 257 | sb.Append("N").Append(SystemInfo.operatingSystem).Append(" | ").Append(SystemInfo.systemMemorySize).Append(" MB RAM\n"); |
| 258 | sb.Append("NCPU ").Append(SystemInfo.processorType).Append(" (").Append(SystemInfo.processorCount).Append(" threads)\n"); |
| 259 | sb.Append("NGPU ").Append(SystemInfo.graphicsDeviceName).Append(" (").Append(SystemInfo.graphicsMemorySize).Append(" MB, ").Append(SystemInfo.graphicsDeviceVersion).Append(")\n"); |
| 260 | sb.Append("NScreen ").Append(Screen.width).Append('x').Append(Screen.height).Append(" @ ").Append(Screen.currentResolution.refreshRateRatio.value.ToString("0", CultureInfo.InvariantCulture)) |
| 261 | .Append(" Hz, ").Append(Screen.fullScreenMode).Append(", vSync ").Append(QualitySettings.vSyncCount).Append(", FPS cap ").Append(Application.targetFrameRate).Append('\n'); |
| 262 | sb.Append("NDisplays the game sees: ").Append(Display.displays != null ? Display.displays.Length : 1).Append('\n'); |
| 263 | DescribeBep(sb); |
| 264 | sb.Append("HHARMONY PATCHES\n"); |
| 265 | try |
| 266 | { |
| 267 | List<string> rows = new List<string>(); |
| 268 | foreach (MethodBase m in Harmony.GetAllPatchedMethods()) |
| 269 | { |
| 270 | Patches p = Harmony.GetPatchInfo(m); |
| 271 | if (p == null) continue; |
| 272 | rows.Add((m.DeclaringType != null ? m.DeclaringType.Name : "?") + "." + m.Name + " <- " + string.Join(", ", new List<string>(p.Owners).ToArray()) |
| 273 | + " (pre " + p.Prefixes.Count + ", post " + p.Postfixes.Count + ", trans " + p.Transpilers.Count + ", fin " + p.Finalizers.Count + ")"); |
| 274 | } |
| 275 | rows.Sort(StringComparer.Ordinal); |
| 276 | sb.Append("D").Append(rows.Count).Append(" patched methods\n"); |
| 277 | foreach (string r in rows) sb.Append("N").Append(r).Append('\n'); |
| 278 | } |
| 279 | catch (Exception e) { sb.Append("RCouldn't list patches: ").Append(e.Message).Append('\n'); } |
| 280 | return sb.ToString(); |
| 281 | } |
| 282 | |
| 283 | private static string Scrub(string s) |
| 284 | { |
| 285 | if (string.IsNullOrEmpty(s)) return s; |
| 286 | try |
| 287 | { |
| 288 | string user = Environment.UserName; |
| 289 | if (!string.IsNullOrEmpty(user) && user.Length > 1) s = s.Replace(user, "<you>"); |
| 290 | } |
| 291 | catch (Exception) { } |
| 292 | return s; |
| 293 | } |
| 294 | |
| 295 | private static string Snapshot() |
| 296 | { |
| 297 | StringBuilder sb = new StringBuilder(); |
| 298 | sb.Append("HENGINE\n"); |
| 299 | sb.Append(fps < 30f ? "Y" : "N").Append("FPS ").Append(fps.ToString("0", CultureInfo.InvariantCulture)).Append(" | frame ").Append((Time.unscaledDeltaTime * 1000f).ToString("0.0", CultureInfo.InvariantCulture)) |
| 300 | .Append(" ms | worst ").Append((worstShown * 1000f).ToString("0", CultureInfo.InvariantCulture)).Append(" ms | time scale ").Append(Time.timeScale.ToString("0.##", CultureInfo.InvariantCulture)) |
| 301 | .Append(PauseHandler.paused ? " | PAUSED" : "").Append('\n'); |
| 302 | sb.Append("NScene ").Append(SceneManager.GetActiveScene().name).Append(" | frame #").Append(Time.frameCount) |
| 303 | .Append(" | memory ").Append((GC.GetTotalMemory(false) / (1024f * 1024f)).ToString("0.0", CultureInfo.InvariantCulture)).Append(" MB\n"); |
| 304 | Rigidbody2D[] rbs = Object.FindObjectsOfType<Rigidbody2D>(); |
| 305 | int awake = 0; |
| 306 | for (int i = 0; i < rbs.Length; i++) if (rbs[i].IsAwake()) awake++; |
| 307 | AudioSource[] audio = Object.FindObjectsOfType<AudioSource>(); |
| 308 | int playing = 0; |
| 309 | for (int i = 0; i < audio.Length; i++) if (audio[i].isPlaying) playing++; |
| 310 | sb.Append("NObjects ").Append(Count<Transform>()).Append(" | physics bodies ").Append(rbs.Length).Append(" (").Append(awake).Append(" awake) | sounds playing ").Append(playing).Append('\n'); |
| 311 | |
| 312 | WorldGeneration w = WorldGeneration.world; |
| 313 | sb.Append("HWORLD\n"); |
| 314 | if (w == null) sb.Append("DNo world loaded (main menu)\n"); |
| 315 | else |
| 316 | { |
| 317 | string biome = (w.biomeTitles != null && w.biomeDepth >= 0 && w.biomeDepth < w.biomeTitles.Length) ? w.biomeTitles[w.biomeDepth] : "?"; |
| 318 | sb.Append("NLayer ").Append(w.biomeDepth + 1).Append('/').Append(w.amountOfLayers).Append(" - ").Append(tags.Replace(biome ?? "?", "")) |
| 319 | .Append(w.endlessLoops > 0 ? " (loop " + w.endlessLoops + ")" : "") |
| 320 | .Append(w.biomeOverride != WorldGeneration.OverrideSceneType.None ? " | special area " + w.biomeOverride : "").Append('\n'); |
| 321 | sb.Append(w.generatingWorld ? "Y" : "N").Append("Generating ").Append(w.generatingWorld ? "YES" : "no").Append(" | since generated ").Append(w.timeSinceFinishedGeneration.ToString("0", CultureInfo.InvariantCulture)) |
| 322 | .Append("s | on this layer ").Append(w.layerTimeSpent.ToString("0", CultureInfo.InvariantCulture)).Append("s | run ").Append(w.realTimeElapsed.ToString("0", CultureInfo.InvariantCulture)).Append("s\n"); |
| 323 | sb.Append("NSize ").Append(w.width).Append('x').Append(w.height).Append(" blocks, ").Append(Count<ChunkScript>()).Append(" chunks | traveled ").Append(w.totalTraveled).Append('\n'); |
| 324 | sb.Append("NTemperature ").Append((w.ambientTemperature + w.temperatureOffset).ToString("0.0", CultureInfo.InvariantCulture)).Append("C | fog ").Append(w.fogAmount.ToString("0.00", CultureInfo.InvariantCulture)) |
| 325 | .Append(" | rain ").Append(w.forceRain ? "forced" : "-").Append(" | fungal rain ").Append(w.fungusRainIntensity.ToString("0.00", CultureInfo.InvariantCulture)).Append('\n'); |
| 326 | sb.Append(w.earthquakeIntensity > 0.01f ? "Y" : "N").Append("Earthquake ").Append(w.earthquakeIntensity.ToString("0.00", CultureInfo.InvariantCulture)).Append(" (next in ").Append(w.earthquakeDelay.ToString("0", CultureInfo.InvariantCulture)) |
| 327 | .Append("s) | cutscene ").Append(w.cutsceneActive ? "yes" : "no").Append(" | unchipped ").Append(w.unchippedMode ? "yes" : "no").Append('\n'); |
| 328 | sb.Append("NLoot x").Append(w.lootRarityMultiplier.ToString("0.##", CultureInfo.InvariantCulture)).Append(" | traps x").Append(w.trapRarityMultiplier.ToString("0.##", CultureInfo.InvariantCulture)) |
| 329 | .Append(" | decay x").Append(WorldGeneration.globalDecayRate.ToString("0.##", CultureInfo.InvariantCulture)).Append(" | time limit ").Append((w.maxTimePerLayer / 60f).ToString("0.#", CultureInfo.InvariantCulture)).Append(" min\n"); |
| 330 | |
| 331 | sb.Append("HENTITIES\n"); |
| 332 | sb.Append("NItems ").Append(Count<Item>()).Append(" | buildings ").Append(Count<BuildingEntity>()).Append(" | mines ").Append(Count<MineScript>()).Append(" | corpses ").Append(Count<CorpseScript>()).Append('\n'); |
| 333 | sb.Append("NTurrets ").Append(Count<TurretScript>() + Count<CeilingTurret>()).Append(" | drones ").Append(Count<DroneScript>() + Count<LaserDroneScript>()) |
| 334 | .Append(" | guards ").Append(Count<GuardScript>()).Append(" | crystals ").Append(Count<CrystalEnemy>()).Append('\n'); |
| 335 | sb.Append("NSpiders ").Append(Count<SpiderHandler>()).Append(" | bloodsuckers ").Append(Count<BloodSuckerScript>()).Append(" | thornbacks ").Append(Count<ElderThornbackBehaviour>()) |
| 336 | .Append(" | xaloris ").Append(Count<XalorisScript>()).Append(" | survivors ").Append(Count<TraderScript>()).Append('\n'); |
| 337 | } |
| 338 | |
| 339 | Body b = PlayerCamera.main != null ? PlayerCamera.main.body : null; |
| 340 | sb.Append("HPLAYER\n"); |
| 341 | if (b == null) sb.Append("DNo player yet\n"); |
| 342 | else Vitals(sb, b); |
| 343 | |
| 344 | if (w != null && b != null) |
| 345 | { |
| 346 | StringBuilder mood = new StringBuilder(); |
| 347 | MoodWatch.Describe(mood, b); |
| 348 | foreach (string raw in mood.ToString().Split('\n')) |
| 349 | { |
| 350 | if (raw.Trim().Length == 0) continue; |
| 351 | string code = "N"; |
| 352 | if (raw.Contains("#7fd4ff")) code = "H"; |
| 353 | else if (raw.Contains("#ff3030") || raw.Contains("#ff6b6b")) code = "R"; |
| 354 | else if (raw.Contains("#ff9a3c") || raw.Contains("#ffd966")) code = "Y"; |
| 355 | else if (raw.Contains("#9cff8a")) code = "G"; |
| 356 | sb.Append(code).Append(tags.Replace(raw, "")).Append('\n'); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | sb.Append("HCHEATS\n"); |
| 361 | Plugin.DescribeCheats(sb); |
| 362 | sb.Append("NVital restores ").Append(restoresPerSec).Append("/s (").Append(Stats.Restores).Append(" total)\n"); |
| 363 | if (Stats.Order.Count == 0) sb.Append("DNothing blocked or changed yet\n"); |
| 364 | else |
| 365 | { |
| 366 | StringBuilder row = new StringBuilder(); |
| 367 | for (int i = 0; i < Stats.Order.Count; i++) |
| 368 | { |
| 369 | string k = Stats.Order[i]; |
| 370 | row.Append(k).Append(' ').Append(Stats.Get(k)); |
| 371 | if (i % 3 == 2 || i == Stats.Order.Count - 1) { sb.Append('N').Append(row).Append('\n'); row.Length = 0; } |
| 372 | else row.Append(" | "); |
| 373 | } |
| 374 | } |
| 375 | return sb.ToString(); |
| 376 | } |
| 377 | |
| 378 | private static void Vitals(StringBuilder sb, Body b) |
| 379 | { |
| 380 | Vector2 pos = b.transform.position; |
| 381 | Vector2 vel = b.rb != null ? b.rb.velocity : Vector2.zero; |
| 382 | sb.Append(b.alive ? (b.conscious ? "G" : "Y") : "R").Append("Character ").Append(b.charType).Append(b.alive ? ", alive" : ", DEAD").Append(b.conscious ? ", conscious" : ", UNCONSCIOUS") |
| 383 | .Append(b.sleeping ? ", sleeping" : "").Append(Plugin.Paused(b) ? ", God Mode PAUSED (" + Plugin.PauseReason + ")" : "").Append('\n'); |
| 384 | sb.Append("NPosition ").Append(F(pos.x)).Append(", ").Append(F(pos.y)).Append(" | velocity ").Append(F(vel.x)).Append(", ").Append(F(vel.y)).Append(" | holding ").Append(Held(b)).Append('\n'); |
| 385 | sb.Append(b.brainHealth < 50f || b.bloodVolume < 60f ? "R" : "N").Append("Brain ").Append(F(b.brainHealth)).Append(" | consciousness ").Append(F(b.consciousness)).Append(" | blood ").Append(F(b.bloodVolume)) |
| 386 | .Append(" | oxygen ").Append(F(b.bloodOxygen)).Append('\n'); |
| 387 | sb.Append("NBlood pressure ").Append(F(b.bloodPressure)).Append(" | heart rate ").Append(F(b.heartRate)).Append(" | fibrillation ").Append(F(b.fibrillationProgress)) |
| 388 | .Append(" | embolism ").Append(b.hasPulmonaryEmbolism ? "YES" : "no").Append('\n'); |
| 389 | sb.Append("NShock ").Append(F(b.shock)).Append(" | pain shock ").Append(F(b.painShock)).Append(" | internal bleed ").Append(F(b.internalBleeding)) |
| 390 | .Append(" | hemothorax ").Append(F(b.hemothorax)).Append(" | septic ").Append(F(b.septicShock)).Append(" | stroke ").Append(F(b.strokeAmount)).Append('\n'); |
| 391 | sb.Append("NBleed speed ").Append(b.totalBleedSpeed.ToString("0.000", CultureInfo.InvariantCulture)).Append(" | sickness ").Append(F(b.sicknessAmount)).Append(" | radiation ").Append(F(b.radiationSickness)) |
| 392 | .Append(" | venom ").Append(F(b.venomCurrent)).Append(" | barotrauma ").Append(F(b.barotrauma)).Append('\n'); |
| 393 | sb.Append("NTemperature ").Append(F(b.temperature)).Append("C | hearing loss ").Append(F(b.hearingLoss)).Append(" | trauma ").Append(F(b.traumaAmount)).Append('\n'); |
| 394 | sb.Append("NHunger ").Append(F(b.hunger)).Append(" | thirst ").Append(F(b.thirst)).Append(" | energy ").Append(F(b.energy)).Append(" | stamina ").Append(F(b.stamina)) |
| 395 | .Append(" | mood ").Append(F(b.totalHappiness)).Append(" (base ").Append(F(b.happiness)).Append(")\n"); |
| 396 | if (b.skills != null) sb.Append("NStrength ").Append(b.skills.STR).Append(" | resilience ").Append(b.skills.RES).Append(" | intelligence ").Append(b.skills.INT).Append('\n'); |
| 397 | Limb[] limbs = b.limbs; |
| 398 | if (limbs == null) return; |
| 399 | sb.Append("HLIMBS\n"); |
| 400 | for (int i = 0; i < limbs.Length; i++) |
| 401 | { |
| 402 | Limb l = limbs[i]; |
| 403 | if (l == null) continue; |
| 404 | string name = l.gameObject.name; |
| 405 | if (l.dismembered) { sb.Append("R").Append(name.PadRight(14)).Append(" LOST\n"); continue; } |
| 406 | string flags = LimbFlags(l); |
| 407 | sb.Append(flags.Length > 0 || l.bleedAmount > 0.1f ? "Y" : "N").Append(name.PadRight(14)) |
| 408 | .Append(" skin ").Append(F(l.skinHealth).PadLeft(5)).Append(" muscle ").Append(F(l.muscleHealth).PadLeft(5)) |
| 409 | .Append(" bleed ").Append(l.bleedAmount.ToString("0.00", CultureInfo.InvariantCulture)).Append(" pain ").Append(F(l.pain)) |
| 410 | .Append(" infection ").Append(F(l.infectionAmount)).Append(" shrapnel ").Append(l.shrapnel).Append(flags).Append('\n'); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | private static string LimbFlags(Limb l) |
| 415 | { |
| 416 | string s = ""; |
| 417 | if (l.broken) s += " BROKEN"; |
| 418 | if (l.dislocated) s += " DISLOCATED"; |
| 419 | if (l.infected) s += " INFECTED"; |
| 420 | return s; |
| 421 | } |
| 422 | |
| 423 | private static string Held(Body b) |
| 424 | { |
| 425 | try |
| 426 | { |
| 427 | Item it = b.GetItem(b.handSlot); |
| 428 | return it != null ? it.id : "nothing"; |
| 429 | } |
| 430 | catch (Exception) { return "?"; } |
| 431 | } |
| 432 | |
| 433 | private static Body wBody; |
| 434 | private static bool wAlive, wConscious, wSleeping, wPaused, wSeen; |
| 435 | private static int wLayer = -2; |
| 436 | private static float wTimeScale = -1f; |
| 437 | private static string wHeld; |
| 438 | private static string[] wLimbs; |
| 439 | |
| 440 | private static void ResetWatch() |
| 441 | { |
| 442 | wBody = null; |
| 443 | wSeen = false; |
| 444 | wLayer = -2; |
| 445 | wTimeScale = -1f; |
| 446 | wLimbs = null; |
| 447 | wPaused = PauseHandler.paused; |
| 448 | } |
| 449 | |
| 450 | private static string LimbState(Limb l) |
| 451 | { |
| 452 | if (l == null) return ""; |
| 453 | if (l.dismembered) return "lost"; |
| 454 | string s = LimbFlags(l).Trim().ToLowerInvariant(); |
| 455 | if (l.bleedAmount > 0.1f) s += (s.Length > 0 ? " " : "") + "bleeding"; |
| 456 | return s.Length == 0 ? "fine" : s; |
| 457 | } |
| 458 | |
| 459 | private static void Watch() |
| 460 | { |
| 461 | if (Time.timeScale != wTimeScale) |
| 462 | { |
| 463 | if (wTimeScale >= 0f) Add("EVENT", "Time scale " + wTimeScale.ToString("0.##", CultureInfo.InvariantCulture) + " -> " + Time.timeScale.ToString("0.##", CultureInfo.InvariantCulture)); |
| 464 | wTimeScale = Time.timeScale; |
| 465 | } |
| 466 | if (PauseHandler.paused != wPaused) |
| 467 | { |
| 468 | wPaused = PauseHandler.paused; |
| 469 | Add("EVENT", wPaused ? "Game paused" : "Game unpaused"); |
| 470 | } |
| 471 | WorldGeneration w = WorldGeneration.world; |
| 472 | int layer = w != null ? w.biomeDepth : -1; |
| 473 | if (layer != wLayer) |
| 474 | { |
| 475 | bool first = wLayer == -2; |
| 476 | wLayer = layer; |
| 477 | if (w != null && !first) |
| 478 | { |
| 479 | string biome = (w.biomeTitles != null && layer >= 0 && layer < w.biomeTitles.Length) ? tags.Replace(w.biomeTitles[layer] ?? "?", "") : "?"; |
| 480 | Add("EVENT", "Now on layer " + (layer + 1) + "/" + w.amountOfLayers + " - " + biome + (w.endlessLoops > 0 ? " (loop " + w.endlessLoops + ")" : "")); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | Body b = PlayerCamera.main != null ? PlayerCamera.main.body : null; |
| 485 | if (b != wBody || !wSeen) |
| 486 | { |
| 487 | bool had = wBody != null; |
| 488 | wBody = b; |
| 489 | wSeen = true; |
| 490 | wLimbs = null; |
| 491 | if (b != null) Add("EVENT", "Player body: " + b.gameObject.name + " (character " + b.charType + ")"); |
| 492 | else if (had) Add("EVENT", "Player body gone"); |
| 493 | if (b == null) return; |
| 494 | wAlive = b.alive; |
| 495 | wConscious = b.conscious; |
| 496 | wSleeping = b.sleeping; |
| 497 | wHeld = Held(b); |
| 498 | } |
| 499 | if (b == null) return; |
| 500 | |
| 501 | if (b.alive != wAlive) { wAlive = b.alive; Add(wAlive ? "GOOD" : "ALERT", wAlive ? "Player is alive again" : "PLAYER DIED"); } |
| 502 | if (b.conscious != wConscious) { wConscious = b.conscious; Add(wConscious ? "EVENT" : "ALERT", wConscious ? "Player regained consciousness" : "Player lost consciousness"); } |
| 503 | if (b.sleeping != wSleeping) { wSleeping = b.sleeping; Add("EVENT", wSleeping ? "Player fell asleep" : "Player woke up"); } |
| 504 | string held = Held(b); |
| 505 | if (held != wHeld) { Add("EVENT", "Holding: " + wHeld + " -> " + held); wHeld = held; } |
| 506 | |
| 507 | Limb[] limbs = b.limbs; |
| 508 | if (limbs == null) return; |
| 509 | if (wLimbs == null || wLimbs.Length != limbs.Length) |
| 510 | { |
| 511 | wLimbs = new string[limbs.Length]; |
| 512 | for (int i = 0; i < limbs.Length; i++) wLimbs[i] = LimbState(limbs[i]); |
| 513 | return; |
| 514 | } |
| 515 | for (int i = 0; i < limbs.Length; i++) |
| 516 | { |
| 517 | string s = LimbState(limbs[i]); |
| 518 | if (s == wLimbs[i]) continue; |
| 519 | Add(s == "fine" ? "GOOD" : "LIMB", (limbs[i] != null ? limbs[i].gameObject.name : "#" + i) + ": " + wLimbs[i] + " -> " + s); |
| 520 | wLimbs[i] = s; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |