Advertisement
deseven

m2cl

Feb 24th, 2012
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; m2 coop loader
  2.  
  3. Global RunDir.s = GetPathPart(ProgramFilename())
  4. ;Global RunDir.s = "C:\games\Mercenaries 2 - World In Flames\game\"
  5. Global SysDir.s = GetEnvironmentVariable("WINDIR") + "\system32\"
  6. Global ServerRunning = #False
  7. Global ClientRunning = #False
  8. Global Mode = 0
  9. Global MercsFound = #False
  10.  
  11. If OpenWindow(0,#PB_Ignore,#PB_Ignore,400,120,"M2 coop loader b3",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  12.   IPAddressGadget(1,10,10,140,20)
  13.   ;GadgetToolTip(1,"Your IP if you want to host a game or remote IP if you want to join a game")
  14.   ButtonGadget(2,10,40,140,30,"Host game")
  15.   ButtonGadget(3,10,80,140,30,"Join game")
  16.   ListViewGadget(4,160,10,230,100)
  17.   ;DisableGadget(4,1)
  18. EndIf
  19.  
  20. Structure FindWindowData
  21.   hFW.l
  22.   sFW.s
  23.   cFW.s
  24.   px.l
  25.   py.l
  26.   height.l
  27.   width.l
  28. EndStructure
  29.  
  30. Global NewList FindWindow.FindWindowData()
  31.  
  32. Procedure.l EnumWindowsProc(hFind,lParam)
  33.   Protected WindowName.s = Space(255)
  34.   Protected WindowClass.s = Space(255)
  35.   Protected WP.WINDOWPLACEMENT
  36.   ClearStructure(@WP,WINDOWPLACEMENT)
  37.   WP\length = SizeOf(WINDOWPLACEMENT)
  38.   If GetWindowText_(hFind,WindowName,255)
  39.     Result = GetClassName_(hFind,WindowClass,255)
  40.     If GetWindowPlacement_(hFind,@WP)
  41.       If IsWindowVisible_(hFind)
  42.         AddElement(FindWindow())
  43.         ;FindWindow()\hFW = hFind
  44.         FindWindow()\sFW = WindowName
  45.         ;FindWindow()\cFW = WindowClass
  46.         ;FindWindow()\px    = WP\rcNormalPosition\left
  47.         ;FindWindow()\py    = WP\rcNormalPosition\top
  48.         ;FindWindow()\height =  WP\rcNormalPosition\bottom-WP\rcNormalPosition\top-2*GetSystemMetrics_(#SM_CYFRAME)-GetSystemMetrics_(#SM_CYCAPTION)
  49.         ;FindWindow()\width = WP\rcNormalPosition\right-WP\rcNormalPosition\left-2*GetSystemMetrics_(#SM_CXFRAME)
  50.       EndIf
  51.     EndIf
  52.   EndIf
  53.   ProcedureReturn #True
  54. EndProcedure
  55.  
  56. Procedure AddToLog(str.s)
  57.   AddGadgetItem(4,-1,str)
  58.   SetGadgetState(4,CountGadgetItems(4)-1)
  59. EndProcedure
  60.  
  61. Procedure LockUI(state)
  62.   If state
  63.     DisableGadget(1,1)
  64.     DisableGadget(2,1)
  65.     DisableGadget(3,1)
  66.   Else
  67.     DisableGadget(1,0)
  68.     DisableGadget(2,0)
  69.     DisableGadget(3,0)
  70.   EndIf
  71. EndProcedure
  72.  
  73. Procedure ServerProcess(dummy)
  74.   If FileSize(RunDir + "mercs2server.exe") > 0
  75.     AddToLog("starting server with IP " + GetGadgetText(1))
  76.     Global ServerID = RunProgram(RunDir + "mercs2server.exe",GetGadgetText(1),RunDir,#PB_Program_Open|#PB_Program_Hide)
  77.     Delay(3000)
  78.     If IsProgram(ServerID)
  79.       If ProgramRunning(ServerID)
  80.         ServerRunning = #True
  81.         AddToLog("server started successfully")
  82.       Else
  83.         AddToLog("can't start server")
  84.         LockUI(0)
  85.         ProcedureReturn #False
  86.       EndIf
  87.     Else
  88.       AddToLog("server crashed!")
  89.       LockUI(0)
  90.       ProcedureReturn #False
  91.     EndIf    
  92.     While IsProgram(ServerID)
  93.       If Not ProgramRunning(ServerID)
  94.         CloseProgram(ServerID)
  95.         AddToLog("server stopped")
  96.         LockUI(0)
  97.         ProcedureReturn #False
  98.       EndIf
  99.       Delay(100)
  100.     Wend
  101.   Else
  102.     AddToLog("can't find mercs2server.exe")
  103.     LockUI(0)
  104.     ProcedureReturn #False
  105.   EndIf
  106. EndProcedure
  107.  
  108. Procedure StopServer()
  109.   RunProgram(SysDir + "taskkill.exe","/F /IM:mercs2server.exe",RunDir,#PB_Program_Hide|#PB_Program_Wait)
  110. EndProcedure
  111.  
  112. Procedure StopClient()
  113.   RunProgram(SysDir + "taskkill.exe","/F /IM:MLoader.exe",RunDir,#PB_Program_Hide|#PB_Program_Wait)
  114.   RunProgram(SysDir + "taskkill.exe","/F /IM:Mercenaries2.exe",RunDir,#PB_Program_Hide|#PB_Program_Wait)
  115.   Delay(300)
  116. EndProcedure
  117.  
  118. Procedure ClientProcess(dummy)
  119.   If FileSize(RunDir + "MLoader.exe") > 0
  120.     Try = 1
  121.     Repeat
  122.       AddToLog("starting client, try #" + Str(Try))
  123.       StopClient()
  124.       Global ClientID = RunProgram(RunDir + "MLoader.exe",GetGadgetText(1),RunDir,#PB_Program_Open)
  125.       Delay(500)
  126.       If ProgramRunning(ClientID)
  127.         ClearList(FindWindow.FindWindowData())
  128.         If EnumWindows_(@EnumWindowsProc(),0)
  129.           ForEach FindWindow.FindWindowData()
  130.             If FindWindow.FindWindowData()\sFW = "Mercenaries 2: World In Flames"
  131.               MercsFound = #True
  132.               Break
  133.             EndIf
  134.           Next
  135.         EndIf
  136.       Else
  137.         CloseProgram(ClientID)
  138.         AddToLog("can't start client")
  139.         LockUI(0)
  140.         ProcedureReturn #False
  141.       EndIf
  142.       Try + 1
  143.     Until MercsFound = #True
  144.     AddToLog("client started successfully")
  145.     ;Delay(10000)
  146.     Delay(3000)
  147.     Repeat
  148.       MercsFound = #False
  149.       ClearList(FindWindow.FindWindowData())
  150.       If EnumWindows_(@EnumWindowsProc(),0)
  151.         ForEach FindWindow.FindWindowData()
  152.           ;Debug FindWindow.FindWindowData()\sFW
  153.           If FindWindow.FindWindowData()\sFW = "Mercenaries 2: World In Flames"
  154.             MercsFound = #True
  155.             Break
  156.           EndIf
  157.         Next
  158.       EndIf
  159.       Delay(1000)
  160.     Until MercsFound = #False
  161.     AddToLog("client exited")
  162.     StopClient()
  163.     If Mode = 1
  164.       StopServer()
  165.       ;AddToLog("server stoppped")
  166.     EndIf
  167.     LockUI(0)
  168.   Else
  169.     AddToLog("can't find MLoader.exe")
  170.     LockUI(0)
  171.     ProcedureReturn #False
  172.   EndIf
  173. EndProcedure
  174.  
  175. Repeat
  176.   ev = WaitWindowEvent(10)
  177.   If ev = #PB_Event_Gadget And EventType() = #PB_EventType_LeftClick
  178.     Select EventGadget()
  179.       Case 2
  180.         Mode = 1
  181.         ClearGadgetItems(4)
  182.         LockUI(1)
  183.         ServerThread = CreateThread(@ServerProcess(),dummy)
  184.       Case 3
  185.         Mode = 2
  186.         ClearGadgetItems(4)
  187.         LockUI(1)
  188.         ClientThread = CreateThread(@ClientProcess(),dummy)
  189.     EndSelect
  190.   EndIf
  191.   If Mode = 1
  192.     If ServerRunning = #True
  193.       ServerRunning = #False
  194.       ClientThread = CreateThread(@ClientProcess(),dummy)
  195.     EndIf
  196.   EndIf
  197.   If ev = #PB_Event_CloseWindow
  198.     If IsThread(ServerThread)
  199.       StopServer()
  200.     EndIf
  201.     If IsThread(ClientThread)
  202.       StopClient()
  203.     EndIf
  204.     Break
  205.   EndIf
  206. ForEver
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement