How to signal I have nothing to return to GenerateNewTrack?

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey for Windows 4.

Moderators: Gurus, Addon Administrators

SatinKnights
Posts: 46
Joined: Wed Jul 18, 2007 9:45 pm

How to signal I have nothing to return to GenerateNewTrack?

Post by SatinKnights »

Hi. My autodj script is having a problem with MM4. How do I signal to GenerateNewTrack that I was not able to find a track to add to the NowPlaying list?
In MM3

Set GenerateNewTrack = Nothing

worked. Now, that gives a "Invalid Variant Operation" which I think is another term for variable type mismatch.
If I do a

Set GenerateNewTack = SDB.NewSongList

so I have sent a pointer to a structure, but the structure is empty, I get a "Error: Read from 00000000", which is worse.

It feels like in MM3, the receiving internal function was checking for the Nothing (null) and dealing with it cleanly. But, now there are more strict checks that are throwing exceptions before getting that far.

So, what is the correct object/structure/value to return if I looked for a track, did not find one and do not have a track to add?

Versions 4.0.1.1461, WinXP
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: How to signal I have nothing to return to GenerateNewTra

Post by trixmoto »

You can't, deliberately. If you're going to create a script of this type, it must always return a track. My "Auto Album DJ" script has a fall back clause where it just selects any random track from the library, if it can't find an album to play, something like this...

Code: Select all

    Set iter = SDB.Database.OpenSQL("SELECT ID FROM Songs WHERE ID>0 ORDER BY Random(*) LIMIT 1")
    Set iter = SDB.Database.QuerySongs("ID="&iter.ValueByIndex(0))
    Set GenerateNewTrack = iter.Item
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
SatinKnights
Posts: 46
Joined: Wed Jul 18, 2007 9:45 pm

Re: How to signal I have nothing to return to GenerateNewTra

Post by SatinKnights »

Well, MM3 worked fine. The script is StayInSameStyle AutoDJ, so it has been around for a long time. If Nothing was returned, the music stopped playing at the end of the current track. If the music stopped, it was because all the songs that fit the criteria of what the user wanted to listen to was played. Just picking a random song does not fit with the style of this script.

Therefore, this is a regression bug. For the main program to not validate data handed to it by an external script before acting on the non-existant data is a bug. Now to go find where to log bugs at.
ZvezdanD
Posts: 3260
Joined: Thu Jun 08, 2006 7:40 pm

Re: How to signal I have nothing to return to GenerateNewTra

Post by ZvezdanD »

SatinKnights wrote:Well, MM3 worked fine. The script is StayInSameStyle AutoDJ, so it has been around for a long time. If Nothing was returned, the music stopped playing at the end of the current track. If the music stopped, it was because all the songs that fit the criteria of what the user wanted to listen to was played. Just picking a random song does not fit with the style of this script.

Therefore, this is a regression bug. For the main program to not validate data handed to it by an external script before acting on the non-existant data is a bug. Now to go find where to log bugs at.
I absolutely agree with you. It is obviously a MM4 bug.
Magic Nodes 4.3.3 / 5.2 RegExp Find & Replace 4.4.9 / 5.2  Invert Selection/Select None 1.5.1  Export/Create Playlists for Child Nodes 4.1.1 / 5.4.1  Expand Child Nodes/Expand All 1.1.2  Event Logger 2.7  Filtered Statistics Report 1.6  Track Redirection & Synchronization 3.4.2  Restore/Synchronize Database 3.1.8 / 4.0.1  Find Currently Playing Track 1.3.2  Queue List 1.2.1  Add to Library on Play 1.0.1  Tree Report for Child Nodes 1.1.1  Update Location of Files in Database 1.4.5 / 2.3  Inherit Child Playlists 1.0.3  Add Currently Playing/Selected Track(s) to Playlist 1.2
SatinKnights
Posts: 46
Joined: Wed Jul 18, 2007 9:45 pm

Re: How to signal I have nothing to return to GenerateNewTra

Post by SatinKnights »

Well, as I was writing things up for a bug report, I was able to figure out a usable solution.

case #1:
Set GenerateNewTrack = Nothing
fails with ""Invalid Variant Operation" when it did work in MM3.

case #2:
Set list = SDB.NewSongList
Set GenerateNewTrack = list.Item(0)
fails with "Read from 00000000"

But case #3:
Set list = SDB.NewSongList
Set GenerateNewTrack = list
or the equivalent
Set GenerateNewTrack = SDB.NewSongList
actually comes through without an error. So, there is some internal validation checking after all. My guess is it is doing a list.Count > 0 in there somewhere. My earlier message had a case #2 triggering where I thought I was testing case #3, so that is the difference between the first post and now.

Therefore replacing
Set GenerateNewTrack = Nothing
with
Set GenerateNewTrack = SDB.NewSongList
appears to work when going from MM3 to MM4. Now I have to go and try the new change on MM3 to see if it works there as well before I update my script for the public.
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: How to signal I have nothing to return to GenerateNewTra

Post by trixmoto »

The wiki clearly states that a SongData object should be returned and says nothing about anything else being valid. There may be a change of functionality here, but the fact remains that you should never return nothing.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
SatinKnights
Posts: 46
Joined: Wed Jul 18, 2007 9:45 pm

Re: How to signal I have nothing to return to GenerateNewTra

Post by SatinKnights »

The key word there is "should". But there are corner cases where there the script cannot return a song and the main program needs to validate if it is receiving a song entry before trying to process it.

A) Assume, the genre is only contains three songs. My script settings say play songs from the current genre without repeats. After three songs, there is nothing left. SELECT RANDOM, while possible, is the wrong thing to do in this situation. The problem is only avoidable by doing the what the script was written to avoid.

B) If the user has done a "Clear Database" while the music is playing, then there isn't a song to select, even with the SELECT RANDOM database call. The script is still going to fire at the start of the last song in the current NowPlaying list. This case is not avoidable by any means.

My original post was essentially "What is the correct way to signal I am empty handed?" If there isn't a proper way to do that, that in its self is a bug. For the moment, I have found a way to accomplish that without generating a popup error, segment fault violation or reading from a null pointer. Eventually, the devs need to look at the function, add the "empty case" and then add documentation for it. While they are in there, they should add extra validation of the data they are receiving from the external script/program so that it deals with bad data cleanly. In programming, you have to validate data from external sources and even from internal sources.
Post Reply