Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit efc13d9

Browse files
committed
Use colored chat for server messages. Add logging to planet reset. Add /admin static command
1 parent 5dc010b commit efc13d9

File tree

6 files changed

+190
-89
lines changed

6 files changed

+190
-89
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//392
1+
//398
22
//
33
// This code was generated by a tool. Any changes made manually will be lost
44
// the next time this code is regenerated.
55
//
66

77
using System.Reflection;
88

9-
[assembly: AssemblyFileVersion("1.13.7.392")]
10-
[assembly: AssemblyVersion("1.13.7.392")]
9+
[assembly: AssemblyFileVersion("1.13.7.398")]
10+
[assembly: AssemblyVersion("1.13.7.398")]

EssentialsPlugin/ChatHandlers/Admin/HandleAdminResetPlanet.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace EssentialsPlugin.ChatHandlers.Admin
22
{
3+
using System.Diagnostics;
34
using System.Linq;
45
using ParallelTasks;
56
using Sandbox.Definitions;
@@ -48,7 +49,14 @@ public override bool HandleCommand( ulong userId, string[ ] words )
4849

4950
//Wrapper.BeginGameAction( ( ) =>
5051
// {
51-
Parallel.Start(()=>voxel.Storage.Reset( VRage.Voxels.MyStorageDataTypeFlags.All ));
52+
Parallel.Start( ( ) =>
53+
{
54+
Essentials.Log.Info( $"Resetting {voxel.StorageName}" );
55+
var stopwatch = Stopwatch.StartNew( );
56+
voxel.Storage.Reset( VRage.Voxels.MyStorageDataTypeFlags.All );
57+
stopwatch.Stop( );
58+
Essentials.Log.Info($"Reset {voxel.StorageName} in {(1000d * stopwatch.ElapsedTicks / Stopwatch.Frequency):N3}ms" );
59+
});
5260
//MyMultiplayer.ReplicateImmediatelly( MyExternalReplicable.FindByObject( ent ) );
5361
// }, null, null );
5462
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
namespace EssentialsPlugin.ChatHandlers.Admin
2+
{
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using ParallelTasks;
6+
using Sandbox.Definitions;
7+
using Sandbox.Engine.Multiplayer;
8+
using Sandbox.Engine.Voxels;
9+
using Sandbox.Game.Entities;
10+
using Sandbox.Game.Entities.Character;
11+
using Sandbox.Game.Replication;
12+
using Utility;
13+
using VRage.Game;
14+
15+
public class HandleAdminStatic : ChatHandlerBase
16+
{
17+
18+
public override string GetHelp()
19+
{
20+
return "Sets grids as static. Usage: /admin static (all, small, large)";
21+
}
22+
23+
public override string GetCommandText()
24+
{
25+
return "/admin static";
26+
}
27+
28+
public override bool IsAdminCommand()
29+
{
30+
return true;
31+
}
32+
33+
public override bool AllowedInConsole()
34+
{
35+
return true;
36+
}
37+
38+
public override bool HandleCommand( ulong userId, string[ ] words )
39+
{
40+
if (words.Length != 1)
41+
{
42+
Communication.SendPrivateInformation( userId, GetHelp( ) );
43+
return false;
44+
}
45+
bool small;
46+
bool large;
47+
48+
switch (words[0].ToLower( ))
49+
{
50+
case "small":
51+
small = true;
52+
large = false;
53+
break;
54+
case "large":
55+
small = false;
56+
large = true;
57+
break;
58+
case "all":
59+
small = true;
60+
large = true;
61+
break;
62+
default:
63+
Communication.SendPrivateInformation( userId, GetHelp( ) );
64+
return false;
65+
}
66+
67+
var ents = MyEntities.GetEntities( ).ToArray( );
68+
int count = 0;
69+
foreach (var ent in ents)
70+
{
71+
var grid = ent as MyCubeGrid;
72+
if (grid == null)
73+
continue;
74+
75+
if (grid.Physics == null || grid.IsStatic)
76+
continue;
77+
78+
if (grid.GridSizeEnum == MyCubeSize.Large && large)
79+
{
80+
grid.RequestConversionToStation( );
81+
Essentials.Log.Info( $"Converted {grid.DisplayName} to static" );
82+
count++;
83+
}
84+
else if (grid.GridSizeEnum == MyCubeSize.Small && small)
85+
{
86+
grid.RequestConversionToStation( );
87+
Essentials.Log.Info( $"Converted {grid.DisplayName} to static" );
88+
count++;
89+
}
90+
}
91+
Communication.SendPrivateInformation( userId, $"Converted {count} grids to static" );
92+
return true;
93+
}
94+
95+
}
96+
97+
}
98+

EssentialsPlugin/Essentials.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454

5555
public class Essentials : IPlugin, IChatEventHandler, IPlayerEventHandler, ICubeGridHandler, ICubeBlockEventHandler, ISectorEventHandler
5656
{
57-
//IMPORTANT: Comment out this field to let the build work on both branches
58-
public const bool StableBuild = false;
59-
6057
public static Logger Log;
6158
#region Private Fields
6259
internal static Essentials Instance;
@@ -1195,7 +1192,7 @@ private void DoInit( string path )
11951192
new HandleAdminFactionCleanup( ),
11961193
new HandleAdminSpawnCargo( ),
11971194
new HandleAdminPlayerCount( ),
1198-
1195+
new HandleAdminStatic( ),
11991196

12001197
//Admin Scan
12011198
new HandleAdminScanAreaAt( ),

EssentialsPlugin/EssentialsPlugin.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="ChatHandlers\AdminSettings\HandleAdminSettings.cs" />
7979
<Compile Include="ChatHandlers\Admin\HandleAdminAsteroidCleanup.cs" />
8080
<Compile Include="ChatHandlers\Admin\HandleAdminFactionCleanup.cs" />
81+
<Compile Include="ChatHandlers\Admin\HandleAdminStatic.cs" />
8182
<Compile Include="ChatHandlers\Admin\HandleAdminStop.cs" />
8283
<Compile Include="ChatHandlers\Admin\HandleAdminSpeed.cs" />
8384
<Compile Include="ChatHandlers\Admin\HandleAdminIdentityCleanup.cs" />

EssentialsPlugin/Utility/Communication.cs

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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;
@@ -21,6 +22,7 @@
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

Comments
 (0)