mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 18:24:05 +02:00
show server send messages as chat
This commit is contained in:
parent
d9ff9d4662
commit
217211f2fb
12 changed files with 785 additions and 45 deletions
|
|
@ -247,39 +247,43 @@ pub struct SayText2Message {
|
|||
pub text: MaybeUtf8String,
|
||||
}
|
||||
|
||||
fn to_plain_text(text: &str) -> String {
|
||||
// 1: normal, 2: old colors, 3: team, 4: location, 5 achievement, 6 custom
|
||||
let mut text = text.replace(|c| c <= char::from(6), "");
|
||||
// 7: 6-char hex
|
||||
while let Some(pos) = text.chars().enumerate().find_map(|(index, c)| {
|
||||
if c == char::from(7) {
|
||||
Some(index)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
text = text
|
||||
.chars()
|
||||
.take(pos)
|
||||
.chain(text.chars().skip(pos + 7))
|
||||
.collect();
|
||||
}
|
||||
// 9: 8-char hex
|
||||
while let Some(pos) = text.chars().enumerate().find_map(|(index, c)| {
|
||||
if c == char::from(9) {
|
||||
Some(index)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
text = text
|
||||
.chars()
|
||||
.take(pos)
|
||||
.chain(text.chars().skip(pos + 9))
|
||||
.collect();
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
impl SayText2Message {
|
||||
pub fn plain_text(&self) -> String {
|
||||
// 1: normal, 2: old colors, 3: team, 4: location, 5 achievement, 6 custom
|
||||
let mut text = self.text.to_string().replace(|c| c <= char::from(6), "");
|
||||
// 7: 6-char hex
|
||||
while let Some(pos) = text.chars().enumerate().find_map(|(index, c)| {
|
||||
if c == char::from(7) {
|
||||
Some(index)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
text = text
|
||||
.chars()
|
||||
.take(pos)
|
||||
.chain(text.chars().skip(pos + 7))
|
||||
.collect();
|
||||
}
|
||||
// 9: 8-char hex
|
||||
while let Some(pos) = text.chars().enumerate().find_map(|(index, c)| {
|
||||
if c == char::from(9) {
|
||||
Some(index)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}) {
|
||||
text = text
|
||||
.chars()
|
||||
.take(pos)
|
||||
.chain(text.chars().skip(pos + 9))
|
||||
.collect();
|
||||
}
|
||||
text
|
||||
to_plain_text(self.text.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -364,6 +368,12 @@ pub struct TextMessage {
|
|||
pub substitute: [MaybeUtf8String; 4],
|
||||
}
|
||||
|
||||
impl TextMessage {
|
||||
pub fn plain_text(&self) -> String {
|
||||
to_plain_text(self.text.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
||||
#[derive(BitRead, BitWrite, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ResetHudMessage {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ use crate::demo::gameevent_gen::{
|
|||
GameEvent, PlayerDeathEvent, PlayerSpawnEvent, TeamPlayRoundWinEvent,
|
||||
};
|
||||
use crate::demo::message::packetentities::EntityId;
|
||||
use crate::demo::message::usermessage::{ChatMessageKind, SayText2Message, UserMessage};
|
||||
use crate::demo::message::usermessage::{
|
||||
ChatMessageKind, HudTextLocation, SayText2Message, TextMessage, UserMessage,
|
||||
};
|
||||
use crate::demo::message::{Message, MessageType};
|
||||
use crate::demo::packet::stringtable::StringTableEntry;
|
||||
use crate::demo::parser::handler::{BorrowMessageHandler, MessageHandler};
|
||||
|
|
@ -39,6 +41,15 @@ impl ChatMessage {
|
|||
tick,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_text(message: &TextMessage, tick: DemoTick) -> Self {
|
||||
ChatMessage {
|
||||
kind: ChatMessageKind::Empty,
|
||||
from: String::new(),
|
||||
text: message.plain_text(),
|
||||
tick,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
|
|
@ -457,16 +468,26 @@ impl Analyser {
|
|||
}
|
||||
|
||||
fn handle_user_message(&mut self, message: &UserMessage, tick: DemoTick) {
|
||||
if let UserMessage::SayText2(text_message) = message {
|
||||
if text_message.kind == ChatMessageKind::NameChange {
|
||||
if let Some(from) = text_message.from.clone() {
|
||||
self.change_name(from.into(), text_message.plain_text());
|
||||
match message {
|
||||
UserMessage::SayText2(text_message) => {
|
||||
if text_message.kind == ChatMessageKind::NameChange {
|
||||
if let Some(from) = text_message.from.clone() {
|
||||
self.change_name(from.into(), text_message.plain_text());
|
||||
}
|
||||
} else {
|
||||
self.state
|
||||
.chat
|
||||
.push(ChatMessage::from_message(text_message, tick));
|
||||
}
|
||||
} else {
|
||||
self.state
|
||||
.chat
|
||||
.push(ChatMessage::from_message(text_message, tick));
|
||||
}
|
||||
UserMessage::Text(text_message) => {
|
||||
if text_message.location == HudTextLocation::PrintTalk {
|
||||
self.state
|
||||
.chat
|
||||
.push(ChatMessage::from_text(text_message, tick));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue