-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cleanup and separate tlk and coalesced loading logic
- Loading branch information
1 parent
e152e0a
commit 9c72e13
Showing
7 changed files
with
158 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use embeddy::Embedded; | ||
use log::error; | ||
use me3_coalesced_parser::Coalesced; | ||
use std::path::Path; | ||
|
||
/// Embedded copy of the default known talk files | ||
#[derive(Embedded)] | ||
#[folder = "src/resources/data/tlk"] | ||
struct DefaultTlkFiles; | ||
|
||
/// Attempts to load a talk file from a local file | ||
pub async fn local_talk_file(lang: &str) -> std::io::Result<Vec<u8>> { | ||
let file_name = format!("{}.tlk", lang); | ||
let local_path = format!("data/{}", file_name); | ||
let local_path = Path::new(&local_path); | ||
tokio::fs::read(local_path).await | ||
} | ||
|
||
/// Loads a fallback talk file from the embedded talk files list | ||
/// using the specified language. Will fallback to default if the | ||
/// language is not found. | ||
pub fn fallback_talk_file(lang: &str) -> &'static [u8] { | ||
let file_name = format!("{}.tlk", lang); | ||
|
||
// Fallback to embedded tlk files | ||
DefaultTlkFiles::get(&file_name) | ||
// Fallback to default tlk | ||
.unwrap_or_else(|| { | ||
DefaultTlkFiles::get("default.tlk").expect("Server missing default embedded tlk file") | ||
}) | ||
} | ||
|
||
/// Embedded default coalesced | ||
static DEFAULT_COALESCED: &[u8] = include_bytes!("../resources/data/coalesced.json"); | ||
|
||
/// Attempts to load the local coalesced file from the data folder | ||
pub async fn local_coalesced_file() -> std::io::Result<Coalesced> { | ||
let local_path = Path::new("data/coalesced.json"); | ||
let bytes = tokio::fs::read(local_path).await?; | ||
|
||
match serde_json::from_slice(&bytes) { | ||
Ok(value) => Ok(value), | ||
Err(err) => { | ||
error!("Failed to parse server coalesced: {}", err); | ||
|
||
Err(std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
"Failed to parse server coalesced", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
/// Loads the fallback coalesced from the embedded bytes | ||
pub fn fallback_coalesced_file() -> Coalesced { | ||
serde_json::from_slice(DEFAULT_COALESCED) | ||
// Game cannot run without a proper coalesced | ||
.expect("Server fallback coalesced is malformed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod config; | ||
pub mod game; | ||
pub mod retriever; | ||
pub mod sessions; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::io::Write; | ||
|
||
use base64ct::{Base64, Encoding}; | ||
use flate2::{write::ZlibEncoder, Compression}; | ||
use tdf::TdfMap; | ||
|
||
/// Type of a base64 chunks map | ||
pub type ChunkMap = TdfMap<String, String>; | ||
|
||
/// Converts to provided slice of bytes into an ordered TdfMap where | ||
/// the keys are the chunk index and the values are the bytes encoded | ||
/// as base64 chunks. The map contains a CHUNK_SIZE key which states | ||
/// how large each chunk is and a DATA_SIZE key indicating the total | ||
/// length of the chunked value | ||
pub fn create_base64_map(bytes: &[u8]) -> ChunkMap { | ||
// The size of the chunks | ||
const CHUNK_LENGTH: usize = 255; | ||
|
||
let encoded: String = Base64::encode_string(bytes); | ||
let length = encoded.len(); | ||
let mut output: ChunkMap = TdfMap::with_capacity((length / CHUNK_LENGTH) + 2); | ||
|
||
let mut index = 0; | ||
let mut offset = 0; | ||
|
||
while offset < length { | ||
let o1 = offset; | ||
offset += CHUNK_LENGTH; | ||
|
||
let slice = if offset < length { | ||
&encoded[o1..offset] | ||
} else { | ||
&encoded[o1..] | ||
}; | ||
|
||
output.insert(format!("CHUNK_{}", index), slice.to_string()); | ||
index += 1; | ||
} | ||
|
||
output.insert("CHUNK_SIZE".to_string(), CHUNK_LENGTH.to_string()); | ||
output.insert("DATA_SIZE".to_string(), length.to_string()); | ||
output | ||
} | ||
|
||
/// Generates a compressed coalesced from the provided bytes | ||
pub fn generate_coalesced(bytes: &[u8]) -> std::io::Result<ChunkMap> { | ||
let compressed: Vec<u8> = { | ||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::new(6)); | ||
encoder.write_all(bytes)?; | ||
encoder.finish()? | ||
}; | ||
|
||
let mut encoded = Vec::with_capacity(16 + compressed.len()); | ||
encoded.extend_from_slice(b"NIBC"); | ||
encoded.extend_from_slice(&1u32.to_le_bytes()); | ||
encoded.extend_from_slice(&(compressed.len() as u32).to_le_bytes()); | ||
encoded.extend_from_slice(&(bytes.len() as u32).to_le_bytes()); | ||
encoded.extend_from_slice(&compressed); | ||
Ok(create_base64_map(&encoded)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod components; | ||
pub mod encoding; | ||
pub mod hashing; | ||
pub mod lock; | ||
pub mod logging; | ||
|