<?xml version="1.0" encoding="utf-8"?>
<CheatTable CheatEngineTableVersion="45">
  <CheatEntries>
    <CheatEntry>
      <ID>700</ID>
      <Description>"Cell to Singularity — Console de débogage v7 (Cheat Engine 7.5)"</Description>
      <GroupHeader>1</GroupHeader>
      <CheatEntries>
        <CheatEntry>
          <ID>701</ID>
          <Description>"ACTIVER ET OUVRIR LA CONSOLE — laisser coché"</Description>
          <Color>008000</Color>
          <VariableType>Auto Assembler Script</VariableType>
          <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end

if process ~= 'CellToSingularity.exe' or not readInteger(process) then
  error('Attache d abord Cheat Engine a CellToSingularity.exe')
end

if not monopipe then
  local result = LaunchMonoDataCollector()
  if not result or result == 0 then
    error('Impossible d activer les fonctions Mono')
  end
end

local function resolveMethod(className, methodName)
  local method = mono_findMethod('', className, methodName)
  if not method or method == 0 then
    error(className .. ':' .. methodName .. ' introuvable')
  end

  local address = mono_compile_method(method)
  if not address or address == 0 then
    error('Impossible de compiler ' .. className .. ':' .. methodName)
  end

  return method, address
end

local function readByteList(address, count)
  local bytes = {}
  for i = 0, count - 1 do
    bytes[#bytes + 1] = readBytes(address + i, 1, false)
  end
  return bytes
end

local function restoreByteList(address, bytes)
  if not address or not bytes then return end
  for i = 1, #bytes do
    writeBytes(address + i - 1, bytes[i])
  end
end

local checkMethod, checkAddress = resolveMethod('DebugController', 'CheckDebugMode')
local tryMethod, tryAddress = resolveMethod('DebugController', 'TryDisableAll')
local turnOffMethod, turnOffAddress = resolveMethod('DebugController', 'TurnOffDebug')
local showMethod, showAddress = resolveMethod('DebugController', 'ShowDebugUI')

_G.CTS_DebugConsoleV7 = {
  patches = {
    { address = checkAddress, original = readByteList(checkAddress, 3) },
    { address = tryAddress, original = readByteList(tryAddress, 1) },
    { address = turnOffAddress, original = readByteList(turnOffAddress, 1) }
  }
}

-- CheckDebugMode => true
writeBytes(checkAddress, 0xB0, 0x01, 0xC3)

-- Empêche le jeu de masquer ou fermer l interface pendant le test.
writeBytes(tryAddress, 0xC3)
writeBytes(turnOffAddress, 0xC3)

local invoked = false
local invokeError = nil

-- Appel Mono propre de la méthode statique ShowDebugUI.
if mono_invoke_method then
  local ok, result = pcall(function()
    local domains = mono_enumDomains and mono_enumDomains() or nil
    local domain = nil
    if type(domains) == 'table' and #domains &gt; 0 then
      domain = domains[1]
    end
    return mono_invoke_method(domain, showMethod, nil, {})
  end)

  if ok then
    invoked = true
  else
    invokeError = tostring(result)
  end
end

-- Repli pour Cheat Engine 7.5 si mono_invoke_method n est pas disponible.
if not invoked then
  local ok, threadOrError = pcall(function()
    return createThread(showAddress)
  end)
  if ok and threadOrError then
    invoked = true
  else
    invokeError = tostring(threadOrError or invokeError)
  end
end

if not invoked then
  error('Les correctifs sont appliques, mais ShowDebugUI n a pas pu etre appelee : ' .. tostring(invokeError))
end

beep()
showMessage(
  'Commande envoyee.\n\n'
  .. 'Laisse cette ligne cochee.\n'
  .. 'Ferme puis rouvre le menu lateral du jeu si la console ne se montre pas immediatement.\n\n'
  .. 'Pour restaurer le comportement normal, decoche simplement cette ligne.'
)
{$asm}

[DISABLE]
{$lua}
if syntaxcheck then return end

local state = _G.CTS_DebugConsoleV7
if state and state.patches then
  for i = #state.patches, 1, -1 do
    restoreByteList(state.patches[i].address, state.patches[i].original)
  end
end

_G.CTS_DebugConsoleV7 = nil
beep()
{$asm}
</AssemblerScript>
        </CheatEntry>
      </CheatEntries>
    </CheatEntry>
  </CheatEntries>
  <UserdefinedSymbols/>
  <Comments>Version 7 — Cheat Engine 7.5.

Une seule ligne a utiliser :
ACTIVER ET OUVRIR LA CONSOLE — laisser coché

Cette version :
- n ouvre aucun dossier Windows ;
- force CheckDebugMode a vrai ;
- bloque temporairement TryDisableAll et TurnOffDebug ;
- appelle directement DebugController:ShowDebugUI ;
- restaure les octets originaux quand la ligne est décochee.

Ne pas utiliser simultanement une ancienne version de la table.</Comments>
</CheatTable>
