Imports Microsoft.Win32
Imports System.Text.RegularExpressions

Imports Stax
Imports Stax.UI

Public MustInherit Class Package
    Private NameValue As String

    Public Property Name() As String
        Get
            Return NameValue
        End Get
        Set(ByVal Value As String)
            NameValue = Value
        End Set
    End Property

    Private VersionValue As String

    Public Property Version() As String
        Get
            Return VersionValue
        End Get
        Set(ByVal Value As String)
            VersionValue = Value
        End Set
    End Property

    Private HomepageValue As String

    Public Property Website() As String
        Get
            Return HomepageValue
        End Get
        Set(ByVal Value As String)
            HomepageValue = Value
        End Set
    End Property

    Private DescriptionValue As String

    Public Property Description() As String
        Get
            Return DescriptionValue
        End Get
        Set(ByVal Value As String)
            DescriptionValue = Value
        End Set
    End Property

    Private VersionDateValue As DateTime

    Public Property VersionDate() As DateTime
        Get
            Return VersionDateValue
        End Get
        Set(ByVal Value As DateTime)
            VersionDateValue = Value
        End Set
    End Property

    Private TreeFolderValue As String

    Public Property TreeFolder() As String
        Get
            Return TreeFolderValue
        End Get
        Set(ByVal Value As String)
            TreeFolderValue = Value
        End Set
    End Property

    Private DownLoadURLValue As String

    Public Property DownLoadURL() As String
        Get
            Return DownLoadURLValue
        End Get
        Set(ByVal Value As String)
            DownLoadURLValue = Value
        End Set
    End Property

    Private DownloadFilenameValue As String

    Public WriteOnly Property DownloadFilename() As String
        Set(ByVal Value As String)
            If Value.Contains("/") Then
                Throw New Exception("invalid download filename")
            End If

            DownLoadURL = PackageRemoteDirValue + Value
        End Set
    End Property

    Private Const PackageRemoteDirValue As String = "http://www.planetdvb.net/dvx/modules/"

    Public Shared ReadOnly Property PackageRemoteDir() As String
        Get
            Return PackageRemoteDirValue
        End Get
    End Property

    Protected Function IsAviSynthPluginRequired(ByVal ParamArray filters As String()) As Boolean
        For Each iFilter As AviSynthFilter In p.TargetAVSDoc.Filters
            For Each i As String In filters
                If iFilter.Script.Contains(i + ".") OrElse iFilter.Script.Contains(i + "(") Then
                    Return True
                End If
            Next
        Next

        Return False
    End Function

    Protected Sub Start(ByVal path As String)
        Dim pw As New ProcessWrapper
        pw.File = path
        pw.Start()
    End Sub

    Public Function VerifyOK() As Boolean
        Return VerifyOK(True)
    End Function

    Public Function VerifyOK(ByVal checkIfRequired As Boolean) As Boolean
        If Not checkIfRequired OrElse IsRequired() Then
            If GetErrorStatus() <> "" Then
                Using f As New ExternalApplicationsForm
                    f.ShowPackage(Me)
                    f.ShowDialog()
                End Using

                If GetErrorStatus() <> "" Then
                    Return False
                End If
            End If

            If Version <> "" AndAlso Not IsCorrectVersion() Then
                If ShowWrongVersionWarning Then
                    Dim m As New Msg
                    m.Title = "Warning!"
                    m.Text = "You are using a unsupported version of " + Name + " and might continue doing so. You might even disable this warning but please note that if you are using a unsupported version there's a very high chance that the version is incompatible." + vbCrLf + vbCrLf + "Please don't send a bug report unless you are very sure the bug is not caused by the fact that you are using a unsupported version."
                    m.AddButton("Continue (not recommended)", DialogResult.Yes)
                    m.AddButton("Get required version", DialogResult.No)
                    m.ShowCheckBox = True
                    m.TextColor = Color.Red
                    m.Icon = MsgIcon.Warning
                    m.CheckBoxText = "Use always wrong " + Name + " version (not recommended)"

                    If m.Show = DialogResult.No Then
                        ShowWrongVersionWarning = Not m.Checked

                        Dim f As New ExternalApplicationsForm
                        f.ShowPackage(Me)
                        f.ShowDialog()
                        f.Dispose()

                        Return False
                    Else
                        ShowWrongVersionWarning = Not m.Checked
                    End If
                End If
            End If
        End If

        Return True
    End Function

    Private FilenameValue As String

    Public Overridable Property Filename() As String
        Get
            Return FilenameValue
        End Get
        Set(ByVal Value As String)
            FilenameValue = Value
        End Set
    End Property

    Public Property ShowWrongVersionWarning() As Boolean
        Get
            Dim arg As List(Of String) = s.WrongVersionWarnings
            Return Not s.WrongVersionWarnings.Contains(Name)
        End Get
        Set(ByVal Value As Boolean)
            If Value Then
                s.WrongVersionWarnings.Remove(Name)
            Else
                s.WrongVersionWarnings.Remove(Name)
                s.WrongVersionWarnings.Add(Name)
            End If
        End Set
    End Property

    Protected Sub Extract(ByVal targetDir As String)
        Dim zipfile As String = GetDownloadPath()

        If File.Exists(zipfile) Then
            Try
                Zip.Unpack(zipfile, targetDir)
                Dim path As String = DirPath.AppendSeparator(targetDir) + Filename

                If File.Exists(path) Then
                    SetPath(path)
                Else
                    Throw New Exception
                End If
            Catch
                Msg.Show("An error occured while extracting the zip archive.", MsgIcon.Error)
            End Try
        End If
    End Sub

    Public Overridable ReadOnly Property FixedDir() As String
        Get
            Return ""
        End Get
    End Property

    Protected Function GetDownloadPath() As String
        Return Paths.DownloadDir + StringHelp.GetRightLast(DownLoadURL, "/")
    End Function

    Public Overridable Function GetErrorStatus() As String
        Dim required As Boolean = IsRequired()

        Dim path As String = GetPath(True)

        If path Is Nothing Then
            path = FindPath()
            SetPath(path)
        End If

        If path Is Nothing Then
            If required Then
                Return "File not found, please click on 'Download' to install this application."
            Else
                Return "File not found but the current project might not require this application. Please click on 'Download' if you want to install this application anyway."
            End If
        End If

        Dim d As String = FixedDir

        If d <> "" AndAlso d.ToUpper <> Filepath.GetDir(path).ToUpper Then
            Return "The file must be located at: """ + d + """. " + Strings.ClickDownload
        End If

        Return ""
    End Function

    Public Overridable Sub Install()
        If DownLoadURL.ToUpper.EndsWith(".EXE") Then
            Start(GetDownloadPath)
        ElseIf DownLoadURL.ToUpper.EndsWith(".ZIP") Then
            Dim d As String = FixedDir

            If d <> "" Then
                Extract(d)
            Else
                Extract(Paths.ExtractDir + Name)
            End If
        End If
    End Sub

    Public Overridable Function IsRequired() As Boolean
        Throw New NotImplementedException
    End Function

    Public Function IsCorrectVersion() As Boolean
        Return IsCorrectVersion(GetPath(False))
    End Function

    Public Function IsCorrectVersion(ByVal path As String) As Boolean
        If Not path Is Nothing Then
            Dim dt As DateTime = File.GetLastWriteTimeUtc(path)

            If dt.AddDays(-2) > VersionDate OrElse dt.AddDays(2) < VersionDate Then
                Return False
            End If

            Return True
        End If
    End Function

    Public Sub SetPath(ByVal path As String)
        s.PackagePaths(Name) = path
    End Sub

    Private Function FindPath() As String
        Dim ret As String = Nothing

        For Each i As String In GetSearchResult()
            ret = i

            If IsCorrectVersion(i) Then
                Return i
            End If
        Next

        Return ret
    End Function

    Public Function GetPath(ByVal checkExist As Boolean) As String
        If s.PackagePaths.ContainsKey(Name) Then
            If checkExist Then
                If File.Exists(s.PackagePaths(Name)) Then
                    Return s.PackagePaths(Name)
                End If
            Else
                Return s.PackagePaths(Name)
            End If
        End If

        Return Nothing
    End Function

    Public Overrides Function ToString() As String
        Return Name
    End Function

    Private Sub AddIfFound(ByVal path As String, ByVal list As List(Of String))
        For Each i As String In list
            If String.Compare(path, i, StringComparison.OrdinalIgnoreCase) = 0 Then
                Exit Sub
            End If
        Next

        If File.Exists(path) Then
            list.Add(path)
        End If
    End Sub

    Public Overridable Function GetSearchResult() As List(Of String)
        Dim list As New List(Of String)
        Dim value As String

        'system
        AddIfFound(CommonDirs.System + Filename, list)

        'system 32 bit wrapper on 64 bit sys
        AddIfFound(CommonDirs.Windows + "syswow64\" + Filename, list)

        'startup
        AddIfFound(CommonDirs.Startup + Filename, list)

        'FixedDir
        If FixedDir <> "" Then
            AddIfFound(FixedDir + Filename, list)
        End If

        'applications
        For Each i As String In DirectoryHelp.FindFiles(Paths.ExtractDir + Name, Filename, StringComparison.OrdinalIgnoreCase, True)
            AddIfFound(i, list)
        Next

        'programs
        For Each i As String In DirectoryHelp.FindFiles(CommonDirs.Programs + Name, Filename, StringComparison.OrdinalIgnoreCase, True)
            AddIfFound(i, list)
        Next

        'AutoGK
        Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\AutoGK")

        If Not key Is Nothing Then
            Dim dir As String = CStr(key.GetValue("InstallDir"))

            If Not dir Is Nothing Then
                For Each i As String In DirectoryHelp.FindFiles(dir, Filename, StringComparison.OrdinalIgnoreCase, True)
                    AddIfFound(i, list)
                Next
            End If

            key.Close()
        End If

        'MUICache
        key = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\ShellNoRoam\MUICache")

        If Not key Is Nothing Then
            For Each i As String In key.GetValueNames
                If i.ToUpper.Contains(Filename.ToUpper) Then
                    Dim m As Match = Regex.Match(i, "\w:\\.+\\" + Filename)

                    If m.Success Then
                        AddIfFound(m.Value, list)
                    End If
                End If
            Next

            key.Close()
        End If

        'Gordian Knot
        key = Registry.CurrentUser.OpenSubKey("Software\GordianKnot")

        If Not key Is Nothing Then
            For Each i As String In key.GetValueNames
                value = CStr(key.GetValue(i))

                If Filepath.GetNameWithExt(value).ToUpper() = _
                    Filename.ToUpper() Then

                    AddIfFound(value, list)
                End If
            Next

            key.Close()
        End If

        'shell applications
        value = RegistryHelp.ClassesRoot.GetString("Applications\" + Filename + "\shell\open\command", Nothing)

        If Not value Is Nothing Then
            value = Regex.Match(value, "\w:\\.+\.exe").Value
            AddIfFound(value, list)
        End If

        'uninstall
        key = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + Name)

        If Not key Is Nothing Then
            value = CStr(key.GetValue("UninstallString"))

            If Not value Is Nothing Then
                Dim m As Match = Regex.Match(value, "\w:\\.+\.exe")

                If m.Success Then
                    Dim path As String = Filepath.GetDir(m.Value) + Filename
                    AddIfFound(path, list)
                End If
            End If

            key.Close()
        End If

        Return list
    End Function
End Class

Public Class AutoCropPackage
    Inherits AviSynthPluginPackage

    Public Sub New()
        Name = "AutoCrop"
        Filename = "AutoCrop.dll"
        Website = "http://www.videofringe.com/autocrop"
        Description = "AutoCrop is a AviSynth plugin used for automatic cropping."
        Version = "1.1"
        VersionDate = New DateTime(2003, 6, 17)
        DownloadFilename = "AutoCrop_1.1.zip"
        TreeFolder = "AviSynth Plugins"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return True
    End Function
End Class

Public Class DirectShowSourcePackage
    Inherits AviSynthPluginPackage

    Public Sub New()
        Name = "DirectShowSource"
        Filename = "DirectShowSource.dll"
        Description = "DirectShowSource is a AviSynth plugin that can open any DirectShow compatible source."
        Version = "2.5.6"
        VersionDate = New DateTime(2005, 10, 28)
        DownloadFilename = "DirectShowSource_2.5.6.zip"
        TreeFolder = "AviSynth Plugins"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        If Not Preparer.ActivePreparer Is Nothing Then
            If TypeOf Preparer.ActivePreparer Is DirectShowDecoderPreparer Then
                Return True
            End If
        End If

        Return IsAviSynthPluginRequired("Deen")
    End Function
End Class

Public Class DeenPackage
    Inherits AviSynthPluginPackage

    Public Sub New()
        Name = "Deen"
        Filename = "Deen.dll"
        Website = "http://ziquash.chez.tiscali.fr"
        Description = "Deen is a AviSynth plugin used for denoising."
        Version = "2005-07-05"
        VersionDate = New DateTime(2005, 7, 5)
        DownloadFilename = "Deen_2005-07-05.zip"
        TreeFolder = "AviSynth Plugins"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return IsAviSynthPluginRequired("Deen")
    End Function
End Class

Public Class UnDotPackage
    Inherits AviSynthPluginPackage

    Public Sub New()
        Name = "UnDot"
        Filename = "UnDot.dll"
        Website = "http://home.comcast.net/~trbarry/"
        Description = "UnDot is a simple median filter for removing dots, that is stray orphan pixels and mosquito noise."
        Version = "2003-01-18"
        VersionDate = New DateTime(2003, 1, 18)
        DownloadFilename = "Undot_2003-01-18.zip"
        TreeFolder = "AviSynth Plugins"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return IsAviSynthPluginRequired("UnDot")
    End Function
End Class

Public Class AviSynthPackage
    Inherits Package

    Public Sub New()
        Name = "AviSynth"
        Filename = "avisynth.dll"
        Version = "2.5.6a"
        Website = "http://www.avisynth.org"
        Description = "AviSynth is used to perform audio and video transformations."
        VersionDate = New DateTime(2005, 10, 28)
        DownLoadURL = "http://kent.dl.sourceforge.net/sourceforge/avisynth2/Avisynth_256.exe"
        TreeFolder = "General"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return True
    End Function
End Class

Public Class AviSynthPluginPackage
    Inherits Package

    Public Overrides ReadOnly Property FixedDir() As String
        Get
            Return Paths.AviSynthPluginsDir
        End Get
    End Property
End Class

Public Class BeSweetPackage
    Inherits Package

    Public Sub New()
        Name = "BeSweet"
        Filename = "BeSweet.exe"
        Version = "1.5b31_library_update"
        Website = "http://dspguru.doom9.net"
        DownloadFilename = "BeSweet_1.5b31_library_update.zip"
        Description = "BeSweet transcodes audio."
        VersionDate = New DateTime(2005, 9, 8)
        TreeFolder = "General"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        If TypeOf p.Audio0.Profile Is CommandLineAudioProfile Then
            Dim profile As CommandLineAudioProfile = DirectCast(p.Audio0.Profile, CommandLineAudioProfile)

            If profile.CommandLines.Contains("-core(") Then
                Return True
            End If
        End If

        If TypeOf p.Audio1.Profile Is CommandLineAudioProfile Then
            Dim profile As CommandLineAudioProfile = DirectCast(p.Audio1.Profile, CommandLineAudioProfile)

            If profile.CommandLines.Contains("-core(") Then
                Return True
            End If
        End If

        If p.Ranges.Count > 0 Then
            Return True
        End If

        Return False
    End Function

    Public Overrides Function GetErrorStatus() As String
        If GetPath(True) Is Nothing Then
            Return MyBase.GetErrorStatus
        End If

        For Each iProfile As AudioProfile In New AudioProfile() {p.Audio0.Profile, p.Audio1.Profile}
            If TypeOf iProfile Is CommandLineAudioProfile Then
                Dim profile As CommandLineAudioProfile = DirectCast(iProfile, CommandLineAudioProfile)

                If profile.CommandLines.Contains("-lame(") Then
                    If File.GetLastWriteTime(Filepath.GetDir(GetPath(False)) + "lame_enc.dll") < New DateTime(2005, 12, 20) Then
                        Return "The lame library is too old. " + Strings.ClickDownload
                    End If
                End If

                If profile.CommandLines.Contains("-bsn(") Then
                    Dim dllMissing As Boolean

                    For Each i As String In New String() {"aacenc32.dll", "neroipp.dll", "aac.dll"}
                        If Not File.Exists(Filepath.GetDir(GetPath(False)) + i) Then
                            dllMissing = True
                        End If
                    Next

                    If dllMissing Then
                        Dim aheadDir As String = DirPath.AppendSeparator( _
                            Environment.GetFolderPath( _
                            Environment.SpecialFolder.CommonProgramFiles)) + _
                            "Ahead\"

                        For Each i As String In New String() {"aacenc32.dll", "aac.dll"}
                            If File.Exists(aheadDir + "AudioPlugins\" + i) Then
                                FileHelp.Copy(aheadDir + "AudioPlugins\" + i, Filepath.GetDir(GetPath(False)) + i)
                            End If
                        Next

                        If File.Exists(aheadDir + "Lib\neroipp.dll") Then
                            FileHelp.Copy(aheadDir + "Lib\neroipp.dll", Filepath.GetDir(GetPath(False)) + "neroipp.dll")
                        End If
                    Else
                        Return MyBase.GetErrorStatus
                    End If

                    For Each i As String In New String() {"aacenc32.dll", "neroipp.dll", "aac.dll"}
                        If Not File.Exists(Filepath.GetDir(GetPath(False)) + i) Then
                            Return "Nero libraries required for AAC encoding couldn't be located. Please install Nero."
                        End If
                    Next
                End If
            End If
        Next

        Return MyBase.GetErrorStatus
    End Function
End Class

Public Class VorbisPackage
    Inherits Package

    Public Sub New()
        Name = "Vorbis"
        Version = "Lancer 2005-11-21"
        Filename = "libvorbis.dll"
        Description = "Vorbis is a audio codec."
        Website = "http://www.vorbis.com"
        VersionDate = New DateTime(2005, 11, 21)
        DownloadFilename = "Vorbis_Lancer_2005-11-21.zip"
        TreeFolder = "Codecs"
    End Sub

    Public Overrides ReadOnly Property FixedDir() As String
        Get
            Return Filepath.GetDir(Paths.BeSweet.GetPath(False))
        End Get
    End Property

    Public Overrides Function IsRequired() As Boolean
        If TypeOf p.Audio0.Profile Is CommandLineAudioProfile Then
            Dim profile As CommandLineAudioProfile = DirectCast(p.Audio0.Profile, CommandLineAudioProfile)

            If profile.CommandLines.Contains("-ogg(") Then
                Return True
            End If
        End If

        Return False
    End Function
End Class

Public Class DecombPackage
    Inherits AviSynthPluginPackage

    Public Sub New()
        Name = "Decomb"
        Version = "5.2.2"
        Filename = "Decomb.dll"
        Description = "Decomb is a AviSynth plugin that performs various field operations like deinterlacing."
        Website = "http://neuron2.net"
        VersionDate = New DateTime(2005, 9, 4)
        DownloadFilename = "Decomb_5.2.2.zip"
        TreeFolder = "AviSynth Plugins"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return IsAviSynthPluginRequired("FieldDeinterlace", "Telecide", "Decimate")
    End Function
End Class

Public Class DGDecodePackage
    Inherits AviSynthPluginPackage

    Public Sub New()
        Name = "DGDecode"
        Filename = "DGDecode.dll"
        Website = "http://www.neuron2.net"
        Description = "DGIndex is used to open MPEG-2 files e.g. DVD's and DVB captures."
        Version = "1.4.5"
        VersionDate = New DateTime(2005, 10, 1)
        DownloadFilename = "DGMPGDec_1.4.5.zip"
        TreeFolder = "AviSynth Plugins"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return IsAviSynthPluginRequired("MPEG2Source")
    End Function
End Class

Public Class DGIndexPackage
    Inherits Package

    Public Sub New()
        Name = "DGIndex"
        Filename = "DGIndex.exe"
        Version = "1.4.5"
        Website = "http://www.neuron2.net"
        Description = "DGIndex is used to open MPEG-2 files e.g. DVD's and DVB captures."
        DownloadFilename = "DGMPGDec_1.4.5.zip"
        VersionDate = New DateTime(2005, 10, 1)
        TreeFolder = "General"
    End Sub

    Public Overrides Sub Install()
        MyBase.Install()

        If Not GetPath(True) Is Nothing Then
            Dim dgindexDir As String = Filepath.GetDir(GetPath(False))

            If File.Exists(dgindexDir + "DGDecode.dll") AndAlso _
                Not File.Exists(Paths.AviSynthPluginsDir + _
                "DGDecode.dll") Then

                File.Copy(dgindexDir + "DGDecode.dll", _
                    Paths.AviSynthPluginsDir + "DGDecode.dll", True)
            End If
        End If
    End Sub

    Public Overrides Function IsRequired() As Boolean
        If IsAviSynthPluginRequired("MPEG2Source") Then
            Return True
        End If

        If Not Preparer.ActivePreparer Is Nothing Then
            If Preparer.ActivePreparer.Name.Contains("DGIndex") Then
                Return True
            End If
        End If

        Return False
    End Function
End Class

Public Class DivXPackage
    Inherits Package

    Public Sub New()
        Name = "DivX"
        Filename = "DivX.dll"
        Version = "6.1.1"
        Website = "http://www.divx.com"
        Description = "DivX is a MPEG4 codec."
        DownLoadURL = "http://download.divx.com/divx/DivXPlay.exe"
        VersionDate = New DateTime(2006, 1, 26)
        TreeFolder = "Codecs"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return TypeOf p.VideoEncoder Is DivXVirtualDubEncoder
    End Function

    Public Overrides Function GetErrorStatus() As String
        If GetPath(True) Is Nothing Then
            Return MyBase.GetErrorStatus
        End If

        If File.GetLastWriteTime(GetPath(False)) < (New DateTime(2005, 9, 28)).AddDays(-2) Then
            Return "StaxRip requires at least DivX version 6.0.3. Please click on 'Download' to install the required version."
        End If

        Return MyBase.GetErrorStatus
    End Function
End Class

Public Class FluxSmoothPackage
    Inherits AviSynthPluginPackage

    Public Sub New()
        Name = "FluxSmooth"
        Filename = "FluxSmooth.dll"
        Description = "FluxSmooth is a AviSynth plugin that removes noise from poor video sources."
        Version = "1.1a"
        VersionDate = New DateTime(2004, 7, 29)
        Website = "http://www.indeus.com/sansgrip/avisynth"
        DownloadFilename = "FluxSmooth_1.1a.zip"
        TreeFolder = "AviSynth Plugins"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return IsAviSynthPluginRequired("FluxSmooth")
    End Function
End Class

Public Class VSFilterPackage
    Inherits AviSynthPluginPackage

    Public Sub New()
        Name = "VSFilter"
        Filename = "VSFilter.dll"
        Description = "VSFilter is a AviSynth plugin to add a forced subtitle. Furthermore it's a DirectShow Filter to display optional subtitles."
        Version = "2.37"
        VersionDate = New DateTime(2005, 11, 25)
        Website = "http://sourceforge.net/projects/guliverkli/"
        DownloadFilename = "VSFilter_2.37.zip"
        TreeFolder = "AviSynth Plugins"
    End Sub

    Public Overrides Sub Install()
        MyBase.Install()

        If File.Exists(GetPath(False)) Then
            Process.Start("regsvr32", "/s """ + GetPath(False) + """")
        End If
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return IsAviSynthPluginRequired("VobSub", "TextSub")
    End Function
End Class

Public Class JavaPackage
    Inherits Package

    Public Sub New()
        Name = "Java"
        Filename = "Java.exe"
        Website = "http://java.com"
        Description = "Java is required by ProjectX."
        TreeFolder = "General"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return Paths.Packages("ProjectX").IsRequired()
    End Function
End Class

Public Class VirtualDubPackage
    Inherits Package

    Public Sub New()
        Name = "VirtualDub"
        Filename = "VirtualDub.exe"
        Version = "1.5.11"
        Website = "http://www.virtualdub.org"
        Description = "VirtualDub is a AVI processing application used to create AVI files."
        VersionDate = New DateTime(2005, 10, 2)
        DownLoadURL = "http://kent.dl.sourceforge.net/sourceforge/virtualdub/VirtualDub-1.6.11.zip"
        TreeFolder = "General"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return TypeOf p.VideoEncoder Is VirtualDubEncoder AndAlso _
            DirectCast(p.VideoEncoder, VirtualDubEncoder).UseVirtualDub
    End Function
End Class

Public Class ProjectXPackage
    Inherits Package

    Public Sub New()
        Name = "ProjectX"
        Filename = "ProjectX.jar"
        Version = "0.90.1"
        Website = "http://www.lucike.info"
        Description = "ProjectX is a MPEG2 deMuxer mainly used for DVB captures."
        VersionDate = New DateTime(2005, 9, 30)
        DownloadFilename = "ProjectX_0.90.1.zip"
        TreeFolder = "General"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        If Not Preparer.ActivePreparer Is Nothing Then
            If Preparer.ActivePreparer.Name.Contains("ProjectX") Then
                Return True
            End If
        End If

        Return False
    End Function
End Class

Public Class XviDPackage
    Inherits Package

    Public Sub New()
        Name = "XviD"
        Filename = "xvidvfw.dll"
        Version = "1.1.0"
        Website = "http://www.koepi.org"
        Description = "XviD is a MPEG-4 codec."
        VersionDate = New DateTime(2005, 12, 30)
        DownLoadURL = "http://www.koepi.org/XviD-1.1.0-30122005.exe"
        TreeFolder = "Codecs"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return TypeOf p.VideoEncoder Is XviDVirtualDubEncoder
    End Function
End Class

Public Class x264Package
    Inherits Package

    Public Sub New()
        Name = "x264"
        Filename = "x264.exe"
        Version = "438"
        Website = "http://developers.videolan.org/x264.html"
        Description = "x264 is a MPEG-4 H264/AVC codec."
        VersionDate = New DateTime(2006, 2, 16)
        DownloadFilename = "x264_438.zip"
        TreeFolder = "Codecs"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return TypeOf p.VideoEncoder Is x264Encoder
    End Function
End Class

Public Class VirtualDubModPackage
    Inherits Package

    Public Sub New()
        Name = "VirtualDubMod"
        Filename = "VirtualDubMod.exe"
        Version = "1.5.10.2"
        Website = "http://virtualdubmod.sourceforge.net"
        Description = "VirtualDubMod is used for tasks like encoding, muxing, demuxing and cutting."
        VersionDate = New DateTime(2005, 8, 25)
        DownLoadURL = "http://kent.dl.sourceforge.net/sourceforge/virtualdubmod/VirtualDubMod_1_5_10_2_All_inclusive.zip"
        TreeFolder = "General"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        If TypeOf p.VideoEncoder Is VirtualDubEncoder AndAlso _
            Not DirectCast(p.VideoEncoder, VirtualDubEncoder).UseVirtualDub Then

            Return True
        End If

        If Not Preparer.ActivePreparer Is Nothing Then
            If TypeOf Preparer.ActivePreparer Is AVIPreparer Then
                Return True
            End If

            If TypeOf Preparer.ActivePreparer Is DirectShowDecoderPreparer Then
                Return True
            End If
        End If

        If p.Ranges.Count > 0 Then
            Return True
        End If

        Return False
    End Function
End Class

Public Class MP4BoxPackage
    Inherits Package

    Public Sub New()
        Name = "MP4Box"
        Filename = "MP4Box.exe"
        Version = "2006-02-14"
        Website = "http://www.aziendeassociate.it/cd.asp?dir=/gpac/dev"
        Description = "MP4Box is a MP4 Muxer."
        VersionDate = New DateTime(2006, 2, 14)
        DownloadFilename = "MP4Box_2006-02-14.zip"
        TreeFolder = "Muxers"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return TypeOf p.VideoEncoder.Muxer Is MP4BoxMuxer
    End Function
End Class

Public Class MKVtoolnixPackage
    Inherits Package

    Public Sub New()
        Name = "MKVtoolnix"
        Filename = "mkvmerge.exe"
        Version = "1.6.5"
        Website = "http://www.bunkus.org/videotools/mkvtoolnix"
        Description = "MKVtoolnix contains mkvmerge which creates MKV files."
        DownLoadURL = "http://www.bunkus.org/videotools/mkvtoolnix/win32/mkvtoolnix-unicode-1.6.5-setup.exe"
        VersionDate = New DateTime(2005, 12, 7)
        TreeFolder = "Muxers"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return TypeOf p.VideoEncoder.Muxer Is mkvmergeMuxer
    End Function
End Class

Public Class MediaInfoPackage
    Inherits Package

    Public Active As Boolean

    Public Sub New()
        Name = "MediaInfo"
        Filename = "MediaInfo.dll"
        Version = "0.7.0.3 DLL"
        Website = "http://mediainfo.sourceforge.net"
        Description = "MediaInfo inspects media files."
        VersionDate = New DateTime(2005, 11, 8)
        DownloadFilename = "MediaInfoDLL_0.7.0.3.zip"
        TreeFolder = "General"
    End Sub

    Public Overrides ReadOnly Property FixedDir() As String
        Get
            Return CommonDirs.Startup
        End Get
    End Property

    Public Overrides Function IsRequired() As Boolean
        Return Active
    End Function
End Class

Public Class DivXMuxPackage
    Inherits Package

    Public Sub New()
        Name = "DivXMux"
        Filename = "DivXMux.exe"
        Version = "6.1 build 24"
        Website = "http://labs.divx.com/archives/000066.html"
        Description = "Creates DivX container files."
        VersionDate = New DateTime(2005, 11, 17)
        DownloadFilename = "DivXMux_6.1build24.zip"
        TreeFolder = "Muxers"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return TypeOf p.VideoEncoder.Muxer Is DMFMuxer
    End Function
End Class

Public Class PMPMuxerPackage
    Inherits Package

    Public Sub New()
        Name = "PMP Muxer"
        Filename = "pmp_muxer.exe"
        Version = "1.02"
        Website = "http://jonny.leffe.dnsalias.com"
        Description = "Creates PMP container files for the PlayStation Portable."
        VersionDate = New DateTime(2006, 1, 25)
        DownloadFilename = "PMP_Muxer_1.02.zip"
        TreeFolder = "Muxers"
    End Sub

    Public Overrides Function IsRequired() As Boolean
        Return TypeOf p.VideoEncoder.Muxer Is PMPMuxer
    End Function
End Class