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

Commit

Permalink
Use colored chat for server messages. Add logging to planet reset. Ad…
Browse files Browse the repository at this point in the history
…d /admin static command
  • Loading branch information
rexxar-tc committed Dec 18, 2016
1 parent 5dc010b commit efc13d9
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 89 deletions.
6 changes: 3 additions & 3 deletions EssentialsPlugin/AssemblyFileVersion.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//392
//398
//
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
//

using System.Reflection;

[assembly: AssemblyFileVersion("1.13.7.392")]
[assembly: AssemblyVersion("1.13.7.392")]
[assembly: AssemblyFileVersion("1.13.7.398")]
[assembly: AssemblyVersion("1.13.7.398")]
10 changes: 9 additions & 1 deletion EssentialsPlugin/ChatHandlers/Admin/HandleAdminResetPlanet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace EssentialsPlugin.ChatHandlers.Admin
{
using System.Diagnostics;
using System.Linq;
using ParallelTasks;
using Sandbox.Definitions;
Expand Down Expand Up @@ -48,7 +49,14 @@ public override bool HandleCommand( ulong userId, string[ ] words )

//Wrapper.BeginGameAction( ( ) =>
// {
Parallel.Start(()=>voxel.Storage.Reset( VRage.Voxels.MyStorageDataTypeFlags.All ));
Parallel.Start( ( ) =>
{
Essentials.Log.Info( $"Resetting {voxel.StorageName}" );
var stopwatch = Stopwatch.StartNew( );
voxel.Storage.Reset( VRage.Voxels.MyStorageDataTypeFlags.All );
stopwatch.Stop( );
Essentials.Log.Info($"Reset {voxel.StorageName} in {(1000d * stopwatch.ElapsedTicks / Stopwatch.Frequency):N3}ms" );
});
//MyMultiplayer.ReplicateImmediatelly( MyExternalReplicable.FindByObject( ent ) );
// }, null, null );
}
Expand Down
98 changes: 98 additions & 0 deletions EssentialsPlugin/ChatHandlers/Admin/HandleAdminStatic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace EssentialsPlugin.ChatHandlers.Admin
{
using System.Diagnostics;
using System.Linq;
using ParallelTasks;
using Sandbox.Definitions;
using Sandbox.Engine.Multiplayer;
using Sandbox.Engine.Voxels;
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Character;
using Sandbox.Game.Replication;
using Utility;
using VRage.Game;

public class HandleAdminStatic : ChatHandlerBase
{

public override string GetHelp()
{
return "Sets grids as static. Usage: /admin static (all, small, large)";
}

public override string GetCommandText()
{
return "/admin static";
}

public override bool IsAdminCommand()
{
return true;
}

public override bool AllowedInConsole()
{
return true;
}

public override bool HandleCommand( ulong userId, string[ ] words )
{
if (words.Length != 1)
{
Communication.SendPrivateInformation( userId, GetHelp( ) );
return false;
}
bool small;
bool large;

switch (words[0].ToLower( ))
{
case "small":
small = true;
large = false;
break;
case "large":
small = false;
large = true;
break;
case "all":
small = true;
large = true;
break;
default:
Communication.SendPrivateInformation( userId, GetHelp( ) );
return false;
}

var ents = MyEntities.GetEntities( ).ToArray( );
int count = 0;
foreach (var ent in ents)
{
var grid = ent as MyCubeGrid;
if (grid == null)
continue;

if (grid.Physics == null || grid.IsStatic)
continue;

if (grid.GridSizeEnum == MyCubeSize.Large && large)
{
grid.RequestConversionToStation( );
Essentials.Log.Info( $"Converted {grid.DisplayName} to static" );
count++;
}
else if (grid.GridSizeEnum == MyCubeSize.Small && small)
{
grid.RequestConversionToStation( );
Essentials.Log.Info( $"Converted {grid.DisplayName} to static" );
count++;
}
}
Communication.SendPrivateInformation( userId, $"Converted {count} grids to static" );
return true;
}

}

}

5 changes: 1 addition & 4 deletions EssentialsPlugin/Essentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@

