3.0.1.1127 --- problems with reading/writing ini

Beta Testing for Windows Products and plugins

Moderator: Gurus

Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

3.0.1.1127 --- problems with reading/writing ini

Post by Teknojnky »

Ok I've been trying to figure out a problem with tweak monkey script regarding the 'fastnploading' option where it supposed to save the now playing list to the library and restore it upon restart.


in the mediamonkey.ini options are:

Code: Select all

[TweakMonkey]
ClearNowPlayingOnExit=False
UseFastNPLoading=True
NPCurrentSong=10

[Player]
NowPlayingPosition=10

using the below code, when startup, change now playing position, then shut down, the now playing positions do not save properly.

Code: Select all

Option Explicit

'note this is a small subset of the full script to help narrow down the problem

Const ScriptName = "TweakMonkey"     ' Script Name
Const Version = "1.02"               ' Version
Const LastUpdated = "2006-04-02"     ' Last updated

Const PlayNow = "Play in MediaMonkey"
Const PlayNext = "Play next in MediaMonkey"
Const PlayLast = "Play last in MediaMonkey"

Dim Tree : Set Tree = SDB.MainTree
Dim INI : Set INI = SDB.IniFile
Dim UI : Set UI = SDB.UI 


' ******************************************************************************************************
' EVENT OnShutdown
' **********************************

Sub SDB_OnShutDown

    Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
    
    SetINIString "PlaybackPosition", SDB.Player.PlayBackTime
    SetINIString "PlaybackState", SDB.Player.IsPlaying & "-" & SDB.Player.IsPaused
    SetINIString "NPCurrentSong", SDB.Player.CurrentSongIndex

    If DoesPlaylistExist("NPSaved") Then SDB.PlaylistByTitle("NPSaved").Delete
    
    If SDB.Player.PlaylistCount > 0 Then
        If GetINIBool("UseFastNPLoading", False) Then

            Dim RootPlaylist : Set RootPlaylist = SDB.PlaylistByTitle("")
            Dim NPSaved : Set NPSaved = RootPlaylist.CreateChildPlaylist("NPSaved")
            NPSaved.Clear
            
            Dim CSL : Set CSL = SDB.Player.CurrentSongList
            Dim i
            For i = 0 To CSL.Count - 1            ' NPSaved.AddTracks CSL    seems to be broken
                NPSaved.AddTrack(CSL.Item(i))
            Next
        
'             SDB.Player.PlaylistClear
        End If
    End If
    
    If GetINIBool("ClearNowPlayingOnExit", False) Then
        SDB.Player.PlaylistClear
    End If

End Sub


' **********************************
' EVENT OnStartup
' **********************************

Sub OnStartup

    Do While Not SDB.IsRunning
      SDB.ProcessMessages
    Loop

    Script.RegisterEvent SDB, "OnShutDown", "SDB_OnShutDown"
    
    If GetINIBool("UseFastNPLoading", False) Then
        If DoesPlaylistExist("NPSaved") Then
            If GetINIBool("AutoRandomize", False) Then
                RandomizePlayList SDB.PlaylistByTitle("NPSaved")
            End If
            SDB.Player.PlaylistClear    ' To make sure that the playlist is really empty
            SDB.Player.PlaylistAddTracks(SDB.PlaylistByTitle("NPSaved").Tracks)
            
            SDB.Player.CurrentSongIndex = (GetINIString("NPCurrentSong", 0))
'             SDB.Player.CurrentSongIndex = CLng(GetINIString("NPCurrentSong", 0))
        End If
    Else
        If GetINIBool("LoadPlaylist", False) Then
            Dim PlaylistName : PlaylistName = GetINIString("Playlist", "")
            If PlaylistName <> "" Then
                If DoesPlaylistExist(PlaylistName) Then
                    SDB.Player.PlaylistClear
                    SDB.Player.PlaylistAddTracks(SDB.PlaylistByTitle(PlaylistName).Tracks)
                End If
            End If      
        End If
        If GetINIBool("AutoRandomize", False) Then
            RandomizeNowPlaying
        End If
    End If
    

End Sub 


' ******************************************************************************************************
' HELPER METHODS
' **********************************

Function DoesPlaylistExist(PlaylistName)
    DoesPlaylistExist = (SDB.PlaylistByTitle(PlaylistName).Id <> 0)
End Function

Sub RandomizePlayList(Playlist)
    Randomize
    Dim Songlist : Set Songlist = Playlist.Tracks
    Dim n : n = SongList.Count
    
    Dim i
    For i = 0 To n - 1
        Playlist.MoveTrack Songlist.Item(i), Songlist.Item(Int(n * Rnd))
    Next
End Sub

Sub RandomizeNowPlaying        
    Randomize
    Dim n : n = SDB.Player.PlaylistCount
    
    Dim i
    For i = 0 To n - 1
        SDB.Player.PlaylistMoveTrack i, Int(n * Rnd)
    Next
End Sub


'%%%%%%%%%%%%%%%
' INI PROPS
'%%%%%%%%%%%%%%%

Function GetINIBool(pName, pValueIfMissing)
    If pValueIfMissing Then
        If SDB.IniFile.StringValue(ScriptName, pName) = "False" Then
            GetINIBool = False
        Else
            GetINIBool = True
        End If
    Else
        If SDB.IniFile.StringValue(ScriptName, pName) = "True" Then
            GetINIBool = True
        Else
            GetINIBool = False
        End If
    End If
End Function

Function GetINIString(pName, pValueIfMissing)
    GetINIString = SDB.IniFile.StringValue(ScriptName, pName)
    If GetINIString = "" Then
        GetINIString = pValueIfMissing
	End If
End Function

