@@ -58,16 +58,23 @@ export class Decoder<T extends Schema = any> {
5858 if ( bytes [ it . offset ] == SWITCH_TO_STRUCTURE ) {
5959 it . offset ++ ;
6060
61- this . currentRefId = decode . number ( bytes , it ) ;
62- const nextRef = $root . refs . get ( this . currentRefId ) as Schema ;
61+ const nextRefId = decode . number ( bytes , it ) ;
62+ const nextRef = $root . refs . get ( nextRefId ) ;
6363
6464 //
6565 // Trying to access a reference that haven't been decoded yet.
6666 //
67- if ( ! nextRef ) { throw new Error ( `"refId" not found: ${ this . currentRefId } ` ) ; }
67+ if ( ! nextRef ) {
68+ console . error ( `"refId" not found: ${ nextRefId } ` , { previousRef : this , previousRefId : this . currentRefId } ) ;
69+ console . warn ( "Please report this to the developers. All refIds =>" ) ;
70+ console . warn ( Schema . debugRefIdsDecoder ( this ) ) ;
71+ this . skipCurrentStructure ( bytes , it , totalBytes ) ;
72+ }
6873 ref [ $onDecodeEnd ] ?.( )
69- ref = nextRef ;
7074
75+ this . currentRefId = nextRefId ;
76+
77+ ref = nextRef ;
7178 decoder = ref . constructor [ $decoder ] ;
7279
7380 continue ;
@@ -77,22 +84,7 @@ export class Decoder<T extends Schema = any> {
7784
7885 if ( result === DEFINITION_MISMATCH ) {
7986 console . warn ( "@colyseus/schema: definition mismatch" ) ;
80-
81- //
82- // keep skipping next bytes until reaches a known structure
83- // by local decoder.
84- //
85- const nextIterator : Iterator = { offset : it . offset } ;
86- while ( it . offset < totalBytes ) {
87- if ( bytes [ it . offset ] === SWITCH_TO_STRUCTURE ) {
88- nextIterator . offset = it . offset + 1 ;
89- if ( $root . refs . has ( decode . number ( bytes , nextIterator ) ) ) {
90- break ;
91- }
92- }
93-
94- it . offset ++ ;
95- }
87+ this . skipCurrentStructure ( bytes , it , totalBytes ) ;
9688 continue ;
9789 }
9890 }
@@ -109,6 +101,23 @@ export class Decoder<T extends Schema = any> {
109101 return allChanges ;
110102 }
111103
104+ skipCurrentStructure ( bytes : Buffer , it : Iterator , totalBytes : number ) {
105+ //
106+ // keep skipping next bytes until reaches a known structure
107+ // by local decoder.
108+ //
109+ const nextIterator : Iterator = { offset : it . offset } ;
110+ while ( it . offset < totalBytes ) {
111+ if ( bytes [ it . offset ] === SWITCH_TO_STRUCTURE ) {
112+ nextIterator . offset = it . offset + 1 ;
113+ if ( this . root . refs . has ( decode . number ( bytes , nextIterator ) ) ) {
114+ break ;
115+ }
116+ }
117+ it . offset ++ ;
118+ }
119+ }
120+
112121 getInstanceType ( bytes : Buffer , it : Iterator , defaultType : typeof Schema ) : typeof Schema {
113122 let type : typeof Schema ;
114123
0 commit comments