local function L(L,P,Y)local B=ModTextFileGetContent(L)if not B then error(table.concat({"\073\078\074\069\067\084\073\079\078 \040\082\069\080\076\065\067\069\041 \070\065\073\076\069\068\058 \078\079 \070\073\076\069\047\110\070\105\108\101\058 ",L}))return end local s=dofile_once("\109\111\100\115\047\110\111\105\116\097-\115\116\114\101\097\107-\101\120\112\108\111\114\101\114\047\108\105\098\115\047\109\100\053\047\109\100\053\046\108\117\097")local y=s.sumhexa(B)local E=y==P if not E then error(table.concat({"\073\078\074\069\067\084\073\079\078 \040\082\069\080\076\065\067\069\041 \070\065\073\076\069\068\058 \073\078\086\065\076\073\068 \072\065\083\072\047\110\070\105\108\101\058 ";L}))return end for P,Y in pairs(Y)do local s,y=B:find(Y.from,0,true)if not s then error(table.concat({"\073\078\074\069\067\084\073\079\078 \040\082\069\080\076\065\067\069\041 \070\065\073\076\069\068\058 \078\079 \072\079\079\075\047\110\070\105\108\101\058 ",L;"\047\110\070\114\111\109\058 ",Y.from;"\047\110\084\111\058 ",Y.to}))return end local E=B:sub(1,s-1)local a=B:sub(y+1)local f=E..(Y.to..a)if B==f then error(table.concat({"\073\078\074\069\067\084\073\079\078 \040\082\069\080\076\065\067\069\041 \070\065\073\076\069\068\058 \078\079 \067\072\065\078\071\069\047\110\070\105\108\101\058 ",L;"\047\110\070\114\111\109\058 ",Y.from,"\047\110\084\111\058 ",Y.to}))return end B=f end ModTextFileSetContent(L,B)end return{replace_if_valid=L}
local function replace_if_valid(file, expectedHash, fromToTable)
  local content = ModTextFileGetContent(file)
  if not content then
    error(table.concat({ "INJECTION (REPLACE) FAILED: NO FILE/nFile: ", file }))
    return
  end

  local md5 = dofile_once("mods/noita-streak-explorer/libs/md5/md5.lua")
  local result = md5.sumhexa(content)
  local valid = result == expectedHash
  if not valid then
    error(table.concat({ "INJECTION (REPLACE) FAILED: INVALID HASH/nFile: ", file }))
    return
  end

  for _, pattern in pairs(fromToTable) do
    local first, last = content:find(pattern.from, 0, true)
    if not first then
      error(table.concat({ "INJECTION (REPLACE) FAILED: NO HOOK/nFile: ", file, "/nFrom: ", pattern.from, "/nTo: ", pattern.to }))
      return
    end

    local before = content:sub(1, first - 1)
    local after = content:sub(last + 1)
    local new = before .. pattern.to .. after
    if content == new then
      error(table.concat({ "INJECTION (REPLACE) FAILED: NO CHANGE/nFile: ", file, "/nFrom: ", pattern.from, "/nTo: ", pattern.to }))
      return
    end
    content = new
  end
  ModTextFileSetContent(file, content)
end

return {
  replace_if_valid = replace_if_valid,
}
