-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabCommLine.cpp
654 lines (525 loc) · 15.3 KB
/
TabCommLine.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// TabCommLine.cpp : implementation file
//
#include <io.h>
#include <direct.h>
#include "stdafx.h"
#include "Launcher.h"
#include "TabCommLine.h"
#include "win32func.h"
#include "misc.h"
#include "launcher_settings.h"
#include "metadata.h"
#include "mod_settings.h"
#include "iniparser/iniparser.h"
#include "iniparser/dictionary.h"
#include "LauncherDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int num_eflags = 0;
int num_params = 0;
EasyFlag *easy_flags = NULL;
Flag *exe_params = NULL;
bool *flag_states = NULL;
#define MAX_CMDLINE_SIZE 3000
#define MAX_CUSTOM_PARAM_SIZE 1200
char command_line[MAX_CMDLINE_SIZE] = "";
/////////////////////////////////////////////////////////////////////////////
// CTabCommLine dialog
CTabCommLine::CTabCommLine(CWnd* pParent /*=NULL*/)
: CDialog(CTabCommLine::IDD, pParent)
{
m_flag_gen_in_process = false;
//{{AFX_DATA_INIT(CTabCommLine)
//}}AFX_DATA_INIT
}
CTabCommLine::~CTabCommLine()
{
if(easy_flags) free(easy_flags);
if(exe_params) free(exe_params);
if(flag_states) free(flag_states);
}
void CTabCommLine::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTabCommLine)
DDX_Control(pDX, IDC_FLAG_TYPE, m_flag_type_list);
DDX_Control(pDX, IDC_FLAG_SETUP, m_easy_flag);
DDX_Control(pDX, IDC_FLAG_LIST, m_flag_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTabCommLine, CDialog)
//{{AFX_MSG_MAP(CTabCommLine)
ON_EN_CHANGE(IDC_CUSTOM_PARAM, OnChangeCustomParam)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_FLAG_LIST, OnItemchangedFlagList)
ON_CBN_SELCHANGE(IDC_FLAG_SETUP, OnSelchangeFlagSetup)
ON_NOTIFY(NM_DBLCLK, IDC_FLAG_LIST, OnDblclkFlagList)
ON_CBN_SELCHANGE(IDC_FLAG_TYPE, OnSelchangeFlagType)
ON_BN_CLICKED(IDC_SETTINGS_NORMAL, OnSettingsNormal)
ON_BN_CLICKED(IDC_SETTINGS_MOD, OnSettingsMod)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTabCommLine message handlers
BOOL CTabCommLine::OnInitDialog()
{
CDialog::OnInitDialog();
// Make sure its a checkbox and insert a column
m_flag_list.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
m_flag_list.InsertColumn(0, "Flag", LVCFMT_LEFT, 200);
return TRUE;
}
/**
* @return CString - string holding command line (may be empty)
*/
CString CTabCommLine::GetCommandLine()
{
return CString(command_line);
}
/**
* Populate the command line variable, and update the dialog window.
*/
void CTabCommLine::UpdateCommandLine()
{
char *mod_params = NULL;
char *standard_params = NULL;
char *custom_params = NULL;
bool enable_reset_to_normal = false; // TODO: what is "normal"?
bool enable_reset_to_mod = false;
strcpy(command_line, "");
// add the EXE to the command line
if (LauncherSettings::is_exe_path_valid())
{
strcat(command_line, LauncherSettings::get_exe_filepath());
}
else
{
goto done_with_command_line;
}
// for non-custom EXEs, that's all we can do
if (LauncherSettings::get_exe_type() != EXE_TYPE_CUSTOM)
{
goto done_with_command_line;
}
// add params specified by mod
mod_params = ModSettings::get_mod_parameters();
if (strlen(mod_params) > 0)
{
strcat(command_line, mod_params);
strcat(command_line, " ");
enable_reset_to_mod = true;
}
// add params specified by checkbox
standard_params = ModSettings::get_standard_parameters();
if (strlen(standard_params) > 0)
{
strcat(command_line, standard_params);
strcat(command_line, " ");
}
// add params entered by user
custom_params = ModSettings::get_custom_parameters();
if (strlen(custom_params) > 0)
{
strcat(command_line, custom_params);
strcat(command_line, " ");
}
trim(command_line);
done_with_command_line:
// draw the dialog window
GetDlgItem(IDC_COMM_LINE_PATH)->SetWindowText(command_line);
GetDlgItem(IDC_SETTINGS_NORMAL)->EnableWindow(enable_reset_to_normal);
GetDlgItem(IDC_SETTINGS_MOD)->EnableWindow(enable_reset_to_mod);
CLauncherDlg::Redraw();
}
/**
* When the flag states array has been changed, call this to update the GUI.
*/
void CTabCommLine::RefreshFlagList()
{
int num_flags = m_flag_list.GetItemCount();
for(int i = 0; i < num_flags; i++)
{
int index = m_flag_list.GetItemData(i);
m_flag_list.SetCheck(i, flag_states[index]);
}
CLauncherDlg::Redraw();
}
/**
* This is called when a tick box is ticked or unticked on the parameter list
*/
void CTabCommLine::OnItemchangedFlagList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0;
if (pNMListView->iItem < 0) return;
if (m_flag_gen_in_process) return;
int index = m_flag_list.GetItemData(pNMListView->iItem);
flag_states[index] = (m_flag_list.GetCheck(pNMListView->iItem) != 0);
// build list of params
char standard_params[MAX_CMDLINE_SIZE];
strcpy(standard_params, "");
for (int i = 0; i < num_params; i++)
{
if (flag_states[i])
{
strcat(standard_params, exe_params[i].name);
strcat(standard_params, " ");
}
}
trim(standard_params);
ModSettings::set_standard_parameters(standard_params);
UpdateCommandLine();
}
/**
* This is called when something is typed in the custom parameter edit box
*/
void CTabCommLine::OnChangeCustomParam()
{
// get trimmed list of params
CString custom_params;
GetDlgItem(IDC_CUSTOM_PARAM)->GetWindowText(custom_params);
custom_params.TrimLeft();
custom_params.TrimRight();
ModSettings::set_custom_parameters(custom_params);
UpdateCommandLine();
}
/**
* @return - EXE_TYPE_NONE, EXE_TYPE_CUSTOM or if recognised exe it returns the flag list.
*/
int CTabCommLine::GetEXEFlags()
{
if(LauncherSettings::get_exe_type() == EXE_TYPE_NONE)
return 0;
return exe_types[LauncherSettings::get_exe_type()].flags;
}
/**
* @return int - type of exe (one of following)
*
* EXE_TYPE_NONE
* EXE_TYPE_CUSTOM
* An integer to reference the 'exe_types' array with
*/
void CTabCommLine::SelectRegPathAndExeType()
{
if ( !LauncherSettings::is_exe_path_valid() ||
!file_exists(LauncherSettings::get_exe_filepath()) )
{
LauncherSettings::set_reg_path("", EXE_TYPE_NONE);
return;
}
int exe_type = EXE_TYPE_CUSTOM;
// Use filename and size to determine official builds
for (int i = 0; i < MAX_EXE_TYPES; i++)
{
// Confirm this by name
if (stricmp(exe_types[i].exe_name, LauncherSettings::get_exe_nameonly()) == 0)
{
exe_type = i;
break;
}
}
char reg_path[MAX_PATH];
if (exe_type < MAX_EXE_TYPES)
{
sprintf(reg_path, "SOFTWARE\\%s\\%s",
exe_types[exe_type].company,
exe_types[exe_type].regname);
}
else
{
sprintf(reg_path, "SOFTWARE\\%s\\%s",
exe_types[EXE_TYPE_FS2].company,
exe_types[EXE_TYPE_FS2].regname);
}
LauncherSettings::set_reg_path(reg_path, exe_type);
}
void CTabCommLine::ConstructFlagList()
{
int k;
// If this is a retail FS2 exe skip all this
if (LauncherSettings::get_exe_type() != EXE_TYPE_CUSTOM)
{
m_easy_flag.ResetContent();
m_easy_flag.EnableWindow(FALSE);
ConstructFlagListRetail();
ConstructFlagListInternal();
return;
}
m_flag_type_list.EnableWindow(TRUE);
m_flag_list.EnableWindow(TRUE);
m_easy_flag.EnableWindow(TRUE);
// Delete and generate new flag file
char flag_file[MAX_PATH];
sprintf(flag_file, "%s\\flags.lch",LauncherSettings::get_exe_pathonly());
DeleteFile(flag_file);
if ( !run_file((LPTSTR) LauncherSettings::get_exe_nameonly(), (LPTSTR) LauncherSettings::get_exe_pathonly()," -get_flags", true) )
{
MessageBox("Unable to query FreeSpace Open for launcher flag information! Is the EXE present?");
return;
}
FILE *fp = fopen(flag_file, "r");
int focount = 2000;
while (fp == NULL && focount)
{
fp = fopen(flag_file, "r");
focount--;
}
if (fp == NULL)
{
MessageBox("Failed to read launcher flag file", "Fatal Error", MB_OK);
return;
}
int eflags_struct_size;
int params_struct_size;
fread(&eflags_struct_size, sizeof(int), 1, fp);
fread(¶ms_struct_size, sizeof(int), 1, fp);
if (eflags_struct_size != sizeof(EasyFlag) ||
params_struct_size != sizeof(Flag))
{
MessageBox("Launcher and fs2_open versions do not work with each other", "Fatal Error", MB_OK);
fclose(fp);
DeleteFile(flag_file);
return;
}
if (easy_flags)
free(easy_flags);
if (exe_params)
free(exe_params);
if (flag_states)
free(flag_states);
fread(&num_eflags, sizeof(int), 1, fp);
easy_flags = (EasyFlag *) malloc(sizeof(EasyFlag) * num_eflags);
if (easy_flags == NULL)
{
MessageBox("Failed to allocate enough memory for easy_flags", "Fatal Error", MB_OK);
fclose(fp);
DeleteFile(flag_file);
return;
}
fread(easy_flags, sizeof(EasyFlag) * num_eflags, 1, fp);
fread(&num_params, sizeof(int), 1, fp);
exe_params = (Flag *) malloc(sizeof(Flag) * num_params);
flag_states = (bool *) malloc(sizeof(bool) * num_params);
memset(flag_states, 0, sizeof(bool) * num_params);
if (exe_params == NULL || flag_states == NULL)
{
MessageBox("Failed to allocate enough memory for exe_params", "Fatal Error", MB_OK);
fclose(fp);
DeleteFile(flag_file);
return;
}
fread(exe_params, sizeof(Flag) * num_params, 1, fp);
// hack for OpenAL check
if ( (filelength(fileno(fp)) - ftell(fp)) == 1 ) {
LauncherSettings::set_openal_build(true);
}
fclose(fp);
// go ahead and delete the file now, it's just taking up space at this point
DeleteFile(flag_file);
// Setup Easy Flags
m_easy_flag.ResetContent();
for (k = 0; k < num_eflags; k++)
{
m_easy_flag.InsertString(k,easy_flags[k].name);
}
// Setup Flag Types
m_flag_type_list.ResetContent();
int count = 0;
char last_word[FLAG_TYPE_LEN] = "";
for (k = 0; k < num_params; k++)
{
if (strcmp(exe_params[k].type, last_word) != 0)
{
m_flag_type_list.InsertString(count, exe_params[k].type);
strcpy(last_word, exe_params[k].type);
count++;
if (count > 19)
{
MessageBox("Flag type count is 20 or more, please report to coder","Error");
count = 19;
break;
}
}
}
m_flag_type_list.SetCurSel(0);
ConstructFlagListInternal();
}
void CTabCommLine::ConstructFlagListRetail()
{
int i, count = 0;
char last_word[FLAG_TYPE_LEN] = "";
if ( exe_types[LauncherSettings::get_exe_type()].flags & FLAG_FS1 )
num_params = Num_retail_params_FS1;
else
num_params = Num_retail_params_FS2;
exe_params = (Flag *) malloc(sizeof(Flag) * num_params);
flag_states = (bool *) malloc(sizeof(bool) * num_params);
if ( (exe_params == NULL) || (flag_states == NULL) )
{
MessageBox("Memory allocation failure!");
return;
}
if ( exe_types[LauncherSettings::get_exe_type()].flags & FLAG_FS1 )
memcpy( exe_params, retail_params_FS1, sizeof(Flag) * num_params );
else
memcpy( exe_params, retail_params_FS2, sizeof(Flag) * num_params );
memset( flag_states, 0, sizeof(bool) * num_params );
// setup flag types
m_flag_type_list.ResetContent();
m_flag_type_list.EnableWindow(TRUE);
for (i = 0; i < num_params; i++) {
if ( strcmp(exe_params[i].type, last_word) ) {
m_flag_type_list.InsertString(count, exe_params[i].type);
strcpy(last_word, exe_params[i].type);
count++;
}
}
m_flag_type_list.SetCurSel(0);
}
void CTabCommLine::ConstructFlagListInternal()
{
int type_index = m_flag_type_list.GetCurSel();
char type_text[FLAG_TYPE_LEN];
m_flag_type_list.GetWindowText(type_text, FLAG_TYPE_LEN);
// Setup flags
m_flag_list.DeleteAllItems();
m_flag_list.EnableWindow(TRUE);
int exe_flags = 0;
// If we are not sure what this exe is give access to all parameters
if(LauncherSettings::get_exe_type() != EXE_TYPE_NONE)
{
exe_flags = exe_types[LauncherSettings::get_exe_type()].flags;
}
m_flag_gen_in_process = true;
// For each parameter
for(int i = 0, j = 0; i < num_params; i++)
{
if(strcmp(exe_params[i].type, type_text) != 0)
continue;
if(exe_params[i].fso_only && !(exe_types[LauncherSettings::get_exe_type()].flags & FLAG_FS2OPEN))
continue;
// Insert this item but keep a record of the index number
// The insert item index will not stay as is and will mess up if there are gaps (unshown params)
if(strlen(exe_params[i].desc) > 0)
m_flag_list.InsertItem(j, exe_params[i].desc, 0);
else
m_flag_list.InsertItem(j, exe_params[i].name, 0);
m_flag_list.SetItemData(j, i);
j++;
}
m_flag_gen_in_process = false;
}
void CTabCommLine::LoadSettings()
{
ModSettings::load_user();
PopulateFlagStates();
RefreshFlagList();
GetDlgItem(IDC_CUSTOM_PARAM)->SetWindowText(ModSettings::get_custom_parameters());
UpdateCommandLine();
}
void CTabCommLine::PopulateFlagStates()
{
char *new_standard_params = strdup(ModSettings::get_standard_parameters());
char *new_custom_params = strdup(ModSettings::get_custom_parameters());
// tokenize the standard parameters and copy them to
for (int i = 0; i < num_params; i++)
{
exe_params[i].name,
}
// Seperate custom flags from standard one
for(int i = 0; i < num_params; i++) {
// while going through the cmdline make sure to grab only the option that we
// are looking for, but if one similar then keep searching for the exact match
do {
found_str = strstr(&custom_param[0] + get_new_offset, exe_params[i].name);
if (found_str && (*(found_str + strlen(exe_params[i].name))) && (*(found_str + strlen(exe_params[i].name)) != ' ') ) {
// the new offset should be our current location + the length of the current option
get_new_offset = (strlen(custom_param) - strlen(found_str) + strlen(exe_params[i].name));
} else {
get_new_offset = 0;
}
} while ( get_new_offset );
if (found_str == NULL)
continue;
flag_states[i] = true;
if (end_string_here == NULL)
end_string_here = found_str;
}
// Cut the standard options out of the custom string
if (end_string_here != NULL) {
if (end_string_here > custom_param && end_string_here[-1] == ' ') {
end_string_here[-1] = '\0';
} else {
end_string_here[0] = '\0';
}
}
/**
* Save command line to settings.ini file.
*/
void CTabCommLine::SaveSettings()
{
ModSettings::save_user();
}
// User has selected new easy select type
void CTabCommLine::OnSelchangeFlagSetup()
{
int easy_flag = m_easy_flag.GetCurSel();
// Custom setting, leave it alone
if(easy_flag == 0) return;
for(int i = 0; i < num_params; i++)
{
if(exe_params[i].on_flags & (1 << easy_flag))
flag_states[i] = true;
if(exe_params[i].off_flags & (1 << easy_flag))
flag_states[i] = false;
}
RefreshFlagList();
UpdateCommandLine();
}
// User has selected a new flag group type
void CTabCommLine::OnSelchangeFlagType()
{
ConstructFlagListInternal();
RefreshFlagList();
}
// User has double clicked flag
void CTabCommLine::OnDblclkFlagList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0;
if(exe_params == NULL) return;
if(pNMListView->iItem < 0) return;
int index = m_flag_list.GetItemData(pNMListView->iItem);
if(index < 0 || index > num_params)
{
MessageBox("Bad m_flag_list index value","Error");
}
if(strlen(exe_params[index].web_url) > 0)
{
if(exe_params[index].web_url[0] == 'h' &&
exe_params[index].web_url[1] == 't' &&
exe_params[index].web_url[2] == 't' &&
exe_params[index].web_url[3] == 'p')
{
open_web_page(exe_params[index].web_url);
}
else
{
MessageBox(exe_params[index].web_url,"Info");
}
}
else
MessageBox("No online help for this feature","Sorry");
}
void CTabCommLine::OnSettingsNormal()
{
UpdateCommandLine();
}
void CTabCommLine::OnSettingsMod()
{
ModSettings::set_standard_parameters("");
ModSettings::set_custom_parameters("");
UpdateCommandLine();
}