-
Notifications
You must be signed in to change notification settings - Fork 5
/
dlg.c
194 lines (189 loc) · 8.14 KB
/
dlg.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**********************************************************************
* ZDL!
* Copyright (C) 2005 BioHazard / Vectec Software
***********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
***********************************************************************
* dlg.c : Most of the important functions are here
**********************************************************************/
#include "zdl.h"
/////////////////////////////////////////////////
// Dlg_Launch : The most important function in ZDL
void Dlg_Launch(HWND dlg,char prompt){
int i=0,q=MAX_PATH*3,pw=0,pt=0;
char *cmd=0,*tmp=0;
// Make sure there is a port selected
if(!SendMessage(GetDlgItem(dlg,LST_PORT),CB_GETCOUNT,0,0)||!SendMessage(GetDlgItem(dlg,LST_IWAD),LB_GETCOUNT,0,0)){MessageBox(dlg,STR_NOITEMS,"Error!",MB_OK|MB_ICONEXCLAMATION);return;}
// Determine how long the cmd string has to be
for(i=0;pwad[i]&&i<MAX_PWAD;i++){q+=strlen(pwad[i])+4;} // Count the length of the PWADs
// Alloc the string
memset((tmp=malloc(MAX_PATH*sizeof(char))),0,MAX_PATH*sizeof(char));
memset((cmd=malloc(q*sizeof(char))),0,q*sizeof(char));
// Print the basic stuff
_snprintf(cmd,q,"\"%s\" -iwad \"%s\"%s%s",port[Cfg_GetSel(SendMessage(GetDlgItem(dlg,LST_PORT),CB_GETCURSEL,0,0),port)]->path,iwad[Cfg_GetSel(SendMessage(GetDlgItem(dlg,LST_IWAD),LB_GETCURSEL,0,0),iwad)]->path,(strlen(cfg.always))?(" "):(""),cfg.always);
// Warp and Skill
SendMessage(GetDlgItem(dlg,LST_WARP),WM_GETTEXT,MAX_PATH,(LPARAM)tmp);
if(stricmp(tmp,"NONE")){
strncat(cmd," +map ",q-strlen(cmd));
strncat(cmd,tmp,q-strlen(cmd));
_snprintf(&cmd[strlen(cmd)],q-strlen(cmd)," -skill %d",SendMessage(GetDlgItem(dlg,LST_SKILL),CB_GETCURSEL,0,0)+1);
}
// PWADs and DEHs
for(i=0;pwad[i]&&i<MAX_PWAD;i++){ // Count up each type of file
if(stricmp(strrchr(pwad[i],'.'),".deh")&&stricmp(strrchr(pwad[i],'.'),".bex")){pw++;}else{pt++;}
}
if(pw){ // Append PWADs
strncat(cmd," -file",q-strlen(cmd));
for(i=0;pwad[i]&&i<MAX_PWAD;i++){
if(stricmp(strrchr(pwad[i],'.'),".deh")&&stricmp(strrchr(pwad[i],'.'),".bex")){
_snprintf(&cmd[strlen(cmd)],q-strlen(cmd)," \"%s\"",pwad[i]);
}
}
}
if(pt){ // Append DEHs
strncat(cmd," -deh",q-strlen(cmd));
for(i=0;pwad[i]&&i<MAX_PWAD;i++){
if(!stricmp(strrchr(pwad[i],'.'),".deh")||!stricmp(strrchr(pwad[i],'.'),".bex")){
_snprintf(&cmd[strlen(cmd)],q-strlen(cmd)," \"%s\"",pwad[i]);
}
}
}
// Extra Args
if(SendMessage(GetDlgItem(dlg,EDT_EXTRA),WM_GETTEXTLENGTH,0,0)){
strncat(cmd," ",q-strlen(cmd));
SendMessage(GetDlgItem(dlg,EDT_EXTRA),WM_GETTEXT,q-strlen(cmd),(LPARAM)&cmd[strlen(cmd)]);
}
// Network stuff
if(SendMessage(GetDlgItem(dlg,LST_GAME),CB_GETCURSEL,0,0)){
if((i=SendMessage(GetDlgItem(dlg,LST_PLAYERS),CB_GETCURSEL,0,0))){ // Hosting
_snprintf(&cmd[strlen(cmd)],q-strlen(cmd)," -host %d",i);
// Deathmatch Flag
if(SendMessage(GetDlgItem(dlg,LST_GAME),CB_GETCURSEL,0,0)==2){
strncat(cmd," -deathmatch",q-strlen(cmd));
// Fragimit
if(SendMessage(GetDlgItem(dlg,EDT_FRAGS),WM_GETTEXTLENGTH,0,0)){
strncat(cmd," +fraglimit ",q-strlen(cmd));
SendMessage(GetDlgItem(dlg,EDT_FRAGS),WM_GETTEXT,q-strlen(cmd),(LPARAM)&cmd[strlen(cmd)]);
}
}
// DMF
if(SendMessage(GetDlgItem(dlg,EDT_DMF),WM_GETTEXTLENGTH,0,0)){
strncat(cmd," +dmflags ",q-strlen(cmd));
SendMessage(GetDlgItem(dlg,EDT_DMF),WM_GETTEXT,q-strlen(cmd),(LPARAM)&cmd[strlen(cmd)]);
}
// DMF2
if(SendMessage(GetDlgItem(dlg,EDT_DMF2),WM_GETTEXTLENGTH,0,0)){
strncat(cmd," +dmflags2 ",q-strlen(cmd));
SendMessage(GetDlgItem(dlg,EDT_DMF2),WM_GETTEXT,q-strlen(cmd),(LPARAM)&cmd[strlen(cmd)]);
}
}else{ // Joining
if(!SendMessage(GetDlgItem(dlg,EDT_HOST),WM_GETTEXTLENGTH,0,0)){MessageBox(dlg,"You need to type a host to join","Error!",MB_OK|MB_ICONHAND);free(cmd);return;}
strcat(cmd," -join ");
SendMessage(GetDlgItem(dlg,EDT_HOST),WM_GETTEXT,q-strlen(cmd),(LPARAM)&cmd[strlen(cmd)]);
}
}
if(prompt&&MessageBox(dlg,cmd,"Run this command-line?",MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDNO){goto exit;}
// Change to the port directory
if(strrchr(tmp,'\\')){
strncpy(tmp,port[Cfg_GetSel(SendMessage(GetDlgItem(dlg,LST_PORT),CB_GETCURSEL,0,0),port)]->path,MAX_PATH);
strrchr(tmp,'\\')[0]='\0';
chdir(tmp);
}
// GoooOOooooOOOOOoooOOO!
WinExec(cmd,SW_NORMAL);
exit:
free(cmd);free(tmp);
}
/////////////////////////////////////////////////
// Dlg_AddPWAD : Adds an item to the PWAD list
int Dlg_AddPWAD(HWND dlg,char *file){
int i=SendMessage(GetDlgItem(dlg,LST_PWAD),LB_GETCOUNT,0,0);
if(i>=MAX_PWAD||!strrchr(file,'.')||!stricmp(strrchr(file,'.'),".zdl")||!stricmp(strrchr(file,'.'),".ini")){return 0;}
memset((pwad[i]=malloc((MAX_PATH+1)*sizeof(char))),0,MAX_PATH*sizeof(char));
strncpy(pwad[i],file,MAX_PATH);
SendMessage(GetDlgItem(dlg,LST_PWAD),LB_ADDSTRING,0,(strrchr(file,'\\'))?((LPARAM)strrchr(file,'\\')+1):((LPARAM)file));
return 1;
}
/////////////////////////////////////////////////
// Dlg_ClearPWAD : Clears all the data from the PWAD list
void Dlg_ClearPWAD(HWND dlg){
int i=0;
SendMessage(GetDlgItem(dlg,LST_PWAD),LB_RESETCONTENT,0,0);
for(i=0;pwad[i]&&i<MAX_PWAD;i++){free(pwad[i]);pwad[i]=0;}
}
/////////////////////////////////////////////////
// Dlg_ClearAll : Clears all the data from the dialog
void Dlg_ClearAll(HWND dlg){
int i=0;
int box[2][5]={{LST_PORT,LST_GAME,LST_PLAYERS},{EDT_EXTRA,EDT_HOST,EDT_FRAGS,EDT_DMF,EDT_DMF2}};
Dlg_ClearPWAD(dlg);
SendMessage(GetDlgItem(dlg,LST_IWAD),LB_SETCURSEL,0,0);
SendMessage(GetDlgItem(dlg,LST_WARP),WM_SETTEXT,0,(LPARAM)"NONE");
SendMessage(GetDlgItem(dlg,LST_SKILL),CB_SETCURSEL,2,0);
for(i=0;i<3;i++){SendMessage(GetDlgItem(dlg,box[0][i]),CB_SETCURSEL,0,0);}
for(i=0;i<5;i++){SendMessage(GetDlgItem(dlg,box[1][i]),WM_SETTEXT,0,(LPARAM)"");}
}
/////////////////////////////////////////////////
// Dlg_Quit : Saves data and exits ZDL
void Dlg_Quit(HWND dlg,char save){
int i=0;
FILE *fptr=0;
// Save the config
if(save){
if(!cfg.always[0]&&!cfg.launch&&!port[0]&&!iwad[0]){ // Do we even need to save at all?
Dlg_ClearPWAD(dlg);
EndDialog(dlg,0);return;
}
if(!(fptr=fopen(cfg.ini,"w"))){MessageBox(HWND_DESKTOP,"The INI file could not be opened for writing!\nYour settings have not been saved!","Error!",MB_OK|MB_ICONHAND);return;}
if(cfg.always[0]||cfg.launch){
fputs("[zdl.general]\n",fptr);
if(cfg.always[0]){fprintf(fptr,"alwaysadd=%s\n",cfg.always);}
if(cfg.launch){fputs("zdllaunch=1\n",fptr);}
}
if(port[0]){ // Write ports
fprintf(fptr,"[zdl.ports]\n");
for(i=0;port[i]&&i<MAX_ITEM;i++){fprintf(fptr,"p%dn=%s\np%df=%s\n",i,port[i]->name,i,port[i]->path);}
}
if(iwad[0]){ // Write IWADs
fprintf(fptr,"[zdl.iwads]\n");
for(i=0;iwad[i]&&i<MAX_ITEM;i++){fprintf(fptr,"i%dn=%s\ni%df=%s\n",i,iwad[i]->name,i,iwad[i]->path);}
}
Cfg_WriteSave(dlg,fptr);
fclose(fptr);
}
// Delete port and IWAD data
for(i=0;port[i]&&i<MAX_ITEM;i++){free(port[i]);}
for(i=0;iwad[i]&&i<MAX_ITEM;i++){free(iwad[i]);}
Dlg_ClearPWAD(dlg);
EndDialog(dlg,0);
}
/////////////////////////////////////////////////
// PopulateWarp : Reads through the wad and finds valid maps
void Dlg_PopulateWarp(HWND dlg,char *file){
unsigned int i=0;
char temp[9]={0};
WADHEAD header;
LUMPHEAD lump[2];
FILE *fptr=0;
SendMessage(GetDlgItem(dlg,LST_WARP),CB_RESETCONTENT,0,0);
SendMessage(GetDlgItem(dlg,LST_WARP),CB_ADDSTRING,0,(LPARAM)"NONE");
if((fptr=fopen(file,"rb"))){
fread(&header,1,sizeof(WADHEAD),fptr);
fseek(fptr,header.dir,SEEK_SET);
for(i=0;i<header.lumps;i++){
fread(&lump,2,sizeof(LUMPHEAD),fptr);
if(!stricmp(lump[1].name,"THINGS")){
strncpy(temp,lump[0].name,8);
SendMessage(GetDlgItem(dlg,LST_WARP),CB_ADDSTRING,0,(LPARAM)temp);
}else{fseek(fptr,sizeof(LUMPHEAD)-(sizeof(LUMPHEAD)*2),SEEK_CUR);}
}
SendMessage(GetDlgItem(dlg,LST_WARP),CB_SETCURSEL,0,0);
fclose(fptr);
}
}