• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Snippet Easy serialisation using SaveState

Gerry Quinn

Active member
Tutorial Author
Joined
Jun 24, 2017
Messages
184
Somebody asked about this so I'll post what I use. The Test function at the end should show how to use it.

Code:
Strict


' Improved serialisation code
' For Android it seems I can't use 8-bit chars freely


Import mojo.app


Private
Global writeStack:Stack< Int >
Global readArray:Int[]
Global readLength:Int
Global index:Int
Global readError:Bool
Public


Function _____________________________________________Setup_________________________________________:Void()
End



Function StartWrite:Void( version:Int = 0 )
    If writeStack = Null
        writeStack = New Stack< Int >()
    End
    writeStack.Clear()
    WriteInt( version )
End


Function FinishWrite:Void()
    SaveState( MakeSaveString( writeStack.ToArray() ) )
End


Function StartRead:Int()
    readArray = MakeLoadArray( LoadState() )
    readLength = readArray.Length()
    index = 0
    readError = False   
    Return ReadInt()
End


Function WasReadError:Bool()
    Return readError
End


Function AllRead:Bool()
    Return index >= readLength
End


' Stretch string into half-bytes
Function MakeSaveString:String( data:Int[] )
    Local BASE:Int = "A"[ 0 ]
    Local len:Int = data.Length()
    Local stretch:Int[] = New Int[ len * 2 ]
    Local index:Int
    For Local i:Int = 0 Until len
        Local val:Int = data[ i ]
        stretch[ index ] = BASE + val Shr 4
        index += 1
        stretch[ index ] = BASE + ( val & 15 )
        index += 1
    Next
    Return String.FromChars( stretch )
End


' Compress back to bytes
Function MakeLoadArray:Int[]( str:String )
    Local BASE:Int = "A"[ 0 ]
    Local stretch:Int[] = str.ToChars()
    Local data:Int[] = New Int[ stretch.Length() / 2 ]
    Local len:Int = data.Length()
    Local index:Int
    For Local i:Int = 0 Until len
        Local xx:Int = stretch[ index ]
        Local val:Int = ( xx - BASE ) Shl 4
        index += 1
        xx = stretch[ index ]
        val |= xx - BASE
        index += 1
        data[ i ] = val
    Next
    Return data
End


Private
Function _____________________________________________Private_______________________________________:Void()
End


Function ReadVal:Int()
    If index >= readLength
        readError = True
    End
    If readError
        Return 0
    End
    Local val:Int = readArray[ index ]
    index += 1
    Return val
End


Function ReadVals:Int[]( nVals:Int )
    If index + nVals > readLength
        readError = True
    End
    If readError
        Return New Int[ nVals ]
    End
    Local vals:Int[] = readArray[ index .. index + nVals ]
    index += nVals
    Return vals
End


Public
Function _____________________________________________Types_________________________________________:Void()
End


Function WriteByte:Void( val:Int )
    writeStack.Push( val )
End


Function ReadByte:Int()
    Return ReadVal()
End


Function WriteShort:Void( val:Int )
    writeStack.Push( ( val Shr 8 ) & $FF )
    writeStack.Push( val & $FF )
End


Function ReadShort:Int()
    Local vals:Int[] = ReadVals( 2 )
    Return ( vals[ 0 ] Shl 8 ) | vals[ 1 ]
End


Function WriteInt:Void( val:Int )
    writeStack.Push( ( val Shr 24 ) & $FF )
    writeStack.Push( ( val Shr 16 ) & $FF )
    writeStack.Push( ( val Shr 8 ) & $FF )
    writeStack.Push( val & $FF )
End


Function ReadInt:Int()
    Local vals:Int[] = ReadVals( 4 )
    Return ( vals[ 0 ] Shl 24 ) | ( vals[ 1 ] Shl 16 ) | ( vals[ 2 ] Shl 8 ) | vals[ 3 ]
End


Function WriteBool:Void( val:Bool )
    If val
        writeStack.Push( 1 )
    Else
        writeStack.Push( 0 )
    End
End


Function ReadBool:Bool()
    Return ReadVal() <> 0
End


Function WriteString:Void( str:String )
    Local chars:Int[] = str.ToChars()
    Local len:Int = chars.Length()
    WriteInt( len )
    For Local i:Int = 0 Until len
        writeStack.Push( chars[ i ] )
    Next
End


Function ReadString:String()
    Local len:Int = ReadInt()
    If len > 0
        Return String.FromChars( ReadVals( len ) )
    Else
        Return ""
    End
End


Function _____________________________________________Test__________________________________________:Void()
End


Function TestReadWrite:Void()
    StartWrite( 10 )
    WriteInt( 100 )
    WriteInt( 1000000000 )
    WriteInt( -1 )
    WriteByte( 7 )
    WriteShort( 983 )
    WriteShort( -1 )
    WriteString( "string" )
    WriteBool( True )
    FinishWrite()
    Print StartRead()
    Print "------"
    Print ReadInt()
    Print ReadInt()
    Print ReadInt()
    Print ReadByte()
    Print ReadShort()
    Print ReadShort()
    Print ReadString()
    If ReadBool() Then Print "True" Else Print "False"
    If AllRead() And Not WasReadError() Then Print "Good" Else Print "Bad"
End
 
Back
Top Bottom