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

CharacterObject Lifetime #986

Merged
merged 4 commits into from
Oct 4, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using E2E.Tests.Environment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Settlements;
using Xunit.Abstractions;

namespace E2E.Tests.Services.CharacterObjects
{
public class CharacterObjectLifetimeTests
{
E2ETestEnvironment TestEnvironment { get; }

public CharacterObjectLifetimeTests(ITestOutputHelper output)
{
TestEnvironment = new E2ETestEnvironment(output);
}

public void Dispose()
{
TestEnvironment.Dispose();
}

[Fact]
public void ServerCreateCharacterObject_SyncAllClients()
{
// Arrange
var server = TestEnvironment.Server;

// Act
string? characterId = null;
server.Call(() =>
{
var characterObject = new CharacterObject();
Assert.True(server.ObjectManager.TryGetId(characterObject, out characterId));
});

// Assert
Assert.True(server.ObjectManager.TryGetObject<CharacterObject>(characterId, out var _));

foreach (var client in TestEnvironment.Clients)
{
Assert.True(client.ObjectManager.TryGetObject<CharacterObject>(characterId, out var _));
}
}


[Fact]
public void ClientCreateCharacterObject_DoesNothing()
{
// Arrange
var client1 = TestEnvironment.Clients.First();
var server = TestEnvironment.Server;

// Act
string? characterId = null;
client1.Call(() =>
{
var characterObject = new CharacterObject();
Assert.False(client1.ObjectManager.TryGetId(characterObject, out characterId));
});

// Assert
Assert.False(server.ObjectManager.TryGetObject<CharacterObject>(characterId, out var _));

foreach (var client in TestEnvironment.Clients)
{
Assert.False(client.ObjectManager.TryGetObject<CharacterObject>(characterId, out var _));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using GameInterface.Services.Registry;
using System;
using System.Threading;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.Settlements.Buildings;

namespace GameInterface.Services.Armies;

/// <summary>
/// Registry for <see cref="CharacterObject"/> type
/// </summary>
internal class CharacterObjectRegistry : RegistryBase<CharacterObject>
{
private const string CharacterObjectPrefix = "CoopCharacterObject";
private static int InstanceCounter = 0;

public CharacterObjectRegistry(IRegistryCollection collection) : base(collection) { }

public override void RegisterAll()
{
foreach(CharacterObject character in Campaign.Current.Characters)
{
if (RegisterNewObject(character, out var _) == false)
{
Logger.Error($"Unable to register {character}");
}
}
}

protected override string GetNewId(CharacterObject obj)
{
return $"{CharacterObjectPrefix}_{Interlocked.Increment(ref InstanceCounter)}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Common.Logging;
using Common.Messaging;
using Common.Network;
using Common.Util;
using GameInterface.Services.Armies.Messages.Lifetime;
using GameInterface.Services.CharacterObjects.Messages;
using GameInterface.Services.ObjectManager;
using Serilog;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Settlements.Buildings;

namespace GameInterface.Services.Buildings.Handlers
{
internal class CharacterObjectLifetimeHandler : IHandler
{
private static readonly ILogger Logger = LogManager.GetLogger<CharacterObjectLifetimeHandler>();
private readonly IMessageBroker messageBroker;
private readonly IObjectManager objectManager;
private readonly INetwork network;

public CharacterObjectLifetimeHandler(IMessageBroker messageBroker, IObjectManager objectManager, INetwork network)
{
this.messageBroker = messageBroker;
this.objectManager = objectManager;
this.network = network;
messageBroker.Subscribe<CharacterObjectCreated>(Handle_CharacterCreated);
messageBroker.Subscribe<NetworkCreateCharacterObject>(Handle_CreateCharacter);
}

public void Dispose()
{
messageBroker.Unsubscribe<CharacterObjectCreated>(Handle_CharacterCreated);
messageBroker.Unsubscribe<NetworkCreateCharacterObject>(Handle_CreateCharacter);
}

private void Handle_CharacterCreated(MessagePayload<CharacterObjectCreated> obj)
{
var payload = obj.What;

if (objectManager.AddNewObject(payload.CharacterObject, out string characterObjectId) == false) return;

var message = new NetworkCreateCharacterObject(characterObjectId);
network.SendAll(message);
}

private void Handle_CreateCharacter(MessagePayload<NetworkCreateCharacterObject> obj)
{
var payload = obj.What;

var characterObject = ObjectHelper.SkipConstructor<CharacterObject>();
if (objectManager.AddExisting(payload.CharacterObjectId, characterObject) == false)
{
Logger.Error("Failed to add existing Building, {id}", payload.CharacterObjectId);
return;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Common.Messaging;
using TaleWorlds.CampaignSystem;

namespace GameInterface.Services.CharacterObjects.Messages
{
internal record CharacterObjectCreated : IEvent
{
public CharacterObject CharacterObject { get; }

public CharacterObjectCreated(CharacterObject characterObject)
{
CharacterObject = characterObject;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Common.Messaging;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Text;

namespace GameInterface.Services.CharacterObjects.Messages
{
[ProtoContract(SkipConstructor = true)]
internal class NetworkCreateCharacterObject : ICommand
{
[ProtoMember(1)]
public string CharacterObjectId;

public NetworkCreateCharacterObject(string characterObjectId)
{
CharacterObjectId = characterObjectId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Common.Logging;
using Common.Messaging;
using GameInterface.Policies;
using GameInterface.Services.CharacterObjects.Messages;
using HarmonyLib;
using Serilog;
using System;
using TaleWorlds.CampaignSystem;

namespace GameInterface.Services.CharacterObjects.Patches
{
/// <summary>
/// Lifetime Patches for CharacterObjects
/// </summary>
[HarmonyPatch]
internal class CharacterObjectLifetimePatches
{
private static ILogger Logger = LogManager.GetLogger<CharacterObjectLifetimePatches>();

[HarmonyPatch(typeof(CharacterObject), MethodType.Constructor)]
[HarmonyPrefix]
private static bool CreateCharacterObjectPrefix(ref CharacterObject __instance)
{
// Call original if we call this function
if (CallOriginalPolicy.IsOriginalAllowed()) return true;

if (ModInformation.IsClient)
{
Logger.Error("Client created unmanaged {name}\n"
+ "Callstack: {callstack}", typeof(CharacterObject), Environment.StackTrace);
return true;
}

var message = new CharacterObjectCreated(__instance);

MessageBroker.Instance.Publish(__instance, message);

return true;
}
}
}
Loading