// npcfindname 遍历 Script.pvf 里的 NPC 定义文件,按 [name] 段反查 NPC 编号。 // // 动机:`npc/npc.lst` 只登记经典全局 NPC;地图专用 NPC(例如"博肯")用编号 // 直接写在地图 [NPC] 段里,编号与文件的对应关系得到定义文件里找。 // 定义文件的 [name] 段是该 NPC 的显示名,所以按名字反查是可靠路径。 // // go run ./cmd/scratch/npcfindname -pvf -name 博肯 // go run ./cmd/scratch/npcfindname -pvf -name 博肯 -all package main import ( "flag" "fmt" "os" "path" "regexp" "sort" "strings" platformpvf "longheng.io/server/internal/platform/pvf" ) var nameRe = regexp.MustCompile("(?m)^\\s*\\[name\\]\\s*\\r?\\n\\s*`([^`]+)`") func main() { pvfPath := flag.String("pvf", `../runtime/data/dnf/Script.pvf`, "Script.pvf path") want := flag.String("name", "", "要查的中文名(子串匹配)") all := flag.Bool("all", false, "打印全部命中(默认 40 条)") flag.Parse() if strings.TrimSpace(*want) == "" { fmt.Fprintln(os.Stderr, "usage: npcfindname -pvf -name <中文名>") os.Exit(2) } archive, err := platformpvf.Open(*pvfPath) if err != nil { fmt.Fprintln(os.Stderr, "open:", err) os.Exit(1) } // npc.lst 是 id -> 文件名 的权威表 listText, _ := archive.ReadText("npc/npc.lst") idToFile := map[string]string{} for _, m := range regexp.MustCompile("(\\d+)\\s+`([^`]+\\.npc)`").FindAllStringSubmatch(listText, -1) { idToFile[m[2]] = m[1] } type hit struct { id string file string name string } var hits []hit // 只扫 npc/ 下的定义文件,避免全库 100 万文件 for _, f := range archive.Files() { p := strings.ToLower(f.ArchivePath) if !strings.HasPrefix(p, "npc/") || !strings.HasSuffix(p, ".npc") { continue } text, err := archive.FileText(f.Index) if err != nil { continue } m := nameRe.FindStringSubmatch(text) if m == nil { continue } if !strings.Contains(m[1], *want) { continue } id := idToFile[path.Base(f.ArchivePath)] if id == "" { id = "(未登记)" } hits = append(hits, hit{id: id, file: f.ArchivePath, name: m[1]}) } sort.Slice(hits, func(i, j int) bool { return hits[i].id < hits[j].id }) fmt.Printf("名字含 %q 的 NPC 定义:%d 条\n", *want, len(hits)) limit := len(hits) if !*all && limit > 40 { limit = 40 } for i := 0; i < limit; i++ { fmt.Printf(" id=%-10s %-34s %s\n", hits[i].id, hits[i].file, hits[i].name) } }