{
  "unlocks": [

    {
      "songId": "ordirehv",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "ordirehv",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "ordirehv",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "ordirehv",
      "ratingClass": 3,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "Glitch_2605",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "Glitch_2605",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "Glitch_2605",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "Glitch_2605",
      "ratingClass": 3,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "northernlimit",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "northernlimit",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "northernlimit",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "northernlimit",
      "ratingClass": 4,
      "conditions": [
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "defection",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "defection",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "defection",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "infinitestrife",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "infinitestrife",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "infinitestrife",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 100,
          "max": 100
        }
      ]
    },
    {
      "songId": "infinitestrife",
      "ratingClass": 3,
      "conditions": [
        {
          "type": 104
        }
      ]
    },
    {
      "songId": "worldender",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "worldender",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "worldender",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 100,
          "max": 100
        }
      ]
    },
    {
      "songId": "worldender",
      "ratingClass": 3,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 10,
          "max": 100
        }
      ]
    },
    {
      "songId": "pentiment",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "pentiment",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "pentiment",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 100,
          "max": 100
        }
      ]
    },
    {
      "songId": "pentiment",
      "ratingClass": 3,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 100,
          "max": 100
        }
      ]
    },
    {
      "songId": "arcanaeden",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "arcanaeden",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 5,
          "max": 15
        }
      ]
    },
    {
      "songId": "arcanaeden",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 100,
          "max": 100
        }
      ]
    },
    {
      "songId": "arcanaeden",
      "ratingClass": 3,
      "conditions": [
        {
          "type": 104
        },
        {
          "type": 101,
          "min": 10,
          "max": 100
        }
      ]
    },
    {
      "songId": "testify",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 2,
          "song_id": "testify",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "testify",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 2,
          "song_id": "testify",
          "song_difficulty": 0
        }
      ]
    },
    {
      "songId": "testify",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 2,
          "song_id": "testify",
          "song_difficulty": 3
        }
      ]
    },
    {
      "songId": "testify",
      "ratingClass": 3,
      "conditions": [
        {
          "type": 2,
          "song_id": "testify",
          "song_difficulty": 2
        }
      ]
    },
    {
      "songId": "callimakarma",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "callimakarma",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "callimakarma",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "last",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "last",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "last",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "lasteternity",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "lasteternity",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "lasteternity",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 2,
          "song_id": "lasteternity",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "tempestissimo",
      "ratingClass": 2,
      "conditions": [
        {
          "type": 2,
          "song_id": "tempestissimo",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "tempestissimo",
      "ratingClass": 1,
      "conditions": [
        {
          "type": 2,
          "song_id": "tempestissimo",
          "song_difficulty": 1
        }
      ]
    },
    {
      "songId": "tempestissimo",
      "ratingClass": 0,
      "conditions": [
        {
          "type": 2,
          "song_id": "tempestissimo",
          "song_difficulty": 1
        }
      ]
    }
  ]
}-- CharacterAbilityViewNoteResults —— 1:1 复刻 630（适配 ArkaHymnCore 5.5.6）
--
-- 630 侧已逆向确认（ida-5575 / 6.3.0 armeabi-v7a）：
--   * CharacterAbilityViewNoteResults 是纯 marker 类，没有技能逻辑；
--     UILayer::init (0x849B34) 里对 characterAbility 做
--     __dynamic_cast(..., CharacterAbilityViewNoteResults)，成功就把
--     UILayer::viewNoteResults 置 1，并把 uiRight 里的 "noteInfo" 节点
--     按设计分辨率重摆位置，最后无条件 noteInfo->setVisible(viewNoteResults)。
--   * 有意偏离 630 的一处：630 的 init 不动 label 文本，csb 里默认写的是
--     "0000"，直到第一次判定总数变化才被 setNoteInfoWithScoreState 刷掉；
--     本脚本在 init 时就先把 5 个数值刷成 "0"（纯展示，不影响判定/存档）。
--
--   * UILayer::updateWithScoreState (0x8DB92C)：
--       if (viewNoteResults && pureCount+farCount+lostCount != lastCount)
--           setNoteInfoWithScoreState(this, scoreState);
--     lastCount 由 ctor memset 为 0。
--   * UILayer::setNoteInfoWithScoreState (0xC9FC64) 逐 label setString：
--       pureLabel-fullnolocalize <- pureCount
--       farLabel-fullnolocalize  <- farCount
--       lostLabel-fullnolocalize <- lostCount
--       lateEarlyShowAll 为真：early = pureEarly+farEarly, late = pureLate+farLate
--       否则：              early = farEarly,            late = farLate
--       最后 lastCount = pure+far+lost
--
-- 5.5.6 适配（本脚本只适用 5.5.6）：
--   * 5.5.6 二进制没有 viewNoteResults 这套逻辑，但用户直接用 630 的
--     layouts/ingame/IngameUIRight.csb —— noteInfo 已经集成在 uiRight 里。
--     Lua 从运行中的 UILayer.uiRight 节点树里现拿 "noteInfo"，把 630
--     UILayer::init 的位置重摆逻辑照抄一遍，再按 updateWithScoreState 的
--     条件刷数字。不再需要单独外置 noteInfo.csb / CSLoader。
--   * csb 里这些计数 label 是 cocos2d::ui::Text，不是 cocos2d::Label：
--     必须走 Native.Text_setString，走 Label_setString 会按 Label vtable
--     调用而崩。
--
-- 偏移（5.5.6 双端一致；严禁拿 630 的偏移来套）：
--   GameModel::scoreState       = +0x38 (56)
--   ScoreState::pureCount       = +0x64   (SPURE+PURE 合并，UI 显示这个)
--   ScoreState::farCount        = +0x68
--   ScoreState::lostCount       = +0x6C
--   ScoreState::farLate         = +0x80
--   ScoreState::farEarly        = +0x84
--   ScoreState::pureLate        = +0x88
--   ScoreState::pureEarly       = +0x8C
--   UserPreferences::lateEarlyShowAll = +0x18 (24)，来自 UserDefault key
--     "lateearly_showall"（iOS UserPreferences::init @ 0x7d92b4 与
--      Android UserPreferences::init @ 0x105E9F0 均已确认）
--
-- 运行要求：
--   * 需要 ENABLE_PLAYER_SKILLS_NATIVE_FFI=1 编译（Config.mk 默认已开）
--   * 只读内存 + 搭 UI，不改判定，也不碰 Write*/Call/CallVirtual → 本局照常
--     存档、上传（见 SKILL_LUA_zh-CN.md 判定/存档互斥）。
--   * 安装：游戏资源里用 630 的 IngameUIRight.csb 替换
--     layouts/ingame/IngameUIRight.csb（noteInfo 在 csb 里直接集成），
--     本 .lua 放到 <Documents>/skills/<角色id>/ 下即可。

