1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Collections . Concurrent ;
4
+ using Terraria ;
5
+ using Terraria . ID ;
3
6
using Terraria . ModLoader ;
4
7
using Terraria . ModLoader . IO ;
5
- using Terraria ;
6
8
using HamstarHelpers . Helpers . Debug ;
7
9
using HamstarHelpers . Helpers . DotNET . Extensions ;
8
10
using HamstarHelpers . Helpers . World ;
13
15
14
16
namespace MountedMagicMirrors {
15
17
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 > ( ) ;
23
20
24
21
////
25
22
@@ -56,31 +53,48 @@ public DiscoveredMirrors CurrentWorldDiscoveredMirrorTiles {
56
53
57
54
////////////////
58
55
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
+
59
71
public override void clientClone ( ModPlayer clientClone ) {
60
72
var myclone = ( MMMPlayer ) clientClone ;
61
73
62
74
myclone . _CurrentWorldDiscoveredMirrorTiles = null ;
63
75
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 ( ) ;
67
78
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 ) ;
71
81
}
72
82
}
73
83
}
74
84
75
-
76
85
////////////////
77
86
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 );
84
98
}
85
99
}
86
100
}
@@ -89,27 +103,25 @@ public override void SyncPlayer( int toWho, int fromWho, bool newPlayer ) {
89
103
////////////////
90
104
91
105
public override void Load ( TagCompound tag ) {
92
- lock ( MMMPlayer . MyCurrentMirrorsLock ) {
93
- this . DiscoveredMirrorTilesPerWorld . Clear ( ) ;
106
+ int count = 0 ;
94
107
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 ) ;
103
112
}
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 ) ;
106
116
}
117
+
118
+ LogHelpers . Log ( "Loaded " + count + " discovered mirrors for " + this . player . name + " (" + this . player . whoAmI + ")" ) ;
107
119
}
108
120
109
121
private int LoadOld ( TagCompound tag ) {
110
122
int count = tag . GetInt ( "discovery_count" ) ;
111
123
112
- this . DiscoveredMirrorTilesPerWorld [ "_" ] = new DiscoveredMirrors ( ) ;
124
+ this . DiscoveredMirrorTilesPerWorld [ "_" ] = new DiscoveredMirrors ( ) ;
113
125
114
126
for ( int i = 0 ; i < count ; i ++ ) {
115
127
int tileX = tag . GetInt ( "discovery_x_" + i ) ;
@@ -156,47 +168,45 @@ private int LoadNew( TagCompound tag ) {
156
168
////
157
169
158
170
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 ) ;
175
185
}
186
+ }
176
187
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 ;
179
190
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 ++ ;
187
197
}
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 ++ ;
196
198
}
197
199
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 ++ ;
199
207
}
208
+
209
+ return tag ;
200
210
}
201
211
202
212
0 commit comments