Adds Network Resource Uploading for admins. (#6904)

Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
Vera Aguilera Puerto
2022-03-26 12:46:37 +01:00
committed by GitHub
parent 5954b7668c
commit eb54f4b224
21 changed files with 2657 additions and 6 deletions

View File

@@ -0,0 +1,39 @@
using Lidgren.Network;
using Robust.Shared.Network;
using Robust.Shared.Utility;
namespace Content.Shared.Administration;
public sealed class NetworkResourceUploadMessage : NetMessage
{
public override NetDeliveryMethod DeliveryMethod => NetDeliveryMethod.ReliableUnordered;
public override MsgGroups MsgGroup => MsgGroups.Command;
public byte[] Data { get; set; } = Array.Empty<byte>();
public ResourcePath RelativePath { get; set; } = ResourcePath.Self;
public NetworkResourceUploadMessage()
{
}
public NetworkResourceUploadMessage(byte[] data, ResourcePath relativePath)
{
Data = data;
RelativePath = relativePath;
}
public override void ReadFromBuffer(NetIncomingMessage buffer)
{
var dataLength = buffer.ReadVariableInt32();
Data = buffer.ReadBytes(dataLength);
RelativePath = new ResourcePath(buffer.ReadString(), buffer.ReadString());
}
public override void WriteToBuffer(NetOutgoingMessage buffer)
{
buffer.WriteVariableInt32(Data.Length);
buffer.Write(Data);
buffer.Write(RelativePath.ToString());
buffer.Write(RelativePath.Separator);
}
}

View File

@@ -0,0 +1,41 @@
using Robust.Shared.ContentPack;
using Robust.Shared.Network;
using Robust.Shared.Utility;
namespace Content.Shared.Administration;
/// <summary>
/// Manager that allows resources to be added at runtime by admins.
/// They will be sent to all clients automatically.
/// </summary>
public abstract class SharedNetworkResourceManager : IDisposable
{
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] protected readonly IResourceManager ResourceManager = default!;
public const double BytesToMegabytes = 0.000001d;
/// <summary>
/// The prefix for any and all downloaded network resources.
/// </summary>
private static readonly ResourcePath Prefix = ResourcePath.Root / "Uploaded";
protected readonly MemoryContentRoot ContentRoot = new();
public virtual void Initialize()
{
_netManager.RegisterNetMessage<NetworkResourceUploadMessage>(ResourceUploadMsg);
// Add our content root to the resource manager.
ResourceManager.AddRoot(Prefix, ContentRoot);
}
protected abstract void ResourceUploadMsg(NetworkResourceUploadMessage msg);
public void Dispose()
{
// This is called automatically when the IoCManager's dependency collection is cleared.
// MemoryContentRoot uses a ReaderWriterLockSlim, which we need to dispose of.
ContentRoot.Dispose();
}
}

View File

@@ -737,5 +737,37 @@ namespace Content.Shared.CCVar
public static readonly CVarDef<string> DestinationFile =
CVarDef.Create("autogen.destination_file", "", CVar.SERVER | CVar.SERVERONLY);
/*
* Network Resource Manager
*/
/// <summary>
/// Controls whether new resources can be uploaded by admins.
/// Does not prevent already uploaded resources from being sent.
/// </summary>
public static readonly CVarDef<bool> ResourceUploadingEnabled =
CVarDef.Create("netres.enabled", true, CVar.REPLICATED | CVar.SERVER);
/// <summary>
/// Controls the data size limit in megabytes for uploaded resources. If they're too big, they will be dropped.
/// Set to zero or a negative value to disable limit.
/// </summary>
public static readonly CVarDef<float> ResourceUploadingLimitMb =
CVarDef.Create("netres.limit", 3f, CVar.REPLICATED | CVar.SERVER);
/// <summary>
/// Whether uploaded files will be stored in the server's database.
/// This is useful to keep "logs" on what files admins have uploaded in the past.
/// </summary>
public static readonly CVarDef<bool> ResourceUploadingStoreEnabled =
CVarDef.Create("netres.store_enabled", true, CVar.SERVER | CVar.SERVERONLY);
/// <summary>
/// Numbers of days before stored uploaded files are deleted. Set to zero or negative to disable auto-delete.
/// This is useful to free some space automatically. Auto-deletion runs only on server boot.
/// </summary>
public static readonly CVarDef<int> ResourceUploadingStoreDeletionDays =
CVarDef.Create("netres.store_deletion_days", 30, CVar.SERVER | CVar.SERVERONLY);
}
}