Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to hide restoration #108

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions NetAF.Tests/Assets/Locations/Matrix_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using NetAF.Assets;
using NetAF.Serialization.Assets;
using NetAF.Serialization;

namespace NetAF.Tests.Assets.Locations
{
Expand Down Expand Up @@ -166,8 +167,8 @@ public void GivenVisitedRoomOn1And1VisitedRoomOn3_When_ThenReturnContaining1And3

var serialization = RoomSerialization.FromRoom(roomPositions[2].Room);
serialization.HasBeenVisited = true;
roomPositions[2].Room.RestoreFrom(serialization);
roomPositions[4].Room.RestoreFrom(serialization);
((IRestoreFromObjectSerialization<RoomSerialization>)roomPositions[2].Room).RestoreFrom(serialization);
((IRestoreFromObjectSerialization<RoomSerialization>)roomPositions[4].Room).RestoreFrom(serialization);

var matrix = new Matrix([.. roomPositions]);

Expand Down
10 changes: 10 additions & 0 deletions NetAF.Tests/Rendering/Console/AnsiColor_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@ public void GivenBlackAndWhite_WhenNotEquality_ThenReturnTrue()

Assert.IsTrue(result);
}

[TestMethod]
public void GivenR10G15B20_WhenToGray_ThenReturnColorWithR15G15B15()
{
var result = new AnsiColor(10, 15, 20).ToGray();

Assert.AreEqual(15, result.R);
Assert.AreEqual(15, result.G);
Assert.AreEqual(15, result.B);
}
}
}
2 changes: 1 addition & 1 deletion NetAF/Assets/Attributes/AttributeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private static int CapValue(int value, int min, int max)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(AttributeManagerSerialization serialization)
void IRestoreFromObjectSerialization<AttributeManagerSerialization>.RestoreFrom(AttributeManagerSerialization serialization)
{
RemoveAll();

Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ public void RemoveItem(Item item)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(CharacterSerialization serialization)
void IRestoreFromObjectSerialization<CharacterSerialization>.RestoreFrom(CharacterSerialization serialization)
{
base.RestoreFrom(serialization);
((IRestoreFromObjectSerialization<ExaminableSerialization>)this).RestoreFrom(serialization);

IsAlive = serialization.IsAlive;

Expand Down
7 changes: 3 additions & 4 deletions NetAF/Assets/Characters/NonPlayableCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ public NonPlayableCharacter(Identifier identifier, IDescription description, boo
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(NonPlayableCharacterSerialization serialization)
void IRestoreFromObjectSerialization<NonPlayableCharacterSerialization>.RestoreFrom(NonPlayableCharacterSerialization serialization)
{
base.RestoreFrom(serialization);

Conversation?.RestoreFrom(serialization.Conversation);
((IRestoreFromObjectSerialization<CharacterSerialization>)this).RestoreFrom(serialization);
((IRestoreFromObjectSerialization<ConversationSerialization>)Conversation)?.RestoreFrom(serialization.Conversation);
}

#endregion
Expand Down
9 changes: 5 additions & 4 deletions NetAF/Assets/ExaminableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NetAF.Assets.Attributes;
using NetAF.Commands;
using NetAF.Extensions;
using NetAF.Serialization;
using NetAF.Serialization.Assets;
using NetAF.Utilities;

Expand All @@ -12,7 +13,7 @@ namespace NetAF.Assets
/// <summary>
/// Represents an object that can be examined.
/// </summary>
public class ExaminableObject : IExaminable
public class ExaminableObject : IExaminable, IRestoreFromObjectSerialization<ExaminableSerialization>
{
#region StaticProperties

Expand Down Expand Up @@ -143,15 +144,15 @@ public Examination Examine(ExaminationScene scene)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(ExaminableSerialization serialization)
void IRestoreFromObjectSerialization<ExaminableSerialization>.RestoreFrom(ExaminableSerialization serialization)
{
IsPlayerVisible = serialization.IsPlayerVisible;
Attributes.RestoreFrom(serialization.AttributeManager);
((IRestoreFromObjectSerialization<AttributeManagerSerialization>)Attributes).RestoreFrom(serialization.AttributeManager);

foreach (var command in serialization.Commands)
{
var match = Array.Find(Commands, x => x.Help.Command.Equals(command.CommandName));
match?.RestoreFrom(command);
((IRestoreFromObjectSerialization<CustomCommandSerialization>)match)?.RestoreFrom(command);
}
}

Expand Down
4 changes: 1 addition & 3 deletions NetAF/Assets/IExaminable.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using NetAF.Assets.Attributes;
using NetAF.Commands;
using NetAF.Serialization.Assets;
using NetAF.Serialization;

namespace NetAF.Assets
{
/// <summary>
/// Represents any object that is examinable.
/// </summary>
public interface IExaminable : IPlayerVisible, IRestoreFromObjectSerialization<ExaminableSerialization>
public interface IExaminable : IPlayerVisible
{
/// <summary>
/// Get this objects identifier.
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public Interaction Interact(Item item)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(ItemSerialization serialization)
void IRestoreFromObjectSerialization<ItemSerialization>.RestoreFrom(ItemSerialization serialization)
{
base.RestoreFrom(serialization);
((IRestoreFromObjectSerialization<ExaminableSerialization>)this).RestoreFrom(serialization);
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/Locations/Exit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public Interaction Interact(Item item)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(ExitSerialization serialization)
void IRestoreFromObjectSerialization<ExitSerialization>.RestoreFrom(ExitSerialization serialization)
{
base.RestoreFrom(serialization);
((IRestoreFromObjectSerialization<ExaminableSerialization>)this).RestoreFrom(serialization);

IsLocked = serialization.IsLocked;
}
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/Locations/Overworld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ private static Examination ExamineThis(ExaminationRequest request)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(OverworldSerialization serialization)
void IRestoreFromObjectSerialization<OverworldSerialization>.RestoreFrom(OverworldSerialization serialization)
{
base.RestoreFrom(serialization);
((IRestoreFromObjectSerialization<ExaminableSerialization>)this).RestoreFrom(serialization);

foreach (var region in Regions)
{
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/Locations/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ internal static void NextPosition(Point3D current, Direction direction, out Poin
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(RegionSerialization serialization)
void IRestoreFromObjectSerialization<RegionSerialization>.RestoreFrom(RegionSerialization serialization)
{
base.RestoreFrom(serialization);
((IRestoreFromObjectSerialization<ExaminableSerialization>)this).RestoreFrom(serialization);

var rooms = roomPositions.Select(x => x.Room).ToArray();

Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/Locations/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,9 @@ public void RemoveItem(Item item)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(RoomSerialization serialization)
void IRestoreFromObjectSerialization<RoomSerialization>.RestoreFrom(RoomSerialization serialization)
{
base.RestoreFrom(serialization);
((IRestoreFromObjectSerialization<ExaminableSerialization>)this).RestoreFrom(serialization);

HasBeenVisited = serialization.HasBeenVisited;

Expand Down
2 changes: 1 addition & 1 deletion NetAF/Commands/CustomCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Reaction Invoke(Logic.Game game)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(CustomCommandSerialization serialization)
void IRestoreFromObjectSerialization<CustomCommandSerialization>.RestoreFrom(CustomCommandSerialization serialization)
{
IsPlayerVisible = serialization.IsPlayerVisible;
}
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Conversations/Conversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Reaction Respond(Response response, Game game)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(ConversationSerialization serialization)
void IRestoreFromObjectSerialization<ConversationSerialization>.RestoreFrom(ConversationSerialization serialization)
{
if (serialization.CurrentParagraph == ConversationSerialization.NoCurrentParagraph)
CurrentParagraph = null;
Expand Down
5 changes: 3 additions & 2 deletions NetAF/Logic/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using NetAF.Logic.Configuration;
using NetAF.Logic.Modes;
using NetAF.Serialization;
using NetAF.Serialization.Assets;
using NetAF.Utilities;

namespace NetAF.Logic
Expand Down Expand Up @@ -474,7 +475,7 @@ public static void Execute(GameCreationCallback creator)
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(GameSerialization serialization)
void IRestoreFromObjectSerialization<GameSerialization>.RestoreFrom(GameSerialization serialization)
{
// resolve asset locations
AssetArranger.Arrange(this, serialization);
Expand All @@ -487,7 +488,7 @@ public void RestoreFrom(GameSerialization serialization)
foreach (var player in serialization.Players)
{
var match = Array.Find(Catalog.Players, x => x.Identifier.Equals(player.Identifier));
match?.RestoreFrom(player);
((IRestoreFromObjectSerialization<CharacterSerialization>)match)?.RestoreFrom(player);
}

// restore active player
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Logic/PlayableCharacterLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class PlayableCharacterLocation(string playerIdentifier, string regionIde
public static PlayableCharacterLocation FromSerialization(PlayableCharacterLocationSerialization serialization)
{
PlayableCharacterLocation location = new(string.Empty, string.Empty, string.Empty);
location.RestoreFrom(serialization);
((IRestoreFromObjectSerialization<PlayableCharacterLocationSerialization>)location).RestoreFrom(serialization);
return location;
}

Expand All @@ -52,7 +52,7 @@ public static PlayableCharacterLocation FromSerialization(PlayableCharacterLocat
/// Restore this object from a serialization.
/// </summary>
/// <param name="serialization">The serialization to restore from.</param>
public void RestoreFrom(PlayableCharacterLocationSerialization serialization)
void IRestoreFromObjectSerialization<PlayableCharacterLocationSerialization>.RestoreFrom(PlayableCharacterLocationSerialization serialization)
{
PlayerIdentifier = serialization.PlayerIdentifier;
RegionIdentifier = serialization.RegionIdentifier;
Expand Down
14 changes: 14 additions & 0 deletions NetAF/Rendering/Console/AnsiColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ public struct AnsiColor(byte r, byte g, byte b) : IEquatable<AnsiColor>

#endregion

#region Methods

/// <summary>
/// Convert this color to the nearest gray.
/// </summary>
/// <returns>The nearest gray.</returns>
public AnsiColor ToGray()
{
var gray = (byte)((R + B + G) / 3);
return new AnsiColor(gray, gray, gray);
}

#endregion

#region Overrides of Object

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Rendering/Console/GridVisualFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class GridVisualFrame(GridVisualBuilder builder) : IFrame
/// <returns>The color.</returns>
private AnsiColor GetForegroundColor(int x, int y, bool suppressColor)
{
return !suppressColor ? builder.GetCellForegroundColor(x, y) : AnsiColor.White;
return !suppressColor ? builder.GetCellForegroundColor(x, y) : builder.GetCellForegroundColor(x, y).ToGray();
}

/// <summary>
Expand All @@ -29,7 +29,7 @@ private AnsiColor GetForegroundColor(int x, int y, bool suppressColor)
/// <returns>The color.</returns>
private AnsiColor GetBackgroundColor(int x, int y, bool suppressColor)
{
return !suppressColor ? builder.GetCellBackgroundColor(x, y) : AnsiColor.White;
return !suppressColor ? builder.GetCellBackgroundColor(x, y) : builder.GetCellBackgroundColor(x, y).ToGray();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static AttributeManagerSerialization FromAttributeManager(AttributeManage
/// <param name="attributeManager">The attribute manager to restore.</param>
public void Restore(AttributeManager attributeManager)
{
attributeManager.RestoreFrom(this);
((IRestoreFromObjectSerialization<AttributeManagerSerialization>)attributeManager).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/Assets/CharacterSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static CharacterSerialization FromCharacter(Character character)
/// <param name="character">The character to restore.</param>
public void Restore(Character character)
{
character.RestoreFrom(this);
((IRestoreFromObjectSerialization<CharacterSerialization>)character).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/Assets/ConversationSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static ConversationSerialization FromConversation(Conversation conversati
/// <param name="conversation">The conversation to restore.</param>
public void Restore(Conversation conversation)
{
conversation.RestoreFrom(this);
((IRestoreFromObjectSerialization<ConversationSerialization>)conversation).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/Assets/ExaminableSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static ExaminableSerialization FromIExaminable(IExaminable examinable)
/// <param name="examinable">The examinable to restore.</param>
public virtual void Restore(IExaminable examinable)
{
examinable.RestoreFrom(this);
((IRestoreFromObjectSerialization<ExaminableSerialization>)examinable).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/Assets/ExitSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static ExitSerialization FromExit(Exit exit)
/// <param name="exit">The exit to restore.</param>
public void Restore(Exit exit)
{
exit.RestoreFrom(this);
((IRestoreFromObjectSerialization<ExitSerialization>)exit).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/Assets/ItemSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ItemSerialization FromItem(Item item)
/// <param name="item">The item to restore.</param>
public void Restore(Item item)
{
item.RestoreFrom(this);
((IRestoreFromObjectSerialization<ItemSerialization>)item).RestoreFrom(this);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static NonPlayableCharacterSerialization FromNonPlayableCharacter(NonPlay
/// <param name="character">The character to restore.</param>
public void Restore(NonPlayableCharacter character)
{
character.RestoreFrom(this);
((IRestoreFromObjectSerialization<NonPlayableCharacterSerialization>)character).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/Assets/OverworldSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static OverworldSerialization FromOverworld(Overworld overworld)
/// <param name="overworld">The overworld to restore.</param>
public void Restore(Overworld overworld)
{
overworld.RestoreFrom(this);
((IRestoreFromObjectSerialization<OverworldSerialization>)overworld).RestoreFrom(this);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static PlayableCharacterLocationSerialization FromPlayableCharacterLocati
/// <param name="location">The attribute to restore.</param>
public void Restore(PlayableCharacterLocation location)
{
location.RestoreFrom(this);
((IRestoreFromObjectSerialization<PlayableCharacterLocationSerialization>)location).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/Assets/RegionSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static RegionSerialization FromRegion(Region region)
/// <param name="region">The region to restore.</param>
public void Restore(Region region)
{
region.RestoreFrom(this);
((IRestoreFromObjectSerialization<RegionSerialization>)region).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/Assets/RoomSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static RoomSerialization FromRoom(Room room)
/// <param name="room">The room to restore.</param>
public void Restore(Room room)
{
room.RestoreFrom(this);
((IRestoreFromObjectSerialization<RoomSerialization>)room).RestoreFrom(this);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Serialization/CustomCommandSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static CustomCommandSerialization FromCustomCommand(CustomCommand customC
/// <param name="command">The command to restore.</param>
public virtual void Restore(CustomCommand command)
{
command.RestoreFrom(this);
((IRestoreFromObjectSerialization<CustomCommandSerialization>)command).RestoreFrom(this);
}

#endregion
Expand Down
Loading
Loading