Advertisement
Guest User

Untitled

a guest
Dec 28th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 11.88 KB | None | 0 0
  1. #region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_Icon=Item_122.ico
  3. #AutoIt3Wrapper_Compression=4
  4. #AutoIt3Wrapper_Res_SaveSource=y
  5. #AutoIt3Wrapper_Res_Icon_Add=processor.ico
  6. #AutoIt3Wrapper_Res_Icon_Add=memory.ico
  7. #AutoIt3Wrapper_Res_Icon_Add=users.ico
  8. #AutoIt3Wrapper_Res_Icon_Add=network-ethernet.ico
  9. #AutoIt3Wrapper_Res_Icon_Add=weather.ico
  10. #AutoIt3Wrapper_Res_Icon_Add=weather-moon.ico
  11. #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
  12. #AutoIt3Wrapper_Run_Before=del TerrariaServe_Obfuscated.au3
  13. #AutoIt3Wrapper_Run_After=del TerrariaServe_Obfuscated.au3
  14. #AutoIt3Wrapper_Run_Tidy=y
  15. #Obfuscator_Parameters=/striponlyincludes
  16. #endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
  17.  
  18. ; DO NOT COMPILE IN BETA 3.3.7.21 BECAUSE OF A _GUICtrlStatusBar_GetText() CRASH!
  19.  
  20. #include <Array.au3>
  21. #include <Constants.au3>
  22. #include <INet.au3>
  23.  
  24. #include <GUIConstantsEx.au3>
  25. #include <GuiRichEdit.au3>
  26. #include <GuiStatusBar.au3>
  27. #include <WindowsConstants.au3>
  28.  
  29. #include '..\~lib\_Debug.au3'
  30. #include '..\~lib\_ProcessGet.au3'
  31. ;~ #include '..\~lib\_ProcessListProperties.au3'
  32.  
  33. Opt('MustDeclareVars', 1)
  34. Opt('TrayAutoPause', 0)
  35. ;~ If Not @Compiled Then Opt('TrayIconDebug', 1)
  36. Opt('TrayIconDebug', 1)
  37.  
  38. OnAutoItExitRegister('_Exit')
  39.  
  40. GUIRegisterMsg($WM_SIZE, 'WM_SIZE')
  41.  
  42. Global Const $NAME = 'TerrariaServe'
  43. Global Const $VERSION = '20111221'
  44.  
  45. Global Const $LOG = @ScriptFullPath & '_' & @YEAR & @MON & @MDAY & '-' & @HOUR & @MIN & @SEC & '.log'
  46.  
  47. Global $sFont
  48. Global $sDir = ''
  49. Global $iPID = 0
  50. Global $bRunning = False
  51.  
  52. Global $hGUI
  53. Global $hRich
  54. Global $hInput
  55. Global $hStatus
  56.  
  57. _Init()
  58. _GUI()
  59. _Main()
  60. Exit
  61.  
  62.  
  63. Func _Init()
  64.     ; $sFont
  65.     $sFont = RegRead('HKLM\Software\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont', '0')
  66.  
  67.     ; $sDir
  68.     $sDir = RegRead('HKCU\Software\Valve\Steam', 'SteamPath')
  69.     If $sDir = '' Then
  70.         MsgBox(16, 'Error!', 'Unable to find Steam install location.')
  71.         Exit
  72.     EndIf
  73.     $sDir = StringReplace($sDir, '/', '\')
  74.     $sDir &= '\steamapps\common\terraria'
  75.     If Not FileExists($sDir) Then
  76.         MsgBox(16, 'Error!', 'Unable to find Terraria install location.')
  77.         Exit
  78.     EndIf
  79.     If Not FileExists($sDir & '\TerrariaServer.exe') Then
  80.         MsgBox(16, 'Error!', 'Unable to find TerrariaServer.exe.')
  81.         Exit
  82.     EndIf
  83. EndFunc   ;==>_Init
  84.  
  85.  
  86. Func _GUI()
  87.     $hGUI = GUICreate($NAME & ' v' & $VERSION, 400, 300 + 23, Default, Default, $WS_OVERLAPPEDWINDOW)
  88.     TraySetToolTip($NAME & ' v' & $VERSION)
  89.  
  90.     $hRich = _GUICtrlRichEdit_Create($hGUI, '', 5, 5, 390, 265, BitOR($ES_MULTILINE, $ES_READONLY, $WS_VSCROLL))
  91.  
  92.     $hInput = GUICtrlCreateInput('', 5, 275, 390, 20)
  93.     GUICtrlSetResizing($hInput, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKBOTTOM + $GUI_DOCKHEIGHT)
  94.     GUICtrlSetFont($hInput, 8.5, Default, Default, $sFont)
  95.     ; make $hInput activate on {ENTER} (http://www.autoitscript.com/forum/topic/112160-guictrlcreateinput-hit-enter-to-process/page__view__findpost__p__786051)
  96.     Local $aAccel[1][2] = [['{ENTER}', $hInput]]
  97.     GUISetAccelerators($aAccel)
  98.  
  99.     $hStatus = _GUICtrlStatusBar_Create($hGUI)
  100.     _GUICtrlStatusBar_SetUnicodeFormat($hStatus, True)
  101.     Local $aStatusParts[4] = [140, 205, 270, -1]
  102.     _GUICtrlStatusBar_SetParts($hStatus, $aStatusParts)
  103.     _GUICtrlStatusBar_SetIcon($hStatus, 0, __GetEXEIconHandle(@ScriptFullPath, 7))
  104.     _GUICtrlStatusBar_SetIcon($hStatus, 1, __GetEXEIconHandle(@ScriptFullPath, 6))
  105.     _GUICtrlStatusBar_SetIcon($hStatus, 2, __GetEXEIconHandle(@ScriptFullPath, 8))
  106.     _GUICtrlStatusBar_SetIcon($hStatus, 3, __GetEXEIconHandle(@ScriptFullPath, 5))
  107.  
  108.     GUISetState()
  109. EndFunc   ;==>_GUI
  110.  
  111. Func WM_SIZE($hWnd, $iMsg, $iwParam, $ilParam)
  112.     #forceref $hWnd, $iMsg, $iwParam, $ilParam
  113.     Local $iWidth = _WinAPI_LoWord($ilParam)
  114.     Local $iHeight = _WinAPI_HiWord($ilParam)
  115.  
  116.     _WinAPI_MoveWindow($hRich, 5, 5, $iWidth - 10, $iHeight - 10 - 25 - 23)
  117.  
  118.     _GUICtrlStatusBar_Resize($hStatus)
  119.  
  120.     Return $GUI_RUNDEFMSG
  121. EndFunc   ;==>WM_SIZE
  122.  
  123.  
  124. Func _Main()
  125.     Local $bInput
  126.     Local $bLast = False
  127.  
  128.     Local $iStart = 0
  129.     Local $iTimer = TimerInit()
  130.  
  131.     While 1
  132.         $bInput = False
  133.  
  134.         Switch GUIGetMsg()
  135.             Case $GUI_EVENT_CLOSE
  136.                 ExitLoop
  137.             Case $hInput
  138.                 If WinActive($hGUI) Then
  139.                     If $bLast = False Then ; prevent a semi-blank message getting sent immediately after a real message
  140.                         If $hInput <> '' Then
  141.                             Local $sInput = GUICtrlRead($hInput)
  142.                             _Send($sInput)
  143.                             GUICtrlSetData($hInput, '')
  144.                             $bInput = True
  145.                             $bLast = True
  146.                         EndIf
  147.                     EndIf
  148.                 EndIf
  149.         EndSwitch
  150.  
  151.         If $bInput = False Then $bLast = False ; only reset $bLast if input wasn't detected
  152.  
  153.         If $iPID > 0 Then
  154.             Local $sOUT = StdoutRead($iPID)
  155.             If $sOUT <> '' Then _Console($sOUT)
  156.             Local $sERR = StderrRead($iPID)
  157.             If $sERR <> '' Then _Console($sERR)
  158.         EndIf
  159.  
  160.         ; Every 1s
  161.         If TimerDiff($iTimer) >= 1000 Then
  162.             ; $iPID
  163.             If ProcessExists($iPID) = 0 Then
  164.                 $bRunning = False
  165.                 $iStart = 0
  166.  
  167.                 _GUICtrlStatusBar_SetText($hStatus, '0/0', 1)
  168.                 _GUICtrlStatusBar_SetText($hStatus, '0:00', 2)
  169.                 _GUICtrlStatusBar_SetText($hStatus, _IP(), 0)
  170.  
  171.                 $iPID = Run($sDir & '\TerrariaServer.exe', $sDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD + $STDIN_CHILD)
  172.                 If $iPID = 0 Then
  173.                     MsgBox(16, 'Error!', 'Execution of TerrariaServer.exe failed - invalid PID.')
  174.                     Exit
  175.                 EndIf
  176.  
  177.                 $iStart = TimerInit()
  178.             EndIf
  179.  
  180.             _GUICtrlStatusBar_SetText($hStatus, _NumSize(_ProcMemory(@AutoItPID, True), 1), 3)
  181.  
  182.             If $bRunning = True Then
  183.                 If Mod(Round(TimerDiff($iStart) / 1000, 0), 15) = 0 Then
  184.                     _Time()
  185. ;~                  MsgBox(0, '', _Players())
  186.                 EndIf
  187.             EndIf
  188.  
  189.             $iTimer = TimerInit()
  190.         EndIf
  191.     WEnd
  192. EndFunc   ;==>_Main
  193.  
  194.  
  195. Func _Exit()
  196.     GUIDelete()
  197.  
  198.     If $iPID > 0 Then ; if process supposedly exists
  199.         _Send('exit') ; try to exit gracefully
  200.         Local $iTimer = TimerInit()
  201.         While ProcessExists($iPID) > 0
  202.             If TimerDiff($iTimer) >= 5 * 1000 Then ; wait 5s to force close
  203.                 ProcessClose($iPID)
  204.             EndIf
  205.             Sleep(10)
  206.         WEnd
  207.     EndIf
  208. EndFunc   ;==>_Exit
  209.  
  210.  
  211. Func _Console($sData)
  212.     $sData = StringRegExpReplace($sData, '[\r\n]+', @CRLF) ; remove extra lines
  213.     $sData = StringRegExpReplace($sData, '[\r\n]+$', '') ; remove trailing returns
  214.  
  215.     If $sData = ': ' Then Return ; ignore just the prompt
  216.  
  217.     ; Append new line at beginning of message if not first message
  218.     If StringLen(_GUICtrlRichEdit_GetText($hRich)) > 0 Then
  219.         _Append(@CRLF)
  220.     EndIf
  221.     ; Append
  222.     _Append(_TimePad($sData))
  223.  
  224.     ; $bRunning
  225.     If StringInStr($sData, 'Server started') Then
  226.         $bRunning = True
  227.         _Port()
  228.         _Players()
  229.         _Time()
  230.     EndIf
  231.  
  232.     ; _Players()
  233.     If StringInStr($sData, 'has joined') Or StringInStr($sData, 'has left') Then
  234.         _Players()
  235.     EndIf
  236. EndFunc   ;==>_Console
  237.  
  238. Func _TimePad($sData)
  239.     Local $sPad = '[' & @HOUR & ':' & @MIN & ':' & @SEC & '] '
  240.     $sData = $sPad & $sData
  241.     $sData = StringReplace($sData, @LF, @LF & $sPad)
  242.     Return $sData
  243. EndFunc   ;==>_TimePad
  244.  
  245.  
  246. Func _Port()
  247.     Local $iPort = 0
  248.     Local $sPort = _SendQuiet('port')
  249.     If $iPort = 0 Then $iPort = _Regex($sPort, 'Port: ([\d]+)', 0)
  250.  
  251.     _Debug('_Port() _GUICtrlStatusBar_GetText() Before')
  252.     Local $sIP = _GUICtrlStatusBar_GetText($hStatus, 0)
  253.     _Debug('_Port() _GUICtrlStatusBar_GetText() After')
  254.     If StringRegExp($sIP, '(\:[\d]+)') = 1 Then
  255.         $sIP = StringRegExpReplace($sIP, '\:[\d]+', $iPort)
  256.     Else
  257.         $sIP &= ':' & $iPort
  258.     EndIf
  259.     _GUICtrlStatusBar_SetText($hStatus, $sIP, 0)
  260. EndFunc   ;==>_Port
  261.  
  262. Func _Players()
  263.     Local $iCur = 0
  264.     Local $sCur = _SendQuiet('playing')
  265.     If $iCur = 0 Then $iCur = _Regex($sCur, '([\d]+) players? connected', 0)
  266.  
  267.     Local $iMax = 0
  268.     Local $sMax = _SendQuiet('maxplayers')
  269.     If $iMax = 0 Then $iMax = _Regex($sMax, 'Player limit: ([\d]+)', 0)
  270.  
  271.     _GUICtrlStatusBar_SetText($hStatus, $iCur & '/' & $iMax, 1)
  272. EndFunc   ;==>_Players
  273.  
  274. Func _Time()
  275.     Local $sTime = _SendQuiet('time')
  276.     Local $iHour = Int(_Regex($sTime, '([\d]+):[\d]+'))
  277.     Local $iMin = Int(_Regex($sTime, '[\d]+:([\d]+)'))
  278.     Local $iMod = _Regex($sTime, '[\d]+:[\d]+ ([\w]+)')
  279.     If $iMod = 'AM' And $iHour = 12 Then $iHour = 0 ; 12:00AM -> 0:00
  280.     If $iMod = 'PM' And $iHour > 12 Then $iHour += 12 ; 1:00PM+ 13:00+
  281.  
  282.     Local $iTime = $iHour * 100 + $iMin
  283.     If $iTime >= 430 And $iTime < 1930 Then ; day (4:30a)
  284.         _GUICtrlStatusBar_SetIcon($hStatus, 2, __GetEXEIconHandle(@ScriptFullPath, 8))
  285.     Else ; night (7:30p)
  286.         _GUICtrlStatusBar_SetIcon($hStatus, 2, __GetEXEIconHandle(@ScriptFullPath, 9))
  287.     EndIf
  288.  
  289.     If $iHour < 10 Then $iHour = '0' & $iHour ; 1:00+ -> 01:00 (after $iHour doesn't need to be Int anymore)
  290.     _GUICtrlStatusBar_SetText($hStatus, $iHour & ':' & $iMin, 2)
  291. EndFunc   ;==>_Time
  292.  
  293.  
  294. Func _Send($sMsg)
  295.     If $iPID > 0 Then
  296.         _Append($sMsg & @CRLF)
  297.         StdinWrite($iPID, $sMsg & @LF)
  298.     EndIf
  299. EndFunc   ;==>_Send
  300.  
  301. Func _SendQuiet($sMsg)
  302.     Local $sRet = ''
  303.     If $iPID > 0 Then
  304.         StdinWrite($iPID, $sMsg & @LF)
  305.         While 1
  306.             Local $sOUT = StdoutRead($iPID)
  307.             $sRet &= $sOUT
  308.             If StringRight($sOUT, 2) = ': ' Then ExitLoop ; wait until a prompt is given (full message delivered)
  309.         WEnd
  310.     EndIf
  311.     $sRet = StringReplace($sRet, @CRLF & ': ', '') ; get rid of prompt junk
  312.     Return $sRet
  313. EndFunc   ;==>_SendQuiet
  314.  
  315. Func _Append($sData)
  316.     _GUICtrlRichEdit_SetFont($hRich, Default, $sFont)
  317.     _GUICtrlRichEdit_AppendText($hRich, $sData)
  318.     FileWrite($LOG, $sData)
  319.     GUICtrlSetState($hInput, $GUI_FOCUS) ; try to keep RichEdit from having focus
  320. EndFunc   ;==>_Append
  321.  
  322.  
  323. Func _ProcMemory($iPID, $bChildren = False)
  324.     Local $iRet = 0
  325.     Local $aMem = ProcessGetStats($iPID)
  326.     If IsArray($aMem) Then
  327.         $iRet += $aMem[0]
  328.     EndIf
  329.     If $bChildren = True Then
  330.         Local $aChildren = _ProcessGetChildren($iPID)
  331.         If IsArray($aChildren) Then
  332.             For $i = 1 To $aChildren[0][0]
  333.                 $iRet += _ProcMemory($aChildren[$i][0])
  334.             Next
  335.         EndIf
  336.     EndIf
  337.     Return $iRet
  338. EndFunc   ;==>_ProcMemory
  339.  
  340.  
  341. Func _Regex($sData, $sPattern, $sDefault = '')
  342.     Local $sRet = $sDefault
  343.     Local $aRegex = StringRegExp($sData, $sPattern, 1)
  344.     If IsArray($aRegex) Then
  345.         $sRet = $aRegex[0]
  346.     EndIf
  347.     Return $sRet
  348. EndFunc   ;==>_Regex
  349.  
  350.  
  351. Func _IP()
  352.     Local $sRet = _GetIP()
  353.     If $sRet = -1 Then $sRet = '0.0.0.0'
  354.     Return String($sRet)
  355. EndFunc   ;==>_IP
  356.  
  357.  
  358. ; ITaskBarList.au3
  359. Func __GetEXEIconHandle($sPath, $iIndex = 0)
  360.     Local $Icon = DllStructCreate("handle")
  361.     _WinAPI_ExtractIconEx($sPath, $iIndex, 0, DllStructGetPtr($Icon), 1)
  362.     If @error Then Return SetError(1, 0, 0)
  363.     Return DllStructGetData($Icon, 1)
  364. EndFunc   ;==>__GetEXEIconHandle
  365.  
  366.  
  367. Func __ArrayConcatenate(ByRef $avArrayTarget, $avArraySource, $iStart = 0)
  368.     ; NO ERROR CHECKING! SHOULD BE FIXED!
  369.     Local $iOffset = UBound($avArrayTarget)
  370.  
  371.     If Not IsArray($avArrayTarget) Then
  372.         Dim $avArrayTarget[UBound($avArraySource, 1) - $iStart][UBound($avArraySource, 2)]
  373.     Else
  374.         ReDim $avArrayTarget[UBound($avArrayTarget, 1) + UBound($avArraySource, 1) - $iStart][UBound($avArrayTarget, 2)]
  375.     EndIf
  376.  
  377.     For $r = $iStart To UBound($avArraySource, 1) - 1
  378.         For $c = 0 To UBound($avArraySource, 2) - 1
  379.             $avArrayTarget[$r + $iOffset - $iStart][$c] = $avArraySource[$r][$c]
  380.         Next
  381.     Next
  382. EndFunc   ;==>__ArrayConcatenate
  383.  
  384.  
  385. Func _NumComma($iNum, $iDigit = 3)
  386.     Local $sRet = ''
  387.     While StringLen($iNum) > 0
  388.         $sRet = StringRight($iNum, $iDigit) & ',' & $sRet
  389.         $iNum = StringTrimRight($iNum, $iDigit)
  390.     WEnd
  391.     $sRet = StringTrimRight($sRet, 1)
  392.     Return $sRet
  393. EndFunc   ;==>_NumComma
  394.  
  395. Func _NumSize($iNum, $iRound = 1)
  396.     Local $sRet = ''
  397.     If $iNum > 1099511627776 Then
  398.         $sRet = _NumRound($iNum / 1099511627776, $iRound) & ' TB'
  399.     ElseIf $iNum > 1073741824 Then
  400.         $sRet = _NumRound($iNum / 1073741824, $iRound) & ' GB'
  401.     ElseIf $iNum > 1048576 Then
  402.         $sRet = _NumRound($iNum / 1048576, $iRound) & ' MB'
  403.     ElseIf $iNum > 1024 Then
  404.         $sRet = _NumRound($iNum / 1024, $iRound) & ' KB'
  405.     Else
  406.         $sRet = $iNum & ' B'
  407.     EndIf
  408.     Return $sRet
  409. EndFunc   ;==>_NumSize
  410.  
  411. Func _NumRound($iNum, $iPlaces)
  412.     Local $sRet = Round($iNum, $iPlaces)
  413.     If Not StringInStr($sRet, ".") Then
  414.         $sRet &= "."
  415.         For $i = 1 To $iPlaces
  416.             $sRet &= "0"
  417.         Next
  418.     EndIf
  419.     Return $sRet
  420. EndFunc   ;==>_NumRound
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement