#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/rm /so
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#cs ----------------------------------------------------------------------------

	AutoIt Version: 3.3.15.0 (Beta)
	Author:         mataran

	Script Function:
	Find missing and unused string of the LanguageSupport UDF string.xml file
	REQUIRES: Compile the script you want to check with the /mo Au3Stripper command:

	This script works but is done in a rush and neither very well commented nor programmed :P
#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <String.au3>
#include <Array.au3>



$PathProgram = FileOpenDialog('Select the "/mo" only stripped .au3 output file!', @ScriptDir, 'AutoIt Source Code (*.au3)', 7) ; 4 allow multi select mit | getrennt
If @error Or $PathProgram = '' Then Exit
If StringInStr($PathProgram, '|') Then
	$sProgram = ReadMultiSelect($PathProgram)
Else
	$sProgram = FileRead($PathProgram)
	If @error Then Exit MsgBox(16, '', 'Could not read ' & $PathProgram)
EndIf
$PathLanguageFile = FileOpenDialog('Select the Language File!', @ScriptDir, 'Language File (*.xml)', 3) ;file + path must exist
If @error Or $PathLanguageFile = '' Then Exit
$sLanguageFile = FileRead($PathLanguageFile)
If @error Then Exit MsgBox(16, '', 'Could not read ' & $PathLanguageFile)

;~ MsgBox(0, '', $sProgram)
;~ $aStringsInProgram = StringRegExp($sProgram, "s\(''\)", 1)
$aStringsInProgram = StringRegExp($sProgram, "[^a-zA-Z]s\('.+'\)", 3) ;suche nach exakt nach s('XXX') im programm
If @error Or UBound($aStringsInProgram) <= 0 Then Exit MsgBox(16, 'Error', 'Keine Strings gefunden! Stelle sicher, dass folgende Compiler Commands verwendet wurden:' & @CRLF & @CRLF & '#AutoIt3Wrapper_Run_Au3Stripper=y' & @CRLF & '#Au3Stripper_Parameters=/mo')

;~ _ArrayDisplay($aStringsInProgram)
$sString = ''
For $i = 0 To UBound($aStringsInProgram) - 1
	$sString &= $aStringsInProgram[$i] & @CRLF ;da output von oben noch s(' usw. contains baue string um s(' usw. zu removen
Next
$aStringsInProgram = _StringBetween($sString, "s('", "')") ;extracte inhalt
If @error Then Exit MsgBox(16, 'StringBetweenError', @error)
;~ _ArrayDisplay($aStringsInProgram)
_ArraySort($aStringsInProgram);, 0, 0, 0, 0, 1)
;~ _ArrayDisplay($aStringsInProgram)
_ArrayDelete($aStringsInProgram, 0) ;since 0 contains data after sorting

$i = 0
$o = UBound($aStringsInProgram) - 2 ;bis zum vorletzten, da immer das danach gecheckt wird
Do ;schleife die doppelte vorkommnisse entfernt
	If $o <= 0 Then ExitLoop
	If $aStringsInProgram[$i] = $aStringsInProgram[$i + 1] Then ;wenn doppelt lösche eintrag
;~ 		_ArrayDisplay($aStringsInProgram, 'Delete: '&$i+1)
		_ArrayDelete($aStringsInProgram, $i + 1)
		$o -= 1
		$i -= 1
	EndIf
	$i += 1
Until $i = $o
;~ _ArrayDisplay($aStringsInProgram, 'Done: Program Data')

Local $aMissingInLangFile[0]
For $i = 0 To UBound($aStringsInProgram) - 1
	ToolTip($i & ' / ' & UBound($aStringsInProgram) - 1, 1, 1)
	If Not StringInStr($sLanguageFile, '"' & $aStringsInProgram[$i] & '"') Then ;suche nach allen strings die im programm gefunden wurden in der lang file
		ReDim $aMissingInLangFile[UBound($aMissingInLangFile) + 1]
		$aMissingInLangFile[UBound($aMissingInLangFile) - 1] = $aStringsInProgram[$i] ;schreibe array mit fehlenden strings
	EndIf
Next
ToolTip('')
If UBound($aMissingInLangFile) = 0 Then
	MsgBox(64, '', 'No missing strings found.')
Else
	_ArrayDisplay($aMissingInLangFile, 'Missing in language file:')
	$iMsgBoxAnswer = MsgBox(36, "", "Import missing strings from other file?")
	Select
		Case $iMsgBoxAnswer = 6 ;Yes
			Local $sImportData = ''
			$PathImportFile = FileOpenDialog('Select the Language File that contains the strings to import!', @ScriptDir, 'Language File (*.xml)', 3) ;file + path must exist
			If @error Or $PathImportFile = '' Then Exit
			$sImportFile = FileRead($PathImportFile)
			If @error Then Exit MsgBox(16, '', 'Could not read ' & $PathImportFile)

			For $i = 0 To UBound($aMissingInLangFile) - 1
				If StringInStr($sImportFile, $aMissingInLangFile[$i]) Then
					$tmp = _StringBetween($sImportFile, '<string name="' & $aMissingInLangFile[$i] & '">', '</string>')
					If @error Then
						MsgBox(16, 'Import SplitError', '<string name="' & $aMissingInLangFile[$i] & '">XXX</string>')
						ContinueLoop
					EndIf
					$sImportData &= '	<string name="' & $aMissingInLangFile[$i] & '">' & $tmp[0] & '</string>' & @CRLF
				Else
					MsgBox(64, 'Import Not Found', 'Could not find string ' & $aMissingInLangFile[$i])
				EndIf
			Next
			FileMove($PathLanguageFile, $PathLanguageFile & '_old.xml')
			FileWrite($PathLanguageFile, StringReplace($sLanguageFile, '</Resources>', '') & @CRLF & $sImportData & '</Resources>')
	EndSelect
EndIf


$sLanguageFile = FileRead($PathLanguageFile)
If @error Then Exit MsgBox(16, '', 'Could not read ' & $PathLanguageFile)
$aStringsInLanguage = _StringBetween($sLanguageFile, '<string name="', '">')
If @error Then Exit MsgBox(16, 'StringBetweenError', @error)

;~ _ArrayDisplay($aStringsInLanguage, 'Done: Language Data')

Local $aNotUsedStrings[0]
Local $sFixedLang = $sLanguageFile
ProgressOn('Language File Checker', 'Checking Code for unused Strings...', '0%')
For $i = 0 To UBound($aStringsInLanguage) - 1
;~ 	ToolTip($i & ' / ' & UBound($aStringsInLanguage) - 1, 1, 1)
	ProgressSet(($i * 100) / UBound($aStringsInLanguage) - 1, Int(($i * 100) / UBound($aStringsInLanguage) - 1) & '%')
;~ 	If $aStringsInLanguage[$i] = @CRLF Or $aStringsInLanguage[$i] = @LF Or $aStringsInLanguage[$i] = @CR Or $aStringsInLanguage[$i] = ''  Then ContinueLoop
	If Not StringInStr($sProgram, $aStringsInLanguage[$i]) Then
		ReDim $aNotUsedStrings[UBound($aNotUsedStrings) + 1]
		$aNotUsedStrings[UBound($aNotUsedStrings) - 1] = $aStringsInLanguage[$i]
		$tmp = _StringBetween($sLanguageFile, '<string name="' & $aStringsInLanguage[$i] & '">', '</string>')
		If @error Then
			MsgBox(16, 'Remove Split Error', '<string name="' & $aStringsInLanguage[$i] & '">XXX</string>')
			If StringInStr($sLanguageFile, '<string name="' & $aStringsInLanguage[$i] & '">') Then MsgBox(0, '', 'first is')
;~ 			MsgBox(16, 'asdfa', $sFixedLang)
			ContinueLoop
		EndIf
;~ 		$sFixedLang = StringReplace($sFixedLang, $aStringsInLanguage[$i], '			'&$aStringsInLanguage[$i])
		$sFixedLang = StringReplace($sFixedLang, '<string name="' & $aStringsInLanguage[$i] & '">' & $tmp[0] & '</string>', '')
	EndIf
Next
ProgressOff()
;~ ToolTip('')
If UBound($aNotUsedStrings) = 0 Then
	MsgBox(64, '', 'No unused strings found.')
Else
	_ArrayDisplay($aNotUsedStrings, 'NOT USED in Program:')
	$iMsgBoxAnswer = MsgBox(36, "", "Remove unused data from language file?")
	Select
		Case $iMsgBoxAnswer = 6 ;Yes
			ClipPut($sFixedLang)
			FileDelete($PathLanguageFile)
			FileWrite($PathLanguageFile, $sFixedLang)
	EndSelect
EndIf

Func ReadMultiSelect($sPath)
;~ 	MsgBox(0, '', $sPath)
	$aTmp = StringSplit($sPath, "|", 2)
	Local $sData = ''
	For $i = 1 To UBound($aTmp) - 1
;~ 		FileWrite(@ScriptDir&'\CodeTemp.txt', FileRead($aTmp[$i])&@CRLF&@CRLF) ;schreibe alle daten in eine datei
		$sData &= FileRead($aTmp[0] & '\' & $aTmp[$i]) & @CRLF & @CRLF ;Lese alle dateien in eine var
	Next
	Return $sData
EndFunc   ;==>ReadMultiSelect
