-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1067 from Bannerlord-Coop-Team/946-Sync-BasicChar…
…acterObjectFields 946 sync basic character object fields
- Loading branch information
Showing
12 changed files
with
382 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
source/GameInterface/Services/CharacterSkills/CharacterSkillsRegistry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using GameInterface.Services.Registry; | ||
using System.Threading; | ||
using TaleWorlds.Core; | ||
using TaleWorlds.ObjectSystem; | ||
|
||
namespace GameInterface.Services.CharacterSkills | ||
{ | ||
internal class CharacterSkillsRegistry : RegistryBase<MBCharacterSkills> | ||
{ | ||
private const string IdPrefix = "CoopCharacterSkills"; | ||
private static int InstanceCounter = 0; | ||
|
||
public CharacterSkillsRegistry(IRegistryCollection collection) : base(collection) | ||
{ | ||
} | ||
|
||
public override void RegisterAll() | ||
{ | ||
var objectManager = MBObjectManager.Instance; | ||
|
||
if (objectManager == null) | ||
{ | ||
Logger.Error("Unable to register objects when CampaignObjectManager is null"); | ||
return; | ||
} | ||
|
||
foreach (var skill in objectManager.GetObjectTypeList<MBCharacterSkills>()) | ||
{ | ||
RegisterExistingObject(skill.StringId, skill); | ||
} | ||
} | ||
|
||
protected override string GetNewId(MBCharacterSkills obj) | ||
{ | ||
return $"{IdPrefix}_{Interlocked.Increment(ref InstanceCounter)}"; | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
source/GameInterface/Services/CharacterSkills/Handlers/CharacterSkillsLifetimeHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Common.Logging; | ||
using Common.Messaging; | ||
using Common.Network; | ||
using Common.Util; | ||
using GameInterface.Services.CharacterSkills.Messages; | ||
using GameInterface.Services.ObjectManager; | ||
using Serilog; | ||
using TaleWorlds.Core; | ||
|
||
namespace GameInterface.Services.CharacterSkills.Handlers | ||
{ | ||
internal class CharacterSkillsLifetimeHandler : IHandler | ||
{ | ||
private static readonly ILogger Logger = LogManager.GetLogger<CharacterSkillsLifetimeHandler>(); | ||
private readonly IMessageBroker messageBroker; | ||
private readonly IObjectManager objectManager; | ||
private readonly INetwork network; | ||
|
||
public CharacterSkillsLifetimeHandler(IMessageBroker messageBroker, IObjectManager objectManager, INetwork network) | ||
{ | ||
this.messageBroker = messageBroker; | ||
this.objectManager = objectManager; | ||
this.network = network; | ||
messageBroker.Subscribe<CharacterSkillsCreated>(Handle); | ||
messageBroker.Subscribe<NetworkCreateCharacterSkills>(Handle); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
messageBroker.Unsubscribe<CharacterSkillsCreated>(Handle); | ||
messageBroker.Unsubscribe<NetworkCreateCharacterSkills>(Handle); | ||
} | ||
|
||
private void Handle(MessagePayload<CharacterSkillsCreated> obj) | ||
{ | ||
var payload = obj.What; | ||
|
||
if (objectManager.AddNewObject(payload.CharacterSkills, out string CharacterSkillsId) == false) return; | ||
|
||
var message = new NetworkCreateCharacterSkills(CharacterSkillsId); | ||
network.SendAll(message); | ||
} | ||
|
||
private void Handle(MessagePayload<NetworkCreateCharacterSkills> obj) | ||
{ | ||
var payload = obj.What; | ||
|
||
var CharacterSkills = ObjectHelper.SkipConstructor<MBCharacterSkills>(); | ||
if (objectManager.AddExisting(payload.CharacterSkillsId, CharacterSkills) == false) | ||
{ | ||
Logger.Error("Failed to add existing CharacterSkill, {id}", payload.CharacterSkillsId); | ||
return; | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
source/GameInterface/Services/CharacterSkills/Messages/CharacterSkillsCreated.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Common.Messaging; | ||
using TaleWorlds.Core; | ||
|
||
namespace GameInterface.Services.CharacterSkills.Messages | ||
{ | ||
internal class CharacterSkillsCreated : IEvent | ||
{ | ||
public MBCharacterSkills CharacterSkills { get; } | ||
|
||
public CharacterSkillsCreated(MBCharacterSkills characterSkills) | ||
{ | ||
CharacterSkills = characterSkills; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
source/GameInterface/Services/CharacterSkills/Messages/NetworkCreateCharacterSkills.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Common.Messaging; | ||
using ProtoBuf; | ||
|
||
namespace GameInterface.Services.CharacterSkills.Messages | ||
{ | ||
[ProtoContract(SkipConstructor = true)] | ||
internal class NetworkCreateCharacterSkills : ICommand | ||
{ | ||
[ProtoMember(1)] | ||
public string CharacterSkillsId; | ||
public NetworkCreateCharacterSkills(string characterSkillsId) | ||
{ | ||
CharacterSkillsId = characterSkillsId; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
source/GameInterface/Services/CharacterSkills/Patches/CharacterSkillsLifetimePatches.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.CharacterSkills.Messages; | ||
using HarmonyLib; | ||
using Serilog; | ||
using System; | ||
using TaleWorlds.Core; | ||
|
||
namespace GameInterface.Services.CharacterSkills.Patches | ||
{ | ||
/// <summary> | ||
/// Lifetime Patches for CharacterSkills | ||
/// </summary> | ||
[HarmonyPatch] | ||
internal class CharacterSkillsLifetimePatches | ||
{ | ||
private static ILogger Logger = LogManager.GetLogger<CharacterSkillsLifetimePatches>(); | ||
|
||
[HarmonyPatch(typeof(MBCharacterSkills), MethodType.Constructor)] | ||
[HarmonyPrefix] | ||
private static bool CreateCharacterSkillsPrefix(ref MBCharacterSkills __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(MBCharacterSkills), Environment.StackTrace); | ||
return false; | ||
} | ||
|
||
var message = new CharacterSkillsCreated(__instance); | ||
|
||
MessageBroker.Instance.Publish(__instance, message); | ||
|
||
return true; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
source/GameInterface/Services/EquipmentRoster/EquipmentRosterRegistry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using GameInterface.Services.Registry; | ||
using System.Threading; | ||
using TaleWorlds.Core; | ||
using TaleWorlds.ObjectSystem; | ||
|
||
namespace GameInterface.Services.EquipmentRoster | ||
{ | ||
internal class EquipmentRosterRegistry : RegistryBase<MBEquipmentRoster> | ||
{ | ||
private const string IdPrefix = "CoopEquipmentRoster"; | ||
private static int InstanceCounter = 0; | ||
|
||
public EquipmentRosterRegistry(IRegistryCollection collection) : base(collection) | ||
{ | ||
} | ||
|
||
public override void RegisterAll() | ||
{ | ||
var objectManager = MBObjectManager.Instance; | ||
|
||
if (objectManager == null) | ||
{ | ||
Logger.Error("Unable to register objects when CampaignObjectManager is null"); | ||
return; | ||
} | ||
|
||
foreach (var equipRoster in objectManager.GetObjectTypeList<MBEquipmentRoster>()) | ||
{ | ||
RegisterExistingObject(equipRoster.StringId, equipRoster); | ||
} | ||
} | ||
|
||
protected override string GetNewId(MBEquipmentRoster obj) | ||
{ | ||
return $"{IdPrefix}_{Interlocked.Increment(ref InstanceCounter)}"; | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
source/GameInterface/Services/EquipmentRoster/Handlers/EquipmentRosterLifetimeHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Common.Logging; | ||
using Common.Messaging; | ||
using Common.Network; | ||
using Common.Util; | ||
using GameInterface.Services.EquipmentRoster.Messages; | ||
using GameInterface.Services.ObjectManager; | ||
using Serilog; | ||
using TaleWorlds.Core; | ||
|
||
namespace GameInterface.Services.EquipmentRoster.Handlers | ||
{ | ||
internal class EquipmentRosterLifetimeHandler : IHandler | ||
{ | ||
private static readonly ILogger Logger = LogManager.GetLogger<EquipmentRosterLifetimeHandler>(); | ||
private readonly IMessageBroker messageBroker; | ||
private readonly IObjectManager objectManager; | ||
private readonly INetwork network; | ||
|
||
public EquipmentRosterLifetimeHandler(IMessageBroker messageBroker, IObjectManager objectManager, INetwork network) | ||
{ | ||
this.messageBroker = messageBroker; | ||
this.objectManager = objectManager; | ||
this.network = network; | ||
messageBroker.Subscribe<EquipmentRosterCreated>(Handle); | ||
messageBroker.Subscribe<NetworkCreateEquipmentRoster>(Handle); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
messageBroker.Unsubscribe<EquipmentRosterCreated>(Handle); | ||
messageBroker.Unsubscribe<NetworkCreateEquipmentRoster>(Handle); | ||
} | ||
|
||
private void Handle(MessagePayload<EquipmentRosterCreated> obj) | ||
{ | ||
var payload = obj.What; | ||
|
||
if (objectManager.AddNewObject(payload.EquipmentRoster, out string EquipmentRosterId) == false) return; | ||
|
||
var message = new NetworkCreateEquipmentRoster(EquipmentRosterId); | ||
network.SendAll(message); | ||
} | ||
|
||
private void Handle(MessagePayload<NetworkCreateEquipmentRoster> obj) | ||
{ | ||
var payload = obj.What; | ||
|
||
var EquipmentRoster = ObjectHelper.SkipConstructor<MBEquipmentRoster>(); | ||
if (objectManager.AddExisting(payload.EquipmentRosterId, EquipmentRoster) == false) | ||
{ | ||
Logger.Error("Failed to add existing EquipmentRoster, {id}", payload.EquipmentRosterId); | ||
return; | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
source/GameInterface/Services/EquipmentRoster/Messages/EquipmentRosterCreated.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Common.Messaging; | ||
using TaleWorlds.Core; | ||
|
||
namespace GameInterface.Services.EquipmentRoster.Messages | ||
{ | ||
internal class EquipmentRosterCreated : IEvent | ||
{ | ||
public MBEquipmentRoster EquipmentRoster { get; } | ||
|
||
public EquipmentRosterCreated(MBEquipmentRoster equipmentRoster) | ||
{ | ||
EquipmentRoster = equipmentRoster; | ||
} | ||
} | ||
} |
Oops, something went wrong.