; Based on UE4 Text tools made by swuforce
; http://zenhax.com/viewtopic.php?t=1022
#include <File.au3>
#include <Binary.au3>
#include <Array.au3>
#include <FileConstants.au3>
Dim $New_Data, $XOR = Binary("0xFFFFFFFF"), $Plus = Binary("0x0000")
$Txt_Path = FileOpenDialog("Select the TXT file",@ScriptDir,"text files (*.txt)",1)
If @Error = 1 Then Exit
_FileReadToArray($Txt_Path,$New_Data)

$Name = StringTrimRight(CompGetFileName($Txt_Path),4)
$File = FileOpen($Name,0+16)	; open original .locres file
If $File = -1 Then
	MsgBox(0,"Error","Can't open " & $Name & " file.")
	Exit
EndIf

FileSetPos($File,0x11,$FILE_BEGIN)
$Values_Start = FileRead($File,8)
FileSetPos($File,0,$FILE_BEGIN)
$Original_Data = FileRead($File,$Values_Start)
$Values_Count = FileRead($File,4)

$New_File = $Original_Data & $Values_Count
For $i = 1 to $Values_Count	
	$New_Data[$i] = StringRegExpReplace($New_Data[$i],"<cf>",@CRLF)
	$New_Data[$i] = StringRegExpReplace($New_Data[$i],"<lf>",@LF)
	$New_Data[$i] = StringRegExpReplace($New_Data[$i],"<cr>",@CR)
	$New_Text = StringToBinary($New_Data[$i],2)
	$New_Text_Length = BinaryLen($New_Text)/2
	$New_File &= _BinaryReverse(Binary("0x" & Hex(BitXOR(Int($New_Text_Length),$XOR),8))) & $New_Text & $Plus
Next

$hNew_File = FileOpen("NEW_" & $Name,2+16)
FileWrite($hNew_File,$New_File)
FileClose($hNew_File)
TrayTip("Importer","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