source_vmf/vmf/
connection.rs1use indexmap::IndexMap;
8use indexmap::map::Entry;
9#[cfg(feature = "serialization")]
10use serde::{Deserialize, Serialize};
11
12const SEP_ESC: char = '\x1b';
14
15const MULTI_SEP: char = '\r';
17const SEP_COMMA: char = ',';
19
20const INSTANCE_PREFIX: &str = "instance:";
22
23#[derive(Debug, Clone, PartialEq)]
25#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
26pub struct Connection {
27 pub output: String,
28 pub target: String,
29 pub input: String,
30 pub params: String,
31 pub delay: f32,
32 pub fire_limit: i32,
33}
34
35impl Connection {
36 pub fn parse(output: String, value: &str) -> Self {
42 let sep = if value.contains(SEP_ESC) {
44 SEP_ESC
45 } else {
46 SEP_COMMA
47 };
48
49 let mut head = value.splitn(3, sep);
50 let target = head.next().unwrap_or("");
51 let input = head.next().unwrap_or("");
52 let rest = head.next().unwrap_or("");
53
54 let mut tail = rest.rsplitn(3, sep);
56 let last = tail.next().unwrap_or("");
57 let mid = tail.next().unwrap_or("");
58 let (params, delay, fire_limit) = match tail.next() {
59 Some(params) => (params, mid, last),
60 None => (rest, "", ""),
61 };
62
63 Connection {
64 output,
65 target: target.to_string(),
66 input: input.to_string(),
67 params: params.to_string(),
68 delay: delay.parse().unwrap_or(0.0),
69 fire_limit: fire_limit.parse().unwrap_or(-1),
70 }
71 }
72
73 pub fn to_value_string(&self) -> String {
75 format!(
76 "{}{sep}{}{sep}{}{sep}{}{sep}{}",
77 self.target,
78 self.input,
79 self.params,
80 self.delay,
81 self.fire_limit,
82 sep = SEP_ESC
83 )
84 }
85
86 pub fn target_kind(&self) -> Target<'_> {
88 Target::parse(&self.target)
89 }
90
91 pub fn instance_input(&self) -> Option<InstanceIo<'_>> {
93 InstanceIo::parse(&self.input)
94 }
95
96 pub fn instance_output(&self) -> Option<InstanceIo<'_>> {
98 InstanceIo::parse(&self.output)
99 }
100
101 pub fn set_instance_input(&mut self, proxy: &str, io: &str) {
103 self.input = format!("{INSTANCE_PREFIX}{proxy};{io}");
104 }
105
106 pub fn set_instance_output(&mut self, proxy: &str, io: &str) {
108 self.output = format!("{INSTANCE_PREFIX}{proxy};{io}");
109 }
110
111 pub fn from_key_values(map: &IndexMap<String, String>) -> Option<Vec<Self>> {
113 if map.is_empty() {
114 return None;
115 }
116
117 let mut result = Vec::with_capacity(map.len() * 2);
118 for (key, value) in map {
119 for part in value.split(MULTI_SEP) {
120 result.push(Connection::parse(key.clone(), part));
121 }
122 }
123 Some(result)
124 }
125
126 pub fn to_key_values(connections: &[Self]) -> IndexMap<String, String> {
128 let mut map = IndexMap::with_capacity(connections.len());
129 for conn in connections {
130 match map.entry(conn.output.clone()) {
131 Entry::Occupied(mut slot) => {
132 let joined: &mut String = slot.get_mut();
133 joined.push(MULTI_SEP);
134 joined.push_str(&conn.to_value_string());
135 }
136 Entry::Vacant(slot) => {
137 slot.insert(conn.to_value_string());
138 }
139 }
140 }
141 map
142 }
143}
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq)]
147pub enum Special<'a> {
148 SelfEnt,
150 Activator,
152 Caller,
154 Player,
156 PvsPlayer,
158 Picker,
160 Other(&'a str),
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
166pub enum Target<'a> {
167 Name(&'a str),
169 Wildcard(&'a str),
172 Special(Special<'a>),
174}
175
176impl<'a> Target<'a> {
177 pub fn parse(target: &'a str) -> Self {
179 if let Some(tag) = target.strip_prefix('!') {
180 let special = if tag.eq_ignore_ascii_case("self") {
181 Special::SelfEnt
182 } else if tag.eq_ignore_ascii_case("activator") {
183 Special::Activator
184 } else if tag.eq_ignore_ascii_case("caller") {
185 Special::Caller
186 } else if tag.eq_ignore_ascii_case("player") {
187 Special::Player
188 } else if tag.eq_ignore_ascii_case("pvsplayer") {
189 Special::PvsPlayer
190 } else if tag.eq_ignore_ascii_case("picker") {
191 Special::Picker
192 } else {
193 Special::Other(tag)
194 };
195 return Target::Special(special);
196 }
197
198 match target.strip_suffix('*') {
199 Some(prefix) => Target::Wildcard(prefix),
200 None => Target::Name(target),
201 }
202 }
203
204 pub fn name(&self) -> Option<&'a str> {
206 match self {
207 Target::Name(name) => Some(name),
208 _ => None,
209 }
210 }
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq)]
216pub struct InstanceIo<'a> {
217 pub proxy: &'a str,
219 pub io: &'a str,
221}
222
223impl<'a> InstanceIo<'a> {
224 pub fn parse(value: &'a str) -> Option<Self> {
226 let head = value.get(..INSTANCE_PREFIX.len())?;
227 if !head.eq_ignore_ascii_case(INSTANCE_PREFIX) {
228 return None;
229 }
230 let (proxy, io) = value[INSTANCE_PREFIX.len()..].split_once(';')?;
231 Some(InstanceIo { proxy, io })
232 }
233}
234
235impl std::fmt::Display for InstanceIo<'_> {
236 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
237 write!(f, "{INSTANCE_PREFIX}{};{}", self.proxy, self.io)
238 }
239}