-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support for LFS 0.7F #170
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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,120 @@ | ||
use std::{default::Default, net::Ipv4Addr}; | ||
|
||
use indexmap::{set::Iter as IndexSetIter, IndexSet}; | ||
use insim_core::binrw::{self, binrw, BinRead, BinResult, BinWrite}; | ||
|
||
use crate::identifiers::RequestId; | ||
|
||
const IPB_MAX_BANS: usize = 120; | ||
|
||
#[binrw::parser(reader, endian)] | ||
fn binrw_parse_ipb_bans(count: u8) -> BinResult<IndexSet<Ipv4Addr>> { | ||
let mut data = IndexSet::new(); | ||
for _i in 0..count { | ||
let ip = Ipv4Addr::from(u32::read_options(reader, endian, ())?); | ||
let _ = data.insert(ip); | ||
} | ||
Ok(data) | ||
} | ||
|
||
#[binrw::writer(writer, endian)] | ||
fn binrw_write_ipb_bans(input: &IndexSet<Ipv4Addr>) -> BinResult<()> { | ||
for i in input.iter() { | ||
u32::from(*i).write_options(writer, endian, ())?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[binrw] | ||
#[bw(assert(banips.len() <= IPB_MAX_BANS))] | ||
#[derive(Debug, Clone, Default, PartialEq)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize))] | ||
/// Mods Allowed - restrict the mods that can be used | ||
pub struct Ipb { | ||
/// Non-zero if the packet is a packet request or a reply to a request | ||
pub reqi: RequestId, | ||
|
||
/// Number of bans in this packet, from the wire | ||
/// This value is not to be trusted as we use an IndexSet to record the bans internally. It is technically | ||
/// possible that LFS could return duplicate entries, but we have no way of verifying that. | ||
#[bw(calc = banips.len() as u8)] | ||
#[brw(pad_after = 4)] | ||
numb: u8, | ||
|
||
#[br(parse_with = binrw_parse_ipb_bans, args(numb))] | ||
#[bw(write_with = binrw_write_ipb_bans)] | ||
banips: IndexSet<Ipv4Addr>, | ||
} | ||
|
||
impl Ipb { | ||
/// Returns `true` if a Vehicle is contained in this packet | ||
pub fn contains(&self, v: &Ipv4Addr) -> bool { | ||
self.banips.contains(v) | ||
} | ||
|
||
/// Push a compressed form of a mod onto the list of allowed mods | ||
/// and update the count. | ||
pub fn insert(&mut self, ip: Ipv4Addr) -> bool { | ||
self.banips.insert(ip) | ||
} | ||
|
||
/// Remove a Vehicle from this packet | ||
pub fn remove(&mut self, ip: &Ipv4Addr) -> bool { | ||
self.banips.shift_remove(ip) | ||
} | ||
|
||
/// Does this packet have no vehicles associated? | ||
pub fn is_empty(&self) -> bool { | ||
self.banips.is_empty() | ||
} | ||
|
||
/// Clear any previously allowed mods. | ||
pub fn clear(&mut self) { | ||
self.banips.clear() | ||
} | ||
|
||
/// Iterator for all allowed mods | ||
pub fn iter(&self) -> IndexSetIter<'_, Ipv4Addr> { | ||
self.banips.iter() | ||
} | ||
|
||
/// Returns the number of allowed mods | ||
pub fn len(&self) -> usize { | ||
self.banips.len() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::io::{Cursor, Seek}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_encoding() { | ||
let mut bans = Ipb::default(); | ||
bans.reqi = RequestId(2); | ||
let _ = bans.insert(Ipv4Addr::new(127, 0, 0, 1)); | ||
|
||
let mut buf = Cursor::new(Vec::new()); | ||
bans.write_le(&mut buf).unwrap(); | ||
buf.rewind().unwrap(); | ||
|
||
let buf2 = buf.clone().into_inner(); | ||
assert_eq!( | ||
buf2, | ||
[ | ||
2, // reqi | ||
1, // numb | ||
0, 0, 0, 0, // padding / unused | ||
1, 0, 0, 127, // mod 1 | ||
] | ||
); | ||
|
||
let data2 = Ipb::read_le(&mut buf).unwrap(); | ||
assert_eq!(bans, data2); | ||
assert_eq!(data2.len(), 1); | ||
assert!(data2.contains(&Ipv4Addr::new(127, 0, 0, 1))); | ||
} | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments in this packet definition still refer to "vehicle" and "mods", that looks like an oversight after a copy&paste.