廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 244 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
11922911
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x42 鮮花 x88
分享: 轉寄此文章 Facebook Plurk Twitter 版主評分版主評分版主評分 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x2
[插件] 方便從 JSON 讀取遊戲資源檔案位置的 API (非OO版本)  (避免寫死檔案位置)
【插件資訊】

插件來源:原創
使用指令:沒有
安裝路徑: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_OnParseJson(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_OnParseJson (forward 會被呼叫一次)
 *
 * @note 當你不想用 asset_loadJson 來幫你載入 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)


本帖包含附件
zip asset_api.zip   (2024-10-12 00:22 / 5 KB)   下載次數:1


[ 此文章被11922911在2024-10-14 01:42重新編輯 ]

此文章被評分,最近評分記錄
財富:500 (by amore12) | 理由: 辛苦了!!



YouTube: @holla16
獻花 x2 回到頂端 [樓 主] From:香港沒有資料 | Posted:2024-10-12 00:22 |
11922911
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x42 鮮花 x88
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

使用範例:

插件
複製程式
#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];

public plugin_precache()
{
       // 載入 JSON
       if (!asset_loadJson("test", "test.json"))
              set_fail_state("failed to load json file"); // 若載入失敗則停止插件
}

// 載入的處理
public asset_OnParseJson(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");

       // 因為 JSON 物件被傳遞了, 所以在這裡你還是可以對 json 做一般正常的處理
}

public plugin_init()
{
       register_plugin("Asset API Test", "0.1", "holla");

       register_clcmd("test_spk",        "CmdTestSpk");
       register_clcmd("test_emit", "CmdTestEmit");

       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]) // 獲得隨機音效
       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 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"
}




另外如果是想用在 SVC_TEMPENTITY 的 SPR, 可以這樣寫
複製程式
new g_spr;

public asset_OnParseJson(JSON:json, const name[])
{
       if (!equal(name, "test")) return;
       g_spr = asset_toString(Asset_Model, json, "spr");
}

JSON:
複製程式
    "spr": "sprites/zbeam4.spr"




假如你的東西在JSON是寫了在一個物件裡面, 像這樣
複製程式
{
       "object" : {
              "test_model" : "models/head.mdl"
       }
}

因為支援 dot notation, 所以你可以這樣寫
複製程式
asset_toString(Asset_Model, json, "object.test_model", g_testModel, charsmax(g_testModel));


[ 此文章被11922911在2024-10-12 09:15重新編輯 ]


YouTube: @holla16
獻花 x0 回到頂端 [1 樓] From:香港沒有資料 | Posted:2024-10-12 00:22 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.029903 second(s),query:16 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言