Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; m2 coop loader
- Global RunDir.s = GetPathPart(ProgramFilename())
- ;Global RunDir.s = "C:\games\Mercenaries 2 - World In Flames\game\"
- Global SysDir.s = GetEnvironmentVariable("WINDIR") + "\system32\"
- Global ServerRunning = #False
- Global ClientRunning = #False
- Global Mode = 0
- Global MercsFound = #False
- If OpenWindow(0,#PB_Ignore,#PB_Ignore,400,120,"M2 coop loader b3",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
- IPAddressGadget(1,10,10,140,20)
- ;GadgetToolTip(1,"Your IP if you want to host a game or remote IP if you want to join a game")
- ButtonGadget(2,10,40,140,30,"Host game")
- ButtonGadget(3,10,80,140,30,"Join game")
- ListViewGadget(4,160,10,230,100)
- ;DisableGadget(4,1)
- EndIf
- Structure FindWindowData
- hFW.l
- sFW.s
- cFW.s
- px.l
- py.l
- height.l
- width.l
- EndStructure
- Global NewList FindWindow.FindWindowData()
- Procedure.l EnumWindowsProc(hFind,lParam)
- Protected WindowName.s = Space(255)
- Protected WindowClass.s = Space(255)
- Protected WP.WINDOWPLACEMENT
- ClearStructure(@WP,WINDOWPLACEMENT)
- WP\length = SizeOf(WINDOWPLACEMENT)
- If GetWindowText_(hFind,WindowName,255)
- Result = GetClassName_(hFind,WindowClass,255)
- If GetWindowPlacement_(hFind,@WP)
- If IsWindowVisible_(hFind)
- AddElement(FindWindow())
- ;FindWindow()\hFW = hFind
- FindWindow()\sFW = WindowName
- ;FindWindow()\cFW = WindowClass
- ;FindWindow()\px = WP\rcNormalPosition\left
- ;FindWindow()\py = WP\rcNormalPosition\top
- ;FindWindow()\height = WP\rcNormalPosition\bottom-WP\rcNormalPosition\top-2*GetSystemMetrics_(#SM_CYFRAME)-GetSystemMetrics_(#SM_CYCAPTION)
- ;FindWindow()\width = WP\rcNormalPosition\right-WP\rcNormalPosition\left-2*GetSystemMetrics_(#SM_CXFRAME)
- EndIf
- EndIf
- EndIf
- ProcedureReturn #True
- EndProcedure
- Procedure AddToLog(str.s)
- AddGadgetItem(4,-1,str)
- SetGadgetState(4,CountGadgetItems(4)-1)
- EndProcedure
- Procedure LockUI(state)
- If state
- DisableGadget(1,1)
- DisableGadget(2,1)
- DisableGadget(3,1)
- Else
- DisableGadget(1,0)
- DisableGadget(2,0)
- DisableGadget(3,0)
- EndIf
- EndProcedure
- Procedure ServerProcess(dummy)
- If FileSize(RunDir + "mercs2server.exe") > 0
- AddToLog("starting server with IP " + GetGadgetText(1))
- Global ServerID = RunProgram(RunDir + "mercs2server.exe",GetGadgetText(1),RunDir,#PB_Program_Open|#PB_Program_Hide)
- Delay(3000)
- If IsProgram(ServerID)
- If ProgramRunning(ServerID)
- ServerRunning = #True
- AddToLog("server started successfully")
- Else
- AddToLog("can't start server")
- LockUI(0)
- ProcedureReturn #False
- EndIf
- Else
- AddToLog("server crashed!")
- LockUI(0)
- ProcedureReturn #False
- EndIf
- While IsProgram(ServerID)
- If Not ProgramRunning(ServerID)
- CloseProgram(ServerID)
- AddToLog("server stopped")
- LockUI(0)
- ProcedureReturn #False
- EndIf
- Delay(100)
- Wend
- Else
- AddToLog("can't find mercs2server.exe")
- LockUI(0)
- ProcedureReturn #False
- EndIf
- EndProcedure
- Procedure StopServer()
- RunProgram(SysDir + "taskkill.exe","/F /IM:mercs2server.exe",RunDir,#PB_Program_Hide|#PB_Program_Wait)
- EndProcedure
- Procedure StopClient()
- RunProgram(SysDir + "taskkill.exe","/F /IM:MLoader.exe",RunDir,#PB_Program_Hide|#PB_Program_Wait)
- RunProgram(SysDir + "taskkill.exe","/F /IM:Mercenaries2.exe",RunDir,#PB_Program_Hide|#PB_Program_Wait)
- Delay(300)
- EndProcedure
- Procedure ClientProcess(dummy)
- If FileSize(RunDir + "MLoader.exe") > 0
- Try = 1
- Repeat
- AddToLog("starting client, try #" + Str(Try))
- StopClient()
- Global ClientID = RunProgram(RunDir + "MLoader.exe",GetGadgetText(1),RunDir,#PB_Program_Open)
- Delay(500)
- If ProgramRunning(ClientID)
- ClearList(FindWindow.FindWindowData())
- If EnumWindows_(@EnumWindowsProc(),0)
- ForEach FindWindow.FindWindowData()
- If FindWindow.FindWindowData()\sFW = "Mercenaries 2: World In Flames"
- MercsFound = #True
- Break
- EndIf
- Next
- EndIf
- Else
- CloseProgram(ClientID)
- AddToLog("can't start client")
- LockUI(0)
- ProcedureReturn #False
- EndIf
- Try + 1
- Until MercsFound = #True
- AddToLog("client started successfully")
- ;Delay(10000)
- Delay(3000)
- Repeat
- MercsFound = #False
- ClearList(FindWindow.FindWindowData())
- If EnumWindows_(@EnumWindowsProc(),0)
- ForEach FindWindow.FindWindowData()
- ;Debug FindWindow.FindWindowData()\sFW
- If FindWindow.FindWindowData()\sFW = "Mercenaries 2: World In Flames"
- MercsFound = #True
- Break
- EndIf
- Next
- EndIf
- Delay(1000)
- Until MercsFound = #False
- AddToLog("client exited")
- StopClient()
- If Mode = 1
- StopServer()
- ;AddToLog("server stoppped")
- EndIf
- LockUI(0)
- Else
- AddToLog("can't find MLoader.exe")
- LockUI(0)
- ProcedureReturn #False
- EndIf
- EndProcedure
- Repeat
- ev = WaitWindowEvent(10)
- If ev = #PB_Event_Gadget And EventType() = #PB_EventType_LeftClick
- Select EventGadget()
- Case 2
- Mode = 1
- ClearGadgetItems(4)
- LockUI(1)
- ServerThread = CreateThread(@ServerProcess(),dummy)
- Case 3
- Mode = 2
- ClearGadgetItems(4)
- LockUI(1)
- ClientThread = CreateThread(@ClientProcess(),dummy)
- EndSelect
- EndIf
- If Mode = 1
- If ServerRunning = #True
- ServerRunning = #False
- ClientThread = CreateThread(@ClientProcess(),dummy)
- EndIf
- EndIf
- If ev = #PB_Event_CloseWindow
- If IsThread(ServerThread)
- StopServer()
- EndIf
- If IsThread(ClientThread)
- StopClient()
- EndIf
- Break
- EndIf
- ForEver
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement