Skip to content

Commit c668b9e

Browse files
author
hamstar0
committed
v1.1.6 - Refactored player mirror sync code (again)
* Changed all dictionary uses to ConcurrentDictionary (wasteful?) * Refactored player mirror sync code (again)
1 parent d24fc7e commit c668b9e

File tree

6 files changed

+196
-103
lines changed

6 files changed

+196
-103
lines changed
Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Runtime.Serialization;
3+
using System.Collections.Concurrent;
44
using Terraria;
55
using HamstarHelpers.Helpers.Debug;
6+
using HamstarHelpers.Helpers.DotNET.Extensions;
67

78

89
namespace MountedMagicMirrors.DataStructures {
9-
class DiscoveredMirrors : Dictionary<int, ISet<int>> {
10+
class DiscoveredMirrors : ConcurrentDictionary<int, ISet<int>> {
11+
public static bool WorldMirrorsEquals(
12+
IDictionary<string, DiscoveredMirrors> world1,
13+
IDictionary<string, DiscoveredMirrors> world2 ) {
14+
if( world1.Count != world2.Count ) {
15+
return false;
16+
}
17+
18+
foreach( (string worldUid, DiscoveredMirrors mirrors) in world1 ) {
19+
if( !world2.ContainsKey(worldUid) ) {
20+
return false;
21+
}
22+
if( !mirrors.DeepEquals( world2[worldUid] ) ) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
29+
30+
31+
////////////////
32+
1033
public DiscoveredMirrors() : base() { }
11-
public DiscoveredMirrors( int capacity ) : base( capacity ) { }
34+
//public DiscoveredMirrors( int capacity ) : base( capacity ) { }
1235
public DiscoveredMirrors( IEqualityComparer<int> comparer )
1336
: base( comparer ) { }
1437
public DiscoveredMirrors( IDictionary<int, ISet<int>> dictionary )
@@ -17,7 +40,44 @@ public DiscoveredMirrors( int capacity, IEqualityComparer<int> comparer )
1740
: base( comparer ) { }
1841
public DiscoveredMirrors( IDictionary<int, ISet<int>> dictionary, IEqualityComparer<int> comparer )
1942
: base( dictionary, comparer ) { }
20-
protected DiscoveredMirrors( SerializationInfo info, StreamingContext context )
21-
: base( info, context ) { }
43+
//protected DiscoveredMirrors( SerializationInfo info, StreamingContext context )
44+
// : base( info, context ) { }
45+
46+
//
47+
48+
public DiscoveredMirrors( IEnumerable<KeyValuePair<int, ISet<int>>> collection )
49+
: base( collection ) { }
50+
51+
public DiscoveredMirrors( int concurrencyLevel, int capacity )
52+
: base( concurrencyLevel, capacity ) { }
53+
54+
public DiscoveredMirrors( IEnumerable<KeyValuePair<int, ISet<int>>> collection, IEqualityComparer<int> comparer )
55+
: base( collection, comparer ) { }
56+
57+
public DiscoveredMirrors(
58+
int concurrencyLevel,
59+
IEnumerable<KeyValuePair<int, ISet<int>>> collection,
60+
IEqualityComparer<int> comparer )
61+
: base( concurrencyLevel, collection, comparer ) { }
62+
63+
public DiscoveredMirrors( int concurrencyLevel, int capacity, IEqualityComparer<int> comparer )
64+
: base( concurrencyLevel, capacity, comparer ) { }
65+
66+
67+
68+
////////////////
69+
70+
public bool DeepEquals( DiscoveredMirrors mirrors ) {
71+
foreach( (int tileX, ISet<int> tileYs) in this ) {
72+
if( !mirrors.ContainsKey(tileX) ) {
73+
return false;
74+
}
75+
if( !mirrors[tileX].SetEquals(tileYs) ) {
76+
return false;
77+
}
78+
}
79+
80+
return true;
81+
}
2282
}
2383
}

MountedMagicMirrors/MyCustomPlayer.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Terraria;
3+
using Terraria.ID;
4+
using HamstarHelpers.Helpers.Debug;
5+
using HamstarHelpers.Classes.PlayerData;
6+
7+
8+
namespace MountedMagicMirrors {
9+
partial class MMMCustomPlayer : CustomPlayerData {
10+
protected override void OnEnter( bool isCurrentPlayer, object data ) {
11+
if( isCurrentPlayer && Main.netMode == NetmodeID.MultiplayerClient ) {
12+
var myplayer = this.Player.GetModPlayer<MMMPlayer>();
13+
14+
myplayer.OnCurrentClientEnter();
15+
}
16+
}
17+
}
18+
}

MountedMagicMirrors/MyPlayer.cs

Lines changed: 78 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Concurrent;
4+
using Terraria;
5+
using Terraria.ID;
36
using Terraria.ModLoader;
47
using Terraria.ModLoader.IO;
5-
using Terraria;
68
using HamstarHelpers.Helpers.Debug;
79
using HamstarHelpers.Helpers.DotNET.Extensions;
810
using HamstarHelpers.Helpers.World;
@@ -13,13 +15,8 @@
1315

1416
namespace MountedMagicMirrors {
1517
partial class MMMPlayer : ModPlayer {
16-
internal static readonly object MyCurrentMirrorsLock = new object();
17-
18-
19-
20-
////////////////
21-
22-
private IDictionary<string, DiscoveredMirrors> DiscoveredMirrorTilesPerWorld = new Dictionary<string, DiscoveredMirrors>();
18+
private ConcurrentDictionary<string, DiscoveredMirrors> DiscoveredMirrorTilesPerWorld
19+
= new ConcurrentDictionary<string, DiscoveredMirrors>();
2320

2421
////
2522

@@ -56,31 +53,48 @@ public DiscoveredMirrors CurrentWorldDiscoveredMirrorTiles {
5653

5754
////////////////
5855

56+
internal void OnCurrentClientEnter() {
57+
PlayerDataProtocol.Broadcast( this.DiscoveredMirrorTilesPerWorld );
58+
}
59+
60+
/*public override void SyncPlayer( int toWho, int fromWho, bool newPlayer ) {
61+
if( Main.netMode == 1 ) {
62+
if( newPlayer ) {
63+
//PlayerDataProtocol.SendToServer( this.DiscoveredMirrorTilesPerWorld );
64+
PlayerDataProtocol.Broadcast( this.DiscoveredMirrorTilesPerWorld );
65+
}
66+
} else {
67+
//PlayerDataProtocol.SendToClients( toWho, fromWho, this.DiscoveredMirrorTilesPerWorld );
68+
}
69+
}*/
70+
5971
public override void clientClone( ModPlayer clientClone ) {
6072
var myclone = (MMMPlayer)clientClone;
6173

6274
myclone._CurrentWorldDiscoveredMirrorTiles = null;
6375

64-
lock( MMMPlayer.MyCurrentMirrorsLock ) {
65-
foreach( (string worldUid, DiscoveredMirrors mirrors) in this.DiscoveredMirrorTilesPerWorld ) {
66-
myclone.DiscoveredMirrorTilesPerWorld[worldUid] = new DiscoveredMirrors();
76+
foreach( (string worldUid, DiscoveredMirrors mirrors) in this.DiscoveredMirrorTilesPerWorld ) {
77+
myclone.DiscoveredMirrorTilesPerWorld[worldUid] = new DiscoveredMirrors();
6778

68-
foreach( (int tileX, ISet<int> tileYs) in mirrors ) {
69-
myclone.DiscoveredMirrorTilesPerWorld[worldUid][tileX] = new HashSet<int>( tileYs );
70-
}
79+
foreach( (int tileX, ISet<int> tileYs) in mirrors ) {
80+
myclone.DiscoveredMirrorTilesPerWorld[worldUid][tileX] = new HashSet<int>( tileYs );
7181
}
7282
}
7383
}
7484

75-
7685
////////////////
7786

78-
public override void SyncPlayer( int toWho, int fromWho, bool newPlayer ) {
79-
lock( MMMPlayer.MyCurrentMirrorsLock ) {
80-
if( Main.netMode == 1 ) {
81-
PlayerDataProtocol.SendToServer( this.DiscoveredMirrorTilesPerWorld );
82-
} else {
83-
PlayerDataProtocol.SendToClients( toWho, fromWho, this.DiscoveredMirrorTilesPerWorld );
87+
public override void SendClientChanges( ModPlayer clientPlayer ) {
88+
var myclone = (MMMPlayer)clientPlayer;
89+
var thisDict = this.DiscoveredMirrorTilesPerWorld;
90+
var thatDict = myclone.DiscoveredMirrorTilesPerWorld;
91+
92+
if( !DiscoveredMirrors.WorldMirrorsEquals(thisDict, thatDict) ) {
93+
if( Main.netMode == NetmodeID.MultiplayerClient ) {
94+
PlayerDataProtocol.Broadcast( this.DiscoveredMirrorTilesPerWorld );
95+
}
96+
else {
97+
//PlayerDataProtocol.SendToClients( -1, -1, this.DiscoveredMirrorTilesPerWorld );
8498
}
8599
}
86100
}
@@ -89,27 +103,25 @@ public override void SyncPlayer( int toWho, int fromWho, bool newPlayer ) {
89103
////////////////
90104

91105
public override void Load( TagCompound tag ) {
92-
lock( MMMPlayer.MyCurrentMirrorsLock ) {
93-
this.DiscoveredMirrorTilesPerWorld.Clear();
106+
int count = 0;
94107

95-
int count = 0;
96-
97-
if( !tag.ContainsKey("world_count") ) {
98-
if( tag.ContainsKey( "discovery_count" ) ) {
99-
count = this.LoadOld( tag );
100-
}
101-
} else {
102-
count = this.LoadNew( tag );
108+
if( !tag.ContainsKey("world_count") ) {
109+
if( tag.ContainsKey( "discovery_count" ) ) {
110+
this.DiscoveredMirrorTilesPerWorld.Clear();
111+
count = this.LoadOld( tag );
103112
}
104-
105-
LogHelpers.Log( "Loaded "+count+" discovered mirrors for "+this.player.name+" ("+this.player.whoAmI+")" );
113+
} else {
114+
this.DiscoveredMirrorTilesPerWorld.Clear();
115+
count = this.LoadNew( tag );
106116
}
117+
118+
LogHelpers.Log( "Loaded "+count+" discovered mirrors for "+this.player.name+" ("+this.player.whoAmI+")" );
107119
}
108120

109121
private int LoadOld( TagCompound tag ) {
110122
int count = tag.GetInt( "discovery_count" );
111123

112-
this.DiscoveredMirrorTilesPerWorld[ "_" ] = new DiscoveredMirrors();
124+
this.DiscoveredMirrorTilesPerWorld["_"] = new DiscoveredMirrors();
113125

114126
for( int i = 0; i < count; i++ ) {
115127
int tileX = tag.GetInt( "discovery_x_" + i );
@@ -156,47 +168,45 @@ private int LoadNew( TagCompound tag ) {
156168
////
157169

158170
public override TagCompound Save() {
159-
lock( MMMPlayer.MyCurrentMirrorsLock ) {
160-
var tag = new TagCompound {
161-
{ "world_count", this.DiscoveredMirrorTilesPerWorld.Count }
162-
};
163-
164-
int i = 0;
165-
foreach( string worldUid in this.DiscoveredMirrorTilesPerWorld.Keys ) {
166-
IDictionary<int, ISet<int>> mirrors = this.DiscoveredMirrorTilesPerWorld[ worldUid ];
167-
int count = mirrors.Count2D();
168-
169-
string myWorldUid = worldUid;
170-
if( worldUid == "_" ) {
171-
myWorldUid = WorldHelpers.GetUniqueIdForCurrentWorld( true );
172-
if( MMMConfig.Instance.DebugModeInfo ) {
173-
LogHelpers.Log( "Saving for world UID " + myWorldUid );
174-
}
171+
var tag = new TagCompound {
172+
{ "world_count", this.DiscoveredMirrorTilesPerWorld.Count }
173+
};
174+
175+
int i = 0;
176+
foreach( string worldUid in this.DiscoveredMirrorTilesPerWorld.Keys ) {
177+
IDictionary<int, ISet<int>> mirrors = this.DiscoveredMirrorTilesPerWorld[ worldUid ];
178+
int count = mirrors.Count2D();
179+
180+
string myWorldUid = worldUid;
181+
if( worldUid == "_" ) {
182+
myWorldUid = WorldHelpers.GetUniqueIdForCurrentWorld( true );
183+
if( MMMConfig.Instance.DebugModeInfo ) {
184+
LogHelpers.Log( "Saving for world UID " + myWorldUid );
175185
}
186+
}
176187

177-
tag[ "world_uid_"+i ] = myWorldUid;
178-
tag[ "discovery_count_for_"+i ] = count;
188+
tag[ "world_uid_"+i ] = myWorldUid;
189+
tag[ "discovery_count_for_"+i ] = count;
179190

180-
int j = 0;
181-
foreach( (int tileX, ISet<int> tileYs) in mirrors ) {
182-
foreach( int tileY in tileYs ) {
183-
tag["discovery_x_"+i+"_"+j] = (int)tileX;
184-
tag["discovery_y_"+i+"_"+j] = (int)tileY;
185-
j++;
186-
}
191+
int j = 0;
192+
foreach( (int tileX, ISet<int> tileYs) in mirrors ) {
193+
foreach( int tileY in tileYs ) {
194+
tag["discovery_x_"+i+"_"+j] = (int)tileX;
195+
tag["discovery_y_"+i+"_"+j] = (int)tileY;
196+
j++;
187197
}
188-
189-
LogHelpers.Log( "Saved "
190-
+j+" of "+count
191-
+" discovered mirrors of world "
192-
+myWorldUid+" ("+i+") for "
193-
+this.player.name+" ("+this.player.whoAmI+")" );
194-
195-
i++;
196198
}
197199

198-
return tag;
200+
LogHelpers.Log( "Saved "
201+
+j+" of "+count
202+
+" discovered mirrors of world "
203+
+myWorldUid+" ("+i+") for "
204+
+this.player.name+" ("+this.player.whoAmI+")" );
205+
206+
i++;
199207
}
208+
209+
return tag;
200210
}
201211

202212

0 commit comments

Comments
 (0)