local GM_SCORE_STATE        = 0x38
local SS_PURE_COUNT         = 0x64
local SS_FAR_COUNT          = 0x68
local SS_LOST_COUNT         = 0x6C
local SS_FAR_LATE           = 0x80
local SS_FAR_EARLY          = 0x84
local SS_PURE_LATE          = 0x88
local SS_PURE_EARLY         = 0x8C
local UP_LATE_EARLY_SHOWALL = 0x18

local nativeOk = Native ~= nil

local function log(msg)
    if Native.Log then Native.Log(msg) end
end

-- 630 的 label 名（csb 里就是这些名字，-fullnolocalize 后缀不要动）
local LABELS = {
    { "pureLabel-fullnolocalize", "PURE" },
    { "farLabel-fullnolocalize",  "FAR"  },
    { "lostLabel-fullnolocalize", "LOST" },
    { "earlyLabel-fullnolocalize","EARLY" },
    { "lateLabel-fullnolocalize", "LATE" },
}

local uiRight  = 0
local panel    = 0
local inited   = false
local lastCount = 0  -- 630: UILayer ctor memset 出来的 0；总数从 0 变化才刷

-- 一次性诊断标记：只打一次，避免每帧刷屏
local loggedUIRight   = false
local loggedPanelMiss = false
local loggedGmNull    = false
local loggedSsNull    = false
local loggedFirstSet  = false

local function ptr(v)
    return v ~= nil and v ~= 0
end

-- 1:1 抄 630 UILayer::init (0x849B34) viewNoteResults 分支的位置重摆
-- （vtable 槽位已逐个核对：vptr+0xA4 = getContentSize、vptr+0x34 = getScaleY，
--   所以用的是 uiright_1 的 contentSize.height 和 scaleY，不是 position/scale）：
--   height >= 890:  y = -16.0 - ((uiright_1.contentHeight + -8.0) * uiright_1.scaleY)
--   否则:           v224 = h<=720 ? 0 : (h<=960 ? h-720 : 240)
--                   y = (height - uiRight.y) - ((v224/240.0)*130.0 + 344.0)
-- 拿不到 uiright_1 就跳过（保留 csb 自带坐标），刷新数字不受影响。
local function repositionNoteInfo(noteInfo)
    local height = Native.Director_getDesignResolutionHeight()
    if height == nil then return end

    local anchor = Native.Node_getChildByName(uiRight, "uiright_1")
    if not ptr(anchor) then return end
    local anchorY = Native.Node_getContentSizeHeight(anchor) or 0
    local anchorScale = Native.Node_getScaleY(anchor) or 1.0
    local uiRightY = Native.Node_getPositionY(uiRight) or 0

    local y
    if height >= 890.0 then
        y = -16.0 - ((anchorY + -8.0) * anchorScale)
    else
        local v224 = 0.0
        if height > 720.0 then
            if height > 960.0 then
                v224 = 240.0
            else
                v224 = height - 720.0
            end
        end
        y = (0.0 - uiRightY + height) - ((v224 / 240.0) * 130.0 + 344.0)
    end

    Native.Node_setPosition(noteInfo, Native.Node_getPositionX(noteInfo) or 0, y)
