Files
wwdpublic/Content.Server/Mapping/MappingManager.cs
sleepyyapril a4ab8448b9 Mapping Mini-Wizmerge & New Central Command (#1610)
# Description

<!--
Explain this PR in as much detail as applicable

Some example prompts to consider:
How might this affect the game? The codebase?
What might be some alternatives to this?
How/Who does this benefit/hurt [the game/codebase]?
-->

Ports https://github.com/space-wizards/space-station-14/pull/32294
Ports https://github.com/ss14-harmony/ss14-harmony/pull/310 (and
everything needed for it to function)
Early-merges
https://github.com/space-wizards/space-station-14/pull/34302

Adds the ability for multiple central command maps that get randomly
selected.
Tested and works.

---

# Changelog

<!--
You can add an author after the `🆑` to change the name that appears
in the changelog (ex: `🆑 Death`)
Leaving it blank will default to your GitHub display name
This includes all available types for the changelog
-->

🆑 Several contributors
- add: Added a new central command map that is randomly picked alongside
the old one (thank you to Spanky from Harmony)
- add: Added Advanced SMES for mappers.
- add: Added the atmospheric network monitor for seeing what the
temperature, moles, and pressure is on every pipe everywhere through a
computer.
- add: Nukie med bundle now contains a compact defibrillator.
- add: Ported a better mapping editor.
- add: Added the throngler plushie.
- remove: Removed the Throngler as a possible loot spawn for gamble
crates.

---------

Signed-off-by: sleepyyapril <123355664+sleepyyapril@users.noreply.github.com>

(cherry picked from commit 9272f65b64392f66a7cd4fd7c84bb152dc93b65a)
2025-01-20 21:34:45 +03:00

133 lines
4.7 KiB
C#

using System.IO;
using System.Linq;
using Content.Server.Administration.Managers;
using Content.Shared.Administration;
using Content.Shared.Mapping;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.ContentPack;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Markdown;
using Robust.Shared.Serialization.Markdown.Mapping;
using Robust.Shared.Utility;
using YamlDotNet.Core;
using YamlDotNet.RepresentationModel;
namespace Content.Server.Mapping;
public sealed class MappingManager : IPostInjectInit
{
[Dependency] private readonly IAdminManager _admin = default!;
[Dependency] private readonly ILogManager _log = default!;
[Dependency] private readonly IMapManager _map = default!;
[Dependency] private readonly IServerNetManager _net = default!;
[Dependency] private readonly IPlayerManager _players = default!;
[Dependency] private readonly IEntitySystemManager _systems = default!;
[Dependency] private readonly ISerializationManager _serialization = default!;
[Dependency] private readonly IResourceManager _resourceMan = default!;
private ISawmill _sawmill = default!;
private ZStdCompressionContext _zstd = default!;
private const string FavoritesPath = "/mapping_editor_favorites.yml";
public void PostInject()
{
_net.RegisterNetMessage<MappingFavoritesSaveMessage>(OnMappingFavoritesSave);
_net.RegisterNetMessage<MappingFavoritesLoadMessage>(OnMappingFavoritesLoad);
_net.RegisterNetMessage<MappingFavoritesDataMessage>();
_sawmill = _log.GetSawmill("mapping");
#if !FULL_RELEASE
_net.RegisterNetMessage<MappingSaveMapMessage>(OnMappingSaveMap);
_net.RegisterNetMessage<MappingSaveMapErrorMessage>();
_net.RegisterNetMessage<MappingMapDataMessage>();
_zstd = new ZStdCompressionContext();
#endif
}
private void OnMappingSaveMap(MappingSaveMapMessage message)
{
#if !FULL_RELEASE
try
{
if (!_players.TryGetSessionByChannel(message.MsgChannel, out var session) ||
!_admin.IsAdmin(session, true) ||
!_admin.HasAdminFlag(session, AdminFlags.Host) ||
session.AttachedEntity is not { } player)
{
return;
}
var mapId = _systems.GetEntitySystem<TransformSystem>().GetMapCoordinates(player).MapId;
var mapEntity = _map.GetMapEntityIdOrThrow(mapId);
var data = _systems.GetEntitySystem<MapLoaderSystem>().GetSaveData(mapEntity);
var document = new YamlDocument(data.ToYaml());
var stream = new YamlStream { document };
var writer = new StringWriter();
stream.Save(new YamlMappingFix(new Emitter(writer)), false);
var msg = new MappingMapDataMessage()
{
Context = _zstd,
Yml = writer.ToString()
};
_net.ServerSendMessage(msg, message.MsgChannel);
}
catch (Exception e)
{
_sawmill.Error($"Error saving map in mapping mode:\n{e}");
var msg = new MappingSaveMapErrorMessage();
_net.ServerSendMessage(msg, message.MsgChannel);
}
#endif
}
private void OnMappingFavoritesSave(MappingFavoritesSaveMessage message)
{
var mapping = new MappingDataNode();
mapping.Add("prototypes", _serialization.WriteValue(message.PrototypeIDs, notNullableOverride: true));
var path = new ResPath(FavoritesPath);
using var writer = _resourceMan.UserData.OpenWriteText(path);
var stream = new YamlStream {new(mapping.ToYaml())};
stream.Save(new YamlMappingFix(new Emitter(writer)), false);
}
private void OnMappingFavoritesLoad(MappingFavoritesLoadMessage message)
{
var path = new ResPath(FavoritesPath);
if (!_resourceMan.UserData.Exists(path))
return;
try
{
var reader = _resourceMan.UserData.OpenText(path);
var documents = DataNodeParser.ParseYamlStream(reader).First();
var mapping = (MappingDataNode) documents.Root;
if (!mapping.TryGet("prototypes", out var prototypesNode))
return;
var ids = _serialization.Read<string[]>(prototypesNode, notNullableOverride: true).ToList();
var msg = new MappingFavoritesDataMessage()
{
PrototypeIDs = ids,
};
_net.ServerSendMessage(msg, message.MsgChannel);
}
catch (Exception e)
{
_sawmill.Error("Failed to load user favorite objects: " + e);
}
}
}