; Based on UE4 Text tools made by swuforce
; http://zenhax.com/viewtopic.php?t=1022
#include<Binary.au3>
#include <FileConstants.au3>
$Path = FileOpenDialog("Select the LOCRES file",@ScriptDir,"locres files (*.locres)",1)
If @Error = 1 Then Exit
$File = Fileopen($Path,16)
Dim $Text
$XOR = Binary("0xFFFFFFFF")
$Block_Hash = FileRead($File,16)
$Unknown_Byte = FileRead($File,1)
$Values_Start = FileRead($File,8)
FileSetPos($File,$Values_Start,$FILE_BEGIN)
$Values_Count = FileRead($File,4)

For $i = 1 to $Values_Count
	$Value_Length = BitXOR(FileRead($File,4),$XOR)*2		; *2 because text is in Unicode
	$Value_Text = BinaryToString(FileRead($File,$Value_Length),2)
	$Value_Text = StringRegExpReplace($Value_Text,@CRLF,"<cf>")
	$Value_Text = StringRegExpReplace($Value_Text,@LF,"<lf>")
	$Value_Text = StringRegExpReplace($Value_Text,@CR,"<cr>")
	$Text &= $Value_Text & @CRLF
	FileRead($File,2)	; skip 2 zero bytes	
Next

$hAllTextFile = FileOpen(CompGetFileName($Path)&".txt",2+32)
FileWrite($hAllTextFile,$Text)
FileClose($hAllTextFile)
TrayTip("Exporter","Finish!",3)
Sleep(3000)

Func CompGetFileName($Path)
If StringLen($Path) < 4 Then Return -1
$Ret = StringSplit($Path,"\",2)
If IsArray($Ret) Then
	Return $Ret[UBound($Ret)-1]
EndIf
If @Error Then Return -1
EndFunc
