mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Add minimal robot factory functionality and arc archive support for #6
This commit is contained in:
@@ -28,6 +28,22 @@ pub fn encode_7_bit_i32(mut src: i32) -> Vec<u8> {
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode_7_bit_i32(reader: &mut dyn std::io::Read) -> std::io::Result<i32> {
|
||||
let mut buf = [0u8; 1];
|
||||
let mut out: i32 = 0;
|
||||
for _ in 0..5 {
|
||||
reader.read_exact(&mut buf)?;
|
||||
let byte = buf[0];
|
||||
let has_more = byte & 0x80;
|
||||
let number = byte & 0x7F;
|
||||
out = (out << 7) | (number as i32);
|
||||
if has_more == 0 {
|
||||
return Ok(out);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn write_str_for_binreader(s: &str, writer: &mut dyn std::io::Write) -> std::io::Result<usize> {
|
||||
let s_bytes = s.as_bytes();
|
||||
let mut total_len = writer.write(&encode_7_bit_i32(s_bytes.len() as i32))?;
|
||||
@@ -35,6 +51,13 @@ pub fn write_str_for_binreader(s: &str, writer: &mut dyn std::io::Write) -> std:
|
||||
Ok(total_len)
|
||||
}
|
||||
|
||||
pub fn read_str_for_binwriter(reader: &mut dyn std::io::Read) -> std::io::Result<String> {
|
||||
let len = decode_7_bit_i32(reader)?;
|
||||
let mut buf = vec![0u8; len as usize];
|
||||
reader.read_exact(&mut buf)?;
|
||||
Ok(String::from_utf8_lossy(&buf).into_owned())
|
||||
}
|
||||
|
||||
pub fn cube_id_to_str(id: u32) -> String {
|
||||
hex::encode(id.to_be_bytes()).into()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user