Files
Starlight_Lancher/packages/app-lib/src/api/instance/synced_servers/codec.rs
T
AxTps ed61a5942c
Sync source to CNB / Sync Git ref (push) Has been cancelled
feat: 跨实例同步移植 WIP 检查点(第1轮+2a+2b+synced_servers)
从 Axolotl 移植跨实例同步子系统,当前进度:

- 第1轮:synced_options/files.rs + mod.rs 骨架,state 层补齐 SyncedOption/InstanceSyncedOptions/CanonicalValue/GameOptionKind 等

- 2a:game_options 的 api_types/options_file/catalog(10文件)

- 2b:game_options 除 resource_pack_options/local_settings_editor 外的全部(settings_editor/write_shared_settings/fullscreen/launch_overrides/instance_support/read_instance_changes/pack_updates/source_selection + locales/5文件);State 补 synced_options_lock/game_locale_indexer/lock_synced_options

- synced_servers:6 文件全部搬入

- 35+20 处 sqlx::query! 宏改写为非宏 query+bind

已知未完成:synced_servers::operations 依赖 synced_options::instance_option_supported(属 orchestration,第5轮),当前编译不通。
2026-10-03 22:18:37 +08:00

113 lines
3.0 KiB
Rust

use super::super::synced_options::{
nbt_from_bytes, nbt_to_bytes, read_nbt_file,
};
use crate::ErrorKind;
use quartz_nbt::{NbtCompound, NbtList, NbtTag};
use std::path::Path;
pub(crate) async fn read_servers(
path: &Path,
) -> crate::Result<Vec<NbtCompound>> {
if !path.exists() {
return Ok(Vec::new());
}
servers_from_root(read_nbt_file(path).await?)
}
pub(super) fn servers_from_bytes(
bytes: Vec<u8>,
) -> crate::Result<Vec<NbtCompound>> {
servers_from_root(nbt_from_bytes(bytes)?)
}
fn servers_from_root(root: NbtCompound) -> crate::Result<Vec<NbtCompound>> {
let list = root.get::<_, &NbtList>("servers").map_err(|_| {
ErrorKind::InputError(
"servers.dat does not contain a valid servers list".to_string(),
)
})?;
list.iter()
.map(|tag| match tag {
NbtTag::Compound(compound) => Ok(compound.clone()),
_ => Err(ErrorKind::InputError(
"servers.dat contains an invalid server entry".to_string(),
)
.into()),
})
.collect()
}
pub(crate) async fn write_servers(
path: &Path,
servers: &[NbtCompound],
) -> crate::Result<()> {
if let Some(parent) = path.parent() {
crate::util::io::create_dir_all(parent).await?;
}
crate::util::io::write(path, servers_to_bytes(servers)?).await?;
Ok(())
}
pub(super) fn servers_to_bytes(
servers: &[NbtCompound],
) -> crate::Result<Vec<u8>> {
let mut list = NbtList::new();
for server in servers {
list.push(server.clone());
}
let mut root = NbtCompound::new();
root.insert("servers", list);
nbt_to_bytes(&root)
}
pub(crate) fn server_data(
name: String,
address: String,
accept_textures: Option<bool>,
) -> NbtCompound {
let mut server = NbtCompound::new();
server.insert("name", name);
server.insert("ip", address);
if let Some(accept_textures) = accept_textures {
server.insert("acceptTextures", i8::from(accept_textures));
}
server.insert("hidden", 0_i8);
server
}
pub(super) fn update_server_data(
server: &mut NbtCompound,
name: String,
address: String,
accept_textures: Option<bool>,
) {
server.insert("name", name);
server.insert("ip", address);
match accept_textures {
Some(value) => server.insert("acceptTextures", i8::from(value)),
None => {
server.inner_mut().remove("acceptTextures");
}
}
}
pub(super) fn server_hidden(server: &NbtCompound) -> bool {
server.get::<_, i8>("hidden").unwrap_or(0) != 0
}
pub(super) fn server_address(server: &NbtCompound) -> String {
server.get::<_, &str>("ip").unwrap_or_default().to_string()
}
pub(super) fn server_identity_address(server: &NbtCompound) -> String {
server_address(server).trim().to_ascii_lowercase()
}
pub(super) fn server_identity_name(server: &NbtCompound) -> String {
server
.get::<_, &str>("name")
.unwrap_or_default()
.trim()
.to_ascii_lowercase()
}