#mod #io #async-io #client #modding #api-client #api-bindings

modio

Rust集成mod.io的接口,mod.io是为游戏开发者提供的模组API。

28个版本

0.11.0 2024年6月4日
0.10.0 2024年3月21日
0.9.1 2023年11月12日
0.8.0 2023年6月26日
0.3.0 2018年10月4日

#22 in HTTP客户端

Download history 7/week @ 2024-04-26 1/week @ 2024-05-03 3/week @ 2024-05-17 2/week @ 2024-05-24 177/week @ 2024-05-31 25/week @ 2024-06-07 3/week @ 2024-06-14 13/week @ 2024-06-28 3/week @ 2024-07-05 5/week @ 2024-07-12

1,740每月下载
用于modbot

MIT/Apache

275KB
7K SLoC

mod.io

Crates.io Released API docs Master API docs Rust version License Workflow Status

modio提供了一组用于与mod.io API交互的构建块。

客户端使用异步I/O,由futurestokiocrate支持,需要同时使用这两个crate。

mod.io

mod.io是由ModDB.com的创始人推出的即插即用模组解决方案,它简化了游戏中模组的上传、搜索、浏览、下载和交易。

用法

要使用modio,执行cargo add modio

基本设置

use modio::{Credentials, Modio, Result};

#[tokio::main]
async fn main() -> Result<()> {
    let modio = Modio::new(
        Credentials::new("user-or-game-apikey"),
    )?;

    // create some tasks and execute them
    // let result = task.await?;
    Ok(())
}

身份验证

// Request a security code be sent to the email address.
modio.auth().request_code("[email protected]").await?;

// Wait for the 5-digit security code
let token = modio.auth().security_code("QWERT").await?;

// Create an endpoint with the new credentials
let modio = modio.with_credentials(token);

请参阅完整示例

游戏

use modio::filter::prelude::*;

// List games with filter `name_id = "0ad"`
let games = modio.games().search(NameId::eq("0ad")).collect().await?;

模组

use modio::filter::prelude::*;

// List all mods for 0 A.D.
let mods = modio.game(5).mods().search(Filter::default()).collect().await?;

// Get the details of the `balancing-mod` mod
let balancing_mod = modio.mod_(5, 110).get().await?;

流搜索结果

use futures::TryStreamExt;

let filter = Fulltext::eq("tftd").limit(10);
let mut st = modio.game(51).mods().search(filter).paged().await?;
let (_page_count, _) = st.size_hint();

// Stream of paged results `Page<Mod>` with page size = 10
while let Some(page) = st.try_next().await? {
    println!("Page {}/{} - Items #{}", page.current(), page.page_count(), page.len());
    for item in page {
        println!("  {}. {}", item.id, item.name);
    }
}

let filter = Fulltext::eq("soldier");
let mut st = modio.game(51).mods().search(filter).iter().await?;
let (_total, _) = st.size_hint();

// Stream of `Mod`
while let Some(mod_) = st.try_next().await? {
    println!("{}. {}", mod_.id, mod_.name);
}

下载

use future_util::{future, TryStreamExt};
use modio::download::{ResolvePolicy, DownloadAction};

// Download the primary file of a mod.
let action = DownloadAction::Primary {
    game_id: 5,
    mod_id: 19,
};
modio
    .download(action)
    .await?
    .save_to_file("mod.zip")
    .await?;

// Download the specific file of a mod.
let action = DownloadAction::File {
    game_id: 5,
    mod_id: 19,
    file_id: 101,
};
modio
    .download(action)
    .await?.save_to_file("mod.zip")
    .await?;

// Download the specific version of a mod.
// if multiple files are found then the latest file is downloaded.
// Set policy to `ResolvePolicy::Fail` to return with `modio::download::Error::MultipleFilesFound`
// as source error.
let action = DownloadAction::Version {
    game_id: 5,
    mod_id: 19,
    version: "0.1".to_string(),
    policy: ResolvePolicy::Latest,
};
modio
    .download(action)
    .await?
    .stream()
    .try_for_each(|bytes| {
        println!("bytes: {:?}")
        future::ok(())
    })
    .await?;

示例

请参阅示例目录以获取一些入门示例。

许可

许可方式为以下之一

贡献

除非您明确声明,否则任何有意提交给作品并由您定义的Apache-2.0许可证包含的贡献,都应按照上述方式双重许可,不得添加任何附加条款或条件。

依赖项

~6–18MB
~255K SLoC