amxmodx-basics

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

AMX Mod X (Pawn) Code Style Guide

AMX Mod X (Pawn) 代码风格指南

This document provides an overview of coding conventions for AMX Mod X projects. Each topic is covered in detail in its own file.
本文档概述了AMX Mod X项目的编码规范,每个主题的详细内容都在独立文件中。

Core Conventions

核心规范

CategoryDescription
BasicsSyntax, basics
Code StyleFile structure, formatting, braces, spacing, plugin registration
Naming ConventionsHungarian notation, variable prefixes, naming patterns
Constants & EnumsDefine constants, enums, TASKID constants
MacrosCommon macros, IS_PLAYER, patterns to avoid
Function DeclarationsReturn types, @ prefix, static variables
ValidationsFM_NULLENT, entity checks, player validation, early returns
分类描述
基础语法、基础知识
代码风格文件结构、格式、大括号、间距、插件注册
命名规范匈牙利命名法、变量前缀、命名模式
常量与枚举定义常量、枚举、TASKID常量
常用宏、IS_PLAYER、需避免的模式
函数声明返回类型、@前缀、静态变量
验证FM_NULLENT、实体检查、玩家验证、提前返回

API Patterns

API模式

CategoryDescription
HooksHam, FakeMeta, ReAPI, Event, Message hooks and handles
ForwardsCreateMultiForward, ExecuteForward, pre/post patterns
NativesNative registration and implementation
CallbacksTasks, SQL queries, CVar queries
MenusMenu creation, callbacks, and patterns
CommandsClient, server, and console commands
CVarsCVar creation, binding, and change hooks
分类描述
钩子Ham、FakeMeta、ReAPI、事件、消息钩子及句柄
转发CreateMultiForward、ExecuteForward、前置/后置模式
原生函数原生函数注册与实现
回调任务、SQL查询、CVar查询
菜单菜单创建、回调及模式
命令客户端、服务器及控制台命令
CVarCVar创建、绑定及变更钩子

Performance & Data

性能与数据

CategoryDescription
OptimizationsNative call reduction, dynamic hooks, model path caching
Data StructuresArrays, Tries, entity access, strings

分类描述
优化减少原生函数调用、动态钩子、模型路径缓存
数据结构数组、Trie、实体访问、字符串

Custom API Reference

自定义API参考

For project-specific APIs, see dedicated skill files:
APIDescription
assetsAsset management from JSON configs
custom-entitiesOOP-style custom entities
custom-eventsPub/sub event system
custom-weaponsCustom weapon framework
entity-forcePhysics force application
entity-grabEntity grab and carry
player-cameraCustom camera views
player-modelCustom player models
player-musicMP3 music playback
player-rolesPlayer role management
roundsRound management
shopsIn-game shop system
statesState machine implementation
For large project organization with namespace constants, see amxx-modding-kit-project.

针对项目特定API,请查看专用技能文件:
API描述
assets基于JSON配置的资源管理
custom-entities面向对象风格的自定义实体
custom-events发布/订阅事件系统
custom-weapons自定义武器框架
entity-force物理力施加
entity-grab实体抓取与携带
player-camera自定义相机视角
player-model自定义玩家模型
player-musicMP3音乐播放
player-roles玩家角色管理
rounds回合管理
shops游戏内商店系统
states状态机实现
如需使用命名空间常量进行大型项目组织,请查看amxx-modding-kit-project

Quick Reference: Best Practices Summary

快速参考:最佳实践摘要

Code Style

代码风格

  1. Always use
    #pragma semicolon 1
    at file start
  2. Use Hungarian notation for all variable names
  3. Use K&R brace style (opening brace on same line)
  4. Pass plugin info directly to
    register_plugin()
    - avoid macros
  5. Group hooks and functions with section comments
  1. 始终在文件开头使用
    #pragma semicolon 1
  2. 所有变量名使用匈牙利命名法
  3. 使用K&R大括号风格(左大括号与代码同行)
  4. 直接将插件信息传入
    register_plugin()
    - 避免使用宏
  5. 使用分段注释对钩子和函数进行分组

Constants & Validation

常量与验证

  1. Use
    FM_NULLENT
    instead of
    -1
    or
    0
    for null entity checks
  2. Always check
    FM_NULLENT
    after entity creation before processing
  3. Return
    FM_NULLENT
    not
    0
    for failures -
    0
    is worldspawn
  4. Return early for invalid conditions
  1. 检查空实体时,使用
    FM_NULLENT
    而非
    -1
    0
  2. 实体创建后,在处理前务必检查
    FM_NULLENT
  3. 失败时返回
    FM_NULLENT
    而非
    0
    -
    0
    代表worldspawn(世界实体)
  4. 遇到无效条件时提前返回

Macros

  1. Don't redefine macros - use shared definitions from includes
  2. Never use
    MACRO()_Suffix
    patterns in code - only in macro definitions
  1. 不要重定义宏 - 使用头文件中的共享定义
  2. 代码中绝不要使用
    MACRO()_Suffix
    模式 - 仅在宏定义中使用

Function Declarations

函数声明

  1. Use
    @
    prefix only for OOP-like methods:
    @{EntityName}_{MethodName}
  2. Never add
    public
    keyword to
    @
    prefixed functions
  3. Use
    const
    prefix for all handle arguments
  4. Use
    const &this
    for "this" argument in OOP-like methods
  5. Use static variables in frequently called class methods
  6. Declare and assign static on same line using semicolon separator
  1. 仅在类对象风格的方法中使用
    @
    前缀:
    @{EntityName}_{MethodName}
  2. @
    前缀的函数绝不要添加
    public
    关键字
  3. 所有句柄参数使用
    const
    前缀
  4. 类对象风格的方法中,为“this”参数使用
    const &this
  5. 频繁调用的类方法中使用静态变量
  6. 静态变量的声明与赋值在同一行,使用分号分隔

Hooks

钩子

  1. Ham hooks must return
    HAM_*
    constants
  2. FakeMeta hooks must return
    FMRES_*
    constants
  3. ReAPI hooks must return
    HC_*
    constants
  4. Message hooks must return
    PLUGIN_*
    constants
  5. Hook handle variables:
    g_pfwham
    (Ham),
    g_pfwfm
    (FakeMeta)
  6. Inline
    get_user_msgid
    when only used once in
    register_message
  1. Ham钩子必须返回
    HAM_*
    常量
  2. FakeMeta钩子必须返回
    FMRES_*
    常量
  3. ReAPI钩子必须返回
    HC_*
    常量
  4. 消息钩子必须返回
    PLUGIN_*
    常量
  5. 钩子句柄变量:
    g_pfwham
    (Ham)、
    g_pfwfm
    (FakeMeta)
  6. get_user_msgid
    仅在
    register_message
    中使用一次,则内联调用

Forwards

转发

  1. Forward names use
    LibraryName_OnSomething
    - not
    Fw_
    prefix
  2. Multi-forward handle variables use
    g_pfw
    prefix
  1. 转发名称使用
    LibraryName_OnSomething
    格式 - 不要使用
    Fw_
    前缀
  2. 多转发句柄变量使用
    g_pfw
    前缀

Natives & Callbacks

原生函数与回调

  1. Native implementations use
    Native_
    prefix with
    const
    for arguments
  2. Task callbacks use
    Task_
    prefix with offset in task ID
  3. SQL callbacks use
    Callback_SQLQuery_{Name}
    prefix
  1. 原生函数实现使用
    Native_
    前缀,参数使用
    const
  2. 任务回调使用
    Task_
    前缀,任务ID中包含偏移量
  3. SQL回调使用
    Callback_SQLQuery_{Name}
    前缀

Menus

菜单

  1. Menu callbacks use
    Callback_Menu_{Name}
    prefix
  2. Use enum for menu items to avoid index mistakes
  3. Destroy dynamic menus in callback with
    menu_destroy
  1. 菜单回调使用
    Callback_Menu_{Name}
    前缀
  2. 使用枚举定义菜单项,避免索引错误
  3. 在回调中使用
    menu_destroy
    销毁动态菜单

Commands & CVars

命令与CVar

  1. Command handlers use
    Command_
    prefix, server commands use
    ServerCommand_
  2. Use
    register_concmd
    for admin commands that should work from RCON
  3. Avoid
    client_cmd
    - use
    engclient_cmd
    for server-side execution
  4. Use
    create_cvar
    instead of deprecated
    register_cvar
  5. Use
    bind_pcvar_*
    instead of
    get_pcvar_*
    for cvar access
  1. 命令处理函数使用
    Command_
    前缀,服务器命令使用
    ServerCommand_
    前缀
  2. 对于可通过RCON执行的管理员命令,使用
    register_concmd
  3. 避免使用
    client_cmd
    - 服务器端执行时使用
    engclient_cmd
  4. 使用
    create_cvar
    替代已废弃的
    register_cvar
  5. 访问CVar时使用
    bind_pcvar_*
    而非
    get_pcvar_*

Optimizations

优化

  1. Minimize native calls in hot paths
  2. Use single global
    g_pTrace
    - never create trace handles per-call
  3. Register high-frequency hooks dynamically
  4. Use
    xs_vec_set
    instead of
    xs_vec_copy
    for vector initialization
  5. Cache model index with static inside function, not in separate global
  1. 在热点路径中尽量减少原生函数调用
  2. 使用单个全局
    g_pTrace
    - 绝不要每次调用都创建追踪句柄
  3. 动态注册高频钩子
  4. 初始化向量时使用
    xs_vec_set
    而非
    xs_vec_copy
  5. 在函数内部使用静态变量缓存模型索引,而非单独的全局变量

Naming Conventions

命名规范

  1. Sound arrays use
    g_rgsz
    prefix with
    g_i*Num
    counter
  2. Message IDs use
    gmsg
    prefix (without underscore)
  3. Player arrays use
    g_rg*[MAX_PLAYERS + 1]
    with type prefix
  4. Don't use
    id
    for players - use
    pPlayer
  5. Don't use
    index
    for entities - use
    pEntity
  1. 声音数组使用
    g_rgsz
    前缀,搭配
    g_i*Num
    计数器
  2. 消息ID使用
    gmsg
    前缀(无下划线)
  3. 玩家数组使用
    g_rg*[MAX_PLAYERS + 1]
    格式,带类型前缀
  4. 不要用
    id
    指代玩家 - 使用
    pPlayer
  5. 不要用
    index
    指代实体 - 使用
    pEntity

Data Structures

数据结构

  1. Always free dynamic handles -
    ArrayDestroy
    ,
    TrieDestroy
    in
    plugin_end()
  1. 务必释放动态句柄 - 在
    plugin_end()
    中调用
    ArrayDestroy
    TrieDestroy