@@ -42,32 +42,49 @@ public Dictionary<int, object> getChalkPacket()
4242
4343 return packet ;
4444 }
45+
46+ // The ammount of chalk pixels needed to trigger the fallback process
47+ const int TRIGGER_FALLBACK_CHALK_SIZE = 100000 ; // around .3mb worth of chalk data
48+
4549 public void chalkUpdate ( Dictionary < int , object > packet )
4650 {
47- foreach ( KeyValuePair < int , object > entry in packet )
51+ // This callback is made when there is too much chalk data
52+ // it runs under the assumption that the newest chalk data is at the end of the packet
53+ // Webfishing will set pixels multiple times in the chalk data
54+ // assuming that the chalk data is in order, we can just take the last value
55+ // Because the newest data is at the end of the packet we update till we reach the point
56+ // of a pixel being set multiple times
57+ if ( packet . Count >= TRIGGER_FALLBACK_CHALK_SIZE )
4858 {
49- Dictionary < int , object > arr = ( Dictionary < int , object > ) entry . Value ;
50- Vector2 vector2 = ( Vector2 ) arr [ 0 ] ;
51- Vector2 pos = new Vector2 ( ( int ) Math . Round ( vector2 . x ) , ( int ) Math . Round ( vector2 . y ) ) ;
52- Int64 color = ( Int64 ) arr [ 1 ] ;
53-
54-
55- // YES, I know this is inefficient
56- // I dont want to break compatibility with the old code
57- // It should be fine, its per canvas
58- // but if it becomes a problem, I hope someone files a issue and
59- // I'll put a performance fix in
60- for ( int i = 0 ; i < chalkImage . Count ; i ++ )
59+ List < Vector2 > expendedPixels = new List < Vector2 > ( chalkImage . Keys ) ;
60+ for ( int i = packet . Count ; i >= 0 ; i -- )
6161 {
62- Vector2 key = chalkImage . ElementAt ( i ) . Key ;
63- if ( key . x == pos . x && key . y == pos . y )
62+ Dictionary < int , object > arr = ( Dictionary < int , object > ) packet [ i - 1 ] ;
63+ Vector2 vector2 = ( Vector2 ) arr [ 0 ] ;
64+ Vector2 pos = new Vector2 ( ( int ) Math . Round ( vector2 . x ) , ( int ) Math . Round ( vector2 . y ) ) ;
65+
66+ if ( expendedPixels . Contains ( pos ) )
6467 {
65- chalkImage . Remove ( key ) ;
6668 break ;
6769 }
70+
71+ expendedPixels . Add ( pos ) ;
72+ chalkImage [ pos ] = ( int ) ( ( Int64 ) arr [ 1 ] ) ;
6873 }
6974
70- chalkImage [ pos ] = ( int ) color ;
75+ Console . WriteLine ( "A packet that was passed to the Fallback function was processed successfully" ) ;
76+
77+ }
78+ else
79+ {
80+ for ( int i = 0 ; i < packet . Count ; i ++ )
81+ {
82+ Dictionary < int , object > arr = ( Dictionary < int , object > ) packet [ i ] ;
83+ Vector2 vector2 = ( Vector2 ) arr [ 0 ] ;
84+ Vector2 pos = new Vector2 ( ( int ) Math . Round ( vector2 . x ) , ( int ) Math . Round ( vector2 . y ) ) ;
85+
86+ chalkImage [ pos ] = ( int ) ( ( Int64 ) arr [ 1 ] ) ;
87+ }
7188 }
7289 }
7390
0 commit comments