88 using Sandbox . Common . ObjectBuilders ;
99 using Sandbox . ModAPI ;
1010 using System . Collections . Generic ;
11+ using System . Reflection ;
1112 using SEModAPIExtensions . API ;
1213 using SEModAPIInternal . API . Common ;
1314 using SEModAPIInternal . API . Entity ;
2122 using System . Threading . Tasks ;
2223 using Sandbox ;
2324 using Sandbox . Definitions ;
25+ using Sandbox . Engine . Multiplayer ;
2426 using Sandbox . Game . Entities ;
2527 using Settings ;
2628 using VRage . Collections ;
@@ -36,20 +38,29 @@ public static void SendPublicInformation( string infoText )
3638 {
3739 if ( infoText == "" )
3840 return ;
39-
40- ServerMessageItem MessageItem = new ServerMessageItem ( ) ;
41- MessageItem . From = PluginSettings . Instance . ServerChatName ;
42- MessageItem . Message = infoText ;
4341
44- string messageString = MyAPIGateway . Utilities . SerializeToXML ( MessageItem ) ;
45- byte [ ] data = Encoding . UTF8 . GetBytes ( messageString ) ;
46-
47- if ( ChatManager . EnableData )
42+ ScriptedChatMsg msg = new ScriptedChatMsg
4843 {
49- BroadcastDataMessage ( DataMessageType . Message , data ) ;
50- }
51- else
52- ChatManager . Instance . SendPublicChatMessage ( infoText ) ;
44+ Author = PluginSettings . Instance . ServerChatName ,
45+ Font = MyFontEnum . Red ,
46+ Text = infoText ,
47+ } ;
48+
49+ var messageMethod = typeof ( MyMultiplayerBase ) . GetMethod ( "OnScriptedChatMessageRecieved" , BindingFlags . NonPublic | BindingFlags . Static ) ;
50+ ServerNetworkManager . Instance . RaiseStaticEvent ( messageMethod , msg ) ;
51+ //ServerMessageItem MessageItem = new ServerMessageItem( );
52+ //MessageItem.From = PluginSettings.Instance.ServerChatName;
53+ //MessageItem.Message = infoText;
54+
55+ //string messageString = MyAPIGateway.Utilities.SerializeToXML( MessageItem );
56+ //byte[ ] data = Encoding.UTF8.GetBytes( messageString );
57+
58+ //if ( ChatManager.EnableData )
59+ //{
60+ // BroadcastDataMessage( DataMessageType.Message, data );
61+ //}
62+ //else
63+ // ChatManager.Instance.SendPublicChatMessage( infoText );
5364
5465 ChatManager . Instance . AddChatHistory ( new ChatManager . ChatEvent ( DateTime . Now , 0 , infoText ) ) ;
5566 }
@@ -58,30 +69,22 @@ public static void SendPrivateInformation( ulong playerId, string infoText, stri
5869 {
5970 if ( infoText == "" )
6071 return ;
61-
62- ServerMessageItem MessageItem = new ServerMessageItem ( ) ;
63-
72+
6473 if ( from == null )
65- MessageItem . From = PluginSettings . Instance . ServerChatName ;
66-
74+ from = PluginSettings . Instance . ServerChatName ;
6775 else if ( PluginSettings . Instance . WhisperChatPrefix )
68- MessageItem . From = "<whisper> " + from ;
69-
70- else
71- MessageItem . From = from ;
72-
73- MessageItem . Message = infoText ;
74-
75- string messageString = MyAPIGateway . Utilities . SerializeToXML ( MessageItem ) ;
76- byte [ ] data = Encoding . UTF8 . GetBytes ( messageString ) ;
76+ from = "<whisper> " + from ;
7777
78- if ( ChatManager . EnableData )
78+ ScriptedChatMsg msg = new ScriptedChatMsg
7979 {
80- SendDataMessage ( playerId , DataMessageType . Message , data ) ;
81- }
82- else
83- ChatManager . Instance . SendPrivateChatMessage ( playerId , infoText ) ;
84-
80+ Author = from ,
81+ Font = MyFontEnum . Red ,
82+ Text = infoText ,
83+ } ;
84+
85+ var messageMethod = typeof ( MyMultiplayerBase ) . GetMethod ( "OnScriptedChatMessageRecieved" , BindingFlags . NonPublic | BindingFlags . Static ) ;
86+ ServerNetworkManager . Instance . RaiseStaticEvent ( messageMethod , playerId , msg ) ;
87+
8588 ChatManager . ChatEvent chatItem = new ChatManager . ChatEvent ( ) ;
8689 chatItem . Timestamp = DateTime . Now ;
8790 chatItem . RemoteUserId = ( from == null ? 0 : PlayerMap . Instance . GetSteamIdFromPlayerName ( from ) ) ;
@@ -91,28 +94,27 @@ public static void SendPrivateInformation( ulong playerId, string infoText, stri
9194
9295 public static void SendFactionClientMessage ( ulong playerSteamId , string message )
9396 {
94- ServerMessageItem MessageItem = new ServerMessageItem ( ) ;
97+ string from ;
98+
9599 if ( PluginSettings . Instance . FactionChatPrefix )
96- MessageItem . From = "<faction> " + PlayerMap . Instance . GetFastPlayerNameFromSteamId ( playerSteamId ) ;
100+ from = "<faction> " + PlayerMap . Instance . GetFastPlayerNameFromSteamId ( playerSteamId ) ;
97101 else
98- MessageItem . From = PlayerMap . Instance . GetFastPlayerNameFromSteamId ( playerSteamId ) ;
99-
100- MessageItem . Message = message ;
101-
102- string messageString = MyAPIGateway . Utilities . SerializeToXML ( MessageItem ) ;
103- byte [ ] data = Encoding . UTF8 . GetBytes ( messageString ) ;
104-
102+ from = PlayerMap . Instance . GetFastPlayerNameFromSteamId ( playerSteamId ) ;
103+
105104 foreach ( ulong steamId in PlayerManager . Instance . ConnectedPlayers )
106105 {
107106 if ( Player . CheckPlayerSameFaction ( playerSteamId , steamId ) )
108107 {
109- if ( ChatManager . EnableData )
108+ ScriptedChatMsg msg = new ScriptedChatMsg
110109 {
111- SendDataMessage ( steamId , DataMessageType . Message , data ) ;
112- ChatManager . Instance . AddChatHistory ( new ChatManager . ChatEvent ( DateTime . Now , playerSteamId , "{faction message}: " + message ) ) ;
113- }
114- else
115- ChatManager . Instance . SendPrivateChatMessage ( steamId , message ) ;
110+ Author = from ,
111+ Font = MyFontEnum . Red ,
112+ Text = message ,
113+ } ;
114+
115+ var messageMethod = typeof ( MyMultiplayerBase ) . GetMethod ( "OnScriptedChatMessageRecieved" , BindingFlags . NonPublic | BindingFlags . Static ) ;
116+ ServerNetworkManager . Instance . RaiseStaticEvent ( messageMethod , msg ) ;
117+ ChatManager . Instance . AddChatHistory ( new ChatManager . ChatEvent ( DateTime . Now , playerSteamId , "{faction message}: " + message ) ) ;
116118 }
117119 }
118120 }
@@ -303,29 +305,25 @@ public static void SendDataMessage( ulong steamId, DataMessageType messageType,
303305 Buffer.BlockCopy( data, 0, newData, msgIdString.Length + 1, data.Length );
304306 */
305307
306- //hash a random long with the current time to make a decent quality guid for each message
307- byte [ ] randLong = new byte [ sizeof ( long ) ] ;
308- _random . NextBytes ( randLong ) ;
309- long uniqueId = 23 ;
310- uniqueId = uniqueId * 37 + BitConverter . ToInt64 ( randLong , 0 ) ;
311- uniqueId = uniqueId * 37 + DateTime . Now . GetHashCode ( ) ;
312-
308+ byte [ ] guidBytes = Guid . NewGuid ( ) . ToByteArray ( ) ;
309+
313310 //this is a much more elegant and lightweight method
314- byte [ ] newData = new byte [ sizeof ( long ) * 2 + data . Length ] ;
315- BitConverter . GetBytes ( uniqueId ) . CopyTo ( newData , 0 ) ;
316- BitConverter . GetBytes ( ( long ) messageType ) . CopyTo ( newData , sizeof ( long ) ) ;
317- data . CopyTo ( newData , sizeof ( long ) * 2 ) ;
311+ byte [ ] newData = new byte [ sizeof ( long ) + guidBytes . Length + data . Length ] ;
312+ guidBytes . CopyTo ( newData , 0 ) ;
313+ BitConverter . GetBytes ( ( long ) messageType ) . CopyTo ( newData , guidBytes . Length ) ;
314+ data . CopyTo ( newData , sizeof ( long ) + guidBytes . Length ) ;
318315
319- if ( newData . Length > 4096 )
320- {
321- SendMessagePartsTo ( steamId , newData ) ;
322- return ;
323- }
316+ // if ( newData.Length > 4096 )
317+ // {
318+ // SendMessagePartsTo( steamId, newData );
319+ // return;
320+ // }
324321
325322 //Wrapper.GameAction( ( ) =>
326323 MySandboxGame . Static . Invoke ( ( ) =>
327324 {
328- MyAPIGateway . Multiplayer . SendMessageTo ( 9000 , newData , steamId ) ;
325+ //MyAPIGateway.Multiplayer.SendMessageTo( 9000, newData, steamId );
326+ ServerNetworkManager . Instance . SendModMessageTo ( 9000 , newData , steamId ) ;
329327 } ) ;
330328 }
331329
@@ -342,27 +340,26 @@ public static void BroadcastDataMessage( DataMessageType messageType, byte[ ] da
342340
343341 Buffer.BlockCopy( data, 0, newData, msgIdString.Length + 1, data.Length );
344342 */
345- byte [ ] randLong = new byte [ sizeof ( long ) ] ;
346- _random . NextBytes ( randLong ) ;
347- long uniqueId = 23 ;
348- uniqueId = uniqueId * 37 + BitConverter . ToInt64 ( randLong , 0 ) ;
349- uniqueId = uniqueId * 37 + DateTime . Now . GetHashCode ( ) ;
350-
351- byte [ ] newData = new byte [ sizeof ( long ) * 2 + data . Length ] ;
352- BitConverter . GetBytes ( uniqueId ) . CopyTo ( newData , 0 ) ;
353- BitConverter . GetBytes ( ( long ) messageType ) . CopyTo ( newData , sizeof ( long ) ) ;
354- data . CopyTo ( newData , sizeof ( long ) * 2 ) ;
355-
356- if ( newData . Length > 4096 )
357- {
358- BroadcastMessageParts ( newData ) ;
359- return ;
360- }
343+
344+ byte [ ] guidBytes = Guid . NewGuid ( ) . ToByteArray ( ) ;
345+
346+ //this is a much more elegant and lightweight method
347+ byte [ ] newData = new byte [ sizeof ( long ) + guidBytes . Length + data . Length ] ;
348+ guidBytes . CopyTo ( newData , 0 ) ;
349+ BitConverter . GetBytes ( ( long ) messageType ) . CopyTo ( newData , guidBytes . Length ) ;
350+ data . CopyTo ( newData , sizeof ( long ) + guidBytes . Length ) ;
351+
352+ //if (newData.Length > 4096)
353+ //{
354+ // BroadcastMessageParts(newData);
355+ // return;
356+ //}
361357
362358 //Wrapper.GameAction( ( ) =>
363359 MySandboxGame . Static . Invoke ( ( ) =>
364360 {
365- MyAPIGateway . Multiplayer . SendMessageToOthers ( 9000 , newData ) ;
361+ MyAPIGateway . Multiplayer . SendMessageToOthers ( 9000 , newData ) ;
362+ //ServerNetworkManager.Instance.BroadcastModMessage( 9000, newData );
366363 } ) ;
367364 }
368365
0 commit comments