Sub SetINIString(pName, pValue)
	SDB.IniFile.StringValue(ScriptName, pName) = pValue
End Sub

Sub SaveCheckValue(Sheet, CheckName)
    SetINIString CheckName, Sheet.Common.ChildControl(CheckName).Checked
End Sub
Sub SaveTextValue(Sheet, CheckName)
    SetINIString CheckName, Sheet.Common.ChildControl(CheckName).Text
End Sub


'%%%%%%%%%%%%%%%
' MEDIAMONKEY
'%%%%%%%%%%%%%%%

Function GetINIPath()
    Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject") 
  
    GetINIPath = SDB.MyMusicPath & "MediaMonkey\MediaMonkey.ini"
    If FSO.FileExists(GetINIPath) Then Exit Function
  
    GetINIPath = SDB.ApplicationPath & "MediaMonkey.ini"
    If FSO.FileExists(GetINIPath) Then Exit Function
   
    GetINIPath = ""
End Function

Function GetINILocation()
    Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject") 
  
    If FSO.FileExists(SDB.MyMusicPath & "MediaMonkey\MediaMonkey.ini") Then
        GetINILocation = 1
    ElseIf FSO.FileExists(SDB.ApplicationPath & "MediaMonkey.ini") Then
        GetINILocation = 2
    Else   
        GetINILocation = 0
    End If
End Function

Function GetDBPath()
    GetDBPath = SDB.Database.Path
End Function
At startup, the now playing position should be @ track 11 as expected.

change track position to track 5 then shut down MM3, results in the following mediamonkey.in settings:

Code: Select all

NPCurrentSong=10

and 

NowPlayingPosition=4
I am not able to consistently get the NPCurrentSong and NowPlayingPosition to save properly. Sometimes the NPCurrentSong will be correct, most times the NowPlayingPosition will be correct and NPCurrentSong Incorrect.

Changing the current track and restarting many different times I even get various read memory errors or other crashes from where I have to end the mediamonkey.exe process from the task manager.

Now I did not write all the helper functions for the ini stuff, I am not sure there is a problem with the script code or with the way MM is writing the ini files, but I can't seem to get this working properly.
Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

Post by Teknojnky »

It has to be something in this helper code which is conflicting or not working right, if I remove these and use SDB.IniFile to get/set the settings, then it seems to work stably.

Code: Select all

'%%%%%%%%%%%%%%% 
' INI PROPS 
'%%%%%%%%%%%%%%% 

Function GetINIBool(pName, pValueIfMissing) 
    If pValueIfMissing Then 
        If SDB.IniFile.StringValue(ScriptName, pName) = "False" Then 
            GetINIBool = False 
        Else 
            GetINIBool = True 
        End If 
    Else 
        If SDB.IniFile.StringValue(ScriptName, pName) = "True" Then 
            GetINIBool = True 
        Else 
            GetINIBool = False 
        End If 
    End If 
End Function 

Function GetINIString(pName, pValueIfMissing) 
    GetINIString = SDB.IniFile.StringValue(ScriptName, pName) 
    If GetINIString = "" Then 
        GetINIString = pValueIfMissing 
   End If 
End Function 

Sub SetINIString(pName, pValue) 
   SDB.IniFile.StringValue(ScriptName, pName) = pValue 
End Sub 

Sub SaveCheckValue(Sheet, CheckName) 
    SetINIString CheckName, Sheet.Common.ChildControl(CheckName).Checked 
End Sub 
Sub SaveTextValue(Sheet, CheckName) 
    SetINIString CheckName, Sheet.Common.ChildControl(CheckName).Text 
End Sub 

Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

Post by Teknojnky »

Actually, this is making no sense to me.

I am setting 2 values on shut down, and randomly I get one of the REMOVED from the mediamonkey.ini and I have no idea how or why.
Teknojnky
Posts: 5537
Joined: Tue Sep 06, 2005 11:01 pm
Contact:

Post by Teknojnky »

Ok really stripped down script:

Code: Select all

Option Explicit

Const ScriptName = "TweakMonkey"     ' Script Name

Dim Tree : Set Tree = SDB.MainTree
Dim INI : Set INI = SDB.IniFile
Dim UI : Set UI = SDB.UI 

Sub SDB_OnShutDown

	SDB.IniFile.StringValue(ScriptName, "NPCurrentSong") = SDB.Player.CurrentSongIndex
	SDB.IniFile.StringValue(ScriptName, "NPCurTest") = SDB.Player.CurrentSongIndex
  msgbox(SDB.Player.CurrentSongIndex)    
End Sub


Sub OnStartup

    Do While Not SDB.IsRunning
      SDB.ProcessMessages
    Loop

    Script.RegisterEvent SDB, "OnShutDown", "SDB_OnShutDown"
    
    If SDB.IniFile.BoolValue (ScriptName, "UseFastNPLoading") = True Then
  msgbox(SDB.Player.CurrentSongIndex)
          
          SDB.Player.CurrentSongIndex = SDB.IniFile.StringValue(ScriptName,"NPCurrentSong")
  msgbox(SDB.Player.CurrentSongIndex)          

    End If
    
End Sub 


Half the time I didn't get the NPCurTest value written.

When it does get written, at startup it is gets removed somehow.

WHY is my NPCurTest getting removed?

and why is the value being read (which is in the INI correct) from the ini into the CurrentSongIndex not correct?

AND I keep getting unhandled win32 errors with only this script loaded (all auto scripts removed and all scripts.ini commented out).
PetrCBR
Posts: 1780
Joined: Tue Mar 07, 2006 5:31 pm
Location: Czech
Contact:

Post by PetrCBR »

This "ini" issue will be fixed in next release.
Post Reply