What's new

Sprawdzony Auto starter.

Status
Not open for further replies.

Oskar

Forum friend
Joined
Jan 24, 2009
Messages
2,256
Reaction score
331
Prosty kodzik, dzi?ki kt?remu mo?emy w??czy? w config.lua globalny save, wraz z opcj? restartu silnika:
PHP:
shutdownAtGlobalSave = true
globalSaveEnabled = true

W game.cpp dodajcie now? funkcj?:
PHP:
bool Game::isRunning(const char* name)
{
	PROCESSENTRY32 proc32;
    HANDLE hSnapshot;

    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    
    proc32.dwSize = sizeof(PROCESSENTRY32);
	bool isRunning = false;
	if(Process32First(hSnapshot, &proc32))
    {
        while(Process32Next(hSnapshot, &proc32))
        {
            std::string filename_str = std::string(proc32.szExeFile);
			if(filename_str == name)
            {
				isRunning = true;
            	break;
			}
        }
    }
    
    CloseHandle(hSnapshot);
    return isRunning;
}

W game.h deklaracja:
PHP:
bool isRunning(const char* name);

W game.cpp znajd? funkcj?:
PHP:
void Game::setGameState(GameState_t newState)

A w niej:
PHP:
Scheduler::getInstance().stop();
Dispatcher::getInstance().stop();

I tu? pod tym dodaj:
PHP:
const char* name = "AutoRun.exe";
if(!isRunning(name))
	WinExec(name, SW_SHOWNORMAL);

W otsystem.h pod:
PHP:
#include <windows.h>

Doklej:
PHP:
#include <tlhelp32.h>



Oraz kod do programu:
PHP:
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>

const int _time = 10;
char* name = "Nazwa serwera.exe";

bool isRunning()
{
	PROCESSENTRY32 proc32;
    HANDLE hSnapshot;

    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    
    proc32.dwSize = sizeof(PROCESSENTRY32);

    std::cout << "Check the server is running." << std::endl;
	bool isRunning = false;
	if(Process32First(hSnapshot, &proc32))
    {
        while(Process32Next(hSnapshot, &proc32))
        {
            std::string filename_str = std::string(proc32.szExeFile);
			if(filename_str == name)
            {
				isRunning = true;
            	break;
			}
        }
    }
    
    CloseHandle(hSnapshot);
    return isRunning;
}

bool checkServer()
{
	while(true)
	{
		Sleep(_time * 1000);
		if(!isRunning())
		{
			std::cout << "Run the server." << std::endl;
			WinExec(name, SW_SHOWNORMAL);
			return true;
		}
	}
	
	return false;
}

int main()
{
    if(checkServer())
    	return 0;

    return 1;
}

Teraz wystarczy skompilowa? kodzik, plik nazwa? AutoRun.exe i wklei? do folderu z silnikiem.
 

okinator191

Banned
Joined
Feb 11, 2016
Messages
10
Reaction score
1
Odp: Auto starter.

A mo?na to jako? z gdb po??czy?? ;p ?eby w razie czego restartowa?o serwer po ewentualnym crashu ? :D Bo z tego co pami?tam to jak ma si? odpalone gdb to wtedy nie restartuje serwera.
 
Status
Not open for further replies.
Top