View Single Post
Join Date: Dec 2008
Posts: 1
Reputation: C0rey is an unknown quantity at this point 
Solved Threads: 0
C0rey C0rey is offline Offline
Newbie Poster

File editor (Unicode?)

 
0
  #1
Dec 10th, 2008
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.

  1. BSTR stl_type
  2. DWORD entry_count
  3. :FOREACH( entry_count )
  4. BSTR string_id
  5. DWORD id
  6. :ENDFOR
  7. DWORD language_count
  8. :FOREACH( language_count )
  9. DWORD language_offset
  10. :ENDFOR
  11. :FOREACH( language_count )
  12. // Seek to <language_offset>
  13. :FOREACH( entry_count )
  14. DWORD entry_offset
  15. :ENDFOR
  16. :ENDFOR
  17. :FOREACH( language_count )
  18. :FOREACH( entry_count )
  19. // Seek to <entry_offset>
  20. BSTR text
  21. :IF( stl_type == “QEST01? OR stl_type == “ITST01? )
  22. BSTR comment
  23. :IF( stl_type == “QEST01? )
  24. BSTR quest1
  25. BSTR quest2
  26. :ENDIF
  27. :ENDIF
  28. :ENDFOR
  29. :ENDFOR

Here is the source:

  1. Public Class STLeditor
  2.  
  3. Dim filenaam As String
  4. Dim fn As String()
  5. Dim content As Byte()
  6. Dim content2 As Byte()
  7. Dim index As Long
  8. Dim index2 As Long
  9. Dim lenheader As Byte
  10. Dim header As String
  11. Dim i As Long
  12. Dim aantal As Long
  13. Dim aantal2 As Long
  14. Dim recordnr As Long
  15. Dim idstring As String()
  16. Dim idnr As Long()
  17. Dim idnaam As String(,)
  18. Dim idomschrijving As String(,)
  19. Dim idomschrijving2 As String(,)
  20. Dim lenstr As Long
  21. Dim taalblokken As Long
  22. Dim taaloffset As Long()
  23. Dim stringoffset As Long(,)
  24. Dim taalblok As Long
  25. Dim heeftomschrijving As Boolean
  26. Dim taalid = 1
  27. Dim checkcancel As Boolean
  28. Dim nieuwaantal As Long
  29. Dim copy As String
  30. Dim cut As String
  31. Dim paste As String
  32. ' Opslaan
  33.  
  34. Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
  35.  
  36. Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
  37. filenaam = OpenFileDialog1.FileName
  38. fn = Split(filenaam, "\", -1)
  39. Me.Text = fn(fn.Length - 1) + " - HolodeckRose STL Editor"
  40. End Sub
  41.  
  42. ' When open is clicked start this
  43. Private Sub OpenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenButton.Click
  44.  
  45. OpenFileDialog1.DefaultExt = "STL"
  46. OpenFileDialog1.Filter = "STL Files (*.STL)|*.STL|All files|*.*"
  47. OpenFileDialog1.ReadOnlyChecked = False
  48. OpenFileDialog1.Title = "Select a file to open"
  49. OpenFileDialog1.FileName = " "
  50.  
  51. If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  52. leeggrid()
  53. leesfile()
  54. uitkleden()
  55. gridvullen()
  56. vulTextBoxen()
  57. End If
  58. End Sub
  59.  
  60. ' When exit is clicked shut down the application
  61. Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
  62. Me.Close()
  63. End Sub
  64.  
  65. ' When save button is clicked start saving
  66. Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  67.  
  68. End Sub
  69.  
  70. ' Fill in the record count
  71. Private Sub vulTextBoxen()
  72. statusrecordnr.Text = "Record Count: " + recordnr.ToString
  73. End Sub
  74.  
  75. ' Empties the grid
  76. Private Sub leeggrid()
  77. grid1.SelectAll()
  78. grid1.Rows.Clear()
  79. 'For i = 0 To grid1.SelectedRows.Count
  80. ' grid1.SelectedRows(i).Cells.Clear()
  81. 'Next i
  82. End Sub
  83.  
  84. ' Read the file
  85. Private Sub leesfile()
  86. Try
  87. content = My.Computer.FileSystem.ReadAllBytes(filenaam)
  88. Catch
  89.  
  90. End Try
  91. End Sub
  92.  
  93. Private Sub uitkleden()
  94. index = 0
  95. lenheader = leesbyte()
  96. header = leesstring(lenheader)
  97. heeftomschrijving = header = "ITST01"
  98. aantal = leeslong() - 1
  99. Dim lenstr As Integer
  100. ReDim idstring(aantal)
  101. ReDim idnr(aantal)
  102. For recordnr = 0 To aantal
  103. lenstr = leeslengte()
  104. idstring(recordnr) = leesstring(lenstr)
  105. idnr(recordnr) = leeslong()
  106. Next recordnr
  107. taalblokken = leeslong()
  108. ReDim taaloffset(taalblokken)
  109. For taalblok = 0 To taalblokken - 1
  110. taaloffset(taalblok) = leeslong()
  111. Next taalblok
  112. ReDim stringoffset(taalblokken, aantal)
  113. ReDim idnaam(taalblokken, aantal)
  114. ReDim idomschrijving(taalblokken, aantal)
  115. For taalblok = 0 To taalblokken - 1
  116. 'index = taaloffset(taalblok)
  117. For recordnr = 0 To aantal
  118. stringoffset(taalblok, recordnr) = leeslong()
  119. Next recordnr
  120. For recordnr = 0 To aantal
  121. index = stringoffset(taalblok, recordnr)
  122. Dim lennaam = leeslengte()
  123. idnaam(taalblok, recordnr) = leesstring(lennaam)
  124. If heeftomschrijving Then
  125. Dim lenomschrijving = leeslengte()
  126. 'If lenomschrijving > 127 Then
  127. ' Dim extra = leesbyte()
  128. ' lenomschrijving = lenomschrijving - 128
  129. ' lenomschrijving = lenomschrijving + extra * 128
  130. ' 'If extra <> 1 Then
  131. ' ' MsgBox("Description is longer than expected (255)" + lenomschrijving.ToString + " " + extra.ToString, MsgBoxStyle.Exclamation)
  132. ' ' 'debuglabel.Text = debuglabel.Text + Convert.ToString(extra) + " "
  133. ' 'End If
  134. 'End If
  135. idomschrijving(taalblok, recordnr) = leesstring(lenomschrijving)
  136. End If
  137. Next recordnr
  138. Next taalblok
  139. End Sub
  140.  
  141. Private Sub gridvullen()
  142. Dim regel As Object()
  143. For recordnr = 0 To aantal
  144. ReDim regel(11)
  145. regel(0) = idstring(recordnr)
  146. regel(1) = idnr(recordnr)
  147. regel(2) = idnaam(taalid, recordnr)
  148. regel(3) = ""
  149. If heeftomschrijving Then regel(3) = idomschrijving(taalid, recordnr)
  150. regel(4) = idnaam(0, recordnr)
  151. regel(5) = idnaam(2, recordnr)
  152. regel(6) = idnaam(3, recordnr)
  153. regel(7) = idnaam(4, recordnr)
  154. regel(8) = ""
  155. regel(9) = ""
  156. regel(10) = ""
  157. regel(11) = ""
  158. If heeftomschrijving Then regel(8) = idomschrijving(0, recordnr)
  159. If heeftomschrijving Then regel(9) = idomschrijving(2, recordnr)
  160. If heeftomschrijving Then regel(10) = idomschrijving(3, recordnr)
  161. If heeftomschrijving Then regel(11) = idomschrijving(4, recordnr)
  162. grid1.Rows.Add(regel)
  163. Next recordnr
  164. End Sub
  165. Private Function leesstring(ByVal aantal As Long) As String
  166. REM read everything including string length to read
  167. Dim buffer As String
  168. buffer = ""
  169. For i = 0 To aantal - 1
  170. buffer = buffer + Chr(content(index + i))
  171. Next
  172. index = index + aantal
  173. Return buffer
  174. End Function
  175. Private Function leeslengte() As Integer
  176. Dim lenstring As Integer
  177. lenstring = leesbyte()
  178. If lenstring > 127 Then
  179. Dim extra = leesbyte()
  180. lenstring = lenstring - 128
  181. lenstring = lenstring + extra * 128
  182. End If
  183. Return lenstring
  184. End Function
  185. Private Function leesbyte() As Byte
  186. index = index + 1
  187. Return content(index - 1)
  188. End Function
  189. Private Function leeslong() As Long
  190. Dim b1 = leesbyte()
  191. Dim b2 = leesbyte()
  192. Dim b3 = leesbyte()
  193. Dim b4 = leesbyte()
  194. Return b1 + (256 * (b2 + (256 * (b3 + 256 * b4))))
  195. End Function
  196.  
  197. Private Sub AboutToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem1.Click
  198. MsgBox("Made by The Holodec Team", MsgBoxStyle.OkOnly, "About")
  199. End Sub
  200.  
  201. ' Select the entire grid
  202. Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  203. grid1.SelectAll()
  204. End Sub
  205.  
  206. Private Sub SaveasButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveasButton.Click
  207.  
  208. SaveFileDialog1.DefaultExt = "STL"
  209. SaveFileDialog1.Filter = "STL Files (*.STL)|*.STL|All files|*.*"
  210. SaveFileDialog1.Title = "Select a file to open"
  211. filenaam = OpenFileDialog1.FileName
  212. fn = Split(filenaam, "\", -1)
  213. SaveFileDialog1.FileName = fn(fn.Length - 1)
  214.  
  215. If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
  216. leesgrid()
  217. aankleden()
  218. schrijffile(SaveFileDialog1.FileName)
  219. End If
  220.  
  221. End Sub
  222.  
  223. Private Sub leesgrid()
  224. Dim regel As Object()
  225. aantal2 = grid1.Rows.Count - 1
  226. Dim j As Integer
  227. ReDim regel(11)
  228. ReDim idstring(aantal2)
  229. ReDim idnr(aantal2)
  230. ReDim taaloffset(taalblokken)
  231. ReDim idnaam(taalblokken, aantal2)
  232. ReDim idomschrijving2(taalblokken, aantal2)
  233. Dim celcontent
  234. For recordnr = 0 To aantal2 - 1
  235. For j = 0 To grid1.ColumnCount - 1
  236. ' grid1.Rows(recordnr).Cells(j).ValueType)
  237. celcontent = grid1.Rows(recordnr).Cells(j).Value
  238. If celcontent = Nothing Then celcontent = ""
  239. regel(j) = celcontent.ToString()
  240. Next j
  241. ' grid1.Rows(recordnr).Cells(j).ValueType)
  242. idstring(recordnr) = regel(0)
  243. idnr(recordnr) = Convert.ToInt64(regel(1))
  244. idnaam(taalid, recordnr) = regel(2)
  245. If heeftomschrijving Then idomschrijving2(taalid, recordnr) = regel(3)
  246. idnaam(0, recordnr) = regel(4)
  247. idnaam(2, recordnr) = regel(5)
  248. idnaam(3, recordnr) = regel(6)
  249. idnaam(4, recordnr) = regel(7)
  250. If heeftomschrijving Then idomschrijving2(0, recordnr) = regel(8)
  251. If heeftomschrijving Then idomschrijving2(2, recordnr) = regel(9)
  252. If heeftomschrijving Then idomschrijving2(3, recordnr) = regel(10)
  253. If heeftomschrijving Then idomschrijving2(4, recordnr) = regel(11)
  254. Next recordnr
  255. End Sub
  256. Private Sub aankleden()
  257. index2 = 0
  258. ReDim content2(content.Length * 2)
  259. 'Header "ITST01" / "nogwat"
  260. 'schrijfbyte(lenheader) ' -> blijft hetzelfde
  261. schrijfstring(header)
  262. ' heeftomschrijving = header = "ITST01"
  263. schrijflong(aantal2)
  264. For recordnr = 0 To aantal2 -1
  265. schrijfstring(idstring(recordnr))
  266. schrijflong(idnr(recordnr))
  267. Next recordnr
  268. schrijflong(taalblokken)
  269. 'later opnieuw vullen
  270. Dim taalblokindex2 = index2
  271. For taalblok = 0 To taalblokken - 1
  272. schrijflong(0)
  273. Next taalblok
  274. ReDim stringoffset(taalblokken, aantal2)
  275. For taalblok = 0 To taalblokken - 1
  276. taaloffset(taalblok) = index2
  277. For recordnr = 0 To aantal2 - 1
  278. schrijflong(0)
  279. Next recordnr
  280. For recordnr = 0 To aantal2 - 1
  281. stringoffset(taalblok, recordnr) = index2
  282. schrijfstring(idnaam(taalblok, recordnr))
  283. If heeftomschrijving Then
  284. schrijfstring(idomschrijving2(taalblok, recordnr))
  285. 'If Len(idomschrijving(taalblok, recordnr)) <> Len(idomschrijving2(taalblok, recordnr)) Then
  286. 'debuglabel.Text = debuglabel.Text + Convert.ToString(taalblok * 100000 + recordnr) + ":" + Convert.ToString(Len(idomschrijving(taalblok, recordnr))) + "<>" + Convert.ToString(Len(idomschrijving2(taalblok, recordnr))) + " "
  287. 'End If
  288. End If
  289. Next recordnr
  290. Next taalblok
  291. REM taalblokoffset nog vullen
  292. Dim saveindex2 = index2
  293. index2 = taalblokindex2
  294. For taalblok = 0 To taalblokken - 1
  295. schrijflong(taaloffset(taalblok))
  296. Next taalblok
  297. REM stringoffset nog invullen
  298. For taalblok = 0 To taalblokken - 1
  299. index2 = taaloffset(taalblok)
  300. For recordnr = 0 To aantal2 - 1
  301. schrijflong(stringoffset(taalblok, recordnr))
  302. Next recordnr
  303. Next taalblok
  304. index2 = saveindex2
  305.  
  306. End Sub
  307. Private Sub schrijfstring(ByVal opslaanstring As String)
  308. Dim aantalbytes As Integer
  309. aantalbytes = Len(opslaanstring)
  310. If aantalbytes > 127 Then
  311. Dim aantalbyteslow
  312. Dim aantalbyteshigh = Math.DivRem(aantalbytes, 128, aantalbyteslow)
  313. schrijfbyte(aantalbyteslow + 128)
  314. schrijfbyte(aantalbyteshigh)
  315. Else
  316. schrijfbyte(aantalbytes)
  317. End If
  318. For i = 0 To aantalbytes - 1
  319. content2(index2 + i) = Asc(Mid(opslaanstring, i + 1))
  320. Next
  321. index2 = index2 + aantalbytes
  322. End Sub
  323. Private Function schrijflengte(ByVal lenstring) As Integer
  324. lenstring = leesbyte()
  325. If lenstring > 127 Then
  326. Dim extra = leesbyte()
  327. lenstring = lenstring - 128
  328. lenstring = lenstring + extra * 128
  329. End If
  330. Return lenstring
  331. End Function
  332. Private Sub schrijfbyte(ByVal opslaanbyte As Byte)
  333. content2(index2) = opslaanbyte
  334. index2 = index2 + 1
  335. End Sub
  336. Private Sub schrijflong(ByVal opslaanlong As Long)
  337.  
  338. Dim b1 As Long
  339. opslaanlong = Math.DivRem(opslaanlong, 256, b1)
  340. Dim b2 As Long
  341. opslaanlong = Math.DivRem(opslaanlong, 256, b2)
  342. Dim b3 As Long
  343. opslaanlong = Math.DivRem(opslaanlong, 256, b3)
  344. Dim b4 As Long
  345. opslaanlong = Math.DivRem(opslaanlong, 256, b4)
  346.  
  347. schrijfbyte(b1)
  348. schrijfbyte(b2)
  349. schrijfbyte(b3)
  350. schrijfbyte(b4)
  351.  
  352. End Sub
  353. Private Sub schrijffile(ByVal filenaam2 As String)
  354. Dim content3 As Byte()
  355. ReDim content3(index2 - 1)
  356. For i = 0 To index2 - 1
  357. content3(i) = content2(i)
  358. Next i
  359. My.Computer.FileSystem.WriteAllBytes(filenaam2, content3,False)
  360. End Sub
  361.  
  362. Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
  363.  
  364. Dim save As String
  365. fn = Split(filenaam, "\", -1)
  366. save = fn(fn.Length - 1)
  367. leesgrid()
  368. aankleden()
  369. schrijffile(save)
  370.  
  371. End Sub
  372.  
  373. Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
  374.  
  375. Dim j As Integer
  376. Dim a As Integer
  377. Dim gevonden As Boolean
  378. gevonden = False
  379. Dim regel As Object()
  380.  
  381. ' For a = 0 To recordnr - 1
  382. 'For j = 0 To grid1.ColumnCount - 1
  383. ' grid1.Rows(recordnr).Cells(j).ValueType)
  384. 'regel(j) = grid1.Rows(a).Cells(j).Value.ToString()
  385. ' If searchtext.Text = grid1.Rows(a).Cells(j).Value.ToString Then
  386. 'MsgBox("hoi", MsgBoxStyle.Critical, "hoi")
  387. 'gevonden = True
  388. 'Exit For
  389. 'End If
  390. 'Next j
  391. 'If gevonden = True Then
  392. 'Exit For
  393. 'End If
  394. ' Next a
  395. 'MsgBox(searchtext.Text + " is found on record: " + b, MsgBoxStyle.OkOnly, "Search")
  396. 'gevonden = True
  397.  
  398. End Sub
  399.  
  400. Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
  401.  
  402.  
  403. If grid1.CurrentCell.Value = Nothing Then
  404. Else
  405. copy = grid1.CurrentCell.Value
  406. End If
  407.  
  408. End Sub
  409.  
  410. Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
  411.  
  412. Dim selected As String
  413.  
  414. If cut = Nothing Then
  415. Else
  416. grid1.CurrentCell.Value = cut
  417. cut = Nothing
  418. Exit Sub
  419. End If
  420.  
  421. If copy = Nothing Then
  422. Else
  423. grid1.CurrentCell.Value = copy
  424. End If
  425.  
  426.  
  427.  
  428. End Sub
  429.  
  430. Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
  431.  
  432. If grid1.CurrentCell.Value = Nothing Then
  433. Else
  434. cut = grid1.CurrentCell.Value
  435. grid1.CurrentCell.Value = ""
  436. copy = Nothing
  437. End If
  438.  
  439. End Sub
  440. End Class
Reply With Quote