Skip to content

Commit c667b18

Browse files
committed
Implement implicit casting for PropertyValue
1 parent 74407c8 commit c667b18

File tree

11 files changed

+79
-97
lines changed

11 files changed

+79
-97
lines changed

src/MHServerEmu/Billing/BillingService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ private void OnBuyItemFromCatalog(FrontendClient client, NetMessageBuyItemFromCa
126126

127127
// Send NetMessageSetProperty message with a CostumeCurrent property for the purchased costume
128128
client.SendMessage(MuxChannel, new(
129-
Property.ToNetMessageSetProperty(replicationId, new(PropertyEnum.CostumeCurrent), new(entry.GuidItems[0].ItemPrototypeRuntimeIdForClient))
129+
Property.ToNetMessageSetProperty(replicationId, new(PropertyEnum.CostumeCurrent), entry.GuidItems[0].ItemPrototypeRuntimeIdForClient)
130130
));
131131

132132
// Update library
133133
PropertyParam enumValue = Property.ToParam(PropertyEnum.AvatarLibraryCostume, 1, (PrototypeId)currentAvatar.Prototype);
134134

135135
client.SendMessage(MuxChannel, new(
136-
Property.ToNetMessageSetProperty(9078332, new(PropertyEnum.AvatarLibraryCostume, 0, enumValue), new(costumePrototype.DataRef))));
136+
Property.ToNetMessageSetProperty(9078332, new(PropertyEnum.AvatarLibraryCostume, 0, enumValue), costumePrototype.DataRef)));
137137

138138
SendBuyItemResponse(client, true, BuyItemResultErrorCodes.BUY_RESULT_ERROR_SUCCESS, buyItemFromCatalog.SkuId);
139139
}

src/MHServerEmu/Common/Commands/GameCommands.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public string Costume(string[] @params, FrontendClient client)
184184

185185
// Send NetMessageSetProperty message with a CostumeCurrent property for the purchased costume
186186
client.SendMessage(1, new(
187-
Property.ToNetMessageSetProperty(replicationId, new(PropertyEnum.CostumeCurrent), new(prototypeId))
187+
Property.ToNetMessageSetProperty(replicationId, new(PropertyEnum.CostumeCurrent), prototypeId)
188188
));
189189
return $"Changing costume to {GameDatabase.GetPrototypeName(prototypeId)}";
190190
}
@@ -208,7 +208,7 @@ public string Points(string[] @params, FrontendClient client)
208208
{
209209
if (client == null) return "You can only invoke this command from the game.";
210210
if (ConfigManager.GameOptions.InfinitySystemEnabled) return "Set InfinitySystemEnabled to false in Config.ini to enable the Omega system.";
211-
client.SendMessage(1, new(Property.ToNetMessageSetProperty(9078332, new(PropertyEnum.OmegaPoints), new(7500))));
211+
client.SendMessage(1, new(Property.ToNetMessageSetProperty(9078332, new(PropertyEnum.OmegaPoints), 7500)));
212212
return "Setting Omega points to 7500.";
213213
}
214214
}

src/MHServerEmu/Games/Entities/EntityManager.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public WorldEntity CreateWorldEntity(ulong regionId, PrototypeId prototypeId, Ve
6565

6666
public WorldEntity CreateWorldEntityEnemy(ulong regionId, PrototypeId prototypeId, Vector3 position, Vector3 orientation,
6767
int health, int mapAreaId, int healthMaxOther, int mapCellId, PrototypeId contextAreaRef, bool requiresEnterGameWorld,
68-
int CombatLevel, int CharacterLevel)
68+
int combatLevel, int characterLevel)
6969
{
7070
EntityBaseData baseData = (requiresEnterGameWorld == false)
7171
? new EntityBaseData(GetNextEntityId(), prototypeId, position, orientation)
@@ -75,8 +75,8 @@ public WorldEntity CreateWorldEntityEnemy(ulong regionId, PrototypeId prototypeI
7575
worldEntity.RegionId = regionId;
7676
_entityDict.Add(baseData.EntityId, worldEntity);
7777

78-
worldEntity.Properties[PropertyEnum.CharacterLevel] = new(CharacterLevel);
79-
worldEntity.Properties[PropertyEnum.CombatLevel] = new(CombatLevel); // zero effect
78+
worldEntity.Properties[PropertyEnum.CharacterLevel] = characterLevel;
79+
worldEntity.Properties[PropertyEnum.CombatLevel] = combatLevel; // zero effect
8080

8181
return worldEntity;
8282
}
@@ -188,7 +188,7 @@ public Entity FindEntityByDestination(Destination destination, ulong regionId)
188188
if (entity.Value.BaseData.PrototypeId == destination.Entity && entity.Value.RegionId == regionId)
189189
{
190190
if (destination.Area == 0) return entity.Value;
191-
PrototypeId area = entity.Value.Properties[PropertyEnum.ContextAreaRef].ToPrototypeId();
191+
PrototypeId area = entity.Value.Properties[PropertyEnum.ContextAreaRef];
192192
if (area == destination.Area)
193193
return entity.Value;
194194
}

src/MHServerEmu/Games/Entities/Items/Item.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public Item(EntityBaseData baseData, ByteString archiveData) : base(baseData, ar
1616
public Item(EntityBaseData baseData, ulong replicationId, PrototypeId rank, int itemLevel, PrototypeId itemRarity, float itemVariation, ItemSpec itemSpec) : base(baseData)
1717
{
1818
Properties = new(replicationId);
19-
Properties[PropertyEnum.Requirement, (PrototypeId)4312898931213406054] = new(itemLevel * 1.0f); // Property/Info/CharacterLevel.defaults
20-
Properties[PropertyEnum.ItemRarity] = new(itemRarity);
21-
Properties[PropertyEnum.ItemVariation] = new(itemVariation);
19+
Properties[PropertyEnum.Requirement, (PrototypeId)4312898931213406054] = itemLevel * 1.0f; // Property/Info/CharacterLevel.defaults
20+
Properties[PropertyEnum.ItemRarity] = itemRarity;
21+
Properties[PropertyEnum.ItemVariation] = itemVariation;
2222

2323
TrackingContextMap = Array.Empty<EntityTrackingContextMap>();
2424
ConditionCollection = Array.Empty<Condition>();

src/MHServerEmu/Games/Entities/Transition.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public Transition(EntityBaseData baseData, ulong replicationId, ulong mapRegionI
2020
ReplicationPolicy = AoiNetworkPolicyValues.AoiChannel0 | AoiNetworkPolicyValues.AoiChannel5;
2121

2222
Properties = new(replicationId);
23-
Properties[PropertyEnum.MapPosition] = new(mapPosition);
24-
Properties[PropertyEnum.MapAreaId] = new(mapAreaId);
25-
Properties[PropertyEnum.MapRegionId] = new(mapRegionId);
26-
Properties[PropertyEnum.MapCellId] = new(mapCellId);
27-
Properties[PropertyEnum.ContextAreaRef] = new(contextAreaRef);
23+
Properties[PropertyEnum.MapPosition] = mapPosition;
24+
Properties[PropertyEnum.MapAreaId] = mapAreaId;
25+
Properties[PropertyEnum.MapRegionId] = mapRegionId;
26+
Properties[PropertyEnum.MapCellId] = mapCellId;
27+
Properties[PropertyEnum.ContextAreaRef] = contextAreaRef;
2828

2929
TrackingContextMap = Array.Empty<EntityTrackingContextMap>();
3030
ConditionCollection = Array.Empty<Condition>();

src/MHServerEmu/Games/Entities/WorldEntity.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public WorldEntity(EntityBaseData baseData, ulong replicationId, Vector3 mapPosi
3636
ReplicationPolicy = AoiNetworkPolicyValues.AoiChannel5;
3737

3838
Properties = new(replicationId);
39-
Properties[PropertyEnum.MapPosition] = new(mapPosition);
40-
Properties[PropertyEnum.Health] = new(health);
41-
Properties[PropertyEnum.MapAreaId] = new(mapAreaId);
42-
Properties[PropertyEnum.HealthMaxOther] = new(healthMaxOther);
43-
Properties[PropertyEnum.MapRegionId] = new(mapRegionId);
44-
Properties[PropertyEnum.MapCellId] = new(mapCellId);
45-
Properties[PropertyEnum.ContextAreaRef] = new(contextAreaRef);
39+
Properties[PropertyEnum.MapPosition] = mapPosition;
40+
Properties[PropertyEnum.Health] = health;
41+
Properties[PropertyEnum.MapAreaId] = mapAreaId;
42+
Properties[PropertyEnum.HealthMaxOther] = healthMaxOther;
43+
Properties[PropertyEnum.MapRegionId] = mapRegionId;
44+
Properties[PropertyEnum.MapCellId] = mapCellId;
45+
Properties[PropertyEnum.ContextAreaRef] = contextAreaRef;
4646

4747
TrackingContextMap = Array.Empty<EntityTrackingContextMap>();
4848
ConditionCollection = Array.Empty<Condition>();

src/MHServerEmu/Games/Events/EventManager.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ private List<QueuedGameMessage> HandleEvent(GameEvent queuedEvent)
107107
true );
108108

109109
// TODO: applyItemSpecProperties
110-
bowlingBall.Properties[PropertyEnum.InventoryStackSizeMax] = new(1000); // Item.StackSettings
111-
bowlingBall.Properties[PropertyEnum.ItemIsTradable] = new(false); // DefaultSettings.IsTradable
112-
bowlingBall.Properties[PropertyEnum.ItemBindsToCharacterOnEquip] = new(true); // DefaultSettings.BindsToAccountOnPickup
113-
bowlingBall.Properties[PropertyEnum.ItemBindsToAccountOnPickup] = new(true); // DefaultSettings.BindsToCharacterOnEquip
110+
bowlingBall.Properties[PropertyEnum.InventoryStackSizeMax] = 1000; // Item.StackSettings
111+
bowlingBall.Properties[PropertyEnum.ItemIsTradable] = false; // DefaultSettings.IsTradable
112+
bowlingBall.Properties[PropertyEnum.ItemBindsToCharacterOnEquip] = true; // DefaultSettings.BindsToAccountOnPickup
113+
bowlingBall.Properties[PropertyEnum.ItemBindsToAccountOnPickup] = true; // DefaultSettings.BindsToCharacterOnEquip
114114

115115
messageList.Add(new(client, new(bowlingBall.ToNetMessageEntityCreate())));
116116

@@ -338,7 +338,7 @@ private List<QueuedGameMessage> HandleEvent(GameEvent queuedEvent)
338338
ulong avatarRepId = (ulong)client.Session.Account.Player.Avatar.ToPropertyCollectionReplicationId();
339339

340340
messageList.Add(new(client, new(
341-
Property.ToNetMessageSetProperty(avatarRepId, new(PropertyEnum.ThrowableOriginatorEntity), new(idTarget))
341+
Property.ToNetMessageSetProperty(avatarRepId, new(PropertyEnum.ThrowableOriginatorEntity), idTarget)
342342
)));
343343
Logger.Warn($"{GameDatabase.GetPrototypeName(client.ThrowingObject.BaseData.PrototypeId)}");
344344
// ThrowObject.Prototype.WorldEntity.UnrealClass
@@ -350,7 +350,7 @@ private List<QueuedGameMessage> HandleEvent(GameEvent queuedEvent)
350350
// if (throwPrototype.Header.ReferenceType != (PrototypeId)HardcodedBlueprintId.ThrowableSmartProp)
351351
// throwPrototype = throwPrototype.Header.ReferenceType.GetPrototype();
352352
messageList.Add(new(client, new(
353-
Property.ToNetMessageSetProperty(avatarRepId, new(PropertyEnum.ThrowableOriginatorAssetRef), new(throwPrototype.UnrealClass))
353+
Property.ToNetMessageSetProperty(avatarRepId, new(PropertyEnum.ThrowableOriginatorAssetRef), throwPrototype.UnrealClass)
354354
)));
355355

356356
// ThrowObject.Prototype.ThrowableRestorePowerProp.Value
@@ -492,7 +492,7 @@ private List<QueuedGameMessage> HandleEvent(GameEvent queuedEvent)
492492
.Build())));
493493

494494
messageList.Add(new(client, new(
495-
Property.ToNetMessageSetProperty(arenaEntity.Properties.ReplicationId, new(PropertyEnum.AttachedToEntityId), new(avatarEntityId))
495+
Property.ToNetMessageSetProperty(arenaEntity.Properties.ReplicationId, new(PropertyEnum.AttachedToEntityId), avatarEntityId)
496496
)));
497497

498498
break;

src/MHServerEmu/Games/Game.Loading.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,19 @@ private GameMessage[] LoadPlayerEntityMessages(DBAccount account)
122122
var avatarPrototype = (PrototypeId)accountAvatar.Prototype;
123123

124124
// Set library costumes according to account data
125-
player.Properties[PropertyEnum.AvatarLibraryCostume, 0, avatarPrototype] = new((PrototypeId)accountAvatar.Costume);
125+
player.Properties[PropertyEnum.AvatarLibraryCostume, 0, avatarPrototype] = (PrototypeId)accountAvatar.Costume;
126126

127127
// Set avatar levels to 60
128128
// Note: setting this to above level 60 sets the prestige level as well
129-
player.Properties[PropertyEnum.AvatarLibraryLevel, 0, avatarPrototype] = new(60);
129+
player.Properties[PropertyEnum.AvatarLibraryLevel, 0, avatarPrototype] = 60;
130130

131131
// Clean up team ups
132-
player.Properties[PropertyEnum.AvatarLibraryTeamUp, 0, avatarPrototype] = new(PrototypeId.Invalid);
132+
player.Properties[PropertyEnum.AvatarLibraryTeamUp, 0, avatarPrototype] = PrototypeId.Invalid;
133133

134134
// Unlock start avatars
135-
AvatarUnlockType avatarUnlock = (AvatarUnlockType)player.Properties[PropertyEnum.AvatarUnlock, enumValue].ToInt();
135+
var avatarUnlock = (AvatarUnlockType)(int)player.Properties[PropertyEnum.AvatarUnlock, enumValue];
136136
if (avatarUnlock == AvatarUnlockType.Starter)
137-
player.Properties[PropertyEnum.AvatarUnlock, avatarPrototype] = new((int)AvatarUnlockType.Type3);
137+
player.Properties[PropertyEnum.AvatarUnlock, avatarPrototype] = (int)AvatarUnlockType.Type3;
138138
}
139139

140140
CommunityMember friend = player.Community.CommunityMemberList[0];
@@ -194,9 +194,9 @@ private GameMessage[] LoadPlayerEntityMessages(DBAccount account)
194194

195195
avatar.PlayerName.Value = account.PlayerName;
196196

197-
avatar.Properties[PropertyEnum.CostumeCurrent] = new((PrototypeId)account.CurrentAvatar.Costume);
198-
avatar.Properties[PropertyEnum.CharacterLevel] = new(60);
199-
avatar.Properties[PropertyEnum.CombatLevel] = new(60);
197+
avatar.Properties[PropertyEnum.CostumeCurrent] = (PrototypeId)account.CurrentAvatar.Costume;
198+
avatar.Properties[PropertyEnum.CharacterLevel] = 60;
199+
avatar.Properties[PropertyEnum.CombatLevel] = 60;
200200
}
201201

202202
messageList.Add(new(avatar.ToNetMessageEntityCreate()));

src/MHServerEmu/Games/Game.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ private void OnUseInteractableObject(FrontendClient client, NetMessageUseInterac
321321

322322
Logger.Trace($"Teleporting to {targetPos}");
323323

324-
uint cellid = target.Properties[PropertyEnum.MapCellId].ToUInt();
325-
uint areaid = target.Properties[PropertyEnum.MapAreaId].ToUInt();
324+
uint cellid = target.Properties[PropertyEnum.MapCellId];
325+
uint areaid = target.Properties[PropertyEnum.MapAreaId];
326326
Logger.Trace($"Teleporting to areaid {areaid} cellid {cellid}");
327327

328328
EnqueueResponse(client, new(NetMessageEntityPosition.CreateBuilder()

src/MHServerEmu/Games/Powers/PowerLoader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static GameMessage[] LoadAvatarPowerCollection(HardcodedAvatarEntityId av
102102

103103
// PowerRankBase needs to be set for the powers window to show powers without changing spec tabs
104104
// NOTE: PowerRankBase is also supposed to have a power prototype param
105-
messageList.Add(new(Property.ToNetMessageSetProperty(replicationId, new(PropertyEnum.PowerRankBase), new(1))));
105+
messageList.Add(new(Property.ToNetMessageSetProperty(replicationId, new(PropertyEnum.PowerRankBase), 1)));
106106

107107
return messageList.ToArray();
108108
}

src/MHServerEmu/Games/Properties/PropertyValue.cs

+38-56
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,17 @@ public struct PropertyValue
1313
[FieldOffset(0)]
1414
public float RawFloat = 0;
1515

16-
public PropertyValue(bool value)
17-
{
18-
RawLong = Convert.ToInt64(value);
19-
}
20-
21-
public PropertyValue(float value)
22-
{
23-
RawFloat = value;
24-
}
25-
26-
public PropertyValue(int value)
27-
{
28-
RawLong = value;
29-
}
30-
31-
public PropertyValue(long value)
32-
{
33-
RawLong = value;
34-
}
35-
36-
public PropertyValue(uint value)
37-
{
38-
RawLong = (int)value;
39-
}
40-
41-
public PropertyValue(ulong value)
42-
{
43-
// for EntityId, Time, Guid, RegionId
44-
RawLong = (long)value;
45-
}
46-
47-
public PropertyValue(PrototypeId prototypeId)
48-
{
49-
RawLong = (long)prototypeId;
50-
}
51-
52-
public PropertyValue(CurveId curveId)
53-
{
54-
RawLong = (long)curveId;
55-
}
56-
57-
public PropertyValue(AssetId assetId)
58-
{
59-
RawLong = (long)assetId;
60-
}
61-
16+
// Constructors
17+
18+
public PropertyValue(bool value) { RawLong = Convert.ToInt64(value); }
19+
public PropertyValue(float value) { RawFloat = value; }
20+
public PropertyValue(int value) { RawLong = value; }
21+
public PropertyValue(long value) { RawLong = value; }
22+
public PropertyValue(uint value) { RawLong = (int)value; }
23+
public PropertyValue(ulong value) { RawLong = (long)value; } // for EntityId, Time, Guid, RegionId
24+
public PropertyValue(PrototypeId prototypeId) { RawLong = (long)prototypeId; }
25+
public PropertyValue(CurveId curveId) { RawLong = (long)curveId; }
26+
public PropertyValue(AssetId assetId) { RawLong = (long)assetId; }
6227
public PropertyValue(Vector3 vector)
6328
{
6429
ulong x = (ulong)vector.X & 0x1FFFFF;
@@ -78,24 +43,16 @@ public PropertyValue(Vector3 vector)
7843
RawLong = (long)(x | y | z);
7944
}
8045

46+
// Conversion to specific value types
8147
public bool ToBool() => RawLong != 0;
82-
8348
public float ToFloat() => RawFloat;
84-
8549
public int ToInt() => (int)RawLong;
86-
8750
public long ToLong() => RawLong;
88-
8951
public uint ToUInt() => (uint)(int)RawLong;
90-
9152
public ulong ToULong() => (ulong)RawLong;
92-
9353
public PrototypeId ToPrototypeId() => (PrototypeId)RawLong;
94-
9554
public CurveId ToCurveId() => (CurveId)RawLong;
96-
9755
public AssetId ToAssetId() => (AssetId)RawLong;
98-
9956
public Vector3 ToVector3()
10057
{
10158
ulong raw = (ulong)RawLong;
@@ -121,11 +78,36 @@ public string Print(PropertyDataType type)
12178
{
12279
case PropertyDataType.Boolean: return ToBool().ToString();
12380
case PropertyDataType.Real: return ToFloat().ToString();
124-
case PropertyDataType.Integer: return ToInt().ToString();
81+
case PropertyDataType.Integer: return ToLong().ToString();
12582
case PropertyDataType.Prototype: return GameDatabase.GetPrototypeName(ToPrototypeId());
83+
//case PropertyDataType.Curve: return "Curve Property Value";
84+
case PropertyDataType.Asset: return GameDatabase.GetAssetName(ToAssetId());
12685
case PropertyDataType.Int21Vector3: return ToVector3().ToString();
12786
default: return $"0x{RawLong:X}";
12887
}
12988
}
89+
90+
// Implicit casting
91+
public static implicit operator PropertyValue(bool value) => new(value);
92+
public static implicit operator PropertyValue(float value) => new(value);
93+
public static implicit operator PropertyValue(int value) => new(value);
94+
public static implicit operator PropertyValue(long value) => new(value);
95+
public static implicit operator PropertyValue(uint value) => new(value);
96+
public static implicit operator PropertyValue(ulong value) => new(value);
97+
public static implicit operator PropertyValue(PrototypeId value) => new(value);
98+
public static implicit operator PropertyValue(CurveId value) => new(value);
99+
public static implicit operator PropertyValue(AssetId value) => new(value);
100+
public static implicit operator PropertyValue(Vector3 value) => new(value);
101+
102+
public static implicit operator bool(PropertyValue value) => value.ToBool();
103+
public static implicit operator float(PropertyValue value) => value.ToFloat();
104+
public static implicit operator int(PropertyValue value) => value.ToInt();
105+
public static implicit operator long(PropertyValue value) => value.ToLong();
106+
public static implicit operator uint(PropertyValue value) => value.ToUInt();
107+
public static implicit operator ulong(PropertyValue value) => value.ToULong();
108+
public static implicit operator PrototypeId(PropertyValue value) => value.ToPrototypeId();
109+
public static implicit operator CurveId(PropertyValue value) => value.ToCurveId();
110+
public static implicit operator AssetId(PropertyValue value) => value.ToAssetId();
111+
public static implicit operator Vector3(PropertyValue value) => value.ToVector3();
130112
}
131113
}

0 commit comments

Comments
 (0)