Files
mtgodot-poc/project/net/channel_status.gd
T
shenandClaude Sonnet 5 47baf6c0c6 Metin2 game client (P0–P11) + mobile asset pipeline
Networked client on the existing Godot 4.7 + libgr2 renderer:
- net: m2dev wire protocol (libsodium KX + XChaCha20), auth/select/game
  phases, EntityStore world model, ~all GC/CG headers. char create/delete,
  private shop / mall / cube, SHOP_GC_START_EX, guild, party (+ CG_PARTY_SET_STATE),
  quests, dragon soul, refine, safebox, exchange.
- UI: in-game windows migrated 1:1 from the reference uiscript/root .py —
  char status (/stat), inventory+equipment, select-item ([SELECT_ITEM] quest
  token), system-option + game-option + ESC system menu, private-shop 39-grid,
  party info board, shop tabs, atlas, minimap, quickbar, chat, …
- EterGrnLib polish: GR2 material blend/two-sided, LOD crossfade, motion-event
  dispatch, contact shadow, ray-AABB picking, weapon grip pre-transform.

Portable asset IO (A1) — all extension/libgr2/formats/mtproto reads routed
through godot::FileAccess (res:// PCK works on iOS/Android); standalone-lib
*_path() kept for the non-Godot CTests. AssetResolver + PropertyRegistry
switched to a baked index (bake_asset_index.gd) instead of std::filesystem.

Mobile builds: build-{android,ios}.sh, export-android.sh, pack-assets.sh,
gen-debug-keystore.sh. Assets ship as a zip mounted at runtime by
project/asset_pack.gd (adb push now; HTTP download is a drop-in later).

ctest 10/10, 34 GDScript suites, macOS/iOS/Android all build.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013EJxkHiNKS4kybHS3XKyAJ
2026-08-31 20:02:12 +09:00

104 lines
2.9 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ChannelStatus (P10 收尾) —— 频道负载查询(对齐 CServerStateChecker)。
#
# var cs := preload("res://net/channel_status.gd").new()
# add_child(cs)
# cs.query(host, port, func(map): ...) # map: { port:int -> status:int }
#
# 明文 TCP(无 libsodium):连上任一频道端口 → 发 CG_STATE_CHECKER(0x000F,len 4) →
# 跳过其它包直到 GC_RESPOND_CHANNELSTATUS(0x0010) → 读 int32 count → count × {i16 port, u8 status}。
# status0 关 / 1 正常 / 2 忙 / 3 满(Metin2 约定,非在线人数——那要改服务器)。
extends Node
const CG_STATE_CHECKER := 0x000F
const GC_RESPOND_CHANNELSTATUS := 0x0010
const STATUS_TEXT := {0: "关闭", 1: "正常", 2: "拥挤", 3: "爆满"}
signal done(status_by_port: Dictionary)
var _peer: StreamPeerTCP
var _cb: Callable
var _deadline := 0.0
var _sent := false
func query(host: String, port: int, callback: Callable = Callable(), timeout_s := 3.0) -> void:
_cb = callback
_peer = StreamPeerTCP.new()
_sent = false
_deadline = _now() + timeout_s
if _peer.connect_to_host(host, port) != OK:
_finish({})
return
set_process(true)
func text_for(status: int) -> String:
return STATUS_TEXT.get(status, "?")
func _now() -> float:
return Time.get_ticks_msec() / 1000.0
func _process(_dt: float) -> void:
if _peer == null:
return
_peer.poll()
var st := _peer.get_status()
if st == StreamPeerTCP.STATUS_ERROR or _now() > _deadline:
_finish({})
return
if st != StreamPeerTCP.STATUS_CONNECTED:
return
if not _sent:
var req := PackedByteArray()
req.resize(4)
req.encode_u16(0, CG_STATE_CHECKER)
req.encode_u16(2, 4)
_peer.put_data(req)
_sent = true
# 攒够一个包就解析
_try_parse()
func _try_parse() -> void:
var avail := _peer.get_available_bytes()
if avail < 4:
return
# 逐包扫,跳到 RESPOND_CHANNELSTATUS
while _peer.get_available_bytes() >= 4:
var head: Array = _peer.get_partial_data(4)
if head[0] != OK:
return
var hb: PackedByteArray = head[1]
var header := hb.decode_u16(0)
var length := hb.decode_u16(2)
if header == GC_RESPOND_CHANNELSTATUS:
_read_body()
return
# 别的包:按 length 跳过剩余(已吃掉 4 字节头)
var rest := maxi(0, length - 4)
if rest > 0:
if _peer.get_available_bytes() < rest:
return # 等更多数据(简化:不缓存半包,靠 timeout 兜底)
_peer.get_data(rest)
func _read_body() -> void:
# 已消费 4 字节头;接着 int32 count + count × {i16 port, u8 status}
if _peer.get_available_bytes() < 4:
return
var cnt_r: Array = _peer.get_data(4)
var count: int = (cnt_r[1] as PackedByteArray).decode_s32(0)
var out := {}
for i in count:
if _peer.get_available_bytes() < 3:
break
var rec: PackedByteArray = _peer.get_data(3)[1]
out[rec.decode_s16(0)] = rec.decode_u8(2)
_finish(out)
func _finish(m: Dictionary) -> void:
set_process(false)
if _peer:
_peer.disconnect_from_host()
_peer = null
done.emit(m)
if _cb.is_valid():
_cb.call(m)