|
引用 | 編輯
11922911
2024-10-12 00:22 |
樓主
▼ |
||||||||||||
|
插件來源:原創 使用指令:沒有 安裝路徑:addons/amxmodx 【插件介紹】 需求: AMXX 1.9 或以上 這是跟我之前發佈的 Asset Manager 差不多的功能 (點我進帖子), 但之前的設計有些缺憾和搞得太複雜, 所以我重做了一個非OO的版本 簡單來說這就是一個 JSON 的 API 方便來讀取遊戲的資源檔案位置給插件使用 避免寫死在插件 asset_api.inc 放在 scripting/include asset_api.sma 需要加在 plugins.ini asset_api_test.sma 是測試插件 configs/test.json 是測試用的JSON #include <asset_api> 來使用 二樓會有使用範例 以下是 INC 一覽: 複製程式 // 遊戲資源類型
enum AssetType
{
Asset_Model, // 模組 (例如: .mdl, .spr)
Asset_Sound, // 音效 (例如: .wav)
Asset_Generic, // 通用 (例如: .wav, .mp3, .txt, ...)
Asset_PlayerModel // 玩家模型
};
複製程式 /* * 會在使用 asset_loadJson 或 asset_passJson 時被呼叫 * * @param json JSON 物件 * @param name 識別名字 * @param filePath 檔案位置 * * @noreturn */ forward asset_OnHandleJson(JSON:json, const name[], const filePath[]); 複製程式 /* * 載入 JSON * * @param name 用以識別的名字 * @param filePath 要讀取的檔案位置 * @param relativePath 如果是 true 使用相對的位置 (amxmodx/configs) 否則將使用絕對位置 * * @return 0 代表載入失敗, 1 代表載入成功 */ native asset_loadJson(const name[], const filePath[], bool:relativePath=true); 複製程式 /* * 傳遞 JSON 物件到 asset_OnHandleJson (forward 會被呼叫一次) * * @note 用這個如果你的JSON物件之前已經載入了一次 * * @param json JSON 物件 * @param name 用以識別的名字 * @param filePath 檔案位置 (可留空) * * @noreturn */ native asset_passJson(JSON:json, const name[], const filePath[]=""); 複製程式 /* * 讀取 JSON 物件的遊戲資源到字串 (會自動預載) * * @param type 遊戲資源的類型 * @param json JSON 物件 * @param key 要被讀取的鍵值 * @param string 輸出到的字串 * @param length 字串長度 * @param defaultFile 當 JSON 物件沒有讀取到東西就使用這個檔案 (可留空) * @param dotNotation JSON 使用 dot notation * @param useValveFs 使用 valve 的檔案系統 (意思是包含 valve/ 裡面的檔案) * * @return 回傳 precache 的 id (0 為失敗) */ stock asset_toString( AssetType:type, JSON:json, const key[], string[]="", length=0, const defaultFile[]="", bool:dotNotation=true, bool:useValveFs=true) 複製程式 /* * 讀取 JSON 物件的遊戲資源到動態陣列 (會自動預載) * * @param type 遊戲資源的類型 * @param json JSON 物件 * @param key 要被讀取的鍵值 * @param stringLength 字串長度 (ArrayCreate) * @param defaultFile 當 JSON 物件沒有讀取到東西就使用這個檔案 (可留空) * @param dotNotation JSON 使用 dot notation * @param useValveFs 使用 valve 的檔案系統 (意思是包含 valve/ 裡面的檔案) * * @return 回傳 Array: 的 handle */ stock Array:asset_toArray( AssetType:type, JSON:json, const key[], stringLength, const defaultFile[]="", bool:dotNotation=true, bool:useValveFs=true) 複製程式 /* * 預載遊戲資源 * * @param type 遊戲資源的類型 * @param file 檔案位置 * @param useValveFs 使用 valve 的檔案系統 (意思是包含 valve/ 裡面的檔案) * * @return 回傳 precache 的 id */ stock asset_precache(AssetType:type, const file[], bool:useValveFs=true)
|
|||||||||||||
|
引用 | 編輯
11922911
2024-10-12 00:22 |
1樓
▲ ▼ |
|
使用範例:
插件 複製程式 #include <amxmodx>
#include <cstrike>
#include <engine>
#include <fakemeta>
#include <hamsandwich>
#include <asset_api>
new Array:g_soundSpk;
new g_soundEmit[64];
new g_playerModel[32];
new g_knifeModel[64];
new g_sprDisk;
public plugin_precache()
{
// 載入 JSON
// 若載入失敗它也會至少以 Invalid_JSON 來呼叫一次 asset_OnHandleJson 來預載預設的資源檔案
asset_loadJson("test", "test.json")
}
public asset_OnHandleJson(JSON:json, const name[])
{
// 檢查識別的名字
if (!equal(name, "test")) return;
// 載入 spk 音效
g_soundSpk = asset_toArray(Asset_Generic, json, "spk", 64,
.defaultFile="sound/events/enemy_died.wav");
// 載入 emit 音效
asset_toString(Asset_Sound, json, "emit", g_soundEmit, charsmax(g_soundEmit),
.defaultFile="player/headshot1.wav");
// 載入玩家模組
asset_toString(Asset_PlayerModel, json, "playermodel", g_playerModel, charsmax(g_playerModel),
.defaultFile="terror");
// 載入小刀模組
asset_toString(Asset_Model, json, "v_knife", g_knifeModel, charsmax(g_knifeModel),
.defaultFile="models/v_knife.mdl");
// 載入 SPR
g_sprDisk = asset_toString(Asset_Model, json, "spr_disk",
.defaultFile="sprites/zbeam1.spr");
}
public plugin_init()
{
register_plugin("Asset API Test", "0.1", "holla");
register_clcmd("test_spk", "CmdTestSpk");
register_clcmd("test_emit", "CmdTestEmit");
register_clcmd("test_spr", "CmdTestSpr");
RegisterHam(Ham_Spawn, "player", "OnPlayerSpawn_Post", 1);
RegisterHam(Ham_Item_Deploy, "weapon_knife", "OnKnifeDeploy_Post", 1);
}
public CmdTestSpk()
{
ARRAY_RANDOM_STR(g_soundSpk, sound[64]) // 獲得隨機音效 (這是 INC 裡面的 macro)
client_cmd(0, "spk %s", sound); // 用 spk 方式播放
}
public CmdTestEmit(id)
{
// 用 emit_sound 播放音效在玩家身上
emit_sound(id, CHAN_AUTO, g_soundEmit, VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
}
public CmdTestSpr(id)
{
new origin[3];
get_user_origin(id, origin, 3);
origin[2] += 1;
message_begin(MSG_BROADCAST ,SVC_TEMPENTITY) //message begin
write_byte(TE_BEAMDISK)
write_coord(origin[0]) // center position
write_coord(origin[1])
write_coord(origin[2])
write_coord(origin[0]) // axis and radius
write_coord(origin[1])
write_coord(origin[2] + 250)
write_short(g_sprDisk) // sprite index
write_byte(0) // starting frame
write_byte(0) // frame rate in 0.1's
write_byte(10) // life in 0.1's
write_byte(10) // line width in 0.1's
write_byte(0) // noise amplitude in 0.01's
write_byte(0) //colour
write_byte(100)
write_byte(200)
write_byte(255) // brightness
write_byte(0) // scroll speed in 0.1's
message_end()
}
public OnPlayerSpawn_Post(id)
{
if (!is_user_alive(id)) return;
cs_set_user_model(id, g_playerModel); // 改變玩家模組
}
public OnKnifeDeploy_Post(ent)
{
if (!is_valid_ent(ent)) return;
new id = get_ent_data_entity(ent, "CBasePlayerItem", "m_pPlayer");
if (id)
entity_set_string(id, EV_SZ_viewmodel, g_knifeModel); // 改變小刀模組
}
測試用的 JSON 設定檔 複製程式 {
"spk": ["sound/hostage/hos1.wav", "sound/hostage/hos2.wav", "sound/hostage/hos3.wav"],
"emit": "scientist/scream1.wav",
"playermodel": "vip",
"v_knife": "models/v_knife_r.mdl",
"spr_disk": "sprites/laserbeam.spr"
}
假如你的東西在JSON是寫了在一個物件裡面, 像這樣 複製程式 {
"object" : {
"test_model" : "models/head.mdl"
}
}
因為支援 dot notation, 所以你可以這樣寫 複製程式 asset_toString(Asset_Model, json, "object.test_model", g_testModel, charsmax(g_testModel)); |
|