diff --git a/src/common/str/fixed.rs b/src/common/str/fixed.rs index 1a7424e..e31fe77 100644 --- a/src/common/str/fixed.rs +++ b/src/common/str/fixed.rs @@ -69,15 +69,27 @@ macro_rules! lu_str { type Error = AsciiError; fn try_from(string: &[u8]) -> Result { + if string.len() >= $n { + // actually length error but whatever + return Err(AsciiError); + } let mut bytes = [0u8; $n]; // todo: ascii range check - for (i, chr) in string.iter().take($n-1).enumerate() { + for (i, chr) in string.iter().enumerate() { bytes[i] = *chr; } let bytes = unsafe { std::mem::transmute(bytes) }; Ok(Self(bytes)) } } + + impl TryFrom<&[u8; N]> for $name { + type Error = AsciiError; + + fn try_from(string: &[u8; N]) -> Result { + Self::try_from(&string[..]) + } + } } } diff --git a/src/common/str/variable.rs b/src/common/str/variable.rs index 9d06728..edc0134 100644 --- a/src/common/str/variable.rs +++ b/src/common/str/variable.rs @@ -40,6 +40,14 @@ impl TryFrom<&[u8]> for LuVarString { } } +impl TryFrom<&[u8; N]> for LuVarString { + type Error = AsciiError; + + fn try_from(string: &[u8; N]) -> Result { + Self::try_from(&string[..]) + } +} + impl From<&LuWStr> for LuVarWString { fn from(string: &LuWStr) -> Self { Self(string.into(), PhantomData) diff --git a/src/lib.rs b/src/lib.rs index 885c5ef..5171b20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ Documentation and (de-)serialization support for LU's network protocol. */ #![feature(arbitrary_enum_discriminant)] +#![feature(min_const_generics)] #![feature(specialization)] #![allow(incomplete_features)] @@ -97,7 +98,7 @@ macro_rules! amf3 { "bool": true, "i64": i64::MAX, "u64": u64::MAX, - "string": b"byte slice"[..], + "string": b"byte slice", }; # } ``` diff --git a/src/raknet/client/replica/tests/FxConstruction.rs b/src/raknet/client/replica/tests/FxConstruction.rs index fca6049..a57d501 100644 --- a/src/raknet/client/replica/tests/FxConstruction.rs +++ b/src/raknet/client/replica/tests/FxConstruction.rs @@ -1,7 +1,7 @@ FxConstruction { active_effects: vec![ EffectInfo { - effect_name: lu!(&b"effect_name"[..]), + effect_name: lu!(b"effect_name"), effect_id: 1, effect_type: lu!("effect_type"), priority: 2.0, diff --git a/src/world/client/tests/CreateCharacter.rs b/src/world/client/tests/CreateCharacter.rs index 4121ae4..2ee8524 100644 --- a/src/world/client/tests/CreateCharacter.rs +++ b/src/world/client/tests/CreateCharacter.rs @@ -19,7 +19,7 @@ ClientMessage::CreateCharacter( "reputation": 0i64, "template": 1i32, "objid": 1152921510115197038u64, - "xmlData": &b""[..], + "xmlData": b"", "editor_enabled": false, "position.y": 613.32623f32, "chatmode": 0i32, diff --git a/src/world/client/tests/TransferToWorld.rs b/src/world/client/tests/TransferToWorld.rs index 681a627..d4b54ed 100644 --- a/src/world/client/tests/TransferToWorld.rs +++ b/src/world/client/tests/TransferToWorld.rs @@ -1,6 +1,6 @@ ClientMessage::TransferToWorld( TransferToWorld { - redirect_ip: lu!(&b"171.20.35.42"[..]), + redirect_ip: lu!(b"171.20.35.42"), redirect_port: 2005, is_maintenance_transfer: false, }, diff --git a/src/world/gm/client/tests/LockNodeRotation.rs b/src/world/gm/client/tests/LockNodeRotation.rs index 8fb914d..4c0e520 100644 --- a/src/world/gm/client/tests/LockNodeRotation.rs +++ b/src/world/gm/client/tests/LockNodeRotation.rs @@ -1,5 +1,5 @@ GameMessage::LockNodeRotation( LockNodeRotation { - node_name: lu!(&b"base"[..]), + node_name: lu!(b"base"), }, ) diff --git a/src/world/gm/client/tests/NotifyClientObject.rs b/src/world/gm/client/tests/NotifyClientObject.rs index 6b568a7..663b003 100644 --- a/src/world/gm/client/tests/NotifyClientObject.rs +++ b/src/world/gm/client/tests/NotifyClientObject.rs @@ -4,6 +4,6 @@ GameMessage::NotifyClientObject( param1: 5, param2: 0, param_obj: 1152921507004579166, - param_str: lu!(&b""[..]), + param_str: lu!(b""), }, ) diff --git a/src/world/gm/client/tests/NotifyClientZoneObject.rs b/src/world/gm/client/tests/NotifyClientZoneObject.rs index 01fba06..957350b 100644 --- a/src/world/gm/client/tests/NotifyClientZoneObject.rs +++ b/src/world/gm/client/tests/NotifyClientZoneObject.rs @@ -4,6 +4,6 @@ GameMessage::NotifyClientZoneObject( param1: 395, param2: 0, param_obj: 1152921507004579166, - param_str: lu!(&b"true"[..]), + param_str: lu!(b"true"), }, ) diff --git a/src/world/gm/client/tests/PlayFxEffect.rs b/src/world/gm/client/tests/PlayFxEffect.rs index 8955417..fcb2777 100644 --- a/src/world/gm/client/tests/PlayFxEffect.rs +++ b/src/world/gm/client/tests/PlayFxEffect.rs @@ -3,7 +3,7 @@ GameMessage::PlayFxEffect( effect_id: -1, effect_type: lu!("equip-head"), scale: 1.0, - name: lu!(&b""[..]), + name: lu!(b""), priority: 1.07, secondary: 0, serialize: true, diff --git a/src/world/gm/client/tests/ScriptNetworkVarUpdate.rs b/src/world/gm/client/tests/ScriptNetworkVarUpdate.rs index 4400e41..a17f73f 100644 --- a/src/world/gm/client/tests/ScriptNetworkVarUpdate.rs +++ b/src/world/gm/client/tests/ScriptNetworkVarUpdate.rs @@ -1,9 +1,9 @@ GameMessage::ScriptNetworkVarUpdate( ScriptNetworkVarUpdate { table_of_vars: lnv! { - "Define_Player_To_UI": b"1152921510757277251"[..], + "Define_Player_To_UI": b"1152921510757277251", "Show_ScoreBoard": true, - "Update_ScoreBoard_Players.1": b"1152921510757277251"[..], + "Update_ScoreBoard_Players.1": b"1152921510757277251", }, }, ) diff --git a/src/world/gm/client/tests/StartCelebrationEffect.rs b/src/world/gm/client/tests/StartCelebrationEffect.rs index ed36be2..39a567e 100644 --- a/src/world/gm/client/tests/StartCelebrationEffect.rs +++ b/src/world/gm/client/tests/StartCelebrationEffect.rs @@ -9,10 +9,10 @@ GameMessage::StartCelebrationEffect( duration: 0.0, icon_id: 0, main_text: lu!(""), - mixer_program: lu!(&b""[..]), - music_cue: lu!(&b""[..]), - path_node_name: lu!(&b""[..]), - sound_guid: lu!(&b""[..]), + mixer_program: lu!(b""), + music_cue: lu!(b""), + path_node_name: lu!(b""), + sound_guid: lu!(b""), sub_text: lu!(""), }, ) diff --git a/src/world/gm/client/tests/StopFxEffect.rs b/src/world/gm/client/tests/StopFxEffect.rs index 9a58e6d..5bbc24d 100644 --- a/src/world/gm/client/tests/StopFxEffect.rs +++ b/src/world/gm/client/tests/StopFxEffect.rs @@ -1,6 +1,6 @@ GameMessage::StopFxEffect( StopFxEffect { kill_immediate: false, - name: lu!(&b"BrickFadeUpVisCompleteEffect"[..]), + name: lu!(b"BrickFadeUpVisCompleteEffect"), }, ) diff --git a/src/world/gm/client/tests/UiMessageServerToSingleClient.rs b/src/world/gm/client/tests/UiMessageServerToSingleClient.rs index b5de890..d4fcbf8 100644 --- a/src/world/gm/client/tests/UiMessageServerToSingleClient.rs +++ b/src/world/gm/client/tests/UiMessageServerToSingleClient.rs @@ -7,6 +7,6 @@ GameMessage::UiMessageServerToSingleClient( "string": "string", "array": amf3! ["inner", "array", true], }, - message_name: lu!(&b"QueueChoiceBox"[..]), + message_name: lu!(b"QueueChoiceBox"), }, ) diff --git a/src/world/gm/server/tests/PropertyEntranceSync.rs b/src/world/gm/server/tests/PropertyEntranceSync.rs index 6f938de..f9780ac 100644 --- a/src/world/gm/server/tests/PropertyEntranceSync.rs +++ b/src/world/gm/server/tests/PropertyEntranceSync.rs @@ -8,6 +8,6 @@ GameMessage::PropertyEntranceSync( reputation_time: 0, sort_method: 5, start_index: 0, - filter_text: lu!(&b""[..]), + filter_text: lu!(b""), }, ) diff --git a/src/world/gm/server/tests/ReportBug.rs b/src/world/gm/server/tests/ReportBug.rs index 8c70724..b0694b2 100644 --- a/src/world/gm/server/tests/ReportBug.rs +++ b/src/world/gm/server/tests/ReportBug.rs @@ -1,8 +1,8 @@ GameMessage::ReportBug( ReportBug { body: lu!("code's haunted"), - client_version: lu!(&b"1.10.64"[..]), - other_player_id: lu!(&b"0"[..]), - selection: lu!(&b"%[UI_HELP_IN_GAME]"[..]), + client_version: lu!(b"1.10.64"), + other_player_id: lu!(b"0"), + selection: lu!(b"%[UI_HELP_IN_GAME]"), }, ) diff --git a/src/world/gm/server/tests/SetMissionTypeState.rs b/src/world/gm/server/tests/SetMissionTypeState.rs index ac787b7..e486d94 100644 --- a/src/world/gm/server/tests/SetMissionTypeState.rs +++ b/src/world/gm/server/tests/SetMissionTypeState.rs @@ -1,7 +1,7 @@ GameMessage::SetMissionTypeState( SetMissionTypeState { state: MissionLockState::New, - mission_subtype: lu!(&b""[..]), - mission_type: lu!(&b"Build"[..]), + mission_subtype: lu!(b""), + mission_type: lu!(b"Build"), }, ) diff --git a/src/world/lnv.rs b/src/world/lnv.rs index cbb5c9d..4ec4332 100644 --- a/src/world/lnv.rs +++ b/src/world/lnv.rs @@ -102,6 +102,10 @@ impl From<&[u8]> for LnvValue { fn from(val: &[u8]) -> Self { LnvValue::String(val.try_into().unwrap()) } } +impl From<&[u8; N]> for LnvValue { + fn from(val: &[u8; N]) -> Self { LnvValue::String(val.try_into().unwrap()) } +} + /// A hash map with values being one of multiple possible types. #[derive(PartialEq)] pub struct LuNameValue(HashMap, LnvValue>);