#commander #command-line #command-line-tool #supreme #faf #faforever #supcom

bin+lib faf-replay-parser

Supreme Commander Forged Alliance 回放解析器

7 个版本

0.5.2 2022 年 4 月 10 日
0.5.1 2021 年 3 月 23 日
0.4.0 2020 年 3 月 22 日
0.3.2 2020 年 3 月 21 日
0.3.1 2019 年 8 月 25 日

#1154 in 解析器实现

LGPL-3.0

1.5MB
2.5K SLoC

FAF 回放解析器

基于 https://github.com/FAForever/faf-scfa-replay-parser 的 Supreme Commander: Forged Alliance 回放解析器。

本项目旨在为 Supreme Commander Forged Alliance (SCFA) 回放数据格式提供一个快速的解析器。这些回放文件相对于现在的标准来说相对较小,然而,在需要解析大量回放时,性能仍然很重要。

此存储库包括作为 Rust 包的解析器实现,以及一个命令行工具。

用法

通过 Parser 类解析回放

extern crate faf_replay_parser;

use faf_replay_parser::{Parser, ParserBuilder};
use std::fs::File;
use std::io::{BufReader, Read};
use std::time::Duration;

let parser = ParserBuilder::new()
    // Skip all commands except the ones defined here
    .commands(&[
       replay_command::ADVANCE,
       replay_command::VERIFY_CHECKSUM,
    ])
    // Throw away commands right after we parse them. Setting this to `True` will
    // increase the parse time.
    .save_commands(false)
    // Parse the whole replay even if it desynced
    .stop_on_desync(false)
    .build();

// Or create a parser with default arguments (turn off save_commands though)
// let parser = Parser::new();

// Read replay file
let mut f = BufReader::new(File::open("12345.scfareplay").expect("Couldn't open file"));
let mut data = Vec::new();
f.read_to_end(&mut data).expect("Couldn't read from file");

// Parse the replay
let replay = parser
    .parse(&mut data.as_slice())
    .expect("Failed to parse replay");

println!("Game time: {:?}", Duration::from_millis(replay.body.sim.tick as u64 * 100));
if !replay.body.sim.desync_ticks.is_empty() {
    println!("Replay desynced!");
}

或使用高性能函数处理特殊情况

use faf_replay_parser::{body_offset, body_ticks};
use std::time::Duration;

// Split replay data into header and body
let offset = body_offset(&data);
let (header_data, body_data) = data.split_at(offset);

// Get replay length in ticks
let ticks = body_ticks(&body_data);
println!("Game time: {:?}", Duration::from_millis(ticks as u64 * 100));

命令行工具

免责声明:此 cli 正在开发中,其中一些部分可能目前会打印出过多的调试信息。

安装

您可以通过克隆此存储库并使用以下命令来获取 cli

$ cargo build --release --features cli

这将生成 target/release/ 中的可执行文件。将其复制到您的 $PATH 中的某个位置,例如 ~/.local/bin/

或者,Linux 二进制文件可在每个标记版本的 GitLab CI 构建工件中找到。

功能

cli 当前执行 3 个主要功能。打印回放的摘要(主要是头部信息),检查命令流的内 容,以及将 fafreplay 文件转换为 scfareplay 文件。

信息

显示回放内容的概述。还可以启用有关地图、玩家、游戏选项等信息。

$ fafreplay info 6176549.fafreplay
processing replay: 6176549.fafreplay
Supreme Commander v1.50.3698 Replay v1.9

Seton's Clutch (00:48:11)
    Dozens of battles have been fought over the years across Seton's Clutch. A patient searcher could find the remains of thousands of units resting beneath the earth and under the waves.

Team 1
    civilian (AI) 5

Team 2
    [PLD] EricaPwnz (1200) Aeon
    Vmcsnekke (1600) UEF
    [AoS] Strogo (2100) UEF
    [JEW] Licious (1600) Seraphim

Team 3
    [SNF] PlodoNoob (1800) Cybran
    Mizer (1400) Seraphim
    [SC] HerzogGorky (1400) Cybran
    [JEW] Robogear (2100) UEF

命令

显示回放文件中出现的命令。默认情况下,这仅显示最常见的命令。

$ fafreplay commands 9000556.scfareplay --limit 10
processing replay: 9000556.fafreplay
Supreme Commander v1.50.3701 Replay v1.9

0 ├── SetCommandSource { id: 0 }
1 ├── VerifyChecksum { digest: [168, 55, 122, 87, 70, 60, 17, 145, 224, 174, 52, 71, 2, 143, 109, 2], tick: 0 }
2 ├── Advance { ticks: 1 }
3 ├── Advance { ticks: 1 }
4 ├── Advance { ticks: 1 }
5 ├── Advance { ticks: 1 }
6 ├── Advance { ticks: 1 }
7 ├── Advance { ticks: 1 }
8 ├── Advance { ticks: 1 }
9 ├── Advance { ticks: 1 }

Total commands parsed: 10

解包

将 .fafreplay 文件转换为 .scfareplay 文件。

$ fafreplay unpack 9000556.fafreplay
Extracting...
Done
Writing 9000556.scfareplay
Wrote 12314246 bytes

构建

要构建命令行工具

$ cargo build --release --features cli

寻找其他语言的解析器?请查看这些其他回放解析器实现

依赖项