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
This commit is contained in:
shen
2026-08-31 20:02:12 +09:00
co-authored by Claude Sonnet 5
parent f4917a2b3b
commit 47baf6c0c6
414 changed files with 69568 additions and 385 deletions
+103
View File
@@ -0,0 +1,103 @@
# 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)
+1
View File
@@ -0,0 +1 @@
uid://015nea7sbodm
+39
View File
@@ -0,0 +1,39 @@
# MapCoord —— 网络实体坐标 ↔ Metin2World 本地坐标。
#
# M2Client 给的实体 pos 是「全局服务器 cm」经 position_to_godot 后的结果:
# net = (sx*0.01, h, -sy*0.01) sx/sy 含地图 BasePosition
# 而 Metin2World 的地形/物件是「地图本地」帧(tile 0 在原点,+Z 朝南):
# world = ((sx-bx)*0.01, h, (sy-by)*0.01)
# 两者差一个 BasePosition 平移 + Z 轴翻向。没对齐时角色会飘在几公里外的黑void里。
#
# MapCoord.set_base(world.get_map_base_cm()) # Vector2cm
# var p := MapCoord.to_world(client.get_entity(vid).pos)
# var sc := MapCoord.to_server_cm(player.position) # -> Vector2 (sx_cm, sy_cm)
class_name MapCoord
extends RefCounted
static var _bx := 0.0 # BasePosition.x, cm
static var _by := 0.0
static func set_base(base_cm: Vector2) -> void:
_bx = base_cm.x
_by = base_cm.y
static func has_base() -> bool:
return _bx != 0.0 or _by != 0.0
# net 帧 (sx*.01, h, -sy*.01) -> world 本地帧 ((sx-bx)*.01, h, (sy-by)*.01)
static func to_world(net_pos: Vector3) -> Vector3:
return Vector3(net_pos.x - _bx * 0.01, net_pos.y, -net_pos.z - _by * 0.01)
# world 本地帧 -> 服务器全局 cm (sx, sy)
static func to_server_cm(world_pos: Vector3) -> Vector2:
return Vector2(world_pos.x * 100.0 + _bx, world_pos.z * 100.0 + _by)
# 服务器 heading(度) -> world 帧 Godot yaw(弧度)。
# net 帧里南=-Z、world 帧里南=+ZZ 翻向 -> 相对 net 帧的换算符号取反。
static func heading_to_yaw(angle_deg: float) -> float:
return deg_to_rad(angle_deg + 90.0)
static func yaw_to_heading(yaw_rad: float) -> float:
return fposmod(rad_to_deg(yaw_rad) - 90.0, 360.0)
+1
View File
@@ -0,0 +1 @@
uid://c36ncgt70wf0a
+91
View File
@@ -0,0 +1,91 @@
# ServerInfo (P10) —— 服务器列表 + 频道 → (host, port)。
#
# var si := ServerInfo.new()
# si.load_file("res://serverlist.txt") # 可选;没有就用内置默认
# for s in si.servers(): ...
# var addr := si.address(server_index, channel) # {auth_host,auth_port,game_host,game_port}
#
# 文件格式(TSV# 注释):
# name auth_host auth_port game_host game_port channels(csv) [port_step] [mark_port]
# 测试服 192.168.21.203 11000 192.168.21.203 11011 1,2,3 1 0
#
# 频道 N 的 game 端口 = game_port + (N-1)*port_step。
# 联调服实测端口是 11011/11012/11013step=1);不同服可在文件第 7 列覆盖。
# 第 8 列 mark_port = 公会会徽服端口(0 = 不下载会徽)。
class_name ServerInfo
extends RefCounted
const DEFAULT_PORT_STEP := 1
var _servers: Array = []
func _init() -> void:
# 内置默认:当前联调服(CServerStateChecker 实测频道口 11011/12/13
_servers = [{
"name": "测试服",
"auth_host": "192.168.21.203", "auth_port": 11000,
"game_host": "192.168.21.203", "game_port": 11011,
"channels": [1, 2, 3], "port_step": 1, "mark_port": 0,
}]
func load_file(path: String) -> bool:
if not FileAccess.file_exists(path):
return false
var f := FileAccess.open(path, FileAccess.READ)
if f == null:
return false
var out: Array = []
while not f.eof_reached():
var line := f.get_line().strip_edges()
if line == "" or line.begins_with("#"):
continue
var c := line.split("\t", false)
if c.size() < 5:
c = line.split(" ", false) # 容忍空格分隔
if c.size() < 5:
continue
var chans: Array = []
if c.size() >= 6:
for t in String(c[5]).split(",", false):
chans.append(int(t))
if chans.is_empty():
chans = [1]
var step := DEFAULT_PORT_STEP
if c.size() >= 7 and int(c[6]) > 0:
step = int(c[6])
var mark_port := 0
if c.size() >= 8 and int(c[7]) > 0:
mark_port = int(c[7])
out.append({
"name": String(c[0]),
"auth_host": String(c[1]), "auth_port": int(c[2]),
"game_host": String(c[3]), "game_port": int(c[4]),
"channels": chans, "port_step": step, "mark_port": mark_port,
})
if out.is_empty():
return false
_servers = out
return true
func servers() -> Array:
return _servers
func count() -> int:
return _servers.size()
func server(idx: int) -> Dictionary:
return _servers[idx] if idx >= 0 and idx < _servers.size() else {}
# 返回 {auth_host, auth_port, game_host, game_port}game_port 已按频道偏移。
func address(server_idx: int, channel: int = 1) -> Dictionary:
var s := server(server_idx)
if s.is_empty():
return {}
var ch: int = maxi(1, channel)
var step: int = int(s.get("port_step", DEFAULT_PORT_STEP))
return {
"auth_host": s["auth_host"], "auth_port": s["auth_port"],
"game_host": s["game_host"],
"game_port": int(s["game_port"]) + (ch - 1) * step,
"mark_host": s["game_host"], "mark_port": int(s.get("mark_port", 0)),
}
+1
View File
@@ -0,0 +1 @@
uid://dgvdccv3267fh