cu-trainer/Ultra Paradise Application/src/Payload.cs
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Globalization; |
| 4 | using System.IO; |
| 5 | using System.Net.Sockets; |
| 6 | using System.Reflection; |
| 7 | using System.Text; |
| 8 | using System.Threading; |
| 9 | using HarmonyLib; |
| 10 | using UnityEngine; |
| 11 | using Object = UnityEngine.Object; |
| 12 | |
| 13 | [assembly: AssemblyMetadata("Notice", "Copyright (c) casualexpie. All rights reserved. Do NOT decompile, disassemble, or reverse-engineer this assembly. Report bug symptoms on the Nexus Bugs page; do not post decompiled or reconstructed source. Reverse-engineering this DLL is not authorized.")] |
| 14 | [assembly: AssemblyTitle("Ultra Paradise CU:Trainer Payload")] |
| 15 | [assembly: AssemblyProduct("Ultra Paradise CU:Trainer")] |
| 16 | [assembly: AssemblyCopyright("Copyright (c) casualexpie")] |
| 17 | |
| 18 | namespace UltraParadise |
| 19 | { |
| 20 | public static class Loader |
| 21 | { |
| 22 | private static int started; |
| 23 | |
| 24 | public static void Init() |
| 25 | { |
| 26 | if (Interlocked.Exchange(ref started, 1) == 1) return; |
| 27 | AppDomain.CurrentDomain.AssemblyResolve += Resolve; |
| 28 | Application.onBeforeRender += Boot; |
| 29 | } |
| 30 | |
| 31 | private static Assembly Resolve(object sender, ResolveEventArgs args) |
| 32 | { |
| 33 | string name; |
| 34 | try { name = new AssemblyName(args.Name).Name; } catch (Exception) { return null; } |
| 35 | foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) |
| 36 | { |
| 37 | try { if (a.GetName().Name == name) return a; } catch (Exception) { } |
| 38 | } |
| 39 | Stream s = typeof(Loader).Assembly.GetManifestResourceStream("lib." + name + ".dll"); |
| 40 | if (s == null) return null; |
| 41 | using (s) |
| 42 | { |
| 43 | MemoryStream ms = new MemoryStream(); |
| 44 | byte[] buf = new byte[81920]; |
| 45 | int n; |
| 46 | while ((n = s.Read(buf, 0, buf.Length)) > 0) ms.Write(buf, 0, n); |
| 47 | return Assembly.Load(ms.ToArray()); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private static void Boot() |
| 52 | { |
| 53 | Application.onBeforeRender -= Boot; |
| 54 | try |
| 55 | { |
| 56 | GameObject go = new GameObject("UltraParadise"); |
| 57 | go.hideFlags = HideFlags.HideAndDontSave; |
| 58 | Object.DontDestroyOnLoad(go); |
| 59 | #if LINUXPLUGIN |
| 60 | go.AddComponent<LinuxHost>(); |
| 61 | #endif |
| 62 | go.AddComponent<Agent>(); |
| 63 | } |
| 64 | catch (Exception e) { Debug.LogError("[Ultra Paradise] Couldn't start: " + e); } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | internal static class Link |
| 69 | { |
| 70 | private static readonly object gate = new object(); |
| 71 | private static readonly Queue<string> inbox = new Queue<string>(); |
| 72 | private static TcpClient client; |
| 73 | private static StreamWriter writer; |
| 74 | private static Thread thread; |
| 75 | public static volatile bool Connected; |
| 76 | #if LINUXPLUGIN |
| 77 | public const string Version = "L1"; |
| 78 | #else |
| 79 | public const string Version = "4"; |
| 80 | #endif |
| 81 | |
| 82 | public static void Start() |
| 83 | { |
| 84 | if (thread != null) return; |
| 85 | thread = new Thread(Loop); |
| 86 | thread.IsBackground = true; |
| 87 | thread.Name = "Ultra Paradise link"; |
| 88 | thread.Start(); |
| 89 | } |
| 90 | |
| 91 | private static string LinkFile() |
| 92 | { |
| 93 | #if LINUXPLUGIN |
| 94 | return LinuxHost.LinkFile; |
| 95 | #endif |
| 96 | return Path.Combine(Path.Combine(Path.GetTempPath(), "UltraParadise"), "link-" + System.Diagnostics.Process.GetCurrentProcess().Id + ".txt"); |
| 97 | } |
| 98 | |
| 99 | private static void Loop() |
| 100 | { |
| 101 | string file = LinkFile(); |
| 102 | while (true) |
| 103 | { |
| 104 | try |
| 105 | { |
| 106 | if (File.Exists(file)) |
| 107 | { |
| 108 | string[] parts = File.ReadAllText(file).Trim().Split(' '); |
| 109 | int port; |
| 110 | if (parts.Length == 2 && int.TryParse(parts[0], out port) && port > 0 && port < 65536) Session(port, parts[1]); |
| 111 | } |
| 112 | } |
| 113 | catch (Exception) { } |
| 114 | Connected = false; |
| 115 | Thread.Sleep(1000); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private static void Session(int port, string token) |
| 120 | { |
| 121 | TcpClient c = new TcpClient(); |
| 122 | try |
| 123 | { |
| 124 | c.NoDelay = true; |
| 125 | c.Connect("127.0.0.1", port); |
| 126 | NetworkStream ns = c.GetStream(); |
| 127 | StreamReader r = new StreamReader(ns, new UTF8Encoding(false)); |
| 128 | StreamWriter w = new StreamWriter(ns, new UTF8Encoding(false)); |
| 129 | w.NewLine = "\n"; |
| 130 | w.AutoFlush = true; |
| 131 | w.WriteLine("HELLO " + token + " " + Version); |
| 132 | if (r.ReadLine() != "OK") return; |
| 133 | lock (gate) { client = c; writer = w; } |
| 134 | Connected = true; |
| 135 | Enqueue("CONNECTED"); |
| 136 | string line; |
| 137 | while ((line = r.ReadLine()) != null) |
| 138 | { |
| 139 | if (line.Length > 512) continue; |
| 140 | Enqueue(line); |
| 141 | } |
| 142 | } |
| 143 | catch (Exception) { } |
| 144 | finally |
| 145 | { |
| 146 | lock (gate) { client = null; writer = null; } |
| 147 | Connected = false; |
| 148 | Enqueue("DISCONNECTED"); |
| 149 | try { c.Close(); } catch (Exception) { } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private static void Enqueue(string s) |
| 154 | { |
| 155 | lock (inbox) inbox.Enqueue(s); |
| 156 | } |
| 157 | |
| 158 | public static bool TryTake(out string s) |
| 159 | { |
| 160 | lock (inbox) |
| 161 | { |
| 162 | if (inbox.Count == 0) { s = null; return false; } |
| 163 | s = inbox.Dequeue(); |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | #if LINUXPLUGIN |
| 169 | internal static void Inject(string s) { Enqueue(s); } |
| 170 | #endif |
| 171 | |
| 172 | public static void Send(string s) |
| 173 | { |
| 174 | #if LINUXPLUGIN |
| 175 | LinuxHost.Sent(s); |
| 176 | #endif |
| 177 | lock (gate) |
| 178 | { |
| 179 | if (writer == null) return; |
| 180 | try { writer.WriteLine(s.Replace('\n', ' ').Replace('\r', ' ')); } |
| 181 | catch (Exception) { try { client.Close(); } catch (Exception) { } } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private static readonly object frameGate = new object(); |
| 186 | private static readonly AutoResetEvent frameReady = new AutoResetEvent(false); |
| 187 | private static readonly Queue<string> bulk = new Queue<string>(); |
| 188 | private static Thread frameThread; |
| 189 | |
| 190 | public static bool FramePending { get { lock (frameGate) return bulk.Count > 0; } } |
| 191 | |
| 192 | public static void PostFrame(string s) { Post(s); } |
| 193 | |
| 194 | public static void Post(string s) |
| 195 | { |
| 196 | lock (frameGate) |
| 197 | { |
| 198 | if (bulk.Count >= 64) return; |
| 199 | bulk.Enqueue(s); |
| 200 | } |
| 201 | if (frameThread == null) |
| 202 | { |
| 203 | frameThread = new Thread(FrameLoop); |
| 204 | frameThread.IsBackground = true; |
| 205 | frameThread.Name = "Ultra Paradise bulk"; |
| 206 | frameThread.Start(); |
| 207 | } |
| 208 | frameReady.Set(); |
| 209 | } |
| 210 | |
| 211 | private static void FrameLoop() |
| 212 | { |
| 213 | while (true) |
| 214 | { |
| 215 | frameReady.WaitOne(); |
| 216 | while (true) |
| 217 | { |
| 218 | string s; |
| 219 | lock (frameGate) |
| 220 | { |
| 221 | if (bulk.Count == 0) break; |
| 222 | s = bulk.Dequeue(); |
| 223 | } |
| 224 | Send(s); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | internal sealed class Flag { public bool Value; } |
| 231 | internal sealed class Num { public float Value; public Num(float v) { Value = v; } } |
| 232 | |
| 233 | internal sealed class LogShim |
| 234 | { |
| 235 | public void LogInfo(object o) { Debug.Log("[Ultra Paradise] " + o); } |
| 236 | public void LogWarning(object o) { Debug.LogWarning("[Ultra Paradise] " + o); } |
| 237 | public void LogError(object o) { Debug.LogError("[Ultra Paradise] " + o); } |
| 238 | } |
| 239 | |
| 240 | internal static class Feed |
| 241 | { |
| 242 | internal static void Mod(string text) { DebugFeed.Feed("MOD", null, text); } |
| 243 | internal static void Add(string tag, string color, string text) { DebugFeed.Feed(tag, color, text); } |
| 244 | } |
| 245 | |
| 246 | internal static partial class Plugin |
| 247 | { |
| 248 | internal static readonly LogShim Log = new LogShim(); |
| 249 | internal static float GameSpeed = 1f; |
| 250 | internal static float CameraZoom = 1f; |
| 251 | |
| 252 | internal static readonly Flag GodMode = new Flag(), NoFallDamage = new Flag(), InfiniteStamina = new Flag(), NoHunger = new Flag(), |
| 253 | NoThirst = new Flag(), NoTiredness = new Flag(), MoodLock = new Flag(), FreezeEnemies = new Flag(), OneHitKills = new Flag(), |
| 254 | MaxTraderRep = new Flag(), FreeShopping = new Flag(), InstantMine = new Flag(), UnlimitedAmmo = new Flag(), LongerEscape = new Flag(), |
| 255 | NoBleeding = new Flag(), NoPain = new Flag(), NoInfection = new Flag(), NoSickness = new Flag(), NoRadiation = new Flag(), |
| 256 | UnlimitedOxygen = new Flag(), StableTemperature = new Flag(), FasterHealing = new Flag(), MoreXp = new Flag(), |
| 257 | FasterMovement = new Flag(), FasterClimbing = new Flag(), HigherJumps = new Flag(), UnlimitedJumps = new Flag(), |
| 258 | UnlimitedWallJumps = new Flag(), LowGravity = new Flag(), FlyMode = new Flag(), Noclip = new Flag(), ClickTeleport = new Flag(), |
| 259 | FreeCrafting = new Flag(), NoCraftFail = new Flag(), AllRecipes = new Flag(), NoDegradation = new Flag(), UnlimitedCarry = new Flag(), |
| 260 | UnlimitedLiquids = new Flag(), UnlimitedBattery = new Flag(), MasterLockpick = new Flag(), NoGunJams = new Flag(), NoRecoil = new Flag(), MoreDamage = new Flag(), |
| 261 | FasterAttacks = new Flag(), Invisibility = new Flag(), DisableTraps = new Flag(), PauseRadiation = new Flag(), NoEarthquakes = new Flag(), |
| 262 | MoreLoot = new Flag(), FullBright = new Flag(), FreeCamera = new Flag(), InfiniteCredit = new Flag(), |
| 263 | FragileBody = new Flag(), FastMetabolism = new Flag(), HopelessStart = new Flag(), BrittleGear = new Flag(), StrongerEnemies = new Flag(), |
| 264 | FastRadiation = new Flag(), UnchippedChallenge = new Flag(), ColdCaves = new Flag(), MoreTraps = new Flag(), LootScarcity = new Flag(); |
| 265 | |
| 266 | internal static readonly Num MoveSpeedMult = new Num(1.6f), ClimbSpeedMult = new Num(2f), JumpMult = new Num(1.5f), GravityScale = new Num(0.4f), |
| 267 | FlySpeed = new Num(18f), HealingMult = new Num(3f), XpMult = new Num(3f), DamageMult = new Num(2f), AttackSpeedMult = new Num(2.5f), |
| 268 | LootMult = new Num(2f); |
| 269 | |
| 270 | private static Dictionary<string, Flag> flags; |
| 271 | private static Dictionary<string, Num> nums; |
| 272 | |
| 273 | private static void Index() |
| 274 | { |
| 275 | if (flags != null) return; |
| 276 | flags = new Dictionary<string, Flag>(); |
| 277 | nums = new Dictionary<string, Num>(); |
| 278 | foreach (FieldInfo f in typeof(Plugin).GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)) |
| 279 | { |
| 280 | if (f.FieldType == typeof(Flag)) flags[f.Name] = (Flag)f.GetValue(null); |
| 281 | else if (f.FieldType == typeof(Num)) nums[f.Name] = (Num)f.GetValue(null); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | internal static Flag FindFlag(string id) |
| 286 | { |
| 287 | Index(); |
| 288 | Flag f; |
| 289 | return flags.TryGetValue(id, out f) ? f : null; |
| 290 | } |
| 291 | |
| 292 | internal static Num FindNum(string id) |
| 293 | { |
| 294 | Index(); |
| 295 | Num n; |
| 296 | return nums.TryGetValue(id, out n) ? n : null; |
| 297 | } |
| 298 | |
| 299 | internal static void AllOff() |
| 300 | { |
| 301 | Index(); |
| 302 | foreach (Flag f in flags.Values) f.Value = false; |
| 303 | GameSpeed = 1f; |
| 304 | CameraZoom = 1f; |
| 305 | MoodTarget = float.NaN; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | internal static class Patcher |
| 310 | { |
| 311 | private static Harmony harmony; |
| 312 | |
| 313 | internal static int Apply() |
| 314 | { |
| 315 | harmony = new Harmony("ultraparadise.cutrainer"); |
| 316 | int fails = 0; |
| 317 | Type[] types; |
| 318 | try { types = typeof(Patcher).Assembly.GetTypes(); } |
| 319 | catch (ReflectionTypeLoadException e) { types = e.Types; } |
| 320 | foreach (Type t in types) |
| 321 | { |
| 322 | if (t == null || t.GetCustomAttributes(typeof(HarmonyPatch), false).Length == 0) continue; |
| 323 | try { harmony.CreateClassProcessor(t).Patch(); } |
| 324 | catch (Exception e) |
| 325 | { |
| 326 | fails++; |
| 327 | Plugin.Log.LogWarning("Couldn't patch " + t.Name + ": " + e.Message); |
| 328 | } |
| 329 | } |
| 330 | return fails; |
| 331 | } |
| 332 | |
| 333 | internal static void Remove() |
| 334 | { |
| 335 | try { if (harmony != null) harmony.UnpatchSelf(); } catch (Exception) { } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | public class Agent : MonoBehaviour |
| 340 | { |
| 341 | private bool blocked; |
| 342 | private string blockedWhy; |
| 343 | private int patchState = int.MinValue; |
| 344 | private float nextInfo, nextMp, nextSlow, nextTickError, nextWorldSync; |
| 345 | private bool lastFullBright, lastNoclip, lastFreecam, weSetUnchipped; |
| 346 | private float appliedSpeed = 1f; |
| 347 | private Camera zoomCam; |
| 348 | private float zoomBase; |
| 349 | private Behaviour zoomPpc; |
| 350 | private bool zoomPpcWasOn; |
| 351 | private TraderScript trader; |
| 352 | private float traderDist; |
| 353 | private const float TraderRange = 20f; |
| 354 | private const int CreditCap = 9999; |
| 355 | private static readonly FieldInfo BonusTemp = typeof(WorldGeneration).GetField("bonusTemperatureOffset", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); |
| 356 | |
| 357 | private void Awake() |
| 358 | { |
| 359 | blockedWhy = MultiplayerCheck(); |
| 360 | blocked = blockedWhy != null; |
| 361 | if (!blocked) StartPatches(); |
| 362 | Link.Start(); |
| 363 | Plugin.Log.LogInfo("Trainer attached." + (blocked ? " Cheats are off: " + blockedWhy : "")); |
| 364 | } |
| 365 | |
| 366 | private void StartPatches() |
| 367 | { |
| 368 | try { patchState = Patcher.Apply(); } |
| 369 | catch (Exception e) |
| 370 | { |
| 371 | patchState = -1; |
| 372 | Plugin.Log.LogError("Patch library failed to load: " + e); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | private static string MultiplayerCheck() |
| 377 | { |
| 378 | string[] words = { "krokmp", "casualtiestogether", "casualtiesmp", "levskittne" }; |
| 379 | foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) |
| 380 | { |
| 381 | string n; |
| 382 | try { n = a.GetName().Name.ToLowerInvariant().Replace(" ", "").Replace("-", "").Replace("_", "").Replace(".", ""); } catch (Exception) { continue; } |
| 383 | foreach (string w in words) |
| 384 | if (n.Contains(w)) return "a multiplayer mod is loaded (" + a.GetName().Name + ")"; |
| 385 | } |
| 386 | return null; |
| 387 | } |
| 388 | |
| 389 | private void Block(string why) |
| 390 | { |
| 391 | blocked = true; |
| 392 | blockedWhy = why; |
| 393 | Plugin.AllOff(); |
| 394 | Patcher.Remove(); |
| 395 | Link.Send("BLOCKED " + why); |
| 396 | } |
| 397 | |
| 398 | private static bool InGame() |
| 399 | { |
| 400 | return PlayerCamera.main != null && PlayerCamera.main.body != null && WorldGeneration.world != null; |
| 401 | } |
| 402 | |
| 403 | private static Body Player() |
| 404 | { |
| 405 | return InGame() ? PlayerCamera.main.body : null; |
| 406 | } |
| 407 | |
| 408 | private void Update() |
| 409 | { |
| 410 | string msg; |
| 411 | int guard = 0; |
| 412 | while (guard++ < 256 && Link.TryTake(out msg)) |
| 413 | { |
| 414 | try { Handle(msg); } |
| 415 | catch (Exception e) { Plugin.Log.LogWarning("Command failed: " + e.Message); } |
| 416 | } |
| 417 | |
| 418 | if (!blocked && Time.unscaledTime >= nextMp) |
| 419 | { |
| 420 | nextMp = Time.unscaledTime + 10f; |
| 421 | string why = MultiplayerCheck(); |
| 422 | if (why != null) Block(why); |
| 423 | } |
| 424 | |
| 425 | try { HealthMirror.Tick(); MoodleFeed.Pump(); BarkFeed.Tick(); } |
| 426 | catch (Exception e) |
| 427 | { |
| 428 | if (Time.unscaledTime >= nextTickError) |
| 429 | { |
| 430 | nextTickError = Time.unscaledTime + 10f; |
| 431 | Plugin.Log.LogWarning("Health readout failed: " + e); |
| 432 | } |
| 433 | } |
| 434 | try { DebugFeed.Tick(); } catch (Exception) { } |
| 435 | try { ModBridge.Tick(); } catch (Exception) { } |
| 436 | try { GameMusic.Tick(); } catch (Exception) { } |
| 437 | if (SteamManager.disableAchievements) SteamManager.disableAchievements = false; |
| 438 | if (blocked) return; |
| 439 | |
| 440 | try { Tick(); } |
| 441 | catch (Exception e) |
| 442 | { |
| 443 | if (Time.unscaledTime >= nextTickError) |
| 444 | { |
| 445 | nextTickError = Time.unscaledTime + 10f; |
| 446 | Plugin.Log.LogWarning("Cheat tick failed: " + e); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (Time.unscaledTime >= nextSlow) |
| 451 | { |
| 452 | nextSlow = Time.unscaledTime + 0.5f; |
| 453 | try { Slow(); } catch (Exception) { } |
| 454 | } |
| 455 | |
| 456 | if (Time.unscaledTime >= nextInfo) |
| 457 | { |
| 458 | nextInfo = Time.unscaledTime + 0.5f; |
| 459 | SendInfo(); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | private void Slow() |
| 464 | { |
| 465 | Durability.Sweep(); |
| 466 | if (SteamManager.disableAchievements) SteamManager.disableAchievements = false; |
| 467 | Plugin.TryResume(); |
| 468 | FindTrader(); |
| 469 | if (Plugin.InfiniteCredit.Value && trader != null && trader.valueGiven < CreditCap) SetCredit(CreditCap); |
| 470 | if (Plugin.MaxTraderRep.Value && trader != null && (trader.reputation < 200f || trader.hostility > 0f)) |
| 471 | { |
| 472 | trader.reputation = 200f; |
| 473 | trader.hostility = 0f; |
| 474 | RefreshTraderUI(); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | private void Tick() |
| 479 | { |
| 480 | bool inGame = InGame(); |
| 481 | ConsoleScript con = ConsoleScript.instance; |
| 482 | Body b = inGame ? PlayerCamera.main.body : null; |
| 483 | |
| 484 | if (con != null) |
| 485 | { |
| 486 | if (Plugin.FullBright.Value) con.fullBright = true; |
| 487 | else if (lastFullBright) con.fullBright = false; |
| 488 | } |
| 489 | lastFullBright = Plugin.FullBright.Value; |
| 490 | |
| 491 | if (con != null && b != null && b.rb != null) |
| 492 | { |
| 493 | if (Plugin.Noclip.Value && !con.noClip) |
| 494 | { |
| 495 | con.noClip = true; |
| 496 | con.noClipPos = b.transform.position; |
| 497 | b.rb.simulated = false; |
| 498 | } |
| 499 | else if (!Plugin.Noclip.Value && lastNoclip && con.noClip) |
| 500 | { |
| 501 | con.noClip = false; |
| 502 | b.rb.simulated = true; |
| 503 | b.rb.velocity = Vector2.zero; |
| 504 | } |
| 505 | } |
| 506 | lastNoclip = Plugin.Noclip.Value; |
| 507 | |
| 508 | if (inGame && con != null) |
| 509 | { |
| 510 | if (Plugin.FreeCamera.Value && !PlayerCamera.main.isFreecam) |
| 511 | { |
| 512 | PlayerCamera.main.isFreecam = true; |
| 513 | con.freecamPos = PlayerCamera.main.transform.position; |
| 514 | } |
| 515 | else if (!Plugin.FreeCamera.Value && lastFreecam && PlayerCamera.main.isFreecam) |
| 516 | { |
| 517 | PlayerCamera.main.isFreecam = false; |
| 518 | } |
| 519 | } |
| 520 | lastFreecam = Plugin.FreeCamera.Value; |
| 521 | |
| 522 | if (inGame && !PauseHandler.paused && Time.timeScale > 0f) |
| 523 | { |
| 524 | float want = Plugin.GameSpeed; |
| 525 | if (want != 1f) |
| 526 | { |
| 527 | if (Mathf.Approximately(Time.timeScale, 1f) || Mathf.Approximately(Time.timeScale, appliedSpeed)) Time.timeScale = want; |
| 528 | appliedSpeed = want; |
| 529 | } |
| 530 | else if (appliedSpeed != 1f) |
| 531 | { |
| 532 | if (Mathf.Approximately(Time.timeScale, appliedSpeed)) Time.timeScale = 1f; |
| 533 | appliedSpeed = 1f; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | Camera cam = Camera.main; |
| 538 | if (zoomCam != null && zoomCam != cam) zoomCam = null; |
| 539 | if (inGame && cam != null && Plugin.CameraZoom != 1f) |
| 540 | { |
| 541 | if (zoomCam == null) |
| 542 | { |
| 543 | zoomCam = cam; |
| 544 | zoomPpc = cam.GetComponent("PixelPerfectCamera") as Behaviour; |
| 545 | zoomPpcWasOn = zoomPpc != null && zoomPpc.enabled; |
| 546 | if (zoomPpc != null) zoomPpc.enabled = false; |
| 547 | zoomBase = cam.orthographicSize; |
| 548 | } |
| 549 | cam.orthographicSize = zoomBase * Plugin.CameraZoom; |
| 550 | } |
| 551 | else if (zoomCam != null) |
| 552 | { |
| 553 | zoomCam.orthographicSize = zoomBase; |
| 554 | if (zoomPpc != null) zoomPpc.enabled = zoomPpcWasOn; |
| 555 | zoomCam = null; |
| 556 | } |
| 557 | |
| 558 | if (b != null && Plugin.ClickTeleport.Value && Input.GetMouseButtonDown(2)) Actions.TeleportToCursor(); |
| 559 | |
| 560 | WorldGeneration w = WorldGeneration.world; |
| 561 | if (b != null && w != null && w.worldExists && Time.unscaledTime >= nextWorldSync) |
| 562 | { |
| 563 | nextWorldSync = Time.unscaledTime + 0.5f; |
| 564 | if (BonusTemp != null) BonusTemp.SetValue(w, WorldGeneration.GetRunSettingFloat("temperatureoffset")); |
| 565 | WorldGeneration.globalDecayRate = WorldGeneration.GetRunSettingFloat("itemdecayrate"); |
| 566 | w.maxTimePerLayer = WorldGeneration.GetRunSettingFloat("timelimit") * 60f; |
| 567 | if (Plugin.UnchippedChallenge.Value && !w.unchippedMode) |
| 568 | { |
| 569 | w.SetUnchipped(true); |
| 570 | weSetUnchipped = true; |
| 571 | } |
| 572 | else if (!Plugin.UnchippedChallenge.Value && weSetUnchipped) |
| 573 | { |
| 574 | weSetUnchipped = false; |
| 575 | if (w.unchippedMode && !WorldGeneration.GetRunSettingBool("unchipped")) w.SetUnchipped(false); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | private void Handle(string line) |
| 581 | { |
| 582 | if (line == "CONNECTED") |
| 583 | { |
| 584 | if (blocked) Link.Send("BLOCKED " + blockedWhy); |
| 585 | else Link.Send("READY " + patchState); |
| 586 | Link.Send("LOADER " + (Environment.GetEnvironmentVariable("ULTRAPARA_VERSION") ?? "none")); |
| 587 | SendInfo(); |
| 588 | MoodleFeed.Reset(); |
| 589 | BarkFeed.Reset(); |
| 590 | #if LINUXPLUGIN |
| 591 | LinuxHost.OnConnected(); |
| 592 | #endif |
| 593 | return; |
| 594 | } |
| 595 | if (line == "DISCONNECTED") |
| 596 | { |
| 597 | #if !LINUXPLUGIN |
| 598 | Plugin.AllOff(); |
| 599 | #endif |
| 600 | HealthMirror.SetMode(0); |
| 601 | DebugFeed.SetOn(false); |
| 602 | ModBridge.ReleaseAll(); |
| 603 | GameMusic.Reset(); |
| 604 | return; |
| 605 | } |
| 606 | string[] p = line.Split(new char[] { ' ' }, 3); |
| 607 | string cmd = p[0]; |
| 608 | if (cmd == "PING") { Link.Send("PONG"); return; } |
| 609 | #if LINUXPLUGIN |
| 610 | if (LinuxHost.Handle(cmd, p)) return; |
| 611 | #endif |
| 612 | if (cmd == "HEALTH" && p.Length >= 2) |
| 613 | { |
| 614 | int m; |
| 615 | if (int.TryParse(p[1], out m)) HealthMirror.SetMode(m); |
| 616 | return; |
| 617 | } |
| 618 | if (ModBridge.Handle(cmd, p)) return; |
| 619 | if (GameMusic.Handle(cmd, p)) return; |
| 620 | if (cmd == "DEBUG" && p.Length >= 2) |
| 621 | { |
| 622 | DebugFeed.SetOn(p[1] == "1"); |
| 623 | return; |
| 624 | } |
| 625 | if (DebugFeed.On && (cmd == "SET" || cmd == "VAL" || cmd == "ACT" || cmd == "SPAWN")) DebugFeed.Add("COMMAND", line); |
| 626 | if (cmd == "GETSFX" && p.Length >= 2) |
| 627 | { |
| 628 | BarkFeed.Request(p[1]); |
| 629 | return; |
| 630 | } |
| 631 | if (blocked) |
| 632 | { |
| 633 | if (cmd == "ACT" || cmd == "SPAWN") Link.Send("MSG Cheats are off: " + blockedWhy); |
| 634 | return; |
| 635 | } |
| 636 | if (cmd == "SET" && p.Length >= 3) |
| 637 | { |
| 638 | Flag f = Plugin.FindFlag(p[1]); |
| 639 | if (f == null) return; |
| 640 | bool v = p[2] == "1"; |
| 641 | bool was = f.Value; |
| 642 | f.Value = v; |
| 643 | if (f == Plugin.MoodLock && v && !was) |
| 644 | { |
| 645 | Body b = Player(); |
| 646 | if (b != null) { Plugin.MoodBody = b; Plugin.MoodTarget = b.happiness; } |
| 647 | } |
| 648 | if (f == Plugin.FreeShopping) RefreshTraderUI(); |
| 649 | } |
| 650 | else if (cmd == "VAL" && p.Length >= 3) |
| 651 | { |
| 652 | float v; |
| 653 | if (!float.TryParse(p[2], NumberStyles.Float, CultureInfo.InvariantCulture, out v) || float.IsNaN(v) || float.IsInfinity(v)) return; |
| 654 | if (p[1] == "GameSpeed") Plugin.GameSpeed = Mathf.Clamp(v, 0.1f, 5f); |
| 655 | else if (p[1] == "CameraZoom") Plugin.CameraZoom = Mathf.Clamp(v, 0.25f, 5f); |
| 656 | else |
| 657 | { |
| 658 | Num n = Plugin.FindNum(p[1]); |
| 659 | if (n != null) n.Value = Mathf.Clamp(v, 0.01f, 100f); |
| 660 | } |
| 661 | } |
| 662 | else if (cmd == "ACT" && p.Length >= 2) |
| 663 | { |
| 664 | string r; |
| 665 | try { r = Act(p[1], p.Length >= 3 ? p[2] : ""); } |
| 666 | catch (Exception e) { r = "That didn't work (" + e.GetType().Name + ")"; } |
| 667 | if (!string.IsNullOrEmpty(r)) Link.Send("MSG " + r); |
| 668 | } |
| 669 | else if (cmd == "SPAWN" && p.Length >= 3) |
| 670 | { |
| 671 | int n; |
| 672 | if (!int.TryParse(p[2], out n)) n = 1; |
| 673 | n = Mathf.Clamp(n, 1, 50); |
| 674 | string r; |
| 675 | try { r = Actions.SpawnItem(p[1], n); } |
| 676 | catch (Exception e) { r = "Couldn't spawn that (" + e.GetType().Name + ")"; } |
| 677 | Link.Send("MSG " + r); |
| 678 | } |
| 679 | else if (cmd == "ITEMS") SendItems(); |
| 680 | } |
| 681 | |
| 682 | private void SendItems() |
| 683 | { |
| 684 | if (Item.GlobalItems == null || Item.GlobalItems.Count == 0) |
| 685 | { |
| 686 | Link.Send("ITEMSEND 0"); |
| 687 | return; |
| 688 | } |
| 689 | int n = 0; |
| 690 | foreach (string key in new List<string>(Item.GlobalItems.Keys)) |
| 691 | { |
| 692 | if (string.IsNullOrEmpty(key)) continue; |
| 693 | Link.Send("ITEM " + key + "\t" + Actions.ItemName(key).Replace('\t', ' ')); |
| 694 | n++; |
| 695 | } |
| 696 | Link.Send("ITEMSEND " + n); |
| 697 | } |
| 698 | |
| 699 | private static string Clean(string s) |
| 700 | { |
| 701 | return s == null ? "" : s.Replace('|', '/').Replace('=', '-'); |
| 702 | } |
| 703 | |
| 704 | private void SendInfo() |
| 705 | { |
| 706 | #if !LINUXPLUGIN |
| 707 | if (!Link.Connected) return; |
| 708 | #endif |
| 709 | StringBuilder sb = new StringBuilder("INFO "); |
| 710 | Body b = Player(); |
| 711 | WorldGeneration w = WorldGeneration.world; |
| 712 | if (b == null || w == null) sb.Append("where=main menu"); |
| 713 | else |
| 714 | { |
| 715 | if (w.biomeOverride != WorldGeneration.OverrideSceneType.None) sb.Append("where=special area"); |
| 716 | else sb.Append("where=layer ").Append(w.biomeDepth + 1).Append(" of ").Append(w.amountOfLayers); |
| 717 | sb.Append("|alive=").Append(b.alive ? 1 : 0); |
| 718 | sb.Append("|mood=").Append(b.totalHappiness.ToString("0", CultureInfo.InvariantCulture)); |
| 719 | sb.Append("|px=").Append(b.transform.position.x.ToString("0.0", CultureInfo.InvariantCulture)).Append("|py=").Append(b.transform.position.y.ToString("0.0", CultureInfo.InvariantCulture)); |
| 720 | sb.Append("|depth=").Append(w.biomeOverride != WorldGeneration.OverrideSceneType.None ? -1 : w.biomeDepth); |
| 721 | if (b.skills != null) sb.Append("|str=").Append(b.skills.STR).Append("|res=").Append(b.skills.RES).Append("|int=").Append(b.skills.INT); |
| 722 | if (Plugin.GodMode.Value && Plugin.Paused(b)) sb.Append("|paused=").Append(Clean(Plugin.PauseReason)); |
| 723 | if (trader != null) |
| 724 | { |
| 725 | sb.Append("|trader=").Append(Clean(trader.gameObject.name.Replace("(Clone)", "").Trim())); |
| 726 | sb.Append("|credit=").Append(trader.valueGiven); |
| 727 | sb.Append("|rep=").Append((int)trader.reputation); |
| 728 | sb.Append("|tdist=").Append(traderDist.ToString("0", CultureInfo.InvariantCulture)); |
| 729 | if (trader.hostile) sb.Append("|hostile=1"); |
| 730 | } |
| 731 | } |
| 732 | sb.Append("|savecheat=").Append(SaveFlag()).Append("|achoff=").Append(SteamManager.disableAchievements ? 1 : 0); |
| 733 | Link.Send(sb.ToString()); |
| 734 | } |
| 735 | |
| 736 | private static DateTime saveStamp = DateTime.MinValue; |
| 737 | private static string saveFlag = "none"; |
| 738 | private static float nextSaveCheck; |
| 739 | |
| 740 | private static string SaveFlag() |
| 741 | { |
| 742 | if (Time.unscaledTime < nextSaveCheck) return saveFlag; |
| 743 | nextSaveCheck = Time.unscaledTime + 2f; |
| 744 | try |
| 745 | { |
| 746 | string p = Path.Combine(Application.persistentDataPath, "save.sv"); |
| 747 | if (!File.Exists(p)) { saveStamp = DateTime.MinValue; saveFlag = "none"; return saveFlag; } |
| 748 | DateTime t = File.GetLastWriteTimeUtc(p); |
| 749 | if (t == saveStamp) return saveFlag; |
| 750 | string json = SaveSystem.Unzip(File.ReadAllBytes(p)); |
| 751 | saveStamp = t; |
| 752 | saveFlag = json.Contains("\"cheatedSave\":true") ? "cheated" : json.Contains("\"cheatedSave\":false") ? "clean" : "unknown"; |
| 753 | } |
| 754 | catch (Exception) { saveFlag = "error"; } |
| 755 | return saveFlag; |
| 756 | } |
| 757 | |
| 758 | private string Act(string id, string arg) |
| 759 | { |
| 760 | Body mb = Player(); |
| 761 | switch (id) |
| 762 | { |
| 763 | case "heal": return Actions.HealWounds(); |
| 764 | case "limbs": return Actions.ReattachLimbs(); |
| 765 | case "em_full": |
| 766 | case "em_bleed": |
| 767 | case "em_breathe": |
| 768 | case "em_heart": |
| 769 | case "em_cure": |
| 770 | case "em_body": |
| 771 | return Actions.Emergency(id); |
| 772 | case "tpgo": |
| 773 | { |
| 774 | if (mb == null) return "Not in a run"; |
| 775 | WorldGeneration tw = WorldGeneration.world; |
| 776 | string[] a = arg.Split(' '); |
| 777 | float tx, ty; int td; |
| 778 | if (a.Length != 3 || !float.TryParse(a[0], NumberStyles.Float, CultureInfo.InvariantCulture, out tx) || !float.TryParse(a[1], NumberStyles.Float, CultureInfo.InvariantCulture, out ty) || !int.TryParse(a[2], out td)) return null; |
| 779 | if (float.IsNaN(tx) || float.IsNaN(ty) || float.IsInfinity(tx) || float.IsInfinity(ty)) return null; |
| 780 | if (tw == null || tw.generatingWorld || !tw.worldExists) return "Wait for the layer to finish loading"; |
| 781 | int cur = tw.biomeOverride != WorldGeneration.OverrideSceneType.None ? -1 : tw.biomeDepth; |
| 782 | if (cur != td) return "That bookmark is on another layer"; |
| 783 | Actions.Teleport(mb, new Vector2(tx, ty)); |
| 784 | return "Teleported to the bookmark"; |
| 785 | } |
| 786 | case "killall": return Actions.KillAllEnemies(); |
| 787 | case "spiders": return Actions.SpiderAmbush(); |
| 788 | case "exit": return Actions.TeleportToExit(); |
| 789 | case "back": return Actions.PreviousLayer(); |
| 790 | case "skip": return Actions.SkipLayer(); |
| 791 | case "quake": return Actions.StartEarthquake(); |
| 792 | case "downpour": return Actions.FungalDownpour(); |
| 793 | case "unlock": return Actions.UnlockCharacters(); |
| 794 | case "starterkit": return Actions.StarterKit(); |
| 795 | case "randomloadout": return Actions.RandomLoadout(); |
| 796 | case "moodmax": |
| 797 | if (mb == null) return "Not in a run"; |
| 798 | Plugin.SetMood(mb, Plugin.MoodMax(mb)); |
| 799 | return "Mood set to max"; |
| 800 | case "mood": |
| 801 | { |
| 802 | if (mb == null) return "Not in a run"; |
| 803 | int d; |
| 804 | if (!int.TryParse(arg, out d)) return null; |
| 805 | Plugin.SetMood(mb, mb.happiness + d); |
| 806 | return "Mood " + (d > 0 ? "+" : "") + d; |
| 807 | } |
| 808 | case "skill": |
| 809 | { |
| 810 | string[] a = arg.Split(' '); |
| 811 | int stat, d; |
| 812 | if (a.Length != 2 || !int.TryParse(a[0], out stat) || !int.TryParse(a[1], out d)) return null; |
| 813 | return Actions.AddSkill(Mathf.Clamp(stat, 0, 2), d); |
| 814 | } |
| 815 | case "credit": |
| 816 | { |
| 817 | if (mb == null) return "Not in a run"; |
| 818 | if (trader == null) return "Walk up to a survivor first"; |
| 819 | if (arg == "zero") |
| 820 | { |
| 821 | Plugin.InfiniteCredit.Value = false; |
| 822 | SetCredit(0); |
| 823 | return "Trade credit set to 0"; |
| 824 | } |
| 825 | int d; |
| 826 | if (!int.TryParse(arg, out d)) return null; |
| 827 | SetCredit(trader.valueGiven + d); |
| 828 | return "Trade credit " + trader.valueGiven; |
| 829 | } |
| 830 | case "restock": |
| 831 | if (mb == null) return "Not in a run"; |
| 832 | if (trader == null) return "Walk up to a survivor first"; |
| 833 | trader.GenerateInventory(); |
| 834 | RefreshTraderUI(); |
| 835 | return "Restocked (" + trader.items.Count + " items)"; |
| 836 | } |
| 837 | return null; |
| 838 | } |
| 839 | |
| 840 | private void FindTrader() |
| 841 | { |
| 842 | trader = null; |
| 843 | traderDist = 0f; |
| 844 | Body b = Player(); |
| 845 | if (b == null) return; |
| 846 | Vector2 p = b.transform.position; |
| 847 | float best = TraderRange; |
| 848 | TraderScript[] all = Object.FindObjectsOfType<TraderScript>(); |
| 849 | for (int i = 0; i < all.Length; i++) |
| 850 | { |
| 851 | float d = Vector2.Distance(p, all[i].transform.position); |
| 852 | if (d <= best) |
| 853 | { |
| 854 | best = d; |
| 855 | trader = all[i]; |
| 856 | } |
| 857 | } |
| 858 | if (trader != null) traderDist = best; |
| 859 | } |
| 860 | |
| 861 | private void RefreshTraderUI() |
| 862 | { |
| 863 | PlayerCamera cam = PlayerCamera.main; |
| 864 | if (cam == null || trader == null) return; |
| 865 | TraderScript prev = cam.currentTrader; |
| 866 | cam.currentTrader = trader; |
| 867 | try { cam.UpdateTradeTexts(); cam.RefreshTraderInventories(); } |
| 868 | catch (Exception) { } |
| 869 | cam.currentTrader = prev; |
| 870 | } |
| 871 | |
| 872 | private void SetCredit(int amount) |
| 873 | { |
| 874 | if (trader == null) return; |
| 875 | trader.valueGiven = Math.Max(0, amount); |
| 876 | RefreshTraderUI(); |
| 877 | } |
| 878 | } |
| 879 | } |