using System; using System.Collections.Generic; using System.Linq; using Core.Runtime; using TMPro; using UnityEngine; using UnityEngine.SceneManagement; namespace Cheat.Runtime { public class Cheat : BaseMonobehaviour { #region Publics // #endregion #region Unity API void OnValidate() { # if UNITY_EDITOR if (!Application.isPlaying) { GetComponentInChildren().enabled = false; } # endif } void Awake() { // Liste des actions possibles _commands.Add("quit", args => Application.Quit()); _commands.Add("add money", args => { if (args.Length == 0) { Warning("Usage: add money "); return; } if (!int.TryParse(args[0], out int amount)) { Warning($"Invalid amount: {args[0]}"); return; } AddMoney(amount); }); _commands.Add("help", args => CommandsList()); _canvas = GetComponentInChildren(); _canvas.enabled = false; _historyZone = GetComponentInChildren(); } void Start() { // } void Update() { if (Input.GetKeyDown(KeyCode.Tab)) { ToggleConsole(); } if (_consoleOpen) { foreach (char c in Input.inputString) { if (c == '\b') // Backspace { if (_input.Length > 0) _input = _input.Substring(0, _input.Length - 1); } else if (c == '\n' || c == '\r') // Entrée { ProcessCommand(_input); _input = string.Empty; } else { _input += c; } RefreshUI(); } } } #endregion #region Main Methods // #endregion #region Utils /* Fonctions privées utiles */ void RefreshUI() { if (_historyZone == null) return; _historyZone.text = string.Join("\n", _history.ToArray()); } void CommandsList() { AppendHistory("Rappel des commandes "); foreach (var cmd in _commands) { AppendHistory(cmd.Key); } } // Ajoute une ligne d'historique et limite à _historyMaxLines void AppendHistory(string line) { if (string.IsNullOrEmpty(line)) return; _history.Enqueue(line); while (_history.Count > _historyMaxLines) _history.Dequeue(); } void ProcessCommand(string input) { if (string.IsNullOrWhiteSpace(input)) return; string raw = input.Trim(); string lower = raw.ToLower(); // Find the longest matching command key at the start of the input string matchedKey = _commands.Keys .OrderByDescending(k => k.Length) .FirstOrDefault(k => lower.StartsWith(k)); if (!string.IsNullOrEmpty(matchedKey)) { string argString = lower.Substring(matchedKey.Length).Trim(); string[] args = string.IsNullOrEmpty(argString) ? Array.Empty() : argString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); AppendHistory(raw); try { _commands[matchedKey].Invoke(args); } catch (Exception e) { Warning($"Error executing '{matchedKey}': {e.Message}"); } } else { Warning($"🚨‼️ Command '{lower}' not found"); } _input = string.Empty; } void ToggleConsole() { _consoleOpen = !_consoleOpen; _canvas.enabled = _consoleOpen; GameManager.Instance.IsOnPause = _consoleOpen; if (_consoleOpen) { TextCaptured(); } } void TextCaptured() { foreach (char c in Input.inputString) { _input += c; } } #endregion #region Commands Console void AddMoney(int amount) { // Update or create a Money fact int current; bool hasMoney = true; try { current = GetFact("Money"); } catch { current = 0; hasMoney = false; } int newValue = current + amount; SetFact("Money", newValue, FactPersistence.Persistent); AppendHistory($"Money set to {newValue}"); } #endregion #region Privates and Protected // Variables privées bool _consoleOpen = false; Canvas _canvas; string _input = string.Empty; Dictionary> _commands = new Dictionary>(); Queue _history = new Queue(); const int _historyMaxLines = 5; TMP_Text _historyZone; #endregion } }