Spaces:
Sleeping
Sleeping
Delete src/processor.rs
Browse files- src/processor.rs +0 -173
src/processor.rs
DELETED
|
@@ -1,173 +0,0 @@
|
|
| 1 |
-
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
|
| 2 |
-
use flate2::{Compression, write::ZlibEncoder};
|
| 3 |
-
use serde::Serialize;
|
| 4 |
-
use serde_json::{Value as JsonValue, json};
|
| 5 |
-
use std::{collections::HashMap, io::Write};
|
| 6 |
-
|
| 7 |
-
pub fn nbt_to_json(value: fastnbt::Value) -> JsonValue {
|
| 8 |
-
match value {
|
| 9 |
-
fastnbt::Value::Compound(map) => {
|
| 10 |
-
let json_map = map
|
| 11 |
-
.into_iter()
|
| 12 |
-
.map(|(k, v)| (k, nbt_to_json(v)))
|
| 13 |
-
.collect::<serde_json::Map<_, _>>();
|
| 14 |
-
JsonValue::Object(json_map)
|
| 15 |
-
}
|
| 16 |
-
fastnbt::Value::List(list) => JsonValue::Array(list.into_iter().map(nbt_to_json).collect()),
|
| 17 |
-
fastnbt::Value::ByteArray(arr) => {
|
| 18 |
-
JsonValue::Array(arr.into_iter().map(|x| json!(x)).collect())
|
| 19 |
-
}
|
| 20 |
-
fastnbt::Value::IntArray(arr) => {
|
| 21 |
-
JsonValue::Array(arr.into_iter().map(|x| json!(x)).collect())
|
| 22 |
-
}
|
| 23 |
-
fastnbt::Value::LongArray(arr) => {
|
| 24 |
-
JsonValue::Array(arr.to_vec().into_iter().map(|x| json!(x)).collect())
|
| 25 |
-
}
|
| 26 |
-
fastnbt::Value::Byte(x) => json!(x),
|
| 27 |
-
fastnbt::Value::Short(x) => json!(x),
|
| 28 |
-
fastnbt::Value::Int(x) => json!(x),
|
| 29 |
-
fastnbt::Value::Long(x) => json!(x),
|
| 30 |
-
fastnbt::Value::Float(x) => json!(x),
|
| 31 |
-
fastnbt::Value::Double(x) => json!(x),
|
| 32 |
-
fastnbt::Value::String(x) => json!(x),
|
| 33 |
-
}
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
/// Сжатие массива байт в zlib и кодирование в Base64
|
| 37 |
-
pub fn compress_and_encode(data: &[u8]) -> Result<String, String> {
|
| 38 |
-
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
| 39 |
-
encoder.write_all(data).map_err(|e| e.to_string())?;
|
| 40 |
-
encoder.flush().map_err(|e| e.to_string())?;
|
| 41 |
-
let compressed = encoder.finish().map_err(|e| e.to_string())?;
|
| 42 |
-
Ok(BASE64.encode(&compressed))
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
/// Универсальная функция для нарезки JSON-данных (палитры, энтити)
|
| 46 |
-
/// limit_chars — приблизительный лимит символов JSON-строки в одном чанке
|
| 47 |
-
pub fn chunk_json_to_b64<T: Serialize>(
|
| 48 |
-
items: Vec<T>,
|
| 49 |
-
limit_chars: usize,
|
| 50 |
-
) -> Result<Vec<String>, String> {
|
| 51 |
-
let mut chunks = Vec::new();
|
| 52 |
-
let mut current_chunk = Vec::new();
|
| 53 |
-
let mut sum_serialized_len = 0; // Сумма длин JSON-строк всех элементов в текущем чанке
|
| 54 |
-
|
| 55 |
-
for item in items {
|
| 56 |
-
let json_str = serde_json::to_string(&item).map_err(|e| e.to_string())?;
|
| 57 |
-
let item_len = json_str.len();
|
| 58 |
-
|
| 59 |
-
let current_count = current_chunk.len();
|
| 60 |
-
let commas = if current_count > 0 { current_count } else { 0 };
|
| 61 |
-
let estimated_total_len = 2 + commas + sum_serialized_len + item_len;
|
| 62 |
-
|
| 63 |
-
if estimated_total_len > limit_chars && !current_chunk.is_empty() {
|
| 64 |
-
// Лимит превышен, сериализуем текущий чанк
|
| 65 |
-
let chunk_bytes = serde_json::to_vec(¤t_chunk).map_err(|e| e.to_string())?;
|
| 66 |
-
chunks.push(compress_and_encode(&chunk_bytes)?);
|
| 67 |
-
|
| 68 |
-
// Сбрасываем состояние для нового чанка
|
| 69 |
-
current_chunk.clear();
|
| 70 |
-
sum_serialized_len = 0;
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
// Добавляем элемент в текущий чанк
|
| 74 |
-
sum_serialized_len += item_len;
|
| 75 |
-
current_chunk.push(item);
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
// Записываем остаток, если он есть
|
| 79 |
-
if !current_chunk.is_empty() {
|
| 80 |
-
let chunk_bytes = serde_json::to_vec(¤t_chunk).map_err(|e| e.to_string())?;
|
| 81 |
-
chunks.push(compress_and_encode(&chunk_bytes)?);
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
Ok(chunks)
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
pub fn extract_litematic_data(
|
| 88 |
-
nbt_value: fastnbt::Value,
|
| 89 |
-
) -> Result<(Vec<JsonValue>, JsonValue, Vec<(String, String)>, Vec<i64>), String> {
|
| 90 |
-
let mut root_map = match nbt_value {
|
| 91 |
-
fastnbt::Value::Compound(map) => map,
|
| 92 |
-
_ => return Err("Корень NBT не является Compound".into()),
|
| 93 |
-
};
|
| 94 |
-
|
| 95 |
-
let mut regions_map = match root_map.remove("Regions") {
|
| 96 |
-
Some(fastnbt::Value::Compound(map)) => map,
|
| 97 |
-
_ => return Err("Не найдена секция Regions".into()),
|
| 98 |
-
};
|
| 99 |
-
|
| 100 |
-
let (_, first_region) = regions_map
|
| 101 |
-
.into_iter()
|
| 102 |
-
.next()
|
| 103 |
-
.ok_or("Секция Regions пуста")?;
|
| 104 |
-
let mut region_map = match first_region {
|
| 105 |
-
fastnbt::Value::Compound(map) => map,
|
| 106 |
-
_ => return Err("Регион не является Compound".into()),
|
| 107 |
-
};
|
| 108 |
-
|
| 109 |
-
let palette_val = region_map
|
| 110 |
-
.remove("BlockStatePalette")
|
| 111 |
-
.ok_or("Нет BlockStatePalette")?;
|
| 112 |
-
let palette_vec = match palette_val {
|
| 113 |
-
fastnbt::Value::List(list) => {
|
| 114 |
-
let mut cleaned = Vec::with_capacity(list.len());
|
| 115 |
-
for item in list {
|
| 116 |
-
if let fastnbt::Value::Compound(mut cmap) = item {
|
| 117 |
-
if let Some(fastnbt::Value::String(name)) = cmap.remove("Name") {
|
| 118 |
-
let mut clean_name =
|
| 119 |
-
name.strip_prefix("minecraft:").unwrap_or(&name).to_string();
|
| 120 |
-
if let Some(fastnbt::Value::Compound(props)) = cmap.remove("Properties") {
|
| 121 |
-
let mut prop_list: Vec<String> = props
|
| 122 |
-
.into_iter()
|
| 123 |
-
.filter_map(|(k, v)| {
|
| 124 |
-
if let fastnbt::Value::String(s) = v {
|
| 125 |
-
Some(format!("{}={}", k, s))
|
| 126 |
-
} else {
|
| 127 |
-
None
|
| 128 |
-
}
|
| 129 |
-
})
|
| 130 |
-
.collect();
|
| 131 |
-
if !prop_list.is_empty() {
|
| 132 |
-
prop_list.sort();
|
| 133 |
-
clean_name = format!("{}[{}]", clean_name, prop_list.join(","));
|
| 134 |
-
}
|
| 135 |
-
}
|
| 136 |
-
cleaned.push(json!(clean_name));
|
| 137 |
-
}
|
| 138 |
-
}
|
| 139 |
-
}
|
| 140 |
-
cleaned
|
| 141 |
-
}
|
| 142 |
-
_ => return Err("BlockStatePalette не является списком".into()),
|
| 143 |
-
};
|
| 144 |
-
|
| 145 |
-
let size_json = nbt_to_json(region_map.remove("Size").ok_or("Нет Size")?);
|
| 146 |
-
|
| 147 |
-
let mut entities_vec = Vec::new();
|
| 148 |
-
if let Some(entities_val) = region_map
|
| 149 |
-
.remove("TileEntities")
|
| 150 |
-
.or_else(|| region_map.remove("BlockEntities"))
|
| 151 |
-
{
|
| 152 |
-
if let fastnbt::Value::List(list) = entities_val {
|
| 153 |
-
for item in list {
|
| 154 |
-
if let fastnbt::Value::Compound(map) = item {
|
| 155 |
-
let x = map.get("x").and_then(|v| v.as_i64()).unwrap_or(0);
|
| 156 |
-
let y = map.get("y").and_then(|v| v.as_i64()).unwrap_or(0);
|
| 157 |
-
let z = map.get("z").and_then(|v| v.as_i64()).unwrap_or(0);
|
| 158 |
-
let json_str = nbt_to_json(fastnbt::Value::Compound(map)).to_string();
|
| 159 |
-
entities_vec.push((format!("{},{},{}", x, y, z), json_str));
|
| 160 |
-
}
|
| 161 |
-
}
|
| 162 |
-
}
|
| 163 |
-
}
|
| 164 |
-
// Сортируем энтити по координатам для консистентного чанкинга
|
| 165 |
-
entities_vec.sort_by(|a, b| a.0.cmp(&b.0));
|
| 166 |
-
|
| 167 |
-
let block_states = match region_map.remove("BlockStates").ok_or("Нет BlockStates")? {
|
| 168 |
-
fastnbt::Value::LongArray(arr) => arr.to_vec(),
|
| 169 |
-
_ => return Err("BlockStates не является LongArray".into()),
|
| 170 |
-
};
|
| 171 |
-
|
| 172 |
-
Ok((palette_vec, size_json, entities_vec, block_states))
|
| 173 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|