public class Essentials : IPlugin, IChatEventHandler, IPlayerEventHandler, ICubeGridHandler, ICubeBlockEventHandler, ISectorEventHandler
{
//IMPORTANT: Comment out this field to let the build work on both branches
public const bool StableBuild = false;

public static Logger Log;
#region Private Fields
internal static Essentials Instance;
Expand Down Expand Up @@ -1195,7 +1192,7 @@ private void DoInit( string path )
new HandleAdminFactionCleanup( ),
new HandleAdminSpawnCargo( ),
new HandleAdminPlayerCount( ),

new HandleAdminStatic( ),

//Admin Scan
new HandleAdminScanAreaAt( ),
Expand Down
1 change: 1 addition & 0 deletions EssentialsPlugin/EssentialsPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="ChatHandlers\AdminSettings\HandleAdminSettings.cs" />
<Compile Include="ChatHandlers\Admin\HandleAdminAsteroidCleanup.cs" />
<Compile Include="ChatHandlers\Admin\HandleAdminFactionCleanup.cs" />
<Compile Include="ChatHandlers\Admin\HandleAdminStatic.cs" />
<Compile Include="ChatHandlers\Admin\HandleAdminStop.cs" />
<Compile Include="ChatHandlers\Admin\HandleAdminSpeed.cs" />
<Compile Include="ChatHandlers\Admin\HandleAdminIdentityCleanup.cs" />
Expand Down
159 changes: 78 additions & 81 deletions EssentialsPlugin/Utility/Communication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Sandbox.Common.ObjectBuilders;
using Sandbox.ModAPI;
using System.Collections.Generic;
using System.Reflection;
using SEModAPIExtensions.API;
using SEModAPIInternal.API.Common;
using SEModAPIInternal.API.Entity;
Expand All @@ -21,6 +22,7 @@
using System.Threading.Tasks;
using Sandbox;
using Sandbox.Definitions;
using Sandbox.Engine.Multiplayer;
using Sandbox.Game.Entities;
using Settings;
using VRage.Collections;
Expand All @@ -36,20 +38,29 @@ public static void SendPublicInformation( string infoText )
{
if ( infoText == "" )
return;

ServerMessageItem MessageItem = new ServerMessageItem( );
MessageItem.From = PluginSettings.Instance.ServerChatName;
MessageItem.Message = infoText;

string messageString = MyAPIGateway.Utilities.SerializeToXML( MessageItem );
byte[ ] data = Encoding.UTF8.GetBytes( messageString );

if ( ChatManager.EnableData )
ScriptedChatMsg msg = new ScriptedChatMsg
{
BroadcastDataMessage( DataMessageType.Message, data );
}
else
ChatManager.Instance.SendPublicChatMessage( infoText );
Author = PluginSettings.Instance.ServerChatName,
Font = MyFontEnum.Red,
Text = infoText,
};

var messageMethod = typeof(MyMultiplayerBase).GetMethod("OnScriptedChatMessageRecieved", BindingFlags.NonPublic | BindingFlags.Static);
ServerNetworkManager.Instance.RaiseStaticEvent(messageMethod, msg);
//ServerMessageItem MessageItem = new ServerMessageItem( );
//MessageItem.From = PluginSettings.Instance.ServerChatName;
//MessageItem.Message = infoText;

//string messageString = MyAPIGateway.Utilities.SerializeToXML( MessageItem );
//byte[ ] data = Encoding.UTF8.GetBytes( messageString );

//if ( ChatManager.EnableData )
//{
// BroadcastDataMessage( DataMessageType.Message, data );
//}
//else
// ChatManager.Instance.SendPublicChatMessage( infoText );

ChatManager.Instance.AddChatHistory( new ChatManager.ChatEvent( DateTime.Now, 0, infoText ) );
}
Expand All @@ -58,30 +69,22 @@ public static void SendPrivateInformation( ulong playerId, string infoText, stri
{
if ( infoText == "" )
return;

ServerMessageItem MessageItem = new ServerMessageItem( );


if ( from == null )
MessageItem.From = PluginSettings.Instance.ServerChatName;

from = PluginSettings.Instance.ServerChatName;
else if ( PluginSettings.Instance.WhisperChatPrefix )
MessageItem.From = "<whisper> " + from;

else
MessageItem.From = from;

MessageItem.Message = infoText;

string messageString = MyAPIGateway.Utilities.SerializeToXML( MessageItem );
byte[ ] data = Encoding.UTF8.GetBytes( messageString );
from = "<whisper> " + from;

if ( ChatManager.EnableData )
ScriptedChatMsg msg = new ScriptedChatMsg
{
SendDataMessage( playerId, DataMessageType.Message, data );
}
else
ChatManager.Instance.SendPrivateChatMessage( playerId, infoText );

Author = from,
Font = MyFontEnum.Red,
Text = infoText,
};

var messageMethod = typeof(MyMultiplayerBase).GetMethod("OnScriptedChatMessageRecieved", BindingFlags.NonPublic | BindingFlags.Static);
ServerNetworkManager.Instance.RaiseStaticEvent(messageMethod, playerId, msg);

ChatManager.ChatEvent chatItem = new ChatManager.ChatEvent( );
chatItem.Timestamp = DateTime.Now;
chatItem.RemoteUserId = (from == null ? 0 : PlayerMap.Instance.GetSteamIdFromPlayerName( from ));
Expand All @@ -91,28 +94,27 @@ public static void SendPrivateInformation( ulong playerId, string infoText, stri

public static void SendFactionClientMessage( ulong playerSteamId, string message )
{
ServerMessageItem MessageItem = new ServerMessageItem( );
string from;

if ( PluginSettings.Instance.FactionChatPrefix )
MessageItem.From = "<faction> " + PlayerMap.Instance.GetFastPlayerNameFromSteamId( playerSteamId );
from = "<faction> " + PlayerMap.Instance.GetFastPlayerNameFromSteamId( playerSteamId );
else
MessageItem.From = PlayerMap.Instance.GetFastPlayerNameFromSteamId( playerSteamId );

MessageItem.Message = message;

string messageString = MyAPIGateway.Utilities.SerializeToXML( MessageItem );
byte[ ] data = Encoding.UTF8.GetBytes( messageString );

from = PlayerMap.Instance.GetFastPlayerNameFromSteamId( playerSteamId );

foreach ( ulong steamId in PlayerManager.Instance.ConnectedPlayers )
{
if ( Player.CheckPlayerSameFaction( playerSteamId, steamId ) )
{
if ( ChatManager.EnableData )
ScriptedChatMsg msg = new ScriptedChatMsg
{
SendDataMessage( steamId, DataMessageType.Message, data );
ChatManager.Instance.AddChatHistory( new ChatManager.ChatEvent( DateTime.Now, playerSteamId, "{faction message}: " + message ) );
}
else
ChatManager.Instance.SendPrivateChatMessage( steamId, message );
Author = from,
Font = MyFontEnum.Red,
Text = message,
};

var messageMethod = typeof(MyMultiplayerBase).GetMethod("OnScriptedChatMessageRecieved", BindingFlags.NonPublic | BindingFlags.Static);
ServerNetworkManager.Instance.RaiseStaticEvent(messageMethod, msg);
ChatManager.Instance.AddChatHistory( new ChatManager.ChatEvent( DateTime.Now, playerSteamId, "{faction message}: " + message ) );
}
}
}
Expand Down Expand Up @@ -303,29 +305,25 @@ public static void SendDataMessage( ulong steamId, DataMessageType messageType,
Buffer.BlockCopy( data, 0, newData, msgIdString.Length + 1, data.Length );
*/

//hash a random long with the current time to make a decent quality guid for each message
byte[] randLong = new byte[sizeof(long)];
_random.NextBytes(randLong);
long uniqueId = 23;
uniqueId = uniqueId * 37 + BitConverter.ToInt64(randLong, 0);
uniqueId = uniqueId * 37 + DateTime.Now.GetHashCode();

byte[] guidBytes = Guid.NewGuid( ).ToByteArray( );

//this is a much more elegant and lightweight method
byte[] newData = new byte[sizeof(long)*2 + data.Length];
BitConverter.GetBytes( uniqueId ).CopyTo( newData, 0 );
BitConverter.GetBytes((long)messageType).CopyTo(newData, sizeof(long));
data.CopyTo( newData, sizeof(long)*2);
byte[] newData = new byte[sizeof(long) + guidBytes.Length + data.Length];
guidBytes.CopyTo( newData, 0 );
BitConverter.GetBytes((long)messageType).CopyTo(newData, guidBytes.Length);
data.CopyTo( newData, sizeof(long) + guidBytes.Length);

if ( newData.Length > 4096 )
{
SendMessagePartsTo( steamId, newData );
return;
}
//if ( newData.Length > 4096 )
//{
// SendMessagePartsTo( steamId, newData );
// return;
//}

//Wrapper.GameAction( ( ) =>
MySandboxGame.Static.Invoke( () =>
{
MyAPIGateway.Multiplayer.SendMessageTo( 9000, newData, steamId );
//MyAPIGateway.Multiplayer.SendMessageTo( 9000, newData, steamId );
ServerNetworkManager.Instance.SendModMessageTo( 9000, newData, steamId );
} );
}

Expand All @@ -342,27 +340,26 @@ public static void BroadcastDataMessage( DataMessageType messageType, byte[ ] da
Buffer.BlockCopy( data, 0, newData, msgIdString.Length + 1, data.Length );
*/
byte[] randLong = new byte[sizeof(long)];
_random.NextBytes(randLong);
long uniqueId = 23;
uniqueId = uniqueId * 37 + BitConverter.ToInt64( randLong, 0 );
uniqueId = uniqueId * 37 + DateTime.Now.GetHashCode();

byte[] newData = new byte[sizeof(long) * 2 + data.Length];
BitConverter.GetBytes(uniqueId).CopyTo(newData, 0);
BitConverter.GetBytes((long)messageType).CopyTo(newData, sizeof(long));
data.CopyTo(newData, sizeof(long) * 2);

if (newData.Length > 4096)
{
BroadcastMessageParts(newData);
return;
}

byte[] guidBytes = Guid.NewGuid().ToByteArray();

//this is a much more elegant and lightweight method
byte[] newData = new byte[sizeof(long) + guidBytes.Length + data.Length];
guidBytes.CopyTo(newData, 0);
BitConverter.GetBytes((long)messageType).CopyTo(newData, guidBytes.Length);
data.CopyTo(newData, sizeof(long) + guidBytes.Length);

//if (newData.Length > 4096)
//{
// BroadcastMessageParts(newData);
// return;
//}

//Wrapper.GameAction( ( ) =>
MySandboxGame.Static.Invoke(() =>
{
MyAPIGateway.Multiplayer.SendMessageToOthers( 9000, newData );
MyAPIGateway.Multiplayer.SendMessageToOthers( 9000, newData );
//ServerNetworkManager.Instance.BroadcastModMessage( 9000, newData );
} );
}

Expand Down

0 comments on commit efc13d9

Please sign in to comment.