order_watch.module_of(path)#order_watch
Watches the orders our scripts send and reports three invariants as a red line with the module names (#1184):
"requires": { "order_watch": "^1.0" }local order_watch = require("lib/order_watch")玩家无需安装:脚本会自动带上库,取范围内的任一版本。发布前可在开发会话中修改 scripts/lib/order_watch/ 来试验。
lib/order_watch
Watches the orders our scripts send and reports three invariants as a red line with the module names (#1184):
channel: a script order ends a channel of the unit it moves (Riki's Tricks cut by illusion.lua's move, #1175; Pudge's blink cut Dismember, #1160).selection: a script changed the player's selection (a burst of writes in one frame that leaves a different set of units selected).conflict: a second module throws away what the first started on the same unit withinM.WINDOW(0.1 s). Two shapes: a cast still in its cast point (not on cooldown yet) answered by another module's body order, and a tug of war over the walk (A, B, A). A single hand-over of the walk, or a cast after another module's walk, is the later module taking the body on purpose and only counts inM.handoffs.
local OW = require("lib/order_watch")
OW.install() -- wraps orders.* and the selection writes once
OW.player_order(o) -- the player's own order (OnPrepareUnitOrders, synthetic == false)
local n = OW.enter(module, allow, "file.lua:12") -- an executor names the author
OW.leave(n)
OW.hits() -- { {kind, mods, detail, t, unit}, ... }
OW.lines() -- "kind: a > b detail" per hit
OW.reset() -- a new game: hits, history and the once-per-game log keys
OW.forget() -- a rewound clock: history only
The author of an order: the innermost enter() frame, else the first script file on the stack
past compat, the executor and this file. A path maps to a module by module_of:
utea/heroes/pudge/x.lua is heroes/pudge, utea/core/autopilot/* is autopilot,
utea/core/f.lua is core/f, utea/x/* is x, scripts/x/* is x.
Not red:
- a queued order; an order to another unit; an ability with
IGNORE_CHANNEL; the channel's own release (a sub-ability whose name extends the channelled one); - an order carrying an allowance (
interrupt_channel, a preempting dodger command): kept inM.breaksfor a test to judge; - selection writes from
hud(the player's own panel click) andautopilot(M.SELECT_OK).
Where it runs: uTEA main installs it in the client and resets it per game; a hit goes to
evolve.log as [order_watch] RED <kind>: ..., once per kind and modules per game. In
tools/utea_harness mock.install() installs it for every test: a hit prints
FAIL order_watch ... at exit and the run exits 1. ORDER_WATCH=warn prints only,
ORDER_WATCH=off skips it, ORDER_WATCH_STATS=1 prints the counts to stderr.
Test: tools\lua\lua.exe scripts\lib\order_watch\selftest.lua from the repo root.
1.0.0 版本的代码,即客户端拿到的内容。包内字节码由这些文件编译而来。
init.lua409 行
-- lib/order_watch: three invariants over the orders our scripts send (#1184).
--
-- channel a script order ends a channel of the unit it moves (illusion.lua's move cut riki's
-- tricks, #1175; pudge's blink cut dismember, #1160)
-- selection a script changed the player's selection
-- conflict two modules gave one unit a body order within 100 ms: the second throws the first away
--
-- install() wraps the native orders.* and the selection writes, so every sender is seen, the
-- executor and a module calling the natives alike. the author: the frame set by enter() (the
-- executor names the module that built the command), else the first script file on the stack.
-- a hit: one log line `[order_watch] RED <kind>: ...` per kind and modules per game, kept in hits().
do
local g = rawget(_G, "__EVOLVE_LIB_REG")
if g and g.order_watch then return g.order_watch end
end
local M = {}
M.WINDOW = 0.1
-- a cast answered inside its cast point is red only if it never lands this long after the answer:
-- the cooldown reaches the client half a round trip late, a 0.05 s cast point read at 50 ms looked
-- cancelled live (disruptor thunder strike, land 12-03)
M.CONFIRM = 1.0
M.hits_list, M.breaks = {}, {}
local seen_key = {}
local last = setmetatable({}, { __mode = "k" }) -- unit key -> { mod, t, order }
local ctx = {} -- enter() stack
local burst, settle -- selection writes of one frame
local pending = {} -- cancelled-looking casts waiting for their cooldown
local player_cast = setmetatable({}, { __mode = "k" }) -- unit -> { ab, until } for a cast still in its cast point
-- a module allowed to change the selection, and why
M.SELECT_OK = {
hud = "the player's own click on a hud panel (courier, shop)",
autopilot = "the bot drives the whole game, the player does not",
}
-- order types that move the body: a new one replaces what the unit was doing and ends a channel
local BODY = { [1] = true, [2] = true, [3] = true, [4] = true, [10] = true, [12] = true, [13] = true,
[14] = true, [15] = true, [21] = true, [26] = true, [28] = true, [29] = true, [33] = true }
local CASTS = { [5] = true, [6] = true, [7] = true, [8] = true, [9] = true }
local IMMEDIATE, IGNORE_CHANNEL, CHANNELLED = 2048, 4194304, 128
-- the harness hands out a new proxy per read over one state table
local function key(u)
if type(u) == "table" and type(rawget(u, "_s")) == "table" then return rawget(u, "_s") end
return u
end
local function now()
local g = rawget(_G, "globals")
local ok, t = pcall(function() return g.curtime() end)
if ok and type(t) == "number" then return t end
return os.clock()
end
local function call(f, ...)
if type(f) ~= "function" then return nil end
local ok, v = pcall(f, ...)
if ok then return v end
end
-- a script path -> its module: utea/heroes/pudge/x.lua -> heroes/pudge, utea/dodger/x.lua -> dodger,
-- scripts/hud/main.lua -> hud, a bare file name -> the name without .lua
function M.module_of(path)
if type(path) ~= "string" or path == "" or path:find("^=") then return nil end
local p = path:gsub("\\", "/"):gsub("^@", ""):gsub(":%d+$", "")
local low = p:lower()
local h = low:match("utea/heroes/([^/]+)/")
if h then return "heroes/" .. h end
local a = low:match("utea/core/(autopilot)/")
if a then return a end
local c = low:match("utea/core/([^/]+)%.lua$")
if c then return "core/" .. c end
local u = low:match("utea/([^/]+)/")
if u then return u end
local s = low:match("scripts/([^/]+)/") or low:match("scripts/([^/]+)%.lua$")
if s then return s end
return (low:match("([^/]+)%.lua$")) or low
end
-- infrastructure between a module and the native: compat (in the harness its chunks are bare names,
-- "@legacy_cmd"), the executor, uTEA main, this file, a harness test
local function skip_source(src)
local low = src:gsub("\\", "/"):lower()
if not low:find("/", 1, true) and not low:find("%.lua$") then return true end
return low:find("lib/order_watch", 1, true) or low:find("/compat/", 1, true)
or low:find("utea/core/execute_commands", 1, true) or low:find("utea/main.lua", 1, true)
or low:find("utea_harness", 1, true) or low:find("bridge_in", 1, true)
end
local function author()
local top = ctx[#ctx]
if top then return top.mod, top.allow, top.at end
for lvl = 3, 40 do
local info = debug.getinfo(lvl, "Sl")
if not info then break end
local src = info.source
if type(src) == "string" and src:sub(1, 1) == "@" and not skip_source(src) then
return M.module_of(src), nil, (src:match("([^/\\]+)$") or src) .. ":" .. tostring(info.currentline)
end
end
return nil, nil, nil
end
-- the executor: the module that built the command, an allowance (`interrupt_channel`, `preempt`),
-- and where (file:line) for the log
function M.enter(mod, allow, at)
ctx[#ctx + 1] = { mod = mod, allow = allow, at = at }
return #ctx
end
function M.leave(n)
for i = #ctx, (n or #ctx), -1 do ctx[i] = nil end
end
local function hit(kind, unit, mods, detail)
local key = kind .. "|" .. table.concat(mods, ">")
local h = { kind = kind, mods = mods, detail = detail, t = now(), unit = unit }
M.hits_list[#M.hits_list + 1] = h
if M.on_hit then pcall(M.on_hit, h) end
if seen_key[key] then return end
seen_key[key] = true
local line = ("[order_watch] RED %s: %s %s"):format(kind, table.concat(mods, " > "), detail or "")
local lg = rawget(_G, "log")
if lg and lg.write then pcall(lg.write, line) end
if M.echo then M.echo(line) end
end
local function ab_name(ab)
if ab == nil then return nil end
local A = rawget(_G, "Ability")
local n = A and call(A.GetName, ab)
if type(n) ~= "string" and type(ab) == "table" and type(ab.name) == "function" then n = call(ab.name, ab) end
return type(n) == "string" and n or nil
end
local function has_bit(ab, bit)
if ab == nil or type(ab) == "number" then return false end
if type(ab) == "table" or type(ab) == "userdata" then
local hb = call(function() return ab.has_behavior end)
if hb then
local v = call(hb, ab, bit)
if v ~= nil then return v == true end
end
end
local A = rawget(_G, "Ability")
local m = A and call(A.GetBehavior, ab)
return type(m) == "number" and (m & bit) ~= 0
end
local function cast_point(ab)
local A = rawget(_G, "Ability")
local v = A and call(A.GetCastPoint, ab)
return type(v) == "number" and v or 0
end
local function local_hero()
local c = rawget(_G, "client")
local h = c and call(c.local_hero)
if h then return h end
local H = rawget(_G, "Heroes")
return H and call(H.GetLocal)
end
local function channelling(unit)
local N = rawget(_G, "NPC")
if N and call(N.IsChannellingAbility, unit) == true then return true end
local pc = player_cast[key(unit)]
return pc and now() < pc.till and pc.chan or false
end
-- the channel's own release (x_end, x_release): its name extends the channelled ability's
local function releases_channel(unit, ab)
local name = ab_name(ab)
local N, A = rawget(_G, "NPC"), rawget(_G, "Ability")
if not (name and N and A) then return false end
local base = name
for _ = 1, 4 do
base = base:match("^(.+)_[^_]+$")
if not base then return false end
local parent = call(N.GetAbility, unit, base)
if parent then return call(A.IsChannelling, parent) == true end
end
return false
end
-- a cast went off: its cooldown runs (the client starts it at the end of the cast point)
local function landed(ab)
local A = rawget(_G, "Ability")
if A and call(A.IsInAbilityPhase, ab) == true then return false end
local cd = A and call(A.GetCooldown, ab)
return type(cd) == "number" and cd > 0
end
local function unit_name(u)
local N = rawget(_G, "NPC")
local n = N and call(N.GetUnitName, u)
return type(n) == "string" and n:gsub("^npc_dota_", "") or "?"
end
-- one script order: type, the unit it moves, the ability, queued
M.seen = 0
function M.note(order, unit, ab, queued)
if M.off then return end
M.seen = M.seen + 1
settle()
local mod, allow, at = author()
unit = unit or local_hero()
if not unit or queued then return end
local is_cast = CASTS[order] and ab ~= nil
local body = BODY[order] or (is_cast and not has_bit(ab, IGNORE_CHANNEL))
if not body then return end
local t = now()
local name = ab_name(ab)
-- 1. channel
if channelling(unit) and not releases_channel(unit, ab) then
local row = { mod = mod or "?", order = order, ability = name, t = t, allow = allow }
M.breaks[#M.breaks + 1] = row
if not allow then
hit("channel", unit, { mod or "?" }, ("order=%d%s (%s) ended the channel of %s"):format(order,
name and (" " .. name) or "", at or "?", unit_name(unit)))
end
end
-- 3. conflict: the second module throws away what the first started. two shapes count:
-- a cast still in its cast point (not on cooldown yet) answered by another module's body order
-- a tug of war: the unit's walk goes A -> B -> A, each hand-over inside the window
-- one hand-over of a walk (the orbwalker's follow, then a pack's step) or a cast after another
-- module's walk is the later module taking the body on purpose: counted in M.handoffs only
local occupies = BODY[order] or (is_cast and not has_bit(ab, IMMEDIATE) and cast_point(ab) > 0)
if not occupies or not mod then return end
local k = key(unit)
local prev = last[k]
local row = { mod = mod, t = t, order = order, ab = is_cast and ab or nil, name = name, at = at }
if prev and prev.mod ~= mod and t - prev.t <= M.WINDOW and t >= prev.t and not allow then
local function say(a, b)
return ("order=%d%s (%s) then order=%d%s (%s)"):format(a.order, a.name and (" " .. a.name) or "",
a.at or "?", b.order, b.name and (" " .. b.name) or "", b.at or "?")
end
local before = prev.prev
if prev.ab ~= nil and not landed(prev.ab) then
pending[#pending + 1] = { ab = prev.ab, t = t, till = t + cast_point(prev.ab) + M.CONFIRM, unit = unit,
mods = { prev.mod, mod }, detail = ("%s on %s %.0f ms apart: the cast was cancelled"):format(
say(prev, row), unit_name(unit), (t - prev.t) * 1000) }
elseif prev.ab == nil and not is_cast and before and before.mod == mod and before.ab == nil
and prev.t - before.t <= M.WINDOW then
hit("conflict", unit, { mod, prev.mod, mod }, ("%s, back in %.0f ms on %s: a tug of war"):format(
say(prev, row), (t - before.t) * 1000, unit_name(unit)))
else
M.handoffs = (M.handoffs or 0) + 1
end
row.prev = prev
prev.prev = nil
end
last[k] = row
end
-- the player's own order (uTEA main sees it before the game): a cast of a channel opens the cast point window
function M.player_order(o)
if type(o) ~= "table" or o.synthetic ~= false then return end
local unit = o.npc or o.unit or local_hero()
if not unit then return end
last[key(unit)] = nil
if CASTS[o.order] and o.ability and has_bit(o.ability, CHANNELLED) then
player_cast[key(unit)] = { chan = true, till = now() + cast_point(o.ability) + 0.25 }
end
end
local function selection_now()
local p = rawget(_G, "players")
local s = p and call(p.selected_units)
return type(s) == "table" and s or {}
end
-- 2. selection: a burst of writes (clear, then adds; one frame) that leaves the selection
-- different from what it was. settled at the next order, the next frame's write, or hits()
-- force: hits() and lines() judge what is still waiting with what they see now
settle = function(force)
if #pending > 0 then
local t, keep = now(), {}
for _, p in ipairs(pending) do
if landed(p.ab) then
M.handoffs = (M.handoffs or 0) + 1
elseif force or t >= p.till then
hit("conflict", p.unit, p.mods, p.detail)
else
keep[#keep + 1] = p
end
end
pending = keep
end
local b = burst
if not b then return end
burst = nil
-- the state right after the burst's own last write: a player's click before the settle is not ours
local now_sel = b.after or selection_now()
local was = {}
for _, x in ipairs(b.before) do was[key(x)] = true end
local changed = #now_sel ~= #b.before
for _, x in ipairs(now_sel) do if not was[key(x)] then changed = true end end
if changed then
local names = {}
for _, x in ipairs(now_sel) do names[#names + 1] = unit_name(x) end
hit("selection", nil, { b.mod or "?" }, ("%s: %d -> %d units%s"):format(table.concat(b.ops, ","),
#b.before, #now_sel, #names > 0 and (" (" .. table.concat(names, ",") .. ")") or ""))
end
end
M.settle = settle
function M.note_selection(op)
if M.off then return end
local mod = author()
if mod and M.SELECT_OK[mod] then return end
local t = now()
if burst and (burst.t ~= t or burst.mod ~= mod) then settle() end
if not burst then burst = { before = selection_now(), t = t, mod = mod, ops = {} } end
burst.ops[#burst.ops + 1] = op
end
local function wrap(t, name, fn, after)
local orig = rawget(t, name) or t[name]
if type(orig) ~= "function" then return end
rawset(t, name, function(...)
pcall(fn, ...)
if not after then return orig(...) end
local r = table.pack(orig(...))
pcall(after)
return table.unpack(r, 1, r.n)
end)
end
function M.install()
local o = rawget(_G, "orders")
if type(o) == "table" and not rawget(o, "__order_watch") then
rawset(o, "__order_watch", true)
wrap(o, "move_to", function(_, q) M.note(1, nil, nil, q) end)
wrap(o, "move_to_target", function(_, q) M.note(2, nil, nil, q) end)
wrap(o, "attack_move", function(_, q) M.note(3, nil, nil, q) end)
wrap(o, "attack", function(_, q) M.note(4, nil, nil, q) end)
wrap(o, "cast_position", function(ab, _, q) M.note(5, nil, ab, q) end)
wrap(o, "cast_target", function(ab, _, q) M.note(6, nil, ab, q) end)
wrap(o, "cast_target_tree", function(ab, _, q) M.note(7, nil, ab, q) end)
wrap(o, "cast_no_target", function(ab, q) M.note(8, nil, ab, q) end)
wrap(o, "cast_toggle", function(ab, q) M.note(9, nil, ab, q) end)
wrap(o, "use_item", function(it, tg, q)
if type(tg) == "boolean" then q, tg = tg, nil end
local kind = tg == nil and 8 or ((type(tg) == "table" and tg.x ~= nil) and 5 or 6)
M.note(kind, nil, it, q)
end)
wrap(o, "hold", function(q) M.note(10, nil, nil, q) end)
wrap(o, "stop", function(q) M.note(21, nil, nil, q) end)
wrap(o, "unit_move_to", function(u, _, q) M.note(1, u, nil, q) end)
wrap(o, "unit_attack", function(u, _, q) M.note(4, u, nil, q) end)
wrap(o, "raw", function(ty, t)
if type(ty) ~= "number" then return end
t = type(t) == "table" and t or {}
local unit = t.unit
-- issuer 0: the selection; one unit selected is that unit, else the hero
if not unit and (t.issuer == 0 or t.issuer == nil) then
local s = selection_now()
unit = #s == 1 and s[1] or nil
end
local q = t.queue
M.note(ty, unit, t.ability, q == true or (type(q) == "number" and q ~= 0))
end)
end
local p = rawget(_G, "players")
if type(p) == "table" and not rawget(p, "__order_watch") then
rawset(p, "__order_watch", true)
local function after() if burst then burst.after = selection_now() end end
wrap(p, "select_unit", function(u) M.note_selection("select", u) end, after)
wrap(p, "add_selected_unit", function(u) M.note_selection("add", u) end, after)
wrap(p, "clear_selected_units", function() M.note_selection("clear") end, after)
end
return M
end
function M.hits() settle(true); return M.hits_list end
-- a new game (or a new harness case): history and the once-per-game log keys go
function M.reset()
M.hits_list, M.breaks, seen_key = {}, {}, {}
burst, pending = nil, {}
last = setmetatable({}, { __mode = "k" })
player_cast = setmetatable({}, { __mode = "k" })
ctx = {}
end
-- a harness case with a rewound clock: the history goes, the hits stay
function M.forget()
settle(true)
last = setmetatable({}, { __mode = "k" })
player_cast = setmetatable({}, { __mode = "k" })
ctx = {}
end
-- `name a > b: detail` per hit, for a test's FAIL line
function M.lines(list)
settle(true)
local out = {}
for _, h in ipairs(list or M.hits_list) do
out[#out + 1] = ("%s: %s %s"):format(h.kind, table.concat(h.mods, " > "), h.detail or "")
end
return out
end
local reg = rawget(_G, "__EVOLVE_LIB_REG") or {}
rawset(_G, "__EVOLVE_LIB_REG", reg)
reg.order_watch = M
return M
pack.json2 行
{ "kind": "lib", "version": "1.0.0" }
这个作品没有版本说明。
还没有已发布的脚本需要它。
该作者的其他作品
lotus
作者 hunnycThe lotus pools: how many lotuses each holds and when the next one grows.
"requires": { "lotus": "^1.0" }local lotus = require("lib/lotus")order_class
作者 hunnycThe class an order carries into `hk_order` (#1103): reaction, chain, window, background, owner.
"requires": { "order_class": "^1.0" }local order_class = require("lib/order_class")fog_creeps
作者 hunnycEvery enemy lane creep, in sight and in the fog, and the waves they make.
"requires": { "fog_creeps": "^1.0" }local fog_creeps = require("lib/fog_creeps")