import re

SRC = open('/tmp/opencode/make_runner.py').read()
shim = re.search(r"shim = r'''(.*?)'''", SRC, re.S).group(1)

extra = r'''
function __V(x)
  local t = type(x)
  if t == "string" then return string.format("%q", x:sub(1, 30)) end
  if t == "number" then return string.format("%.10g", x) end
  if t == "table" then return "tbl#" .. tostring(#x) end
  return t
end
local function __g(t, k)
  local ok, v = pcall(function() return t[k] end)
  if ok then return v end
  return nil
end
__NINS = {}
local function __nins(O)
  local n = __NINS[O]
  if n then return n end
  local o3 = __g(O, 3)
  if type(o3) == "table" then n = #o3 else n = -1 end
  __NINS[O] = n
  return n
end
__MAKE140 = function(O, m)
  if type(O) == "table" and type(O[3]) == "table" and #O[3] == 140 then
    local mm = type(m) == "table" and #m or -1
    local parts = {}
    if type(m) == "table" then
      local n = 0
      for k, v in pairs(m) do n = n + 1; if n <= 12 then parts[#parts+1] = tostring(k) .. "=" .. __V(v) end end
      mm = n
    end
    __realprint("MAKE140", tostring(O), "m", type(m), mm, table.concat(parts, " "), "from", tostring(__LASTO), "pc", __LASTPC)
  end
end
__FORCED = false
__OPN = 0
function __OP(O, pc, z, a, b, c, d, e, f, regs)
  __OPN = __OPN + 1
  local ni = __nins(O)
  if ni == 140 and pc == 8 and not __FORCED then
    __FORCED = true
    __realprint("FORCE", tostring(O), "r2", __V(regs[2]), "r8", __V(regs[8]), "r9", __V(regs[9]))
    regs[9] = (tonumber(regs[2]) or 0) - 1
  end
end
__I = 0
function __G3(k, v)
  __I = __I + 1
  if __I <= 300 then
    local vt = type(v)
    local vs = vt == "string" and string.format("%q", v:sub(1, 50)) or tostring(v)
    __realprint("G3", tostring(k), vt, vs)
  end
end
__PLOGN = 0
function __PLOG(name, k)
  if __PLOGN < 400 then
    __PLOGN = __PLOGN + 1
    __realprint("PROXY", tostring(name), tostring(k))
  end
end
'''

old = '''    __index = function(self, k)
      if k == "ClassName" then return "Proxy" end'''
new = '''    __index = function(self, k)
      if __PLOG then __PLOG(name, k) end
      if k == "ClassName" then return "Proxy" end'''
shim = shim.replace(old, new, 1)
shim += extra

raw = open('/tmp/opencode/stage2.lua').read()
rhex = re.search(r'__RHEX__\t([0-9a-f]+)', open('/tmp/opencode/cap4.txt').read()).group(1)
rbytes = bytes.fromhex(rhex)
rlit = '"' + ''.join('\\x%02x' % b for b in rbytes) + '"'

s = raw
s = 'local R=buffer.fromstring(' + rlit + ');' + s[len('local R=...;'):]
old_factory='(l)[55]=function(O,m,A)'
assert s.count(old_factory)==1
s=s.replace(old_factory, '(l)[55]=function(O,m,A)__MAKE140(O,m);', 1)
get = '(q)[d[Q]]=S[p[Q]];'
assert s.count(get) == 1
s = s.replace(get, '  local __sv=S[p[Q]];__G3(p[Q],__sv);(q)[d[Q]]=__sv;', 1)
head = 'while true do local Z=o[Q];'
assert s.count(head) == 1
s = s.replace(head, 'while true do local Z=o[Q];__OP(O,Q,Z,t[Q],d[Q],P[Q],y[Q],b[Q],p[Q],q);', 1)

tail = r'''
local __ok,__err = pcall(function()
''' + s + r'''
end)
__realprint("EXEC1", __ok, type(__err))
if __ok then
  local __ok2,__err2 = pcall(__err)
  __realprint("EXEC2", __ok2, tostring(__err2))
end
'''
open('/tmp/opencode/probe13.luau','w').write(shim + tail)
print("wrote")
