-- Archangel Decompiler v1.0
-- Dumps all decompilable scripts to file

local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

-- Output path (adjust for your executor)
local outputPath = "decompiled_" .. game.PlaceId .. "_" .. os.time() .. ".txt"
local outputBuffer = {}

local function log(msg)
    table.insert(outputBuffer, msg)
    print(msg)
end

log("-- Decompiled: " .. game:GetService("MarketplaceService"):GetProductInfo(game.PlaceId).Name)
log("-- PlaceId: " .. game.PlaceId)
log("-- Time: " .. os.date("%Y-%m-%d %H:%M:%S"))
log("-- Executor: " .. (identifyexecutor and identifyexecutor() or "Unknown"))
log("")

-- Check if decompile function exists
if not decompile then
    error("Your executor doesn't support decompile() - try Krnl, Fluxus, or Script-Ware")
end

local stats = { total = 0, success = 0, failed = 0, skipped = 0 }

local function decompileScript(scriptObj)
    stats.total += 1
    
    -- Skip certain script types that break decompilers
    if scriptObj:IsA("CoreScript") or scriptObj:IsA("CoreGui") then
        stats.skipped += 1
        return
    end
    
    local success, source = pcall(function()
        return decompile(scriptObj)
    end)
    
    if success and source and source ~= "" then
        stats.success += 1
        log("--- SCRIPT: " .. scriptObj:GetFullName() .. " ---")
        log(source)
        log("--- END " .. scriptObj:GetFullName() .. " ---\n")
    else
        stats.failed += 1
        log("-- FAILED: " .. scriptObj:GetFullName() .. " | Error: " .. tostring(source))
    end
end

-- Grab all ModuleScripts and LocalScripts
log("-- SCANNING FOR SCRIPTS --")

local function scan(parent, depth)
    depth = depth or 0
    if depth > 10 then return end -- Safety limit
    
    for _, child in pairs(parent:GetChildren()) do
        if child:IsA("ModuleScript") or child:IsA("LocalScript") then
            decompileScript(child)
        end
        -- Recurse but be careful with large games
        if #child:GetChildren() > 0 and depth < 5 then
            scan(child, depth + 1)
        end
    end
end

-- Scan key locations
local locations = {
    game:GetService("ReplicatedStorage"),
    game:GetService("Players"),
    game:GetService("Workspace"),
    game:GetService("StarterGui"),
    game:GetService("StarterPack"),
    game:GetService("StarterPlayer"),
    game.Workspace
}

for _, location in pairs(locations) do
    log("-- Scanning: " .. location:GetFullName())
    scan(location)
end

-- Also grab PlayerScripts and PlayerGui
if LocalPlayer then
    log("-- Scanning PlayerScripts")
    scan(LocalPlayer:FindFirstChild("PlayerScripts"))
    
    log("-- Scanning PlayerGui")
    scan(LocalPlayer:FindFirstChild("PlayerGui"))
end

-- Summary
log("")
log("-- DECOMPILATION COMPLETE --")
log("-- Total: " .. stats.total)
log("-- Success: " .. stats.success)
log("-- Failed: " .. stats.failed)
log("-- Skipped: " .. stats.skipped)

-- Write to file (executor-dependent)
local fullOutput = table.concat(outputBuffer, "\n")

if writefile then
    writefile(outputPath, fullOutput)
    print("Saved to: " .. outputPath)
elseif savestring then
    savestring(outputPath, fullOutput)
    print("Saved to: " .. outputPath)
else
    -- Fallback: copy to clipboard
    if setclipboard then
        setclipboard(fullOutput)
        print("Output copied to clipboard (file write not supported)")
    end
end

return stats