Advertisement
dragonbane

SearchForActor

Feb 8th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 17.45 KB | None | 0 0
  1. Imports System.Linq
  2. Public Class Form1
  3.  
  4.     'Read RARC Stuff
  5.     Const HeaderSize As Integer = 64
  6.     Const NodeSize As Integer = 16
  7.     Const EntrySize As Integer = 20
  8.  
  9.     'File Header Vars
  10.     Dim headerTag As String
  11.     Dim DataStartOffset As UInt32
  12.     Dim NumNodes As UInt32
  13.     Dim FileEntriesOffset As UInt32
  14.     Dim StringTableOffset As UInt32
  15.  
  16.     'Global Stuff
  17.     Dim CurrStageName As String
  18.     Dim CurrRoomName As String
  19.  
  20.     Dim AllStages As String
  21.     Dim AllRooms As String
  22.  
  23.     Dim Output As String
  24.     Dim FoundSCLS As Boolean
  25.     Dim TableType As String
  26.  
  27.     Dim CurrentExitTable_Stages As New Collection()
  28.     Dim CurrentExitTable_Rooms As New Collection()
  29.     Dim CurrentExitTable_Spawns As New Collection()
  30.     Dim CurrentExitTable_States As New Collection()
  31.  
  32.     Private Sub Main()
  33.  
  34.         Dim StageFolder As New IO.DirectoryInfo("ExtractedStages")
  35.  
  36.         For Each Stage As IO.DirectoryInfo In StageFolder.GetDirectories()
  37.  
  38.             CurrStageName = Stage.Name
  39.  
  40.             For Each Room As IO.FileInfo In Stage.GetFiles() 'Only Stage (put it first)
  41.  
  42.                 If Room.Name.Contains("STG") = False Then
  43.  
  44.                     Continue For
  45.  
  46.                 End If
  47.  
  48.                 CurrRoomName = "Stage (Global)"
  49.  
  50.                 Dim FileContent As Byte() = My.Computer.FileSystem.ReadAllBytes(Room.FullName)
  51.  
  52.                 Dim index As String = ReadString(FileContent, 0, 4)
  53.  
  54.                 Dim outputSize As UInt32
  55.                 Dim outputBuffer As Byte()
  56.  
  57.                 'Decompress if neccesary
  58.                 If (index = "Yaz0") Then
  59.  
  60.                     outputSize = Read32(FileContent, 4)
  61.                     outputBuffer = New Byte(outputSize) {}
  62.  
  63.                     DecompressYaz0(FileContent, 0, outputBuffer)
  64.  
  65.                 Else
  66.  
  67.                     outputSize = FileContent.GetLength(0)
  68.                     outputBuffer = FileContent
  69.  
  70.                 End If
  71.  
  72.                 'File Header
  73.                 headerTag = ReadString(outputBuffer, 0, 4)
  74.                 DataStartOffset = Read32(outputBuffer, 12)
  75.                 NumNodes = Read32(outputBuffer, 32)
  76.                 FileEntriesOffset = Read32(outputBuffer, 44)
  77.                 StringTableOffset = Read32(outputBuffer, 52)
  78.  
  79.                 'Get Root and Files
  80.                 GetFileNode(outputBuffer, 0, outputSize)
  81.  
  82.             Next
  83.  
  84.             Dim Searching As Boolean = True
  85.             Dim Counter_Rooms As Integer = 0
  86.  
  87.             For Each Room As IO.FileInfo In Stage.GetFiles()
  88.  
  89.                 If Room.Name.Contains("STG") = True Then
  90.  
  91.                     Continue For
  92.  
  93.                 End If
  94.  
  95.                 CurrRoomName = Room.Name
  96.  
  97.                 Dim FileContent As Byte() = My.Computer.FileSystem.ReadAllBytes(Room.FullName)
  98.  
  99.                 Dim index As String = ReadString(FileContent, 0, 4)
  100.  
  101.                 Dim outputSize As UInt32
  102.                 Dim outputBuffer As Byte()
  103.  
  104.                 'Decompress if neccesary
  105.                 If (index = "Yaz0") Then
  106.  
  107.                     outputSize = Read32(FileContent, 4)
  108.                     outputBuffer = New Byte(outputSize) {}
  109.  
  110.                     DecompressYaz0(FileContent, 0, outputBuffer)
  111.  
  112.                 Else
  113.  
  114.                     outputSize = FileContent.GetLength(0)
  115.                     outputBuffer = FileContent
  116.  
  117.                 End If
  118.  
  119.                 'File Header
  120.                 headerTag = ReadString(outputBuffer, 0, 4)
  121.                 DataStartOffset = Read32(outputBuffer, 12)
  122.                 NumNodes = Read32(outputBuffer, 32)
  123.                 FileEntriesOffset = Read32(outputBuffer, 44)
  124.                 StringTableOffset = Read32(outputBuffer, 52)
  125.  
  126.                 'Get Root and Files
  127.                 GetFileNode(outputBuffer, 0, outputSize)
  128.  
  129.  
  130.             Next
  131.  
  132.         Next
  133.  
  134.             MsgBox("Done!", vbInformation, "Done")
  135.  
  136.     End Sub
  137.  
  138.  
  139.     Public Function FindStageDesc(StageName As String) As String
  140.  
  141.         If AllStages.Contains(StageName) = False Then
  142.  
  143.             Return "Deleted"
  144.  
  145.         End If
  146.  
  147.  
  148.         Try
  149.  
  150.             Dim StageDesc As String = AllStages.Substring(AllStages.IndexOf(StageName))
  151.  
  152.             StageDesc = StageDesc.Substring(StageDesc.IndexOf("wxT(") + 5)
  153.             StageDesc = StageDesc.Substring(0, StageDesc.IndexOf(Chr(34)))
  154.             StageDesc = StageDesc.Replace("--", "")
  155.  
  156.             Return StageDesc
  157.  
  158.         Catch ex As Exception
  159.  
  160.             Return "Deleted"
  161.  
  162.         End Try
  163.  
  164.     End Function
  165.  
  166.     Public Function FindRoomDesc(StageName As String, RoomNumber As String) As String
  167.  
  168.         If AllRooms.Contains(StageName) = False Then
  169.  
  170.             Return FindStageDesc(StageName)
  171.  
  172.         End If
  173.  
  174.         Dim tempNum As Integer = Convert.ToInt32(RoomNumber)
  175.         RoomNumber = tempNum
  176.  
  177.         Try
  178.  
  179.             Dim RoomDesc As String = AllRooms.Substring(AllRooms.IndexOf(StageName))
  180.  
  181.             RoomDesc = RoomDesc.Substring(0, RoomDesc.IndexOf("};"))
  182.             RoomDesc = RoomDesc.Substring(0, RoomDesc.IndexOf("," & vbCrLf & RoomNumber) - 1)
  183.             RoomDesc = RoomDesc.Substring(RoomDesc.LastIndexOf(Chr(34)) + 1)
  184.  
  185.             RoomDesc = RoomDesc.Replace("--", "")
  186.  
  187.             Return RoomDesc
  188.  
  189.         Catch ex As Exception
  190.  
  191.             Return "Deleted"
  192.  
  193.         End Try
  194.  
  195.     End Function
  196.  
  197.     Private Sub GetFileNode(Data() As Byte, Offset As UInt32, Size As UInt32)
  198.  
  199.         'File Node
  200.         Dim nodeTag As String
  201.         Dim FilenameOffset As UInt32
  202.         Dim NumFileEntries As UInt16
  203.         Dim FirstFileEntryOffset As UInt32
  204.  
  205.         Dim NodeName As String
  206.  
  207.         Offset = HeaderSize + (Offset * NodeSize)
  208.  
  209.         nodeTag = ReadString(Data, Convert.ToInt32(Offset), 4)
  210.         FilenameOffset = Read32(Data, Convert.ToInt32(Offset) + 4)
  211.         NumFileEntries = Read16(Data, Convert.ToInt32(Offset) + 10)
  212.         FirstFileEntryOffset = Read32(Data, Convert.ToInt32(Offset) + 12)
  213.  
  214.         NodeName = ReadStringFull(Data, Convert.ToInt32((FilenameOffset + StringTableOffset + 32)), Size)
  215.  
  216.         Dim i As Integer = 0
  217.  
  218.         While (i < NumFileEntries)
  219.  
  220.             Dim ReadOffset As UInt32 = Convert.ToInt32((FileEntriesOffset + (FirstFileEntryOffset * EntrySize) + (i * EntrySize) + 32))
  221.  
  222.             'File Entry
  223.             Dim ID As UInt16
  224.             Dim Unknown1 As UInt16
  225.             Dim Unknown2 As UInt16
  226.             Dim FilenameOffset_FileEntry As UInt16
  227.             Dim DataOffset As UInt32
  228.             Dim DataSize As UInt32
  229.  
  230.             Dim FileName As String
  231.             Dim IsCompressed As Boolean
  232.  
  233.             ID = Read16(Data, Convert.ToInt32(ReadOffset))
  234.             Unknown1 = Read16(Data, Convert.ToInt32(ReadOffset + 2))
  235.             Unknown2 = Read16(Data, Convert.ToInt32(ReadOffset + 4))
  236.             FilenameOffset_FileEntry = Read16(Data, Convert.ToInt32(ReadOffset + 6))
  237.             DataOffset = Read32(Data, Convert.ToInt32(ReadOffset + 8))
  238.             DataSize = Read32(Data, Convert.ToInt32(ReadOffset + 12))
  239.  
  240.             FileName = ReadStringFull(Data, Convert.ToInt32((FilenameOffset_FileEntry + StringTableOffset + 32)), Size)
  241.  
  242.             If CurrRoomName.Contains("Stage") = True Then
  243.  
  244.                 'MsgBox(ID)
  245.                 'MsgBox(FileName)
  246.  
  247.             End If
  248.  
  249.  
  250.             If (ID = &HFFFF Or Unknown2 = &H200) Then  '0x2000 correct???
  251.  
  252.                 If (FilenameOffset_FileEntry <> 0 And FilenameOffset_FileEntry <> 2) Then
  253.  
  254.                     GetFileNode(Data, DataOffset, Size)
  255.  
  256.                 End If
  257.  
  258.             Else
  259.  
  260.                 Dim fileExtension As String
  261.  
  262.                 Try
  263.  
  264.                     fileExtension = FileName.Substring(FileName.LastIndexOf("."))
  265.  
  266.                 Catch ex As Exception
  267.  
  268.                     fileExtension = "." & FileName
  269.  
  270.                 End Try
  271.  
  272.                 If (fileExtension = ".dzr" Or fileExtension = ".dzs") Then
  273.  
  274.                     Dim tempOffset As UInt32 = (DataStartOffset + DataOffset + 32)
  275.                     Dim tempString As String = ReadString(Data, Convert.ToInt32(tempOffset), 4)
  276.  
  277.                     If (tempString = "Yaz0") Then
  278.  
  279.                         IsCompressed = True
  280.  
  281.                     Else
  282.  
  283.                         IsCompressed = False
  284.  
  285.                     End If
  286.  
  287.                     Dim fileData As Byte()
  288.                     Dim realSize As UInt32
  289.  
  290.                     If (IsCompressed = True) Then
  291.  
  292.                         Dim fileDataSize As UInt32 = Read32(Data, Convert.ToInt32((tempOffset + 4)))
  293.  
  294.                         fileData = New Byte(fileDataSize) {}
  295.                         DecompressYaz0(Data, tempOffset, fileData)
  296.  
  297.                         realSize = fileDataSize
  298.  
  299.                     Else
  300.  
  301.                         fileData = New Byte(DataSize) {}
  302.  
  303.                         Dim n As UInt32 = 0
  304.  
  305.                         While n < DataSize
  306.  
  307.                             Dim address As UInt32 = tempOffset + n
  308.                             fileData(n) = Data(address)
  309.  
  310.                             n = n + 1
  311.  
  312.                         End While
  313.  
  314.                         realSize = DataSize
  315.  
  316.                     End If
  317.  
  318.                     'Check File Contents
  319.  
  320.                     'DZx
  321.                     Dim dZxOffset As Integer = 0
  322.  
  323.                     Dim ChunkCount As UInt32 = Read32(fileData, dZxOffset)
  324.  
  325.                     If (ChunkCount = 0) Then
  326.  
  327.                         i = i + 1
  328.                         Continue While
  329.  
  330.                     End If
  331.  
  332.                     dZxOffset += 4
  333.  
  334.                     Dim z As UInt32 = 0
  335.  
  336.                     While z < ChunkCount 'Chunks
  337.  
  338.                         Dim Tag As String
  339.                         Dim Elements, chunkOffset As UInt32
  340.  
  341.                         Tag = ReadString(fileData, dZxOffset, 4)
  342.                         Elements = Read32(fileData, dZxOffset + 4)
  343.                         chunkOffset = Read32(fileData, dZxOffset + 8)
  344.  
  345.                         Dim chunkReadOffset As Integer = Convert.ToInt32(chunkOffset)
  346.  
  347.                         Dim x As UInt32 = 0
  348.                         Dim counter As Integer = 0
  349.  
  350.                         While x < Elements 'Chunk Elements
  351.  
  352.                             'First Switch
  353.                             If (Tag = "TGOB" Or Tag = "TRES") Then
  354.  
  355.                                 chunkReadOffset += &H20
  356.                                 Exit While
  357.  
  358.                             ElseIf (Tag = "RPPN" Or Tag = "SHIP") Then
  359.  
  360.                                 chunkReadOffset += &H10
  361.                                 Exit While
  362.  
  363.                             ElseIf (Tag = "Door") Then
  364.  
  365.                                 chunkReadOffset += &H24
  366.                                 Exit While
  367.  
  368.                             ElseIf (Tag = "LGTV") Then
  369.  
  370.                                 chunkReadOffset += &H1C
  371.                                 Exit While  '?????
  372.  
  373.                             ElseIf (Tag = "MULT") Then 'Typically in DZS
  374.  
  375.                                 chunkReadOffset += &HC
  376.                                 Exit While
  377.  
  378.                             ElseIf (Tag = "PLYR") Then 'Typically in DZR
  379.  
  380.                                 'Add Spawn Point to list
  381.                                 'AddSpawnPoint(&fileData[0], chunkReadOffset);
  382.  
  383.                                 chunkReadOffset += &H20
  384.  
  385.                                 'x = x + 1
  386.                                 'Continue While
  387.                                 Exit While
  388.  
  389.                             ElseIf (Tag = "SCLS") Then 'Read Exit IDs
  390.  
  391.                                 chunkReadOffset += &HC
  392.                                 Exit While
  393.  
  394.                             End If
  395.  
  396.  
  397.                             'Second Switch
  398.                             If (Tag.Substring(0, 3) = "ACT") Then
  399.  
  400.                                 Dim Name As String = ReadString(fileData, chunkReadOffset, 8)
  401.  
  402.                                 If (Name.ToLower().Contains(TextBox4.Text.ToLower())) Then
  403.  
  404.                                     TextBox1.Text = TextBox1.Text & vbCrLf & "Actor: " & Name & " found in " & CurrStageName & "->" & CurrRoomName
  405.  
  406.                                 End If
  407.  
  408.                                 chunkReadOffset += &H20
  409.                                 x = x + 1
  410.  
  411.                                 Continue While
  412.  
  413.                             ElseIf (Tag.Substring(0, 3) = "TRE") Then
  414.  
  415.                                 chunkReadOffset += &H20
  416.                                 Exit While
  417.  
  418.                             ElseIf (Tag.Substring(0, 3) = "SCO") Then
  419.  
  420.                                 Dim Name As String = ReadString(fileData, chunkReadOffset, 8)
  421.  
  422.                                 If (Name.ToLower().Contains(TextBox4.Text.ToLower())) Then
  423.  
  424.                                     TextBox1.Text = TextBox1.Text & vbCrLf & "Scaleable Object: " & Name & " found in " & CurrStageName & "->" & CurrRoomName
  425.  
  426.                                 End If
  427.  
  428.  
  429.                                 chunkReadOffset += &H24
  430.                                 x = x + 1
  431.  
  432.                                 Continue While
  433.  
  434.                             ElseIf (Tag.Substring(0, 3) = "PLY") Then
  435.  
  436.  
  437.  
  438.                                 chunkReadOffset += &H20
  439.  
  440.                                 Exit While
  441.  
  442.                             Else
  443.  
  444.                                 Exit While
  445.  
  446.                             End If
  447.  
  448.                             x = x + 1
  449.  
  450.                         End While
  451.  
  452.                         dZxOffset += 12
  453.  
  454.                         z = z + 1
  455.  
  456.                     End While
  457.  
  458.                 End If
  459.  
  460.             End If
  461.  
  462.             i = i + 1
  463.  
  464.         End While
  465.  
  466.     End Sub
  467.  
  468.     Private Sub DecompressYaz0(Input() As Byte, Offset As UInt32, ByRef Output() As Byte)
  469.  
  470.         Dim Size As UInt32 = Read32(Input, Convert.ToInt32(Offset + 4))
  471.  
  472.         Output = New Byte(Size) {}
  473.  
  474.         Dim SrcPlace As Integer = Convert.ToInt32(Offset) + &H10
  475.         Dim DstPlace As Integer = 0
  476.  
  477.         Dim ValidBitCount As UInt32 = 0
  478.         Dim CodeByte As Byte = 0
  479.  
  480.         While (DstPlace < Size)
  481.  
  482.             If (ValidBitCount = 0) Then
  483.  
  484.                 CodeByte = Input(SrcPlace)
  485.                 SrcPlace = SrcPlace + 1
  486.                 ValidBitCount = 8
  487.  
  488.             End If
  489.  
  490.             If ((CodeByte And &H80) <> 0) Then
  491.  
  492.                 Output(DstPlace) = Input(SrcPlace)
  493.                 DstPlace = DstPlace + 1
  494.                 SrcPlace = SrcPlace + 1
  495.  
  496.             Else
  497.  
  498.                 Dim Byte1 As Byte = Input(SrcPlace)
  499.                 Dim Byte2 As Byte = Input(SrcPlace + 1)
  500.  
  501.                 SrcPlace += 2
  502.  
  503.                 Dim Dist As UInt32 = Convert.ToUInt32((((Byte1 And &HF) << 8) Or Byte2))
  504.                 Dim CopySource As UInt32 = Convert.ToUInt32((DstPlace - (Dist + 1)))
  505.                 Dim NumBytes As UInt32 = Convert.ToUInt32((Byte1 >> 4))
  506.  
  507.                 If (NumBytes = 0) Then
  508.  
  509.                     NumBytes = Convert.ToUInt32((Input(SrcPlace) + &H12))
  510.                     SrcPlace = SrcPlace + 1
  511.  
  512.                 Else
  513.  
  514.                     NumBytes += 2
  515.  
  516.                 End If
  517.  
  518.                 Dim i As Integer = 0
  519.  
  520.                 While (i < NumBytes)
  521.  
  522.                     Output(DstPlace) = Output(CopySource)
  523.                     CopySource = CopySource + 1
  524.                     DstPlace = DstPlace + 1
  525.  
  526.                     i = i + 1
  527.  
  528.                 End While
  529.  
  530.             End If
  531.  
  532.             CodeByte <<= 1
  533.             ValidBitCount -= 1
  534.  
  535.         End While
  536.  
  537.     End Sub
  538.  
  539.     Private Function Read8(Data() As Byte, Offset As Integer) As Byte
  540.  
  541.         Return Buffer.GetByte(Data, Offset)
  542.  
  543.     End Function
  544.  
  545.     Private Function Read16(Data() As Byte, Offset As Integer) As UInt16
  546.  
  547.         Dim Output(1) As Byte
  548.  
  549.         Output(0) = Buffer.GetByte(Data, Offset + 1)
  550.         Output(1) = Buffer.GetByte(Data, Offset) << 8
  551.  
  552.         Return BitConverter.ToUInt16(Output, 0)
  553.  
  554.     End Function
  555.  
  556.     Private Function Read32(Data() As Byte, Offset As Integer) As UInt32
  557.  
  558.         Dim Output(3) As Byte
  559.  
  560.         Output(0) = Buffer.GetByte(Data, Offset + 3)
  561.         Output(1) = Buffer.GetByte(Data, Offset + 2) << 8
  562.         Output(2) = Buffer.GetByte(Data, Offset + 1) << 16
  563.         Output(3) = Buffer.GetByte(Data, Offset) << 24
  564.  
  565.         Return BitConverter.ToUInt32(Output, 0)
  566.  
  567.     End Function
  568.  
  569.     Private Function ReadString(Data() As Byte, Offset As Integer, Length As Integer) As String
  570.  
  571.         Dim output As String = ""
  572.         Dim i As Integer = 0
  573.  
  574.         While (i < Length)
  575.  
  576.             Dim address As Integer = Offset + i
  577.             Dim result As Char = ""
  578.  
  579.             Dim var As Byte = Data(address)
  580.  
  581.             If var = 0 Then
  582.  
  583.                 Exit While
  584.  
  585.             End If
  586.  
  587.             result = ChrW(var)
  588.  
  589.             output = output & result
  590.  
  591.             i = i + 1
  592.  
  593.         End While
  594.  
  595.         Return output
  596.  
  597.     End Function
  598.  
  599.     Function ReadStringFull(Data() As Byte, Offset As Integer, dataSize As UInt32) As String
  600.  
  601.         If (Convert.ToUInt32(Offset) >= dataSize) Then
  602.  
  603.             Return ""
  604.  
  605.         End If
  606.  
  607.         Dim startOffset As Integer
  608.         Dim Length As Integer = 0
  609.  
  610.         While (Data(Offset) = 0)
  611.  
  612.             Offset = Offset + 1
  613.  
  614.         End While
  615.  
  616.         startOffset = Offset
  617.  
  618.         While (Data(Offset) <> 0)
  619.  
  620.             Offset = Offset + 1
  621.             Length = Length + 1
  622.  
  623.         End While
  624.  
  625.         Return ReadString(Data, startOffset, Length)
  626.  
  627.     End Function
  628.  
  629. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement