| 1 |
Imports System.Text.Encoding |
| 2 |
Imports System.Xml |
| 3 |
|
| 4 |
|
| 5 |
Public Class clsBlorb |
| 6 |
|
| 7 |
Private Shared iReadOffset As Integer = 0 |
| 8 |
Private Shared stmBlorb As IO.FileStream |
| 9 |
Private Shared BlorbOffset As Long ' Where does the Blorb start within the file |
| 10 |
|
| 11 |
Friend Shared ExecResource As Byte() |
| 12 |
Friend Shared bObfuscated As Boolean = True |
| 13 |
Friend Shared ExecType As String = Nothing |
| 14 |
'Friend Shared ImageResources As New Dictionary(Of Integer, Image) ' - These are just a local cache for Runner |
| 15 |
Friend Shared SoundResources As New Dictionary(Of Integer, SoundFile) ' / |
| 16 |
Friend Shared MetaData As System.Xml.XmlDocument |
| 17 |
Friend Shared Frontispiece As Integer = -1 |
| 18 |
Friend Shared ResourceIndex As New Dictionary(Of String, UInt32) |
| 19 |
Friend Shared sFilename As String |
| 20 |
Friend Shared bEXE As Boolean = False |
| 21 |
Friend Shared Data As String |
| 22 |
Friend Shared DataType As String |
| 23 |
|
| 24 |
Private Shared cnkFORM As FormChunk |
| 25 |
|
| 26 |
#If Not Adravalon Then |
| 27 |
Public Function GetImage(ByVal iResourceNumber As Integer, Optional ByVal bStore As Boolean = False, Optional ByRef sExtn As String = Nothing) As Image |
| 28 |
Dim bStreamOpen As Boolean = stmBlorb.CanRead |
| 29 |
|
| 30 |
Try |
| 31 |
If Not ImageResources.ContainsKey(iResourceNumber) Then |
| 32 |
If ResourceIndex.ContainsKey("Pict" & iResourceNumber) Then |
| 33 |
Dim iOffset As UInt32 = ResourceIndex("Pict" & iResourceNumber) |
| 34 |
Dim cnkImage As New PictResourceChunk |
| 35 |
|
| 36 |
If Not bStreamOpen Then stmBlorb = New IO.FileStream(sFilename, IO.FileMode.Open, IO.FileAccess.Read) |
| 37 |
stmBlorb.Position = iOffset + BlorbOffset |
| 38 |
|
| 39 |
If cnkImage.LoadChunk Then |
| 40 |
sExtn = cnkImage.img.sExtn |
| 41 |
If Not bStore Then Return cnkImage.img.Image |
| 42 |
ImageResources.Add(iResourceNumber, cnkImage.img.Image) |
| 43 |
End If |
| 44 |
End If |
| 45 |
End If |
| 46 |
If ImageResources.ContainsKey(iResourceNumber) Then Return ImageResources(iResourceNumber) |
| 47 |
|
| 48 |
Catch ex As Exception |
| 49 |
ErrMsg("GetImage error", ex) |
| 50 |
Finally |
| 51 |
If Not bStreamOpen Then stmBlorb.Close() |
| 52 |
End Try |
| 53 |
Return Nothing |
| 54 |
|
| 55 |
End Function |
| 56 |
#Else |
| 57 |
Public Function GetImage(ByVal iResourceNumber As Integer, Optional ByVal bStore As Boolean = False, Optional ByRef sExtn As String = Nothing) As Byte() |
| 58 |
Dim bStreamOpen As Boolean = stmBlorb.CanRead |
| 59 |
Try |
| 60 |
If ResourceIndex.ContainsKey("Pict" & iResourceNumber) Then |
| 61 |
Dim iOffset As UInt32 = ResourceIndex("Pict" & iResourceNumber) |
| 62 |
Dim cnkImage As New PictResourceChunk |
| 63 |
|
| 64 |
If Not bStreamOpen Then stmBlorb = New IO.FileStream(sFilename, IO.FileMode.Open, IO.FileAccess.Read) |
| 65 |
stmBlorb.Position = iOffset + BlorbOffset |
| 66 |
|
| 67 |
If cnkImage.LoadChunk Then |
| 68 |
Return cnkImage.img.bytImage |
| 69 |
End If |
| 70 |
End If |
| 71 |
Catch ex As Exception |
| 72 |
ErrMsg("GetImage error", ex) |
| 73 |
Finally |
| 74 |
If Not bStreamOpen Then stmBlorb.Close() |
| 75 |
End Try |
| 76 |
Return Nothing |
| 77 |
End Function |
| 78 |
#End If |
| 79 |
|
| 80 |
Friend Function GetSound(ByVal iResourceNumber As Integer, Optional ByVal bStore As Boolean = False, Optional ByRef sExtn As String = Nothing) As SoundFile |
| 81 |
|
| 82 |
Dim bStreamOpen As Boolean = stmBlorb.CanRead |
| 83 |
|
| 84 |
Try |
| 85 |
If Not SoundResources.ContainsKey(iResourceNumber) Then |
| 86 |
If ResourceIndex.ContainsKey("Snd " & iResourceNumber) Then |
| 87 |
Dim iOffset As UInt32 = ResourceIndex("Snd " & iResourceNumber) |
| 88 |
Dim cnkSound As New SoundResourceChunk |
| 89 |
|
| 90 |
If Not bStreamOpen Then stmBlorb = New IO.FileStream(sFilename, IO.FileMode.Open, IO.FileAccess.Read) |
| 91 |
stmBlorb.Position = iOffset + BlorbOffset |
| 92 |
|
| 93 |
If cnkSound.LoadChunk Then |
| 94 |
sExtn = cnkSound.snd.sExtn |
| 95 |
If Not bStore Then Return cnkSound.snd |
| 96 |
SoundResources.Add(iResourceNumber, cnkSound.snd) |
| 97 |
End If |
| 98 |
End If |
| 99 |
End If |
| 100 |
If SoundResources.ContainsKey(iResourceNumber) Then Return SoundResources(iResourceNumber) |
| 101 |
|
| 102 |
Catch ex As Exception |
| 103 |
ErrMsg("GetSound error", ex) |
| 104 |
Finally |
| 105 |
If Not bStreamOpen Then stmBlorb.Close() |
| 106 |
End Try |
| 107 |
Return Nothing |
| 108 |
|
| 109 |
End Function |
| 110 |
|
| 111 |
Friend Class SoundFile ' Temp, for now |
| 112 |
Public bytSound As Byte() = {} |
| 113 |
Public sExtn As String |
| 114 |
|
| 115 |
Public Function Save(ByVal sFilename As String) As Boolean |
| 116 |
Dim fs As New IO.FileStream(sFilename, IO.FileMode.CreateNew) |
| 117 |
fs.Write(bytSound, 0, bytSound.Length - 1) |
| 118 |
fs.Close() |
| 119 |
End Function |
| 120 |
|
| 121 |
End Class |
| 122 |
|
| 123 |
Friend Class ImageFile |
| 124 |
Public bytImage As Byte() = {} |
| 125 |
Public sExtn As String |
| 126 |
|
| 127 |
#If Not Adravalon Then |
| 128 |
Public ReadOnly Property Image As Image |
| 129 |
Get |
| 130 |
Dim msImage As New IO.MemoryStream(bytImage) |
| 131 |
Return New Bitmap(msImage) |
| 132 |
End Get |
| 133 |
End Property |
| 134 |
#End If |
| 135 |
End Class |
| 136 |
|
| 137 |
Private MustInherit Class Chunk |
| 138 |
Public MustOverride Property ID As String |
| 139 |
|
| 140 |
Private iLength As UInt32 |
| 141 |
Public Overridable Property Length As UInt32 |
| 142 |
Get |
| 143 |
Return iLength |
| 144 |
End Get |
| 145 |
Set(value As UInt32) |
| 146 |
iLength = value |
| 147 |
End Set |
| 148 |
End Property |
| 149 |
|
| 150 |
|
| 151 |
Public Overridable Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 152 |
Try |
| 153 |
If iStartPos > -1 Then stmBlorb.Position = iStartPos |
| 154 |
|
| 155 |
' First 4 bytes tell us what sort of chunk this is |
| 156 |
Dim bytID(3) As Byte |
| 157 |
iReadOffset += stmBlorb.Read(bytID, 0, 4) |
| 158 |
ID = UTF8.GetString(bytID) |
| 159 |
|
| 160 |
' Next 4 bytes tell us the size of the chunk |
| 161 |
Dim bytSize(3) As Byte |
| 162 |
iReadOffset += stmBlorb.Read(bytSize, 0, 4) |
| 163 |
iLength = ByteToInt(bytSize) |
| 164 |
|
| 165 |
Return True |
| 166 |
Catch ex As Exception |
| 167 |
Return False |
| 168 |
End Try |
| 169 |
|
| 170 |
End Function |
| 171 |
|
| 172 |
Public Sub SkipPadding() |
| 173 |
If stmBlorb.Position Mod 2 = 1 Then stmBlorb.Position += 1 |
| 174 |
End Sub |
| 175 |
|
| 176 |
Public Sub WritePadding() |
| 177 |
If stmBlorb.Position Mod 2 = 1 Then stmBlorb.WriteByte(CByte(0)) |
| 178 |
End Sub |
| 179 |
|
| 180 |
Public Chunks As New List(Of Chunk) |
| 181 |
Public Overridable Function WriteChunk() As Boolean |
| 182 |
Try |
| 183 |
stmBlorb.Write(UTF8.GetBytes(ID), 0, 4) ' Chunk Type |
| 184 |
stmBlorb.Write(IntToByte(Length), 0, 4) ' Size |
| 185 |
|
| 186 |
Return True |
| 187 |
Catch ex As Exception |
| 188 |
Return False |
| 189 |
End Try |
| 190 |
End Function |
| 191 |
|
| 192 |
End Class |
| 193 |
|
| 194 |
|
| 195 |
Private Class SkipChunk |
| 196 |
Inherits Chunk |
| 197 |
|
| 198 |
Private bKnown As Boolean = True |
| 199 |
Public Sub New(Optional ByVal bKnown As Boolean = True) |
| 200 |
Me.bKnown = bKnown |
| 201 |
End Sub |
| 202 |
|
| 203 |
Dim sID As String |
| 204 |
Public Overrides Property ID As String |
| 205 |
Get |
| 206 |
Return sID |
| 207 |
End Get |
| 208 |
Set(value As String) |
| 209 |
sID = value |
| 210 |
End Set |
| 211 |
End Property |
| 212 |
|
| 213 |
Public Overrides Function LoadChunk(Optional iStartPos As Integer = -1) As Boolean |
| 214 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 215 |
|
| 216 |
If Not bKnown Then |
| 217 |
#If DEBUG Then |
| 218 |
ErrMsg("Skipping unknown chunk type """ & ID & """") |
| 219 |
#End If |
| 220 |
End If |
| 221 |
|
| 222 |
|
| 223 |
stmBlorb.Position += Me.Length |
| 224 |
|
| 225 |
SkipPadding() |
| 226 |
|
| 227 |
Return True |
| 228 |
End Function |
| 229 |
End Class |
| 230 |
|
| 231 |
Private Class FrontispieceChunk |
| 232 |
Inherits Chunk |
| 233 |
|
| 234 |
Public Overrides Property ID As String |
| 235 |
Get |
| 236 |
Return "Fspc" |
| 237 |
End Get |
| 238 |
Set(value As String) |
| 239 |
If value <> "Fspc" Then Throw New Exception("Bad ID in Frontispiece Chunk: " & value) |
| 240 |
End Set |
| 241 |
End Property |
| 242 |
|
| 243 |
Public Overrides Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 244 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 245 |
|
| 246 |
' Number of a Pict resource |
| 247 |
Dim bytSize(3) As Byte |
| 248 |
iReadOffset += stmBlorb.Read(bytSize, 0, 4) |
| 249 |
Frontispiece = CInt(ByteToInt(bytSize)) |
| 250 |
|
| 251 |
SkipPadding() |
| 252 |
|
| 253 |
Return True |
| 254 |
End Function |
| 255 |
|
| 256 |
Public Overrides Function WriteChunk() As Boolean |
| 257 |
|
| 258 |
If Frontispiece > -1 Then |
| 259 |
Length = CUInt(4) |
| 260 |
If Not MyBase.WriteChunk Then Return False |
| 261 |
|
| 262 |
stmBlorb.Write(IntToByte(CUInt(Frontispiece)), 0, 4) ' Resource Number |
| 263 |
|
| 264 |
WritePadding() |
| 265 |
End If |
| 266 |
Return True |
| 267 |
End Function |
| 268 |
End Class |
| 269 |
|
| 270 |
|
| 271 |
Private Class MetaDataChunk |
| 272 |
Inherits Chunk |
| 273 |
|
| 274 |
Public Overrides Property ID As String |
| 275 |
Get |
| 276 |
Return "IFmd" |
| 277 |
End Get |
| 278 |
Set(value As String) |
| 279 |
If value <> "IFmd" Then Throw New Exception("Bad ID in Metadata Chunk: " & value) |
| 280 |
End Set |
| 281 |
End Property |
| 282 |
|
| 283 |
Public Overrides Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 284 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 285 |
|
| 286 |
Dim bytMeta(CInt(Length) - 1) As Byte |
| 287 |
iReadOffset += stmBlorb.Read(bytMeta, 0, CInt(Length)) |
| 288 |
Dim sXML As String = UTF8.GetString(bytMeta) |
| 289 |
|
| 290 |
Dim xmlMeta As New XmlDocument |
| 291 |
xmlMeta.LoadXml(sXML) |
| 292 |
MetaData = xmlMeta |
| 293 |
|
| 294 |
SkipPadding() |
| 295 |
|
| 296 |
Return True |
| 297 |
End Function |
| 298 |
|
| 299 |
Public Overrides Function WriteChunk() As Boolean |
| 300 |
|
| 301 |
If MetaData IsNot Nothing Then |
| 302 |
Length = CUInt(UTF8.GetBytes(MetaData.OuterXml).Length) ' UTC bytes may be different from character count |
| 303 |
If Not MyBase.WriteChunk Then Return False |
| 304 |
|
| 305 |
stmBlorb.Write(UTF8.GetBytes(MetaData.OuterXml), 0, CInt(Length)) |
| 306 |
|
| 307 |
WritePadding() |
| 308 |
End If |
| 309 |
Return True |
| 310 |
End Function |
| 311 |
|
| 312 |
End Class |
| 313 |
|
| 314 |
Private Class DataChunk |
| 315 |
Inherits Chunk |
| 316 |
|
| 317 |
Private sID As String |
| 318 |
Public Overrides Property ID As String |
| 319 |
Get |
| 320 |
If sID Is Nothing Then sID = "TEXT" |
| 321 |
Return sID |
| 322 |
End Get |
| 323 |
Set(value As String) |
| 324 |
Select Case value |
| 325 |
Case "TEXT", "BINA" |
| 326 |
sID = value |
| 327 |
Case Else |
| 328 |
Throw New Exception("Bad ID in Data Resource Chunk: " & value) |
| 329 |
End Select |
| 330 |
End Set |
| 331 |
End Property |
| 332 |
|
| 333 |
Public Overrides Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 334 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 335 |
|
| 336 |
Dim bytData(CInt(Length) - 1) As Byte |
| 337 |
iReadOffset += stmBlorb.Read(bytData, 0, CInt(Length)) |
| 338 |
Dim sData As String = UTF8.GetString(bytData) |
| 339 |
DataType = sData.Substring(0, 4) |
| 340 |
Data = sData.Substring(4) |
| 341 |
|
| 342 |
SkipPadding() |
| 343 |
|
| 344 |
Select Case DataType |
| 345 |
Case "RLAY" |
| 346 |
' Restore Runner Layout |
| 347 |
Dim sIFID As String = "" |
| 348 |
If MetaData IsNot Nothing Then |
| 349 |
sIFID = MetaData.GetElementsByTagName("ifid").Item(0).InnerText |
| 350 |
If sIFID <> "" Then sIFID = "-" & sIFID |
| 351 |
End If |
| 352 |
Dim sXMLFile As String = DataPath(True) & "RunnerLayout" & sIFID & ".xml" |
| 353 |
|
| 354 |
If Not IO.File.Exists(sXMLFile) Then |
| 355 |
IO.File.WriteAllText(sXMLFile, Data) |
| 356 |
End If |
| 357 |
End Select |
| 358 |
|
| 359 |
Return True |
| 360 |
End Function |
| 361 |
|
| 362 |
Public Overrides Function WriteChunk() As Boolean |
| 363 |
|
| 364 |
If DataType <> "" Then |
| 365 |
Dim bytData As Byte() = UTF8.GetBytes(DataType & Data) |
| 366 |
Length = CUInt(bytData.Length) ' UTC bytes may be different from character count |
| 367 |
If Not MyBase.WriteChunk Then Return False |
| 368 |
|
| 369 |
stmBlorb.Write(bytData, 0, CInt(Length)) |
| 370 |
|
| 371 |
WritePadding() |
| 372 |
End If |
| 373 |
Return True |
| 374 |
End Function |
| 375 |
|
| 376 |
End Class |
| 377 |
|
| 378 |
|
| 379 |
Private Class ExecResourceChunk |
| 380 |
Inherits Chunk |
| 381 |
|
| 382 |
Private sID As String |
| 383 |
Public Overrides Property ID As String |
| 384 |
Get |
| 385 |
Return sID |
| 386 |
End Get |
| 387 |
Set(value As String) |
| 388 |
Select Case value |
| 389 |
Case "ZCOD", "GLUL", "TAD2", "TAD3", "HUGO", "ALAN", "ADRI", "LEVE", "AGT ", "MAGS", "ADVS", "EXEC" |
| 390 |
sID = value |
| 391 |
Case Else |
| 392 |
Throw New Exception("Bad ID in Exec Resource Chunk: " & value) |
| 393 |
End Select |
| 394 |
End Set |
| 395 |
End Property |
| 396 |
|
| 397 |
Public Overrides Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 398 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 399 |
|
| 400 |
clsBlorb.ExecType = sID |
| 401 |
|
| 402 |
Dim bytExec(CInt(Length) - 1) As Byte |
| 403 |
iReadOffset += stmBlorb.Read(bytExec, 0, CInt(Length)) |
| 404 |
clsBlorb.ExecResource = bytExec |
| 405 |
SkipPadding() |
| 406 |
|
| 407 |
Return True |
| 408 |
End Function |
| 409 |
|
| 410 |
Public Overrides Function WriteChunk() As Boolean |
| 411 |
|
| 412 |
If ExecResource IsNot Nothing Then |
| 413 |
If Not MyBase.WriteChunk Then Return False |
| 414 |
|
| 415 |
stmBlorb.Write(ExecResource, 0, ExecResource.Length) |
| 416 |
|
| 417 |
WritePadding() |
| 418 |
End If |
| 419 |
Return True |
| 420 |
End Function |
| 421 |
|
| 422 |
End Class |
| 423 |
|
| 424 |
|
| 425 |
Private Class PictResourceChunk |
| 426 |
Inherits Chunk |
| 427 |
|
| 428 |
Private sID As String |
| 429 |
Public Overrides Property ID As String |
| 430 |
Get |
| 431 |
If img IsNot Nothing Then |
| 432 |
Select Case img.sExtn |
| 433 |
Case "jpeg", "jpg" |
| 434 |
Return "JPEG" |
| 435 |
Case "png" |
| 436 |
Return "PNG " |
| 437 |
Case "gif" |
| 438 |
Return "GIF " ' Not valid Blorb |
| 439 |
Case Else |
| 440 |
ErrMsg("Blorb does not support " & img.sExtn & " format") |
| 441 |
End Select |
| 442 |
Return Nothing |
| 443 |
Else |
| 444 |
Return sID |
| 445 |
End If |
| 446 |
End Get |
| 447 |
Set(value As String) |
| 448 |
Select Case value |
| 449 |
Case "PNG ", "JPEG", "GIF " |
| 450 |
sID = value |
| 451 |
Case Else |
| 452 |
Throw New Exception("Bad ID in Picture Resource Chunk: " & value) |
| 453 |
End Select |
| 454 |
End Set |
| 455 |
End Property |
| 456 |
|
| 457 |
Public img As ImageFile |
| 458 |
|
| 459 |
|
| 460 |
Public Overrides Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 461 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 462 |
|
| 463 |
Dim bytPict(CInt(Length) - 1) As Byte |
| 464 |
iReadOffset += stmBlorb.Read(bytPict, 0, CInt(Length)) |
| 465 |
|
| 466 |
Dim sExtn As String = "" |
| 467 |
Select Case ID |
| 468 |
Case "GIF " |
| 469 |
sExtn = "gif" ' Not valid Blorb |
| 470 |
Case "PNG " |
| 471 |
sExtn = "png" |
| 472 |
Case "JPEG" |
| 473 |
sExtn = "jpg" |
| 474 |
End Select |
| 475 |
|
| 476 |
img = New ImageFile |
| 477 |
img.bytImage = bytPict |
| 478 |
img.sExtn = sExtn |
| 479 |
|
| 480 |
SkipPadding() |
| 481 |
|
| 482 |
Return True |
| 483 |
End Function |
| 484 |
|
| 485 |
|
| 486 |
Public Overrides Function WriteChunk() As Boolean |
| 487 |
If Not MyBase.WriteChunk Then Return False |
| 488 |
stmBlorb.Write(img.bytImage, 0, img.bytImage.Length) |
| 489 |
|
| 490 |
WritePadding() |
| 491 |
Return True |
| 492 |
End Function |
| 493 |
|
| 494 |
End Class |
| 495 |
|
| 496 |
|
| 497 |
Private Class SoundResourceChunk |
| 498 |
Inherits Chunk |
| 499 |
|
| 500 |
Private sID As String |
| 501 |
Public Overrides Property ID As String |
| 502 |
Get |
| 503 |
If snd IsNot Nothing Then |
| 504 |
Select Case snd.sExtn |
| 505 |
Case "mp3" |
| 506 |
Return "MP3 " ' Not valid Blorb |
| 507 |
Case "wav" |
| 508 |
Return "WAVE" ' Not valid Blorb |
| 509 |
Case "mid" |
| 510 |
Return "MIDI" ' Not valid Blorb |
| 511 |
Case "aiff", "aif" |
| 512 |
Return "AIFF" |
| 513 |
Case "ogg" |
| 514 |
Return "OGGV" |
| 515 |
Case "mod" |
| 516 |
Return "MOD " |
| 517 |
End Select |
| 518 |
End If |
| 519 |
Return sID |
| 520 |
End Get |
| 521 |
Set(value As String) |
| 522 |
Select Case value |
| 523 |
Case "AIFF", "OGGV", "MOD " |
| 524 |
sID = value |
| 525 |
Case "MP3 ", "WAVE", "MIDI" |
| 526 |
sID = value ' Tho not valid Blorb |
| 527 |
Case Else |
| 528 |
Throw New Exception("Bad ID in Sound Resource Chunk: " & value) |
| 529 |
End Select |
| 530 |
End Set |
| 531 |
End Property |
| 532 |
|
| 533 |
Public snd As SoundFile |
| 534 |
|
| 535 |
|
| 536 |
Public Sub SetSound(ByVal sSound As String) |
| 537 |
Try |
| 538 |
If IO.File.Exists(sSound) Then |
| 539 |
Length = CUInt(FileLen(sSound)) |
| 540 |
Dim fs As New IO.FileStream(sSound, IO.FileMode.Open, IO.FileAccess.Read) |
| 541 |
snd = New SoundFile |
| 542 |
ReDim snd.bytSound(CInt(Length) - 1) |
| 543 |
fs.Read(snd.bytSound, 0, CInt(Length)) |
| 544 |
snd.sExtn = IO.Path.GetExtension(sSound).ToLower.Substring(1) |
| 545 |
fs.Close() |
| 546 |
End If |
| 547 |
Catch ex As Exception |
| 548 |
ErrMsg("Error storing sound", ex) |
| 549 |
End Try |
| 550 |
End Sub |
| 551 |
|
| 552 |
|
| 553 |
Public Overrides Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 554 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 555 |
|
| 556 |
Dim bytSound(CInt(Length) - 1) As Byte |
| 557 |
iReadOffset += stmBlorb.Read(bytSound, 0, CInt(Length)) |
| 558 |
|
| 559 |
snd = New SoundFile |
| 560 |
snd.bytSound = bytSound |
| 561 |
|
| 562 |
With snd |
| 563 |
Dim msImage As New IO.MemoryStream(bytSound) |
| 564 |
Select Case ID |
| 565 |
Case "AIFF" |
| 566 |
.sExtn = "aif" |
| 567 |
Case "OGGV" |
| 568 |
.sExtn = "ogg" |
| 569 |
Case "MOD " |
| 570 |
.sExtn = "mod" |
| 571 |
Case "MP3 " |
| 572 |
.sExtn = "mp3" |
| 573 |
Case "MIDI" |
| 574 |
.sExtn = "mid" |
| 575 |
Case "WAVE" |
| 576 |
.sExtn = "wav" |
| 577 |
End Select |
| 578 |
End With |
| 579 |
|
| 580 |
SkipPadding() |
| 581 |
|
| 582 |
Return True |
| 583 |
End Function |
| 584 |
|
| 585 |
Public Overrides Function WriteChunk() As Boolean |
| 586 |
If Not MyBase.WriteChunk Then Return False |
| 587 |
|
| 588 |
stmBlorb.Write(snd.bytSound, 0, snd.bytSound.Length) |
| 589 |
|
| 590 |
WritePadding() |
| 591 |
Return True |
| 592 |
End Function |
| 593 |
|
| 594 |
End Class |
| 595 |
|
| 596 |
|
| 597 |
Private Class ResourceIndexChunk |
| 598 |
Inherits Chunk |
| 599 |
|
| 600 |
Public Overrides Property ID As String |
| 601 |
Get |
| 602 |
Return "RIdx" |
| 603 |
End Get |
| 604 |
Set(value As String) |
| 605 |
If value <> "RIdx" Then Throw New Exception("Bad ID in Resource Index Chunk: " & value) |
| 606 |
End Set |
| 607 |
End Property |
| 608 |
|
| 609 |
Public ReadOnly Property NumberOfResources As Integer |
| 610 |
Get |
| 611 |
Return clsBlorb.ResourceIndex.Count |
| 612 |
End Get |
| 613 |
End Property |
| 614 |
|
| 615 |
|
| 616 |
Public Overrides Property Length As UInteger |
| 617 |
Get |
| 618 |
Return CUInt(4 + (12 * NumberOfResources)) |
| 619 |
End Get |
| 620 |
Set(value As UInteger) |
| 621 |
MyBase.Length = value |
| 622 |
End Set |
| 623 |
End Property |
| 624 |
|
| 625 |
|
| 626 |
Friend Class ResourceIndex |
| 627 |
Private sUsage As String |
| 628 |
Public Property Usage As String |
| 629 |
Get |
| 630 |
Return sUsage |
| 631 |
End Get |
| 632 |
Set(value As String) |
| 633 |
Select Case value |
| 634 |
Case "Pict", "Snd ", "Exec" |
| 635 |
sUsage = value |
| 636 |
Case Else |
| 637 |
Throw New Exception("Unknown Resource Usage: " & value) |
| 638 |
End Select |
| 639 |
End Set |
| 640 |
End Property |
| 641 |
|
| 642 |
Private iNumber As UInt32 ' Number of resource |
| 643 |
Public Property Number As UInt32 |
| 644 |
Get |
| 645 |
Return iNumber |
| 646 |
End Get |
| 647 |
Set(value As UInt32) |
| 648 |
iNumber = value |
| 649 |
End Set |
| 650 |
End Property |
| 651 |
|
| 652 |
Private iStart As UInt32 ' Starting position of resource |
| 653 |
Public Property Start As UInt32 |
| 654 |
Get |
| 655 |
Return iStart |
| 656 |
End Get |
| 657 |
Set(value As UInt32) |
| 658 |
iStart = value |
| 659 |
End Set |
| 660 |
End Property |
| 661 |
End Class |
| 662 |
|
| 663 |
Public Overrides Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 664 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 665 |
|
| 666 |
' Next 4 bytes tell us the number of resources in the index |
| 667 |
Dim bytNum(3) As Byte |
| 668 |
iReadOffset += stmBlorb.Read(bytNum, 0, 4) |
| 669 |
Dim iNumberOfResources As Integer = CInt(ByteToInt(bytNum)) |
| 670 |
|
| 671 |
For iResource As Integer = 0 To iNumberOfResources - 1 |
| 672 |
|
| 673 |
Dim sKey As String |
| 674 |
Dim bytUsage(3) As Byte |
| 675 |
iReadOffset += stmBlorb.Read(bytUsage, 0, 4) |
| 676 |
sKey = UTF8.GetString(bytUsage) |
| 677 |
|
| 678 |
Dim btyNumber(3) As Byte |
| 679 |
iReadOffset += stmBlorb.Read(btyNumber, 0, 4) |
| 680 |
sKey &= ByteToInt(btyNumber) |
| 681 |
|
| 682 |
Dim bytStart(3) As Byte |
| 683 |
iReadOffset += stmBlorb.Read(bytStart, 0, 4) |
| 684 |
|
| 685 |
clsBlorb.ResourceIndex.Add(sKey, ByteToInt(bytStart)) |
| 686 |
Next |
| 687 |
|
| 688 |
SkipPadding() |
| 689 |
|
| 690 |
Return True |
| 691 |
End Function |
| 692 |
|
| 693 |
|
| 694 |
Public Overrides Function WriteChunk() As Boolean |
| 695 |
If Not MyBase.WriteChunk Then Return False |
| 696 |
|
| 697 |
stmBlorb.Write(IntToByte(CUInt(clsBlorb.ResourceIndex.Count)), 0, 4) |
| 698 |
|
| 699 |
For Each sKey As String In clsBlorb.ResourceIndex.Keys |
| 700 |
stmBlorb.Write(UTF8.GetBytes(sKey.Substring(0, 4)), 0, 4) |
| 701 |
stmBlorb.Write(IntToByte(CUInt(sKey.Substring(4))), 0, 4) |
| 702 |
stmBlorb.Write(IntToByte(clsBlorb.ResourceIndex(sKey)), 0, 4) |
| 703 |
Next |
| 704 |
|
| 705 |
Return True |
| 706 |
|
| 707 |
End Function |
| 708 |
|
| 709 |
End Class |
| 710 |
|
| 711 |
|
| 712 |
Private Class FormChunk |
| 713 |
Inherits Chunk |
| 714 |
Public Overrides Property ID As String |
| 715 |
Get |
| 716 |
Return "FORM" |
| 717 |
End Get |
| 718 |
Set(value As String) |
| 719 |
If value <> "FORM" Then Throw New Exception("Bad ID in Form Chunk: " & value) |
| 720 |
End Set |
| 721 |
End Property |
| 722 |
|
| 723 |
Public Overrides Property Length As UInteger |
| 724 |
Get |
| 725 |
Dim iLength As UInt32 = 0 |
| 726 |
For Each c As Chunk In Chunks |
| 727 |
iLength += c.Length |
| 728 |
Next |
| 729 |
Return iLength |
| 730 |
End Get |
| 731 |
Set(value As UInteger) |
| 732 |
MyBase.Length = value |
| 733 |
End Set |
| 734 |
End Property |
| 735 |
Private sSubTypeID As String |
| 736 |
Public Property SubTypeID As String |
| 737 |
Get |
| 738 |
Return sSubTypeID |
| 739 |
End Get |
| 740 |
Set(value As String) |
| 741 |
sSubTypeID = value |
| 742 |
End Set |
| 743 |
End Property |
| 744 |
|
| 745 |
Public cnkResourceIndex As ResourceIndexChunk |
| 746 |
|
| 747 |
Public Overrides Function LoadChunk(Optional ByVal iStartPos As Integer = -1) As Boolean |
| 748 |
If Not MyBase.LoadChunk(iStartPos) Then Return False |
| 749 |
|
| 750 |
' Next 4 bytes tell us the FORM type |
| 751 |
Dim bytID(3) As Byte |
| 752 |
iReadOffset += stmBlorb.Read(bytID, 0, 4) |
| 753 |
SubTypeID = UTF8.GetString(bytID) |
| 754 |
|
| 755 |
cnkResourceIndex = New ResourceIndexChunk |
| 756 |
If Not cnkResourceIndex.LoadChunk() Then Return False |
| 757 |
|
| 758 |
Dim iStreamLength As Long = stmBlorb.Length |
| 759 |
If bEXE Then iStreamLength -= 6 |
| 760 |
While stmBlorb.Position < iStreamLength |
| 761 |
Dim cnk As Chunk = Nothing |
| 762 |
|
| 763 |
' Peek at the next 4 bytes to work out what chunk type it is |
| 764 |
Dim bytNext(3) As Byte |
| 765 |
iReadOffset = CInt(stmBlorb.Position) |
| 766 |
iReadOffset += stmBlorb.Read(bytNext, 0, 4) |
| 767 |
Select Case UTF8.GetString(bytNext) |
| 768 |
Case "ZCOD", "GLUL", "TAD2", "TAD3", "HUGO", "ALAN", "ADRI", "LEVE", "AGT ", "MAGS", "ADVS", "EXEC" |
| 769 |
cnk = New ExecResourceChunk |
| 770 |
Case "GIF ", "PNG ", "JPEG" |
| 771 |
cnk = New SkipChunk |
| 772 |
Case "AIFF", "OGGV", "MOD ", "MP3 ", "WAVE", "MIDI" |
| 773 |
cnk = New SkipChunk |
| 774 |
Case "IFmd" |
| 775 |
cnk = New MetaDataChunk |
| 776 |
Case "Fspc" |
| 777 |
cnk = New FrontispieceChunk |
| 778 |
Case "Plte", "IFhd" ' Colour Palette, Game Identifier |
| 779 |
cnk = New SkipChunk ' Just ignore them |
| 780 |
Case "TEXT", "BINA" |
| 781 |
cnk = New DataChunk |
| 782 |
Case Else |
| 783 |
cnk = New SkipChunk(False) |
| 784 |
End Select |
| 785 |
If cnk Is Nothing OrElse Not cnk.LoadChunk(iReadOffset - 4) Then Return False |
| 786 |
End While |
| 787 |
|
| 788 |
SkipPadding() |
| 789 |
|
| 790 |
Return True |
| 791 |
End Function |
| 792 |
|
| 793 |
|
| 794 |
Public Overrides Function WriteChunk() As Boolean |
| 795 |
|
| 796 |
' Gotta write the resources to the index so we can calculate the size of the index resource |
| 797 |
Dim iResource As UInt32 = 0 |
| 798 |
For Each c As Chunk In Chunks |
| 799 |
Dim sKey As String = "" |
| 800 |
Select Case True |
| 801 |
Case TypeOf c Is ExecResourceChunk |
| 802 |
sKey = "Exec" |
| 803 |
Case TypeOf c Is PictResourceChunk |
| 804 |
sKey = "Pict" |
| 805 |
Case TypeOf c Is SoundResourceChunk |
| 806 |
sKey = "Snd " |
| 807 |
End Select |
| 808 |
If sKey <> "" Then |
| 809 |
clsBlorb.ResourceIndex.Add(sKey & iResource, 0) |
| 810 |
iResource = CUInt(iResource + 1) |
| 811 |
End If |
| 812 |
Next |
| 813 |
|
| 814 |
' Then write out the resource lengths |
| 815 |
Dim iOffset As UInt32 = 12 |
| 816 |
iResource = 0 |
| 817 |
For Each c As Chunk In Chunks |
| 818 |
iOffset = CUInt(iOffset) |
| 819 |
Dim sKey As String = "" |
| 820 |
Select Case True |
| 821 |
Case TypeOf c Is ExecResourceChunk |
| 822 |
sKey = "Exec" |
| 823 |
Case TypeOf c Is PictResourceChunk |
| 824 |
sKey = "Pict" |
| 825 |
Case TypeOf c Is SoundResourceChunk |
| 826 |
sKey = "Snd " |
| 827 |
End Select |
| 828 |
If sKey <> "" Then |
| 829 |
clsBlorb.ResourceIndex(sKey & iResource) = iOffset |
| 830 |
iResource = CUInt(iResource + 1) |
| 831 |
End If |
| 832 |
iOffset = CUInt(iOffset + c.Length + 8) |
| 833 |
If iOffset Mod 2 = 1 Then iOffset = CUInt(iOffset + 1) |
| 834 |
Next |
| 835 |
|
| 836 |
If Not MyBase.WriteChunk() Then Return False |
| 837 |
stmBlorb.Write(UTF8.GetBytes("IFRS"), 0, 4) |
| 838 |
|
| 839 |
For Each c As Chunk In Chunks |
| 840 |
If Not c.WriteChunk() Then Return False |
| 841 |
Next |
| 842 |
|
| 843 |
|
| 844 |
WritePadding() |
| 845 |
Return True |
| 846 |
|
| 847 |
End Function |
| 848 |
End Class |
| 849 |
|
| 850 |
Private Sub ClearBlorb() |
| 851 |
ExecType = Nothing |
| 852 |
ExecResource = Nothing |
| 853 |
'ImageResources.Clear() |
| 854 |
SoundResources.Clear() |
| 855 |
ResourceIndex.Clear() |
| 856 |
MetaData = Nothing |
| 857 |
Frontispiece = -1 |
| 858 |
End Sub |
| 859 |
|
| 860 |
Public Function LoadBlorb(ByVal stmBlorb As IO.FileStream, ByVal sFilename As String, Optional ByVal BlorbOffset As Long = 0) As Boolean |
| 861 |
ClearBlorb() |
| 862 |
clsBlorb.BlorbOffset = BlorbOffset |
| 863 |
clsBlorb.sFilename = sFilename |
| 864 |
clsBlorb.stmBlorb = stmBlorb |
| 865 |
iReadOffset = 0 |
| 866 |
cnkFORM = New FormChunk |
| 867 |
Return cnkFORM.LoadChunk() |
| 868 |
End Function |
| 869 |
|
| 870 |
Friend Function CurrentIFID() As String |
| 871 |
|
| 872 |
If Adventure IsNot Nothing AndAlso Adventure.BabelTreatyInfo IsNot Nothing AndAlso Adventure.BabelTreatyInfo.Stories.Length = 1 Then |
| 873 |
Return "-" & Adventure.BabelTreatyInfo.Stories(0).Identification.IFID |
| 874 |
End If |
| 875 |
Return "" |
| 876 |
|
| 877 |
End Function |
| 878 |
|
| 879 |
Private Shared Function ByteToInt(ByVal byt() As Byte) As UInt32 |
| 880 |
' Reverse the bytes |
| 881 |
Dim bytRev(byt.Length - 1) As Byte |
| 882 |
For i As Integer = 0 To byt.Length - 1 |
| 883 |
bytRev(i) = byt(byt.Length - 1 - i) |
| 884 |
Next |
| 885 |
Return BitConverter.ToUInt32(bytRev, 0) |
| 886 |
End Function |
| 887 |
|
| 888 |
Private Shared Function IntToByte(ByVal int As UInt32) As Byte() |
| 889 |
Dim bytRev(3) As Byte |
| 890 |
Dim byt(3) As Byte |
| 891 |
bytRev = BitConverter.GetBytes(int) |
| 892 |
For i As Integer = 0 To byt.Length - 1 |
| 893 |
byt(i) = bytRev(bytRev.Length - 1 - i) |
| 894 |
Next |
| 895 |
Return byt |
| 896 |
End Function |
| 897 |
|
| 898 |
End Class |