@@ -63,6 +63,11 @@ public ButtplugJsonMessageParser(IButtplugLogManager aLogManager = null)
6363 _schema = JsonSchema4 . FromJsonAsync ( result ) . GetAwaiter ( ) . GetResult ( ) ;
6464 }
6565 }
66+ catch ( Exception e )
67+ {
68+ _bpLogger . LogException ( e ) ;
69+ throw e ;
70+ }
6671 finally
6772 {
6873 stream ? . Dispose ( ) ;
@@ -152,25 +157,75 @@ public ButtplugMessage[] Deserialize(string aJsonMsg)
152157 return res . ToArray ( ) ;
153158 }
154159
155- public string Serialize ( [ NotNull ] ButtplugMessage aMsg )
160+ public string Serialize ( [ NotNull ] ButtplugMessage aMsg , uint clientSchemaVersion )
156161 {
157162 // Warning: Any log messages in this function must be localOnly. They will possibly recurse.
158- var o = new JObject ( new JProperty ( aMsg . GetType ( ) . Name , JObject . FromObject ( aMsg ) ) ) ;
163+
164+ // Support downgrading messages
165+ var tmp = aMsg ;
166+ while ( tmp . MessageVersioningVersion > clientSchemaVersion )
167+ {
168+ if ( tmp . MessageVersioningPrevious == null )
169+ {
170+ if ( tmp . Id == ButtplugConsts . SystemMsgId )
171+ {
172+ // There's no previous version of this system message
173+ _bpLogger ? . Warn ( $ "No messages serialized!") ;
174+ return null ;
175+ }
176+
177+ tmp = new Error ( $ "No backwards compatible version for message #{ tmp . GetType ( ) . Name } !",
178+ ErrorClass . ERROR_MSG , tmp . Id ) ;
179+ continue ;
180+ }
181+
182+ tmp = ( ButtplugMessage ) aMsg . MessageVersioningPrevious . GetConstructor (
183+ new Type [ ] { tmp . GetType ( ) } ) . Invoke ( new object [ ] { tmp } ) ;
184+ }
185+
186+ var o = new JObject ( new JProperty ( aMsg . GetType ( ) . Name , JObject . FromObject ( tmp ) ) ) ;
159187 var a = new JArray ( o ) ;
160188 _bpLogger ? . Trace ( $ "Message serialized to: { a . ToString ( Formatting . None ) } ", true ) ;
161189 return a . ToString ( Formatting . None ) ;
162190 }
163191
164- public string Serialize ( [ NotNull ] IEnumerable < ButtplugMessage > aMsgs )
192+ public string Serialize ( [ NotNull ] IEnumerable < ButtplugMessage > aMsgs , uint clientSchemaVersion )
165193 {
166194 // Warning: Any log messages in this function must be localOnly. They will possibly recurse.
167195 var a = new JArray ( ) ;
168196 foreach ( var msg in aMsgs )
169197 {
170- var o = new JObject ( new JProperty ( msg . GetType ( ) . Name , JObject . FromObject ( msg ) ) ) ;
198+ // Support downgrading messages
199+ var tmp = msg ;
200+ while ( tmp . MessageVersioningVersion > clientSchemaVersion )
201+ {
202+ if ( tmp . MessageVersioningPrevious == null )
203+ {
204+ if ( tmp . Id == ButtplugConsts . SystemMsgId )
205+ {
206+ // There's no previous version of this system message
207+ continue ;
208+ }
209+
210+ tmp = new Error ( $ "No backwards compatible version for message #{ tmp . GetType ( ) . Name } !",
211+ ErrorClass . ERROR_MSG , tmp . Id ) ;
212+ continue ;
213+ }
214+
215+ tmp = ( ButtplugMessage ) tmp . MessageVersioningPrevious . GetConstructor (
216+ new Type [ ] { tmp . GetType ( ) } ) . Invoke ( new object [ ] { tmp } ) ;
217+ }
218+
219+ var o = new JObject ( new JProperty ( msg . GetType ( ) . Name , JObject . FromObject ( tmp ) ) ) ;
171220 a . Add ( o ) ;
172221 }
173222
223+ if ( ! a . Any ( ) )
224+ {
225+ _bpLogger ? . Warn ( $ "No messages serialized!") ;
226+ return null ;
227+ }
228+
174229 _bpLogger ? . Trace ( $ "Message serialized to: { a . ToString ( Formatting . None ) } ", true ) ;
175230 return a . ToString ( Formatting . None ) ;
176231 }
0 commit comments