C0rey 0 Newbie Poster

Hello all,

I'm new to VB .Net, I found this open source file editor. Its can read unicode text fine, but its doesn't save unicode and its read only ITST01 and not QEST01. How to make its detect both and save as unicode?

Here is the file format.

BSTR stl_type
DWORD entry_count
:FOREACH( entry_count )
BSTR string_id
DWORD id
:ENDFOR
DWORD language_count
:FOREACH( language_count )
DWORD language_offset
:ENDFOR
:FOREACH( language_count )
// Seek to <language_offset>
:FOREACH( entry_count )
DWORD entry_offset
:ENDFOR
:ENDFOR
:FOREACH( language_count )
:FOREACH( entry_count )
// Seek to <entry_offset>
BSTR text
:IF( stl_type == “QEST01? OR stl_type == “ITST01? )
BSTR comment
:IF( stl_type == “QEST01? )
BSTR quest1
BSTR quest2
:ENDIF
:ENDIF
:ENDFOR
:ENDFOR

Here is the source:

Public Class STLeditor

    Dim filenaam As String
    Dim fn As String()
    Dim content As Byte()
    Dim content2 As Byte()
    Dim index As Long
    Dim index2 As Long
    Dim lenheader As Byte
    Dim header As String
    Dim i As Long
    Dim aantal As Long
    Dim aantal2 As Long
    Dim recordnr As Long
    Dim idstring As String()
    Dim idnr As Long()
    Dim idnaam As String(,)
    Dim idomschrijving As String(,)
    Dim idomschrijving2 As String(,)
    Dim lenstr As Long
    Dim taalblokken As Long
    Dim taaloffset As Long()
    Dim stringoffset As Long(,)
    Dim taalblok As Long
    Dim heeftomschrijving As Boolean
    Dim taalid = 1
    Dim checkcancel As Boolean
    Dim nieuwaantal As Long
    Dim copy As String
    Dim cut As String
    Dim paste As String
    ' Opslaan

    Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        filenaam = OpenFileDialog1.FileName
        fn = Split(filenaam, "\", -1)
        Me.Text = fn(fn.Length - 1) + " - HolodeckRose STL Editor"
    End Sub

    ' When open is clicked start this
    Private Sub OpenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenButton.Click

        OpenFileDialog1.DefaultExt = "STL"
        OpenFileDialog1.Filter = "STL Files (*.STL)|*.STL|All files|*.*"
        OpenFileDialog1.ReadOnlyChecked = False
        OpenFileDialog1.Title = "Select a file to open"
        OpenFileDialog1.FileName = " "

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            leeggrid()
            leesfile()
            uitkleden()
            gridvullen()
            vulTextBoxen()
        End If
    End Sub

    ' When exit is clicked shut down the application
    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()
    End Sub

    ' When save button is clicked start saving
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    ' Fill in the record count
    Private Sub vulTextBoxen()
        statusrecordnr.Text = "Record Count: " + recordnr.ToString
    End Sub

    ' Empties the grid
    Private Sub leeggrid()
        grid1.SelectAll()
        grid1.Rows.Clear()
        'For i = 0 To grid1.SelectedRows.Count
        '    grid1.SelectedRows(i).Cells.Clear()
        'Next i
    End Sub

    ' Read the file
    Private Sub leesfile()
        Try
            content = My.Computer.FileSystem.ReadAllBytes(filenaam)
        Catch

        End Try
    End Sub

    Private Sub uitkleden()
        index = 0
        lenheader = leesbyte()
        header = leesstring(lenheader)
        heeftomschrijving = header = "ITST01"
        aantal = leeslong() - 1
        Dim lenstr As Integer
        ReDim idstring(aantal)
        ReDim idnr(aantal)
        For recordnr = 0 To aantal
            lenstr = leeslengte()
            idstring(recordnr) = leesstring(lenstr)
            idnr(recordnr) = leeslong()
        Next recordnr
        taalblokken = leeslong()
        ReDim taaloffset(taalblokken)
        For taalblok = 0 To taalblokken - 1
            taaloffset(taalblok) = leeslong()
        Next taalblok
        ReDim stringoffset(taalblokken, aantal)
        ReDim idnaam(taalblokken, aantal)
        ReDim idomschrijving(taalblokken, aantal)
        For taalblok = 0 To taalblokken - 1
            'index = taaloffset(taalblok)
            For recordnr = 0 To aantal
                stringoffset(taalblok, recordnr) = leeslong()
            Next recordnr
            For recordnr = 0 To aantal
                index = stringoffset(taalblok, recordnr)
                Dim lennaam = leeslengte()
                idnaam(taalblok, recordnr) = leesstring(lennaam)
                If heeftomschrijving Then
                    Dim lenomschrijving = leeslengte()
                    'If lenomschrijving > 127 Then
                    '    Dim extra = leesbyte()
                    '    lenomschrijving = lenomschrijving - 128
                    '    lenomschrijving = lenomschrijving + extra * 128
                    '    'If extra <> 1 Then
                    '    '    MsgBox("Description is longer than expected (255)" + lenomschrijving.ToString + " " + extra.ToString, MsgBoxStyle.Exclamation)
                    '    '    'debuglabel.Text = debuglabel.Text + Convert.ToString(extra) + " "
                    '    'End If
                    'End If
                    idomschrijving(taalblok, recordnr) = leesstring(lenomschrijving)
                End If
            Next recordnr
        Next taalblok
    End Sub

    Private Sub gridvullen()
        Dim regel As Object()
        For recordnr = 0 To aantal
            ReDim regel(11)
            regel(0) = idstring(recordnr)
            regel(1) = idnr(recordnr)
            regel(2) = idnaam(taalid, recordnr)
            regel(3) = ""
            If heeftomschrijving Then regel(3) = idomschrijving(taalid, recordnr)
            regel(4) = idnaam(0, recordnr)
            regel(5) = idnaam(2, recordnr)
            regel(6) = idnaam(3, recordnr)
            regel(7) = idnaam(4, recordnr)
            regel(8) = ""
            regel(9) = ""
            regel(10) = ""
            regel(11) = ""
            If heeftomschrijving Then regel(8) = idomschrijving(0, recordnr)
            If heeftomschrijving Then regel(9) = idomschrijving(2, recordnr)
            If heeftomschrijving Then regel(10) = idomschrijving(3, recordnr)
            If heeftomschrijving Then regel(11) = idomschrijving(4, recordnr)
            grid1.Rows.Add(regel)
        Next recordnr
    End Sub
    Private Function leesstring(ByVal aantal As Long) As String
        REM read everything including string length to read
        Dim buffer As String
        buffer = ""
        For i = 0 To aantal - 1
            buffer = buffer + Chr(content(index + i))
        Next
        index = index + aantal
        Return buffer
    End Function
    Private Function leeslengte() As Integer
        Dim lenstring As Integer
        lenstring = leesbyte()
        If lenstring > 127 Then
            Dim extra = leesbyte()
            lenstring = lenstring - 128
            lenstring = lenstring + extra * 128
        End If
        Return lenstring
    End Function
    Private Function leesbyte() As Byte
        index = index + 1
        Return content(index - 1)
    End Function
    Private Function leeslong() As Long
        Dim b1 = leesbyte()
        Dim b2 = leesbyte()
        Dim b3 = leesbyte()
        Dim b4 = leesbyte()
        Return b1 + (256 * (b2 + (256 * (b3 + 256 * b4))))
    End Function

    Private Sub AboutToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem1.Click
        MsgBox("Made by The Holodec Team", MsgBoxStyle.OkOnly, "About")
    End Sub

    ' Select the entire grid
    Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        grid1.SelectAll()
    End Sub

    Private Sub SaveasButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveasButton.Click

        SaveFileDialog1.DefaultExt = "STL"
        SaveFileDialog1.Filter = "STL Files (*.STL)|*.STL|All files|*.*"
        SaveFileDialog1.Title = "Select a file to open"
        filenaam = OpenFileDialog1.FileName
        fn = Split(filenaam, "\", -1)
        SaveFileDialog1.FileName = fn(fn.Length - 1)

        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            leesgrid()
            aankleden()
            schrijffile(SaveFileDialog1.FileName)
        End If

    End Sub

    Private Sub leesgrid()
        Dim regel As Object()
        aantal2 = grid1.Rows.Count - 1
        Dim j As Integer
        ReDim regel(11)
        ReDim idstring(aantal2)
        ReDim idnr(aantal2)
        ReDim taaloffset(taalblokken)
        ReDim idnaam(taalblokken, aantal2)
        ReDim idomschrijving2(taalblokken, aantal2)
        Dim celcontent
        For recordnr = 0 To aantal2 - 1
            For j = 0 To grid1.ColumnCount - 1
                ' grid1.Rows(recordnr).Cells(j).ValueType)
                celcontent = grid1.Rows(recordnr).Cells(j).Value
                If celcontent = Nothing Then celcontent = ""
                regel(j) = celcontent.ToString()
            Next j
            ' grid1.Rows(recordnr).Cells(j).ValueType)
            idstring(recordnr) = regel(0)
            idnr(recordnr) = Convert.ToInt64(regel(1))
            idnaam(taalid, recordnr) = regel(2)
            If heeftomschrijving Then idomschrijving2(taalid, recordnr) = regel(3)
            idnaam(0, recordnr) = regel(4)
            idnaam(2, recordnr) = regel(5)
            idnaam(3, recordnr) = regel(6)
            idnaam(4, recordnr) = regel(7)
            If heeftomschrijving Then idomschrijving2(0, recordnr) = regel(8)
            If heeftomschrijving Then idomschrijving2(2, recordnr) = regel(9)
            If heeftomschrijving Then idomschrijving2(3, recordnr) = regel(10)
            If heeftomschrijving Then idomschrijving2(4, recordnr) = regel(11)
        Next recordnr
    End Sub
    Private Sub aankleden()
        index2 = 0
        ReDim content2(content.Length * 2)
        'Header "ITST01" / "nogwat"
        'schrijfbyte(lenheader) ' -> blijft hetzelfde
        schrijfstring(header)
        ' heeftomschrijving = header = "ITST01"
        schrijflong(aantal2)
        For recordnr = 0 To aantal2 -1
            schrijfstring(idstring(recordnr))
            schrijflong(idnr(recordnr))
        Next recordnr
        schrijflong(taalblokken)
        'later opnieuw vullen
        Dim taalblokindex2 = index2
        For taalblok = 0 To taalblokken - 1
            schrijflong(0)
        Next taalblok
        ReDim stringoffset(taalblokken, aantal2)
        For taalblok = 0 To taalblokken - 1
            taaloffset(taalblok) = index2
            For recordnr = 0 To aantal2 - 1
                schrijflong(0)
            Next recordnr
            For recordnr = 0 To aantal2 - 1
                stringoffset(taalblok, recordnr) = index2
                schrijfstring(idnaam(taalblok, recordnr))
                If heeftomschrijving Then
                    schrijfstring(idomschrijving2(taalblok, recordnr))
                    'If Len(idomschrijving(taalblok, recordnr)) <> Len(idomschrijving2(taalblok, recordnr)) Then
                    'debuglabel.Text = debuglabel.Text + Convert.ToString(taalblok * 100000 + recordnr) + ":" + Convert.ToString(Len(idomschrijving(taalblok, recordnr))) + "<>" + Convert.ToString(Len(idomschrijving2(taalblok, recordnr))) + " "
                    'End If
                End If
            Next recordnr
        Next taalblok
        REM taalblokoffset nog vullen
        Dim saveindex2 = index2
        index2 = taalblokindex2
        For taalblok = 0 To taalblokken - 1
            schrijflong(taaloffset(taalblok))
        Next taalblok
        REM stringoffset nog invullen
        For taalblok = 0 To taalblokken - 1
            index2 = taaloffset(taalblok)
            For recordnr = 0 To aantal2 - 1
                schrijflong(stringoffset(taalblok, recordnr))
            Next recordnr
        Next taalblok
        index2 = saveindex2

    End Sub
    Private Sub schrijfstring(ByVal opslaanstring As String)
        Dim aantalbytes As Integer
        aantalbytes = Len(opslaanstring)
        If aantalbytes > 127 Then
            Dim aantalbyteslow
            Dim aantalbyteshigh = Math.DivRem(aantalbytes, 128, aantalbyteslow)
            schrijfbyte(aantalbyteslow + 128)
            schrijfbyte(aantalbyteshigh)
        Else
            schrijfbyte(aantalbytes)
        End If
        For i = 0 To aantalbytes - 1
            content2(index2 + i) = Asc(Mid(opslaanstring, i + 1))
        Next
        index2 = index2 + aantalbytes
    End Sub
    Private Function schrijflengte(ByVal lenstring) As Integer
        lenstring = leesbyte()
        If lenstring > 127 Then
            Dim extra = leesbyte()
            lenstring = lenstring - 128
            lenstring = lenstring + extra * 128
        End If
        Return lenstring
    End Function
    Private Sub schrijfbyte(ByVal opslaanbyte As Byte)
        content2(index2) = opslaanbyte
        index2 = index2 + 1
    End Sub
    Private Sub schrijflong(ByVal opslaanlong As Long)

        Dim b1 As Long
        opslaanlong = Math.DivRem(opslaanlong, 256, b1)
        Dim b2 As Long
        opslaanlong = Math.DivRem(opslaanlong, 256, b2)
        Dim b3 As Long
        opslaanlong = Math.DivRem(opslaanlong, 256, b3)
        Dim b4 As Long
        opslaanlong = Math.DivRem(opslaanlong, 256, b4)

        schrijfbyte(b1)
        schrijfbyte(b2)
        schrijfbyte(b3)
        schrijfbyte(b4)

    End Sub
    Private Sub schrijffile(ByVal filenaam2 As String)
        Dim content3 As Byte()
        ReDim content3(index2 - 1)
        For i = 0 To index2 - 1
            content3(i) = content2(i)
        Next i
        My.Computer.FileSystem.WriteAllBytes(filenaam2, content3,False)
    End Sub

    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

        Dim save As String
        fn = Split(filenaam, "\", -1)
        save = fn(fn.Length - 1)
        leesgrid()
        aankleden()
        schrijffile(save)

    End Sub

    Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click

        Dim j As Integer
        Dim a As Integer
        Dim gevonden As Boolean
        gevonden = False
        Dim regel As Object()

        ' For a = 0 To recordnr - 1
        'For j = 0 To grid1.ColumnCount - 1
        ' grid1.Rows(recordnr).Cells(j).ValueType)
        'regel(j) = grid1.Rows(a).Cells(j).Value.ToString()
        ' If searchtext.Text = grid1.Rows(a).Cells(j).Value.ToString Then
        'MsgBox("hoi", MsgBoxStyle.Critical, "hoi")
        'gevonden = True
        'Exit For
        'End If
        'Next j
        'If gevonden = True Then
        'Exit For
        'End If
        ' Next a
        'MsgBox(searchtext.Text + " is found on record: " + b, MsgBoxStyle.OkOnly, "Search")
        'gevonden = True

    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click


        If grid1.CurrentCell.Value = Nothing Then
        Else
            copy = grid1.CurrentCell.Value
        End If

    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click

        Dim selected As String

        If cut = Nothing Then
        Else
            grid1.CurrentCell.Value = cut
            cut = Nothing
            Exit Sub
        End If

        If copy = Nothing Then
        Else
            grid1.CurrentCell.Value = copy
        End If



    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click

        If grid1.CurrentCell.Value = Nothing Then
        Else
            cut = grid1.CurrentCell.Value
            grid1.CurrentCell.Value = ""
            copy = Nothing
        End If

    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.