private object GetScriptMessage(int script_id, bool return_string_as_byte_array, bool get_error = false)
{
CHDK_ScriptMsgType type;
int subtype, script_id2;
byte[] data;
while (true)
{
_session.CHDK_ReadScriptMsg(out type, out subtype, out script_id2, out data);
if (type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_NONE) // no more messages; no return value
return null;
if (script_id2 != script_id) // ignore message from other scripts
continue;
if (!get_error && type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_RET) // return info!
{
switch ((CHDK_ScriptDataType)subtype)
{
case CHDK_ScriptDataType.PTP_CHDK_TYPE_BOOLEAN:
return (data[0] | data[1] | data[2] | data[3]) != 0;
case CHDK_ScriptDataType.PTP_CHDK_TYPE_INTEGER:
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
case CHDK_ScriptDataType.PTP_CHDK_TYPE_STRING:
if (return_string_as_byte_array)
return data;
else
return (new ASCIIEncoding()).GetString(data);
default:
throw new Exception("script returned unsupported data type: " + type.ToString());
}
}
if (type == CHDK_ScriptMsgType.PTP_CHDK_S_MSGTYPE_ERR) // hmm.. error
{
if (get_error)
{
return (new ASCIIEncoding()).GetString(data);
}
else
{
throw new Exception("error running script: " + (new ASCIIEncoding()).GetString(data));
}
}
// ignore other (user) messages
}
}