summaryrefslogtreecommitdiff
path: root/KPL_Start.cpp
blob: 5bc46fd29e31457b7243a6e1d897d20d488b28be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//---------------------------------------------------------------------------
#include "KPL_Start.h"
#include "KPLTypes.h"
#include "KPLFillModSpecific.h"
#include "WriteScript.h"
#include "GenerateScript.h"
#ifndef __WIN32__
    #include <string.h>
    #include <stdio.h>
#endif

#ifdef __WIN32__
    #include "process.h"// For spawnlp
    #include <windows.h>
    #pragma package(smart_init)
    static DWORD processID=0;
#else
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #define P_NOWAIT 0
    static pid_t processID=0;
    static int spawnl(int flg, const char* file, const char* name, const char* arg1, const char* arg2) {
    	int pid = fork();
	    if (pid == 0) {
		    // We are in the son
    		execlp(file, name, arg1, arg2, NULL);
    		perror("execlp");
	    	exit(1);
    	}
	    return pid;
    }
#endif

/*
 * Wait()s without blocking to cleanup exited children processes.
 * Returns:
 *   -1 for error
 *   0 for no running children
 *   1 for running children
 */
static int cleanupWait() {
    if (!processID)
        return 0;
    #ifdef __WIN32__
        HANDLE ProcessHandle;
        ProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION,false,processID);
        if(ProcessHandle!=NULL) {
            // Spring is already running
            return 1;
        }
    #else
        pid_t termedProc = waitpid(processID, NULL, WNOHANG);
        if (termedProc == -1) {
            perror("waitpid");
            return -1;
        }
        if (termedProc == 0) {
            // Spring is already running
            return 1;
        }
        processID = 0;
    #endif
    return 0;
}

int StartSpectator(void*)
{
    return LaunchSpringExecutable(0);
}

int StartEasy(void*)
{
    return LaunchSpringExecutable(1);
}

int StartMedium(void*)
{
    return LaunchSpringExecutable(2);
}

int StartHard(void*)
{
    return LaunchSpringExecutable(3);
}

int StartVeryHard(void*)
{
    return LaunchSpringExecutable(4);
}

int StartMultiPlayer(void*)
{
    if (cleanupWait() != 0)
        return 1;
    processID=spawnl(P_NOWAIT,cKPgame::ClientExecutableFileName,cKPgame::ClientExecutableFileName,"",NULL);
    return 0;
}

int StartSettings(void*)
{
    if (cleanupWait() != 0)
        return 1;
    processID=spawnl(P_NOWAIT,cKPgame::SettingsExecutableFileName,cKPgame::SettingsExecutableFileName,"",NULL);
    return 0;
}

int LaunchSpringExecutable(int Difficulty)
{
    // Check that Spring isn't already running
    if (cleanupWait() != 0)
        return 2;

    KPLFillModSpecific();

    // Create and allocate "game" and its "players" and its "modoptions"
    cKPgame* game=(cKPgame*)malloc(sizeof(cKPgame));
    game->players=(cKPplayer**)malloc(32*sizeof(cKPplayer*));
    for(int p=0;p<32;++p)
        game->players[p]=(cKPplayer*)malloc(sizeof(cKPplayer));
    game->ModOptions=(cKPmodoptions*)malloc(sizeof(cKPmodoptions));

    // Generate a random script according to difficulty
    GenerateScript(game,Difficulty);

    // Writing that script on the disk
    WriteScript(game);

    #ifdef __WIN32__
        Sleep(500);
    #endif

    free(game->ModOptions);
    for(int p=0;p<32;++p)
        free(game->players[p]);
    free(game->players);
    free(game);

    const char *scriptName = game->ScriptFileName;
#ifndef __WIN32__
    char fullName[256];
    if (getcwd(fullName, sizeof(fullName) - strlen(game->ExecutableFileName) - 1) == NULL) {
        fprintf(stderr, "Current working directory too large\n");
        return 1;
    }
    strcat(fullName, "/");
    strcat(fullName, game->ScriptFileName);
    scriptName = fullName;
#endif

    // Running Spring
    //ShellExecute(NULL,"open",game->ExecutableFileName,game->ScriptFileName,NULL,SW_SHOWNORMAL);
    processID=spawnl(P_NOWAIT,cKPgame::ExecutableFileName,game->ExecutableFileName,scriptName,NULL);

    // Check that Spring has run
    if (processID == -1) {
        perror("spawnl");
        processID = 0;
        return 1;
    }

    //Application->Terminate();
    return 0;
}