public Tox(ToxOptions options, ToxData data, ToxEncryptionKey key = null)
{
if (data == null)
throw new ArgumentNullException("data");
var optionsStruct = options.Struct;
if (key == null || !data.IsEncrypted)
{
var error = ToxErrorNew.Ok;
optionsStruct.SetData(data.Bytes, ToxSaveDataType.ToxSave);
_tox = ToxFunctions.New(ref optionsStruct, ref error);
if (_tox == null || _tox.IsInvalid || error != ToxErrorNew.Ok)
throw new Exception("Could not create a new instance of tox, error: " + error.ToString());
}
else
{
var error = ToxErrorNew.Ok;
var decryptError = ToxErrorDecryption.Ok;
byte[] decryptedData = ToxEncryption.DecryptData(data.Bytes, key, out decryptError);
optionsStruct.SetData(decryptedData, ToxSaveDataType.ToxSave);
_tox = ToxFunctions.New(ref optionsStruct, ref error);
if (_tox == null || _tox.IsInvalid || error != ToxErrorNew.Ok || decryptError != ToxErrorDecryption.Ok)
throw new Exception(string.Format("Could not create a new instance of tox, error: {0}, decrypt error: {1}" + error.ToString(), decryptError.ToString()));
}
optionsStruct.Free();
Options = options;
}