Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region ;**** Directives created by AutoIt3Wrapper_GUI ****
- #AutoIt3Wrapper_Icon=Item_122.ico
- #AutoIt3Wrapper_Compression=4
- #AutoIt3Wrapper_Res_SaveSource=y
- #AutoIt3Wrapper_Res_Icon_Add=processor.ico
- #AutoIt3Wrapper_Res_Icon_Add=memory.ico
- #AutoIt3Wrapper_Res_Icon_Add=users.ico
- #AutoIt3Wrapper_Res_Icon_Add=network-ethernet.ico
- #AutoIt3Wrapper_Res_Icon_Add=weather.ico
- #AutoIt3Wrapper_Res_Icon_Add=weather-moon.ico
- #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
- #AutoIt3Wrapper_Run_Before=del TerrariaServe_Obfuscated.au3
- #AutoIt3Wrapper_Run_After=del TerrariaServe_Obfuscated.au3
- #AutoIt3Wrapper_Run_Tidy=y
- #Obfuscator_Parameters=/striponlyincludes
- #endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
- ; DO NOT COMPILE IN BETA 3.3.7.21 BECAUSE OF A _GUICtrlStatusBar_GetText() CRASH!
- #include <Array.au3>
- #include <Constants.au3>
- #include <INet.au3>
- #include <GUIConstantsEx.au3>
- #include <GuiRichEdit.au3>
- #include <GuiStatusBar.au3>
- #include <WindowsConstants.au3>
- #include '..\~lib\_Debug.au3'
- #include '..\~lib\_ProcessGet.au3'
- ;~ #include '..\~lib\_ProcessListProperties.au3'
- Opt('MustDeclareVars', 1)
- Opt('TrayAutoPause', 0)
- ;~ If Not @Compiled Then Opt('TrayIconDebug', 1)
- Opt('TrayIconDebug', 1)
- OnAutoItExitRegister('_Exit')
- GUIRegisterMsg($WM_SIZE, 'WM_SIZE')
- Global Const $NAME = 'TerrariaServe'
- Global Const $VERSION = '20111221'
- Global Const $LOG = @ScriptFullPath & '_' & @YEAR & @MON & @MDAY & '-' & @HOUR & @MIN & @SEC & '.log'
- Global $sFont
- Global $sDir = ''
- Global $iPID = 0
- Global $bRunning = False
- Global $hGUI
- Global $hRich
- Global $hInput
- Global $hStatus
- _Init()
- _GUI()
- _Main()
- Exit
- Func _Init()
- ; $sFont
- $sFont = RegRead('HKLM\Software\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont', '0')
- ; $sDir
- $sDir = RegRead('HKCU\Software\Valve\Steam', 'SteamPath')
- If $sDir = '' Then
- MsgBox(16, 'Error!', 'Unable to find Steam install location.')
- Exit
- EndIf
- $sDir = StringReplace($sDir, '/', '\')
- $sDir &= '\steamapps\common\terraria'
- If Not FileExists($sDir) Then
- MsgBox(16, 'Error!', 'Unable to find Terraria install location.')
- Exit
- EndIf
- If Not FileExists($sDir & '\TerrariaServer.exe') Then
- MsgBox(16, 'Error!', 'Unable to find TerrariaServer.exe.')
- Exit
- EndIf
- EndFunc ;==>_Init
- Func _GUI()
- $hGUI = GUICreate($NAME & ' v' & $VERSION, 400, 300 + 23, Default, Default, $WS_OVERLAPPEDWINDOW)
- TraySetToolTip($NAME & ' v' & $VERSION)
- $hRich = _GUICtrlRichEdit_Create($hGUI, '', 5, 5, 390, 265, BitOR($ES_MULTILINE, $ES_READONLY, $WS_VSCROLL))
- $hInput = GUICtrlCreateInput('', 5, 275, 390, 20)
- GUICtrlSetResizing($hInput, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKBOTTOM + $GUI_DOCKHEIGHT)
- GUICtrlSetFont($hInput, 8.5, Default, Default, $sFont)
- ; make $hInput activate on {ENTER} (http://www.autoitscript.com/forum/topic/112160-guictrlcreateinput-hit-enter-to-process/page__view__findpost__p__786051)
- Local $aAccel[1][2] = [['{ENTER}', $hInput]]
- GUISetAccelerators($aAccel)
- $hStatus = _GUICtrlStatusBar_Create($hGUI)
- _GUICtrlStatusBar_SetUnicodeFormat($hStatus, True)
- Local $aStatusParts[4] = [140, 205, 270, -1]
- _GUICtrlStatusBar_SetParts($hStatus, $aStatusParts)
- _GUICtrlStatusBar_SetIcon($hStatus, 0, __GetEXEIconHandle(@ScriptFullPath, 7))
- _GUICtrlStatusBar_SetIcon($hStatus, 1, __GetEXEIconHandle(@ScriptFullPath, 6))
- _GUICtrlStatusBar_SetIcon($hStatus, 2, __GetEXEIconHandle(@ScriptFullPath, 8))
- _GUICtrlStatusBar_SetIcon($hStatus, 3, __GetEXEIconHandle(@ScriptFullPath, 5))
- GUISetState()
- EndFunc ;==>_GUI
- Func WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam)
- #forceref $hWnd, $iMsg, $iwParam, $ilParam
- Local $iWidth = _WinAPI_LoWord($ilParam)
- Local $iHeight = _WinAPI_HiWord($ilParam)
- _WinAPI_MoveWindow($hRich, 5, 5, $iWidth - 10, $iHeight - 10 - 25 - 23)
- _GUICtrlStatusBar_Resize($hStatus)
- Return $GUI_RUNDEFMSG
- EndFunc ;==>WM_SIZE
- Func _Main()
- Local $bInput
- Local $bLast = False
- Local $iStart = 0
- Local $iTimer = TimerInit()
- While 1
- $bInput = False
- Switch GUIGetMsg()
- Case $GUI_EVENT_CLOSE
- ExitLoop
- Case $hInput
- If WinActive($hGUI) Then
- If $bLast = False Then ; prevent a semi-blank message getting sent immediately after a real message
- If $hInput <> '' Then
- Local $sInput = GUICtrlRead($hInput)
- _Send($sInput)
- GUICtrlSetData($hInput, '')
- $bInput = True
- $bLast = True
- EndIf
- EndIf
- EndIf
- EndSwitch
- If $bInput = False Then $bLast = False ; only reset $bLast if input wasn't detected
- If $iPID > 0 Then
- Local $sOUT = StdoutRead($iPID)
- If $sOUT <> '' Then _Console($sOUT)
- Local $sERR = StderrRead($iPID)
- If $sERR <> '' Then _Console($sERR)
- EndIf
- ; Every 1s
- If TimerDiff($iTimer) >= 1000 Then
- ; $iPID
- If ProcessExists($iPID) = 0 Then
- $bRunning = False
- $iStart = 0
- _GUICtrlStatusBar_SetText($hStatus, '0/0', 1)
- _GUICtrlStatusBar_SetText($hStatus, '0:00', 2)
- _GUICtrlStatusBar_SetText($hStatus, _IP(), 0)
- $iPID = Run($sDir & '\TerrariaServer.exe', $sDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD + $STDIN_CHILD)
- If $iPID = 0 Then
- MsgBox(16, 'Error!', 'Execution of TerrariaServer.exe failed - invalid PID.')
- Exit
- EndIf
- $iStart = TimerInit()
- EndIf
- _GUICtrlStatusBar_SetText($hStatus, _NumSize(_ProcMemory(@AutoItPID, True), 1), 3)
- If $bRunning = True Then
- If Mod(Round(TimerDiff($iStart) / 1000, 0), 15) = 0 Then
- _Time()
- ;~ MsgBox(0, '', _Players())
- EndIf
- EndIf
- $iTimer = TimerInit()
- EndIf
- WEnd
- EndFunc ;==>_Main
- Func _Exit()
- GUIDelete()
- If $iPID > 0 Then ; if process supposedly exists
- _Send('exit') ; try to exit gracefully
- Local $iTimer = TimerInit()
- While ProcessExists($iPID) > 0
- If TimerDiff($iTimer) >= 5 * 1000 Then ; wait 5s to force close
- ProcessClose($iPID)
- EndIf
- Sleep(10)
- WEnd
- EndIf
- EndFunc ;==>_Exit
- Func _Console($sData)
- $sData = StringRegExpReplace($sData, '[\r\n]+', @CRLF) ; remove extra lines
- $sData = StringRegExpReplace($sData, '[\r\n]+$', '') ; remove trailing returns
- If $sData = ': ' Then Return ; ignore just the prompt
- ; Append new line at beginning of message if not first message
- If StringLen(_GUICtrlRichEdit_GetText($hRich)) > 0 Then
- _Append(@CRLF)
- EndIf
- ; Append
- _Append(_TimePad($sData))
- ; $bRunning
- If StringInStr($sData, 'Server started') Then
- $bRunning = True
- _Port()
- _Players()
- _Time()
- EndIf
- ; _Players()
- If StringInStr($sData, 'has joined') Or StringInStr($sData, 'has left') Then
- _Players()
- EndIf
- EndFunc ;==>_Console
- Func _TimePad($sData)
- Local $sPad = '[' & @HOUR & ':' & @MIN & ':' & @SEC & '] '
- $sData = $sPad & $sData
- $sData = StringReplace($sData, @LF, @LF & $sPad)
- Return $sData
- EndFunc ;==>_TimePad
- Func _Port()
- Local $iPort = 0
- Local $sPort = _SendQuiet('port')
- If $iPort = 0 Then $iPort = _Regex($sPort, 'Port: ([\d]+)', 0)
- _Debug('_Port() _GUICtrlStatusBar_GetText() Before')
- Local $sIP = _GUICtrlStatusBar_GetText($hStatus, 0)
- _Debug('_Port() _GUICtrlStatusBar_GetText() After')
- If StringRegExp($sIP, '(\:[\d]+)') = 1 Then
- $sIP = StringRegExpReplace($sIP, '\:[\d]+', $iPort)
- Else
- $sIP &= ':' & $iPort
- EndIf
- _GUICtrlStatusBar_SetText($hStatus, $sIP, 0)
- EndFunc ;==>_Port
- Func _Players()
- Local $iCur = 0
- Local $sCur = _SendQuiet('playing')
- If $iCur = 0 Then $iCur = _Regex($sCur, '([\d]+) players? connected', 0)
- Local $iMax = 0
- Local $sMax = _SendQuiet('maxplayers')
- If $iMax = 0 Then $iMax = _Regex($sMax, 'Player limit: ([\d]+)', 0)
- _GUICtrlStatusBar_SetText($hStatus, $iCur & '/' & $iMax, 1)
- EndFunc ;==>_Players
- Func _Time()
- Local $sTime = _SendQuiet('time')
- Local $iHour = Int(_Regex($sTime, '([\d]+):[\d]+'))
- Local $iMin = Int(_Regex($sTime, '[\d]+:([\d]+)'))
- Local $iMod = _Regex($sTime, '[\d]+:[\d]+ ([\w]+)')
- If $iMod = 'AM' And $iHour = 12 Then $iHour = 0 ; 12:00AM -> 0:00
- If $iMod = 'PM' And $iHour > 12 Then $iHour += 12 ; 1:00PM+ 13:00+
- Local $iTime = $iHour * 100 + $iMin
- If $iTime >= 430 And $iTime < 1930 Then ; day (4:30a)
- _GUICtrlStatusBar_SetIcon($hStatus, 2, __GetEXEIconHandle(@ScriptFullPath, 8))
- Else ; night (7:30p)
- _GUICtrlStatusBar_SetIcon($hStatus, 2, __GetEXEIconHandle(@ScriptFullPath, 9))
- EndIf
- If $iHour < 10 Then $iHour = '0' & $iHour ; 1:00+ -> 01:00 (after $iHour doesn't need to be Int anymore)
- _GUICtrlStatusBar_SetText($hStatus, $iHour & ':' & $iMin, 2)
- EndFunc ;==>_Time
- Func _Send($sMsg)
- If $iPID > 0 Then
- _Append($sMsg & @CRLF)
- StdinWrite($iPID, $sMsg & @LF)
- EndIf
- EndFunc ;==>_Send
- Func _SendQuiet($sMsg)
- Local $sRet = ''
- If $iPID > 0 Then
- StdinWrite($iPID, $sMsg & @LF)
- While 1
- Local $sOUT = StdoutRead($iPID)
- $sRet &= $sOUT
- If StringRight($sOUT, 2) = ': ' Then ExitLoop ; wait until a prompt is given (full message delivered)
- WEnd
- EndIf
- $sRet = StringReplace($sRet, @CRLF & ': ', '') ; get rid of prompt junk
- Return $sRet
- EndFunc ;==>_SendQuiet
- Func _Append($sData)
- _GUICtrlRichEdit_SetFont($hRich, Default, $sFont)
- _GUICtrlRichEdit_AppendText($hRich, $sData)
- FileWrite($LOG, $sData)
- GUICtrlSetState($hInput, $GUI_FOCUS) ; try to keep RichEdit from having focus
- EndFunc ;==>_Append
- Func _ProcMemory($iPID, $bChildren = False)
- Local $iRet = 0
- Local $aMem = ProcessGetStats($iPID)
- If IsArray($aMem) Then
- $iRet += $aMem[0]
- EndIf
- If $bChildren = True Then
- Local $aChildren = _ProcessGetChildren($iPID)
- If IsArray($aChildren) Then
- For $i = 1 To $aChildren[0][0]
- $iRet += _ProcMemory($aChildren[$i][0])
- Next
- EndIf
- EndIf
- Return $iRet
- EndFunc ;==>_ProcMemory
- Func _Regex($sData, $sPattern, $sDefault = '')
- Local $sRet = $sDefault
- Local $aRegex = StringRegExp($sData, $sPattern, 1)
- If IsArray($aRegex) Then
- $sRet = $aRegex[0]
- EndIf
- Return $sRet
- EndFunc ;==>_Regex
- Func _IP()
- Local $sRet = _GetIP()
- If $sRet = -1 Then $sRet = '0.0.0.0'
- Return String($sRet)
- EndFunc ;==>_IP
- ; ITaskBarList.au3
- Func __GetEXEIconHandle($sPath, $iIndex = 0)
- Local $Icon = DllStructCreate("handle")
- _WinAPI_ExtractIconEx($sPath, $iIndex, 0, DllStructGetPtr($Icon), 1)
- If @error Then Return SetError(1, 0, 0)
- Return DllStructGetData($Icon, 1)
- EndFunc ;==>__GetEXEIconHandle
- Func __ArrayConcatenate(ByRef $avArrayTarget, $avArraySource, $iStart = 0)
- ; NO ERROR CHECKING! SHOULD BE FIXED!
- Local $iOffset = UBound($avArrayTarget)
- If Not IsArray($avArrayTarget) Then
- Dim $avArrayTarget[UBound($avArraySource, 1) - $iStart][UBound($avArraySource, 2)]
- Else
- ReDim $avArrayTarget[UBound($avArrayTarget, 1) + UBound($avArraySource, 1) - $iStart][UBound($avArrayTarget, 2)]
- EndIf
- For $r = $iStart To UBound($avArraySource, 1) - 1
- For $c = 0 To UBound($avArraySource, 2) - 1
- $avArrayTarget[$r + $iOffset - $iStart][$c] = $avArraySource[$r][$c]
- Next
- Next
- EndFunc ;==>__ArrayConcatenate
- Func _NumComma($iNum, $iDigit = 3)
- Local $sRet = ''
- While StringLen($iNum) > 0
- $sRet = StringRight($iNum, $iDigit) & ',' & $sRet
- $iNum = StringTrimRight($iNum, $iDigit)
- WEnd
- $sRet = StringTrimRight($sRet, 1)
- Return $sRet
- EndFunc ;==>_NumComma
- Func _NumSize($iNum, $iRound = 1)
- Local $sRet = ''
- If $iNum > 1099511627776 Then
- $sRet = _NumRound($iNum / 1099511627776, $iRound) & ' TB'
- ElseIf $iNum > 1073741824 Then
- $sRet = _NumRound($iNum / 1073741824, $iRound) & ' GB'
- ElseIf $iNum > 1048576 Then
- $sRet = _NumRound($iNum / 1048576, $iRound) & ' MB'
- ElseIf $iNum > 1024 Then
- $sRet = _NumRound($iNum / 1024, $iRound) & ' KB'
- Else
- $sRet = $iNum & ' B'
- EndIf
- Return $sRet
- EndFunc ;==>_NumSize
- Func _NumRound($iNum, $iPlaces)
- Local $sRet = Round($iNum, $iPlaces)
- If Not StringInStr($sRet, ".") Then
- $sRet &= "."
- For $i = 1 To $iPlaces
- $sRet &= "0"
- Next
- EndIf
- Return $sRet
- EndFunc ;==>_NumRound
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement