Imports System
Imports System.IO

Public Class ISOTools

    Public Function getCDfile(ByVal ISOname As String, ByVal StartSector As Integer, ByVal FileSize As Integer)


        Dim fileArr = New List(Of Byte)
        Dim fs As New FileStream(ISOname, FileMode.Open, FileAccess.Read)
        Dim br As New BinaryReader(fs)


        If Not Form1.Mode2352.Checked Then


            Dim SectorsToRead As Integer = FileSize \ 2048
            If FileSize Mod 2048 > 0 Then SectorsToRead += 1

            Dim bytesToread = 2048

            For a = StartSector To SectorsToRead + StartSector - 1

                Dim bytepos = a * &H930 + 24
                br.BaseStream.Position = bytepos
                fileArr.AddRange(br.ReadBytes(2048))

            Next



        Else

            ''''''''''READ RAW FILE WITH 2352 MODE!

            br.BaseStream.Position = StartSector * &H930
            fileArr.AddRange(br.ReadBytes(FileSize))


        End If

        br.Dispose()
        fs.Dispose()

        Return fileArr

    End Function


    Public Function saveCDfile(ByVal ISOname As String, ByVal StartSector As Integer, ByVal FileSize As Integer, ByRef bytes As Byte())
        Try


            Dim fs As New FileStream(ISOname, FileMode.Open, FileAccess.Write)
            Dim br As New BinaryWriter(fs)

            If Not Form1.Mode2352.Checked Then



                Dim SectorsToWrite As Integer = FileSize \ 2048
                If FileSize Mod 2048 > 0 Then
                    SectorsToWrite += 1
                    ReDim Preserve bytes(FileSize + (2048 - (FileSize Mod 2048)))
                End If

                Dim bytecounter = 0

                For a = StartSector To SectorsToWrite + StartSector - 1

                    Dim bytepos = a * &H930 + 24

                    br.BaseStream.Position = bytepos

                    br.Write(bytes, bytecounter, 2048)
                    bytecounter += 2048

                Next



            Else            ''''''''''READ RAW FILE WITH 2352 MODE!


                'Todo - 2336 Mode Write



                'br.BaseStream.Position = StartSector * &H930
                'br.Write(bytes, 0, FileSize)


            End If



            br.Dispose()
            fs.Dispose()


        Catch ex As Exception
            MsgBox("Error! Exception: " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Function

    Public Sub UpdateFileListTable(ByVal ISOname As String, ByRef fileInf As List(Of fileInfo))

        Dim tab = New List(Of Byte)

        For Each fil In fileInf
            tab.AddRange(BitConverter.GetBytes(fil.Sector).ToList)
            tab.AddRange(BitConverter.GetBytes(fil.Sizw).ToList)
        Next
        saveCDfile(ISOname, &H17, &H1B88, tab.ToArray)


    End Sub

    Public Function makeFileList(ByVal ISOname As String)
        Dim a = New List(Of fileInfo)
        Dim maxFiles = 880
        Dim FilesSectors = getCDfile(ISOname, &H17, &H4000).ToArray
        If ISOname.Contains("KUDOS") Then FilesSectors = getCDfile(ISOname, &H31, &H1B88).ToArray
        If ISOname.Contains("Sumaru TV") Then maxFiles = 1124

        For x = 0 To maxFiles
            a.Add(New fileInfo With {.FileID = x, .Sector = Form1.Read32bitNum(FilesSectors, x * 8), .Sizw = Form1.Read32bitNum(FilesSectors, x * 8 + 4)})
        Next

        Return a
    End Function


    Public Sub ReallocateDown(ByVal ISOname As String, ByRef fileInf As List(Of fileInfo), ByVal StartFile As Integer, ByVal BySectors As Integer)




        'Dim lastSector = fileInf.Last.Sector + (fileInf.Last.Sizw \ 2048)
        'If fileInf.Last.Sizw Mod 2048 > 0 Then lastSector += 1


        'For a = lastSector To fileInf(StartFile).Sector Step -1




        'Next


        ''Reading eachFile from end and Save it +Bysectors
        'For a = 1143 To StartFile Step -1
        '    fileInf(a).Sector += BySectors
        'Next
        ''UpdateFileListTable(ISOname, fileInf)

    End Sub


End Class