end

-- 1:1 抄 630 UILayer::setNoteInfoWithScoreState（0xC9FC64）
local function setNoteInfoWithScoreState(ss)
    local pure = Native.ReadI32(ss + SS_PURE_COUNT)
    local far  = Native.ReadI32(ss + SS_FAR_COUNT)
    local lost = Native.ReadI32(ss + SS_LOST_COUNT)

    local showAll = false
    local up = Native.UserPreferences_getInstance and Native.UserPreferences_getInstance() or 0
    if ptr(up) then
        showAll = Native.ReadI8(up + UP_LATE_EARLY_SHOWALL) ~= 0
    end

    local early, late
    if showAll then
        early = Native.ReadI32(ss + SS_PURE_EARLY) + Native.ReadI32(ss + SS_FAR_EARLY)
        late  = Native.ReadI32(ss + SS_PURE_LATE)  + Native.ReadI32(ss + SS_FAR_LATE)
    else
        early = Native.ReadI32(ss + SS_FAR_EARLY)
        late  = Native.ReadI32(ss + SS_FAR_LATE)
    end

    local values = { pure, far, lost, early, late }
    for i, row in ipairs(LABELS) do
        local node = Native.Node_getChildByName(panel, row[1])
        if ptr(node) then
            Native.Text_setString(node, tostring(values[i]))
        end
    end

    -- 630: lastCount = pureCount + farCount + lostCount
    return pure + far + lost
end

function OnEnter()
    -- 面板不在这里强拿：slot 50（setupFlickNotes）在 UILayer::init 之前也可能
    -- 先跑，uiRight 还没建好，交给 OnFrame 惰性获取、就绪后一次性初始化。
    uiRight  = 0
    panel    = 0
    inited   = false
    lastCount = 0
    loggedUIRight   = false
    loggedPanelMiss = false
    loggedGmNull    = false
    loggedSsNull    = false
    loggedFirstSet  = false
    log("OnEnter")
end

function OnFrame(timingMs)
    if not nativeOk then return end

    -- 惰性拿 uiRight（本局 GameScene->uiLayer->uiRight）
    if not ptr(uiRight) then
        uiRight = Native.ArkaUtil_getUIRight() or 0
        if ptr(uiRight) and not loggedUIRight then
            loggedUIRight = true
            log("uiRight=" .. tostring(uiRight))
        end
    end
    if not ptr(uiRight) then return end

    -- 首次就绪：定位 + 显示（630 init 里 noteInfo->setVisible(viewNoteResults)，
    -- 本技能对应的角色 viewNoteResults 恒为 1）
    if not inited then
        panel = Native.Node_getChildByName(uiRight, "noteInfo")
        if not ptr(panel) then
            if not loggedPanelMiss then
                loggedPanelMiss = true
                log("noteInfo NOT found under uiRight=" .. tostring(uiRight))
            end
            return
        end
        repositionNoteInfo(panel)
        Native.Node_setVisible(panel, true)
        -- 用户要求：init 时所有数值默认刷成 0（630 csb 里写死的是 "0000"）。
        -- 只读 Text_setString，不影响判定/存档；等第一次判定把 total 改掉后
        -- 下面的 setNoteInfoWithScoreState 才会再刷成真实计数。
        for i, row in ipairs(LABELS) do
            local node = Native.Node_getChildByName(panel, row[1])
            if ptr(node) then
                Native.Text_setString(node, "0")
            end
        end
        inited = true
        log("noteInfo panel=" .. tostring(panel) .. " inited")
    end

    -- 面板已被摘离节点树（场景销毁/切结算）就不碰，避免悬垂指针
    if not ptr(Native.Node_getParent(panel)) then return end

    local gm = Native.Skill_getCurrentGameModel()
    if not ptr(gm) then
        if not loggedGmNull then
            loggedGmNull = true
            log("gm null")
        end
        return
    end
    local ss = Native.ReadI64(gm + GM_SCORE_STATE)
    if not ptr(ss) then
        if not loggedSsNull then
            loggedSsNull = true
            log("ss null gm=" .. tostring(gm))
        end
        return
    end

    local total = Native.ReadI32(ss + SS_PURE_COUNT)
               + Native.ReadI32(ss + SS_FAR_COUNT)
               + Native.ReadI32(ss + SS_LOST_COUNT)
    -- 与 630 一致：总数没变化就不刷（省字符串分配）
    if total == lastCount then return end

    lastCount = setNoteInfoWithScoreState(ss)
    if not loggedFirstSet then
        loggedFirstSet = true
        log("first set: total=" .. tostring(total) .. " ss=" .. tostring(ss))
    end
end

function OnExit()
    -- 节点属于场景节点树，场景销毁时自动释放；这里只清引用，不做 release
    uiRight  = 0
    panel    = 0
    inited   = false
    lastCount = 0
    log("OnExit")
end
[
    {
        "skill_id": "skill_saya_uncap",
        "file": "skill_saya_uncap.lua",
        "description": "显示判定详情"
    }
]
