简体   繁体   中英

Finding a specific value in a file

I am trying to make a simple program to find some values in a file. These files are .arkprofile files and belong to the game ARK .

These .arkprofile files include some readable text as well as some scrambled text if you open it in a regular editor. All the values I need are in ASCII so I could get them all manually, but the intention of this program is to go through a lot of files to grab out the character names. Here is how it looks like in the file in a regular text editor:

PlayerCharacterName.....StrProperty.............CHARACTERNAMEHERE

Can I find this string without decompiling the file as hex first? Is it possible to convert the string to bytes and do the search from there?

This is the code I have at the moment, but it is not suited for my need as the hex offset for the string is not the same for all the files.

Dim pos1 As Long = 864

Dim requiredBytes As Integer = 160
Dim value(0 To requiredBytes - 1) As Byte
Using reader As New BinaryReader(File.Open(fd.FileName, FileMode.Open))
    ' Loop through length of file.
    Dim fileLength As Long = reader.BaseStream.Length
    Dim byteCount As Integer = 0
    reader.BaseStream.Seek(pos1, SeekOrigin.Begin)
    While pos1 < fileLength And byteCount < requiredBytes
        value(byteCount) = reader.ReadByte()
        pos1 += 1
        byteCount += 1
    End While
End Using

The dots between the strings change hex value from file to file, but its always the same amount of dots. Max character name allowed on ARK is 24, so my idea now was to first find that string in the file, start writing bytes from the end of that file for 128 bytes. "PlayerCharacterName.....StrProperty............." = 60 bytes. This is where the username would start and can be up to 24 characters long which is 48 bytes. Then I could filter out the remaining characters that are not a part of the username and display it in ASCII.

Am I way off track here?

Since you can read it in a text editor, you can just open it as a text file.

Imports System.IO

Module Module1

    Sub Main()
        Dim line As String = ""
        Using sr As New StreamReader("c:\temp\test.abc")
            While Not sr.EndOfStream
                line = sr.ReadLine()
                If line.StartsWith("PlayerCharacterName") Then
                    Exit While
                End If
            End While
        End Using
        If line = "" Then
            Console.WriteLine("Did not find the name!")
        Else
            Dim s = line.Split("."c)
            Dim name = s.Last()
            Console.WriteLine("Found name ""{0}""", name)
        End If
        Console.Read()
    End Sub

End Module

I put a file at c:\\temp\\test.abc to test. With this in it:

5tmodvVa640xaDv0fZ650R85uWo0R
CqwhYMD9e8h
FIEeAHhER6Qm2sWY38tKYO
i7diJRVGiZJUZHx26URbCwsewhby3NhPLMSMOv
w51Ft4I8aK2bjdu0OmzD3V5tDjlXnCXGfTk1NqAE
PlayerCharacterName.....StrProperty.............CHARACTERNAMEHERE
J4H73RcfdMVHkLIaXv
Yo5TCC6MmnkA51BZJcrCkzj62xucQ
8TR1QfSL1IRdmF2ScjjlokTHYHNa2suBk
1FBphwSK8aQWdfY1H9tKHSr
kLbQvNhUhILdcBv1EXXJgwZtQh37JZu2oXoHuCHRf2bpKsKmlZyf055Q5
ly0WxwtFP47BE0BAVD1sfWBogFR0Qb9r3DKBWiinRk9xLitqT
g5FgAyCQ5P7v3Z9hz04hQR1KU1SuoscYH7s5SYbHV1mJEnJKIb0

And this was the output of my program:

Found name "CHARACTERNAMEHERE"

This sounds like a job for a regular expression:

Public Function GetCharacterName(ByVal filePath As String) As String
    Dim exp As New RegEx("PlayerCharacterName.{5}StrProperty.{13}(.{1,24})")
    For Each line As String In File.ReadLines(filePath)
        Dim result = exp.Match(line)
        If result.Success Then
            Return result.Groups(1).Value
        End If
    Next line
    Return Nothing
End Function

My only concern with this is whether I built the expression correctly (I didn't have Visual Studio or real sample data handy) and whether some of the unprintable characters might produce an unexpected newline or multi-byte character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM