Here is typically how I would encrypt my data: Give your sound types the DeSerialize(TStream) and Serialize(TStream) methods, and have them save (Serialize) and load (DeSerialize) in the same order. So you could, for example, serialize the type into a bankstream (in memory) and encrypt the bankstream (using
RC4) before outputting to the external file. When DeSerializing you just have to load the stream into a bankstream again and decrypt before passing it off to the DeSerialize type.
Here is some example code (progressive saving):
'The TTile type..
Method DeSerialize:TTile(stream:TStream)
SetId(stream.ReadInt())
SetName(ReadNString(stream))
setFlags(stream.ReadInt())
texture = TTileTexture.Load(stream)
Return Self
End Method
Method Serialize(stream:TStream)
stream.WriteInt(getID())
WriteNString(stream, GetName())
stream.WriteInt(GetFlags())
texture.Serialize(stream)
End Method
'The TTileTexture type
Method Serialize(stream:TStream)
PixmapDataType.WriteObject(getPixmap(), stream)
End Method
Method DeSerialize:TTileTexture(stream:TStream)
Local pix:Object
pix = PixmapDataType.ReadObject(stream)
SetPixmap(TPixmap(pix))
Return Self
End Method
Now, I'm not certain the sound module has implemented the saving of sound data, if it does there should be a SoundDataType (or similar) global instance defined somewhere in one of the sound/audio modules - you might have to figure out how to save it without!
And loading a 'soundmap,' if you will, could be done with a While...Wend loop (TTile.Load() is a wrapper function for creating and DeSerializing a TTile):
'The TTileMap type..
Method DeSerialize:TTileMap(stream:TStream)
While Not stream.Eof()
InsertTile(TTile.Load(stream))
Wend
Return Self
End Method
A Serializing example:
Local mainset:TTileMap = New(TTileMap).Create()
'Insert some tiles...
'Save the information in-memory and then output it to 'set00.dts'
Local stream:TBankStream = CreateBankStream(Null)
mainset.Serialize(stream)
CryptStream(stream, "inputkeyhere!")
stream._bank.Save("set00.dts")
stream.Close()
'Since RC4 can be used the exact same way to encrypt and decrpyt information, this function can be used to Decrypt or Encrypt the stream
Function CryptStream:TBankStream(url:Object, Key:String)
Local bstream:TBankStream = TBankStream(url)
If bstream = Null
bstream = TBankStream.Create(TBank.Load(url))
End If
If bstream <> Null
RC4_Bytes(bstream._bank.Lock(), bstream._bank.Capacity(), Key)
End If
Return bstream
End FunctionI'll be releasing all the memcrypt modules within the next few weeks.