-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
1673 lines (1483 loc) · 62 KB
/
main.cpp
File metadata and controls
1673 lines (1483 loc) · 62 KB
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstddef>
#include <cstring>
#include <functional>
#include <filesystem>
#include <string_view>
#include <windows.h>
#include <shellapi.h>
#include <vector>
#include <memory>
#include <iomanip>
#include <sstream>
#include <string>
#include <algorithm>
#include <set>
#include <cmath>
#include <common/version/version.h>
#include <common/config/BuildConfig.h>
#include <common/utils/os-utils.h>
#include <xlog/xlog.h>
#include <xlog/ConsoleAppender.h>
#include <xlog/FileAppender.h>
#include <xlog/Win32Appender.h>
#include <patch_common/MemUtils.h>
#include <patch_common/CallHook.h>
#include <patch_common/FunHook.h>
#include <patch_common/AsmWriter.h>
#include <patch_common/CodeInjection.h>
#include <crash_handler_stub.h>
#include "../game_patch/rf/os/array.h"
#include "exports.h"
#include "resources.h"
#include "mfc_types.h"
#include "vtypes.h"
#include "level.h"
#include "event.h"
#include "mesh.h"
#include "alpine_obj.h"
#include "geometry.h"
#include "textures.h"
#include "meshes.h"
#define LAUNCHER_FILENAME "AlpineFactionLauncher.exe"
HMODULE g_module;
static std::string g_launcher_pathname;
static bool g_is_play_in_multi = false;
static std::string g_red_cmdline;
constexpr size_t max_texture_name_len = 31;
bool g_skip_wnd_set_text = false;
static bool g_is_saving_af_version = true;
static bool g_skip_legacy_level_warning = false;
static int g_current_level_version = -1;
static const auto g_editor_app = reinterpret_cast<std::byte*>(0x006F9DA0);
void *GetMainFrame()
{
return struct_field_ref<void*>(g_editor_app, 0xC8);
}
void *GetLogDlg()
{
return struct_field_ref<void*>(GetMainFrame(), 692);
}
HWND GetMainFrameHandle()
{
auto* main_frame = struct_field_ref<CWnd*>(g_editor_app, 0xC8);
return WndToHandle(main_frame);
}
bool get_is_saving_af_version()
{
return g_is_saving_af_version;
}
int get_level_rfl_version()
{
return g_current_level_version;
}
void set_initial_level_rfl_version()
{
g_current_level_version = MAXIMUM_RFL_VERSION;
}
void OpenLevel(const char* level_path)
{
void* doc_manager = *reinterpret_cast<void**>(g_editor_app + 0x80);
void** doc_manager_vtbl = *reinterpret_cast<void***>(doc_manager);
using CDocManager_OpenDocumentFile_Ptr = int(__thiscall*)(void* this_, LPCSTR path);
auto DocManager_OpenDocumentFile = reinterpret_cast<CDocManager_OpenDocumentFile_Ptr>(doc_manager_vtbl[7]);
DocManager_OpenDocumentFile(doc_manager, level_path);
}
CodeInjection CMainFrame_PreCreateWindow_injection{
0x0044713C,
[](auto& regs) {
auto& cs = addr_as_ref<CREATESTRUCTA>(regs.eax);
cs.style |= WS_MAXIMIZEBOX|WS_THICKFRAME;
cs.dwExStyle |= WS_EX_ACCEPTFILES;
},
};
CodeInjection CEditorApp_InitInstance_additional_file_paths_injection{
0x0048290D,
[]() {
meshes_init_paths();
},
};
CodeInjection CEditorApp_InitInstance_open_level_injection{
0x00482BF1,
[]() {
static auto& argv = addr_as_ref<char**>(0x01DBF8E4);
static auto& argc = addr_as_ref<int>(0x01DBF8E0);
const char* level_param = nullptr;
for (int i = 1; i < argc; ++i) {
xlog::trace("argv[{}] = {}", i, argv[i]);
std::string_view arg = argv[i];
if (arg == "-level") {
++i;
if (i < argc) {
level_param = argv[i];
}
}
}
if (level_param) {
OpenLevel(level_param);
}
},
};
// ===== Brush "Is Geoable" support =====
static int compute_geoable_state_from_selected()
{
auto* level = CDedLevel::Get();
if (!level) return BST_UNCHECKED;
auto& props = level->GetAlpineLevelProperties();
BrushNode* node = level->brush_list;
if (!node) return BST_UNCHECKED;
int num_selected = 0;
int num_geoable = 0;
do {
if (node->state == BRUSH_STATE_SELECTED) {
num_selected++;
if (std::find(props.geoable_brush_uids.begin(),
props.geoable_brush_uids.end(), node->uid)
!= props.geoable_brush_uids.end()) {
num_geoable++;
}
}
node = node->next;
} while (node != level->brush_list);
if (num_selected == 0 || num_geoable == 0) return BST_UNCHECKED;
if (num_geoable == num_selected) return BST_CHECKED;
return BST_INDETERMINATE;
}
static void apply_geoable_to_selected_brushes(int new_state)
{
if (new_state == BST_INDETERMINATE) return;
auto* level = CDedLevel::Get();
if (!level) return;
auto& props = level->GetAlpineLevelProperties();
BrushNode* node = level->brush_list;
if (!node) return;
do {
if (node->state == BRUSH_STATE_SELECTED) {
auto it = std::find(props.geoable_brush_uids.begin(),
props.geoable_brush_uids.end(), node->uid);
if (new_state == BST_CHECKED) {
if (it == props.geoable_brush_uids.end()) {
props.geoable_brush_uids.push_back(node->uid);
}
} else {
if (it != props.geoable_brush_uids.end()) {
props.geoable_brush_uids.erase(it);
}
}
}
node = node->next;
} while (node != level->brush_list);
}
// Returns true if at least one selected brush is a breakable detail brush (is_detail && life != -1)
static bool selection_has_breakable_detail()
{
auto* level = CDedLevel::Get();
if (!level) return false;
BrushNode* node = level->brush_list;
if (!node) return false;
do {
if (node->state == BRUSH_STATE_SELECTED && node->is_detail && node->life != -1)
return true;
node = node->next;
} while (node != level->brush_list);
return false;
}
// Returns material index for selected breakable brushes, or -1 if mixed
static int compute_material_from_selected()
{
auto* level = CDedLevel::Get();
if (!level) return 0;
auto& props = level->GetAlpineLevelProperties();
BrushNode* node = level->brush_list;
if (!node) return 0;
int result = -2; // not set yet
do {
if (node->state == BRUSH_STATE_SELECTED && node->is_detail && node->life != -1) {
uint8_t mat = 0; // default = Glass
auto it = std::find(props.breakable_brush_uids.begin(),
props.breakable_brush_uids.end(), node->uid);
if (it != props.breakable_brush_uids.end()) {
auto idx = std::distance(props.breakable_brush_uids.begin(), it);
mat = props.breakable_materials[idx] & 0x7F; // mask out flags (bit 7 = no_debris)
}
if (result == -2) {
result = mat;
} else if (result != mat) {
return -1; // mixed
}
}
node = node->next;
} while (node != level->brush_list);
return (result == -2) ? 0 : result;
}
static void apply_material_to_selected_brushes(uint8_t mat)
{
auto* level = CDedLevel::Get();
if (!level) return;
auto& props = level->GetAlpineLevelProperties();
BrushNode* node = level->brush_list;
if (!node) return;
do {
if (node->state == BRUSH_STATE_SELECTED && node->is_detail && node->life != -1) {
auto it = std::find(props.breakable_brush_uids.begin(),
props.breakable_brush_uids.end(), node->uid);
if (mat == 0) {
// Glass = default, remove from list (also clears no_debris flag)
if (it != props.breakable_brush_uids.end()) {
auto idx = std::distance(props.breakable_brush_uids.begin(), it);
props.breakable_brush_uids.erase(it);
props.breakable_room_uids.erase(props.breakable_room_uids.begin() + idx);
props.breakable_materials.erase(props.breakable_materials.begin() + idx);
xlog::info("[Material] brush uid={} set to Glass (removed from list)", node->uid);
}
} else {
// Non-glass: add or update (preserve flags in high bit)
if (it != props.breakable_brush_uids.end()) {
auto idx = std::distance(props.breakable_brush_uids.begin(), it);
uint8_t flags = props.breakable_materials[idx] & 0x80;
props.breakable_materials[idx] = mat | flags;
xlog::info("[Material] brush uid={} updated material to {}", node->uid, mat);
} else {
props.breakable_brush_uids.push_back(node->uid);
props.breakable_room_uids.push_back(0); // filled at save time
props.breakable_materials.push_back(mat);
xlog::info("[Material] brush uid={} added with material {} (total breakable={})",
node->uid, mat, props.breakable_brush_uids.size());
}
}
}
node = node->next;
} while (node != level->brush_list);
}
static void init_material_combo(HWND hdlg)
{
HWND combo = GetDlgItem(hdlg, IDC_MATERIAL_COMBO);
HWND label = GetDlgItem(hdlg, IDC_MATERIAL_LABEL);
if (!combo) return;
// Reset content
SendMessageA(combo, CB_RESETCONTENT, 0, 0);
SendMessageA(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>("Glass"));
SendMessageA(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>("Rock"));
SendMessageA(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>("Wood"));
SendMessageA(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>("Metal"));
SendMessageA(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>("Cement"));
SendMessageA(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>("Ice"));
bool has_breakable = selection_has_breakable_detail();
EnableWindow(combo, has_breakable ? TRUE : FALSE);
if (label) EnableWindow(label, has_breakable ? TRUE : FALSE);
if (has_breakable) {
int mat = compute_material_from_selected();
if (mat >= 0) {
SendMessageA(combo, CB_SETCURSEL, mat, 0);
} else {
// Mixed — don't select anything
SendMessageA(combo, CB_SETCURSEL, static_cast<WPARAM>(-1), 0);
}
} else {
SendMessageA(combo, CB_SETCURSEL, 0, 0); // show Glass as default
}
}
// ===== Brush "No Debris" support =====
// Returns BST_CHECKED/UNCHECKED/INDETERMINATE for the no_debris flag on selected breakable brushes
static int compute_no_debris_state_from_selected()
{
auto* level = CDedLevel::Get();
if (!level) return BST_UNCHECKED;
auto& props = level->GetAlpineLevelProperties();
BrushNode* node = level->brush_list;
if (!node) return BST_UNCHECKED;
int num_checked = 0;
int num_applicable = 0;
do {
if (node->state == BRUSH_STATE_SELECTED && node->is_detail && node->life != -1) {
auto it = std::find(props.breakable_brush_uids.begin(),
props.breakable_brush_uids.end(), node->uid);
if (it != props.breakable_brush_uids.end()) {
auto idx = std::distance(props.breakable_brush_uids.begin(), it);
uint8_t mat = props.breakable_materials[idx] & 0x7F;
if (mat > 0) { // non-glass only
num_applicable++;
if (props.breakable_materials[idx] & 0x80)
num_checked++;
}
}
}
node = node->next;
} while (node != level->brush_list);
if (num_applicable == 0) return BST_UNCHECKED;
if (num_checked == num_applicable) return BST_CHECKED;
if (num_checked == 0) return BST_UNCHECKED;
return BST_INDETERMINATE;
}
static void apply_no_debris_to_selected_brushes(int new_state)
{
if (new_state == BST_INDETERMINATE) return;
auto* level = CDedLevel::Get();
if (!level) return;
auto& props = level->GetAlpineLevelProperties();
BrushNode* node = level->brush_list;
if (!node) return;
do {
if (node->state == BRUSH_STATE_SELECTED && node->is_detail && node->life != -1) {
auto it = std::find(props.breakable_brush_uids.begin(),
props.breakable_brush_uids.end(), node->uid);
if (it != props.breakable_brush_uids.end()) {
auto idx = std::distance(props.breakable_brush_uids.begin(), it);
if (new_state == BST_CHECKED) {
props.breakable_materials[idx] |= 0x80;
} else {
props.breakable_materials[idx] &= 0x7F;
}
}
}
node = node->next;
} while (node != level->brush_list);
}
static void init_no_debris_checkbox(HWND hdlg)
{
HWND ctrl = GetDlgItem(hdlg, IDC_NO_DEBRIS);
if (!ctrl) return;
bool has_breakable = selection_has_breakable_detail();
int mat = has_breakable ? compute_material_from_selected() : 0;
bool enabled = has_breakable && mat > 0; // non-glass breakable only
EnableWindow(ctrl, enabled ? TRUE : FALSE);
if (enabled) {
CheckDlgButton(hdlg, IDC_NO_DEBRIS, compute_no_debris_state_from_selected());
} else {
CheckDlgButton(hdlg, IDC_NO_DEBRIS, BST_UNCHECKED);
}
}
// Dialog 205 (brush panel) subclass for Is Geoable click handling
static WNDPROC g_brush_panel_orig_wndproc = nullptr;
static HWND g_brush_panel_hwnd = nullptr;
static LRESULT CALLBACK BrushPanelSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_COMMAND && LOWORD(wParam) == IDC_BRUSH_MIRROR) {
handle_brush_mirror();
return 0;
}
if (msg == WM_COMMAND && LOWORD(wParam) == IDC_BRUSH_CONVERT) {
handle_brush_convert();
return 0;
}
if (msg == WM_COMMAND && LOWORD(wParam) == IDC_IS_GEOABLE) {
int state = IsDlgButtonChecked(hwnd, IDC_IS_GEOABLE);
// Normalize: skip indeterminate state, treat as unchecked
if (state == BST_INDETERMINATE) {
state = BST_UNCHECKED;
CheckDlgButton(hwnd, IDC_IS_GEOABLE, BST_UNCHECKED);
}
apply_geoable_to_selected_brushes(state);
// Auto-check Is Detail when Is Geoable is checked
if (state == BST_CHECKED && IsDlgButtonChecked(hwnd, 1215) != BST_CHECKED) {
CheckDlgButton(hwnd, 1215, BST_CHECKED);
// Trigger stock handler to apply detail flag to selected brushes
CallWindowProcA(g_brush_panel_orig_wndproc, hwnd, WM_COMMAND,
MAKEWPARAM(1215, BN_CLICKED),
reinterpret_cast<LPARAM>(GetDlgItem(hwnd, 1215)));
}
}
if (msg == WM_COMMAND && LOWORD(wParam) == IDC_MATERIAL_COMBO && HIWORD(wParam) == CBN_SELCHANGE) {
int sel = static_cast<int>(SendDlgItemMessageA(hwnd, IDC_MATERIAL_COMBO, CB_GETCURSEL, 0, 0));
if (sel >= 0) {
apply_material_to_selected_brushes(static_cast<uint8_t>(sel));
}
// Refresh no_debris checkbox enable state when material changes
init_no_debris_checkbox(hwnd);
}
if (msg == WM_COMMAND && LOWORD(wParam) == IDC_NO_DEBRIS) {
int state = IsDlgButtonChecked(hwnd, IDC_NO_DEBRIS);
apply_no_debris_to_selected_brushes(state);
}
if (msg == WM_COMMAND && LOWORD(wParam) == 1215) {
// Auto-uncheck Is Geoable when Is Detail is no longer checked
if (IsDlgButtonChecked(hwnd, 1215) != BST_CHECKED) {
CheckDlgButton(hwnd, IDC_IS_GEOABLE, BST_UNCHECKED);
apply_geoable_to_selected_brushes(BST_UNCHECKED);
}
// Refresh material combo and no_debris enable state when detail flag changes
init_material_combo(hwnd);
init_no_debris_checkbox(hwnd);
}
return CallWindowProcA(g_brush_panel_orig_wndproc, hwnd, msg, wParam, lParam);
}
// Dialog 270 (brush properties popup) hook and subclass
static HHOOK g_brush_props_msg_hook = nullptr;
static WNDPROC g_brush_props_orig_wndproc = nullptr;
static LRESULT CALLBACK BrushPropsSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_COMMAND && LOWORD(wParam) == IDOK) {
int state = IsDlgButtonChecked(hwnd, IDC_IS_GEOABLE);
apply_geoable_to_selected_brushes(state);
// Apply material from combo
int sel = static_cast<int>(SendDlgItemMessageA(hwnd, IDC_MATERIAL_COMBO, CB_GETCURSEL, 0, 0));
if (sel >= 0) {
apply_material_to_selected_brushes(static_cast<uint8_t>(sel));
}
// Apply no_debris checkbox
int nd_state = IsDlgButtonChecked(hwnd, IDC_NO_DEBRIS);
apply_no_debris_to_selected_brushes(nd_state);
}
if (msg == WM_COMMAND && LOWORD(wParam) == IDC_IS_GEOABLE) {
int state = IsDlgButtonChecked(hwnd, IDC_IS_GEOABLE);
// Normalize: skip indeterminate state, treat as unchecked
if (state == BST_INDETERMINATE) {
state = BST_UNCHECKED;
CheckDlgButton(hwnd, IDC_IS_GEOABLE, BST_UNCHECKED);
}
// Auto-check Is Detail when Is Geoable is checked
if (state == BST_CHECKED && IsDlgButtonChecked(hwnd, 1215) != BST_CHECKED) {
CheckDlgButton(hwnd, 1215, BST_CHECKED);
}
}
if (msg == WM_COMMAND && LOWORD(wParam) == IDC_MATERIAL_COMBO && HIWORD(wParam) == CBN_SELCHANGE) {
// Refresh no_debris checkbox when material changes
init_no_debris_checkbox(hwnd);
}
if (msg == WM_COMMAND && LOWORD(wParam) == 1215) {
// Auto-uncheck Is Geoable when Is Detail is no longer checked
if (IsDlgButtonChecked(hwnd, 1215) != BST_CHECKED) {
CheckDlgButton(hwnd, IDC_IS_GEOABLE, BST_UNCHECKED);
}
// Refresh material combo and no_debris enable state when detail flag changes
init_material_combo(hwnd);
init_no_debris_checkbox(hwnd);
}
if (msg == WM_NCDESTROY) {
SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(g_brush_props_orig_wndproc));
auto result = CallWindowProcA(g_brush_props_orig_wndproc, hwnd, msg, wParam, lParam);
g_brush_props_orig_wndproc = nullptr;
return result;
}
return CallWindowProcA(g_brush_props_orig_wndproc, hwnd, msg, wParam, lParam);
}
static LRESULT CALLBACK BrushPropsMsgHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION) {
auto* msg = reinterpret_cast<CWPRETSTRUCT*>(lParam);
if (msg->message == WM_INITDIALOG && GetDlgItem(msg->hwnd, IDC_IS_GEOABLE)) {
int state = compute_geoable_state_from_selected();
CheckDlgButton(msg->hwnd, IDC_IS_GEOABLE, state);
init_material_combo(msg->hwnd);
init_no_debris_checkbox(msg->hwnd);
g_brush_props_orig_wndproc = reinterpret_cast<WNDPROC>(
SetWindowLongPtrA(msg->hwnd, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(BrushPropsSubclassProc)));
UnhookWindowsHookEx(g_brush_props_msg_hook);
g_brush_props_msg_hook = nullptr;
}
}
return CallNextHookEx(g_brush_props_msg_hook, nCode, wParam, lParam);
}
// CWnd::CreateDlg path — used by CDialogBar::Create for toolbar-style dialog bars
CodeInjection CWnd_CreateDlg_injection{
0x0052F112,
[](auto& regs) {
auto& hCurrentResourceHandle = regs.esi;
auto lpszTemplateName = addr_as_ref<LPCSTR>(regs.esp);
// - 136: main window top bar (added tool buttons)
if (lpszTemplateName == MAKEINTRESOURCE(IDD_MAIN_FRAME_TOP_BAR)) {
hCurrentResourceHandle = reinterpret_cast<int>(g_module);
}
},
};
// CFormView::Create path — used for form view panels like the brush mode side panel
// At 0x0052F08D: ESI = hModule, EBX = lpszTemplateName (MAKEINTRESOURCE)
CodeInjection CFormView_Create_injection{
0x0052F08D,
[](auto& regs) {
auto& hCurrentResourceHandle = regs.esi;
auto lpszTemplateName = static_cast<LPCSTR>(reinterpret_cast<void*>(static_cast<uintptr_t>(regs.ebx)));
if (lpszTemplateName == MAKEINTRESOURCE(IDD_BRUSH_MODE_PANEL)
|| lpszTemplateName == MAKEINTRESOURCE(IDD_FACE_MODE_PANEL)
|| lpszTemplateName == MAKEINTRESOURCE(IDD_VERTEX_MODE_PANEL)
|| lpszTemplateName == MAKEINTRESOURCE(IDD_GROUP_MODE_PANEL)) {
hCurrentResourceHandle = reinterpret_cast<int>(g_module);
}
},
};
// Post-creation hook for CFormView panels. At 0x0052F0A9 (after CreateDlgIndirect),
// EDI = CWnd*, EBX = template name. CWnd::m_hWnd is at CWnd+0x1C.
// Subclasses group panel immediately after creation.
static LRESULT CALLBACK GroupPanelSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static void subclass_group_panel_post_create(HWND hwnd);
CodeInjection CFormView_PostCreate_injection{
0x0052F0A9,
[](auto& regs) {
auto lpszTemplateName = static_cast<LPCSTR>(reinterpret_cast<void*>(static_cast<uintptr_t>(regs.ebx)));
if (lpszTemplateName == MAKEINTRESOURCE(IDD_GROUP_MODE_PANEL)) {
HWND hwnd = *reinterpret_cast<HWND*>(
reinterpret_cast<uint8_t*>(static_cast<uintptr_t>(regs.edi)) + 0x1C);
if (hwnd) {
subclass_group_panel_post_create(hwnd);
}
}
},
};
CodeInjection CDialog_DoModal_injection{
0x0052F461,
[](auto& regs) {
auto& hCurrentResourceHandle = regs.ebx;
auto lpszTemplateName = addr_as_ref<LPCSTR>(regs.esp);
if (lpszTemplateName == MAKEINTRESOURCE(IDD_TRIGGER_PROPERTIES) ||
lpszTemplateName == MAKEINTRESOURCE(IDD_LEVEL_PROPERTIES) ||
lpszTemplateName == MAKEINTRESOURCE(IDD_UV_UNWRAP) ||
lpszTemplateName == MAKEINTRESOURCE(IDD_BRUSH_PROPERTIES) ||
lpszTemplateName == MAKEINTRESOURCE(IDD_FACE_SPLIT) ||
lpszTemplateName == MAKEINTRESOURCE(IDD_BRUSH_MIRROR)
) {
hCurrentResourceHandle = reinterpret_cast<int>(g_module);
}
if (lpszTemplateName == MAKEINTRESOURCE(IDD_BRUSH_PROPERTIES)) {
g_brush_props_msg_hook = SetWindowsHookExA(
WH_CALLWNDPROCRET, BrushPropsMsgHookProc, nullptr, GetCurrentThreadId());
}
},
};
HMENU WINAPI LoadMenuA_new(HINSTANCE hInstance, LPCSTR lpMenuName) {
HMENU result = LoadMenuA(g_module, lpMenuName);
if (!result) {
result = LoadMenuA(hInstance, lpMenuName);
}
return result;
}
HICON WINAPI LoadIconA_new(HINSTANCE hInstance, LPCSTR lpIconName) {
HICON result = LoadIconA(g_module, lpIconName);
if (!result) {
result = LoadIconA(hInstance, lpIconName);
}
return result;
}
HACCEL WINAPI LoadAcceleratorsA_new(HINSTANCE hInstance, LPCSTR lpTableName) {
HACCEL result = LoadAcceleratorsA(g_module, lpTableName);
if (!result) {
result = LoadAcceleratorsA(hInstance, lpTableName);
}
return result;
}
CodeInjection CDedLevel_OpenRespawnPointProperties_injection{
0x00404CB8,
[](auto& regs) {
// Fix wrong respawn point reference being used when copying booleans from checkboxes
regs.esi = regs.ebx;
},
};
using wnd_set_text_type = int __fastcall(void*, void*, const char*);
extern CallHook<wnd_set_text_type> log_append_wnd_set_text_hook;
int __fastcall log_append_wnd_set_text_new(void* self, void* edx, const char* str)
{
if (g_skip_wnd_set_text) {
return std::strlen(str);
}
return log_append_wnd_set_text_hook.call_target(self, edx, str);
}
CallHook<wnd_set_text_type> log_append_wnd_set_text_hook{0x004449C6, log_append_wnd_set_text_new};
using group_mode_handle_selection_type = void __fastcall(void* self);
extern FunHook<group_mode_handle_selection_type> group_mode_handle_selection_hook;
void __fastcall group_mode_handle_selection_new(void* self)
{
g_skip_wnd_set_text = true;
group_mode_handle_selection_hook.call_target(self);
g_skip_wnd_set_text = false;
// TODO: print
LogDlg_Append(GetLogDlg(), "");
}
FunHook<group_mode_handle_selection_type> group_mode_handle_selection_hook{0x00423460, group_mode_handle_selection_new};
using brush_mode_handle_selection_type = void __fastcall(void* self);
extern FunHook<brush_mode_handle_selection_type> brush_mode_handle_selection_hook;
void __fastcall brush_mode_handle_selection_new(void* self)
{
g_skip_wnd_set_text = true;
brush_mode_handle_selection_hook.call_target(self);
g_skip_wnd_set_text = false;
LogDlg_Append(GetLogDlg(), "");
// Update Is Geoable checkbox and material combo in brush panel
HWND hdlg = WndToHandle(reinterpret_cast<CWnd*>(self));
if (hdlg && GetDlgItem(hdlg, IDC_IS_GEOABLE)) {
int state = compute_geoable_state_from_selected();
CheckDlgButton(hdlg, IDC_IS_GEOABLE, state);
init_material_combo(hdlg);
init_no_debris_checkbox(hdlg);
// Subclass panel for Is Geoable click handling (once per HWND)
if (hdlg != g_brush_panel_hwnd) {
if (g_brush_panel_hwnd && g_brush_panel_orig_wndproc) {
SetWindowLongPtrA(g_brush_panel_hwnd, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(g_brush_panel_orig_wndproc));
}
g_brush_panel_orig_wndproc = reinterpret_cast<WNDPROC>(
SetWindowLongPtrA(hdlg, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(BrushPanelSubclassProc)));
g_brush_panel_hwnd = hdlg;
}
}
}
FunHook<brush_mode_handle_selection_type> brush_mode_handle_selection_hook{0x0043F430, brush_mode_handle_selection_new};
void __fastcall DedLight_UpdateLevelLight_new(void *this_);
FunHook DedLight_UpdateLevelLight_hook{
0x00453200,
DedLight_UpdateLevelLight_new,
};
void __fastcall DedLight_UpdateLevelLight_new(void *this_)
{
auto& this_is_enabled = struct_field_ref<bool>(this_, 0xD5);
auto& level_light = struct_field_ref<void*>(this_, 0xD8);
auto& level_light_is_enabled = struct_field_ref<bool>(level_light, 0x91);
level_light_is_enabled = this_is_enabled;
DedLight_UpdateLevelLight_hook.call_target(this_);
}
struct CTextureBrowserDialog;
rf::String * __fastcall CTextureBrowserDialog_GetFolderName(CTextureBrowserDialog *this_, int edx, rf::String *folder_name);
FunHook CTextureBrowserDialog_GetFolderName_hook{
0x00471260,
CTextureBrowserDialog_GetFolderName,
};
rf::String * __fastcall CTextureBrowserDialog_GetFolderName(CTextureBrowserDialog *this_, int edx, rf::String *folder_name)
{
auto& texture_browser_folder_index = addr_as_ref<int>(0x006CA404);
if (texture_browser_folder_index > 0) {
return CTextureBrowserDialog_GetFolderName_hook.call_target(this_, edx, folder_name);
}
folder_name->buf = nullptr;
folder_name->max_len = 0;
return folder_name;
}
CodeInjection CCutscenePropertiesDialog_ct_crash_fix{
0x00458A84,
[](auto& regs) {
void* this_ = regs.esi;
auto& this_num_shots = struct_field_ref<int>(this_, 0x60);
this_num_shots = 0;
},
};
static auto RedrawEditorAfterModification = addr_as_ref<int __cdecl()>(0x00483560);
void* GetLevelFromMainFrame(CWnd* main_frame)
{
auto* doc = struct_field_ref<void*>(main_frame, 0xD0);
return &struct_field_ref<int>(doc, 0x60);
}
void CMainFrame_ToggleMaximizeViewport(CMainFrame* this_)
{
if (g_maximized_viewport == -1) {
this_->MaximizeActiveViewport();
} else {
this_->RestoreAllViewports();
}
}
void CMainFrame_OpenHelp(CWnd* this_)
{
ShellExecuteA(WndToHandle(this_), "open", "https://redfactionwiki.com/wiki/RF1_Editing_Main_Page", nullptr, nullptr, SW_SHOW);
}
void CMainFrame_OpenHotkeysHelp(CWnd* this_)
{
ShellExecuteA(WndToHandle(this_), "open", "https://redfactionwiki.com/wiki/RED_Hotkey_Reference", nullptr, nullptr, SW_SHOW);
}
void CMainFrame_OpenAlpineHelp(CWnd* this_)
{
ShellExecuteA(WndToHandle(this_), "open", "https://redfactionwiki.com/wiki/Alpine_Level_Design", nullptr, nullptr, SW_SHOW);
}
void CMainFrame_HideAllObjects(CWnd* this_)
{
AddrCaller{0x0042DCA0}.this_call(GetLevelFromMainFrame(this_));
RedrawEditorAfterModification();
}
void CMainFrame_ShowAllObjects(CWnd* this_)
{
AddrCaller{0x0042DDA0}.this_call(GetLevelFromMainFrame(this_));
RedrawEditorAfterModification();
}
void CMainFrame_HideSelected(CWnd* this_)
{
AddrCaller{0x0042DBF0}.this_call(GetLevelFromMainFrame(this_));
RedrawEditorAfterModification();
}
void CMainFrame_SelectObjectByUid(CWnd* this_)
{
AddrCaller{0x0042E720}.this_call(GetLevelFromMainFrame(this_));
RedrawEditorAfterModification();
}
void CMainFrame_InvertSelection(CWnd* this_)
{
AddrCaller{0x0042E890}.this_call(GetLevelFromMainFrame(this_));
RedrawEditorAfterModification();
}
static char* patch_cmdline_if_multi(char* original)
{
if (!original) {
return original;
}
if (!g_is_play_in_multi) {
return original;
}
g_red_cmdline.assign(original);
constexpr const char needle[] = "-level ";
auto pos = g_red_cmdline.find(needle);
if (pos != std::string::npos) {
// already has -levelm (defensive)
if (g_red_cmdline.compare(pos, 8, "-levelm ") != 0) {
// insert m before the space
g_red_cmdline.insert(pos + 6, "m");
}
}
return g_red_cmdline.data();
}
CodeInjection CMainFrame_OnPlayLevel_injection{
0x00447B32,
[](auto& regs) {
char* original = regs.edx;
auto* patched = patch_cmdline_if_multi(original);
regs.edx = reinterpret_cast<uintptr_t>(patched);
},
};
CodeInjection CMainFrame_OnPlayLevelFromCameraCmd_injection{
0x00448024,
[](auto& regs) {
char* original = regs.edx;
auto* patched = patch_cmdline_if_multi(original);
regs.edx = reinterpret_cast<uintptr_t>(patched);
},
};
void CMainFrame_PlayMulti(CWnd* this_)
{
g_is_play_in_multi = true;
AddrCaller{0x004478B0}.this_call<int>(this_); // CMainFrame_OnPlayLevel
g_is_play_in_multi = false;
}
void CMainFrame_PlayMultiFromCamera(CWnd* this_)
{
g_is_play_in_multi = true;
AddrCaller{0x00447B90}.this_call<int>(this_); // CMainFrame_OnPlayLevelFromCameraCmd
g_is_play_in_multi = false;
}
void CMainFrame_BackLink([[maybe_unused]] CWnd* this_)
{
DedLevel_DoBackLink();
RedrawEditorAfterModification();
}
// Clip tool fix: replace buggy single-pass Cohen-Sutherland with proper slab intersection
// ====================
// Stock FUN_004c9af0 fails on diagonal clip lines because it only clips one endpoint
// against one boundary per axis without iterating. This causes the clip tool to silently
// skip brushes when the clip plane is sufficiently diagonal.
static bool __cdecl line_aabb_intersect(
const Vector3* bbox_min, const Vector3* bbox_max,
const Vector3* p0, const Vector3* p1,
Vector3* intersection_out)
{
// Slab (Liang-Barsky) line-AABB intersection
float dir[3] = {p1->x - p0->x, p1->y - p0->y, p1->z - p0->z};
const float* origin = &p0->x;
const float* bmin = &bbox_min->x;
const float* bmax = &bbox_max->x;
float t_enter = 0.0f;
float t_exit = 1.0f;
for (int i = 0; i < 3; i++) {
if (std::abs(dir[i]) < 1e-12f) {
// Line is parallel to this slab — check if origin is inside
if (origin[i] < bmin[i] || origin[i] > bmax[i])
return false;
}
else {
float inv_dir = 1.0f / dir[i];
float t0 = (bmin[i] - origin[i]) * inv_dir;
float t1 = (bmax[i] - origin[i]) * inv_dir;
if (t0 > t1) { float tmp = t0; t0 = t1; t1 = tmp; }
if (t0 > t_enter) t_enter = t0;
if (t1 < t_exit) t_exit = t1;
if (t_enter > t_exit)
return false;
}
}
// Clamp to line segment [0, 1]
if (t_enter > 1.0f || t_exit < 0.0f)
return false;
float t = (t_enter >= 0.0f) ? t_enter : 0.0f;
intersection_out->x = p0->x + dir[0] * t;
intersection_out->y = p0->y + dir[1] * t;
intersection_out->z = p0->z + dir[2] * t;
return true;
}
FunHook<bool __cdecl(const Vector3*, const Vector3*, const Vector3*, const Vector3*, Vector3*)>
line_aabb_intersect_hook{0x004c9af0, line_aabb_intersect};
// Fix crash when moving/rotating/undoing decal objects (DED_DECAL, type 0x12).
// FUN_0042a460 case 0x12 calls FUN_0044e9a0 which dereferences the sub-object pointer at +0xA4
// without a null check. The sub-object is created by FUN_00494390, which returns NULL if the
// decal count exceeds 127 or allocation fails. Moving/rotating/undoing such a decal crashes
// in FUN_00494400 when it dereferences NULL+0x34 etc.
static void __fastcall object_rotation_update_new(void* self, int /*edx*/, void* param);
FunHook<decltype(object_rotation_update_new)> object_rotation_update_hook{
0x0044e9a0, object_rotation_update_new};
static void __fastcall object_rotation_update_new(void* self, int /*edx*/, void* param)
{
auto* sub_obj = *reinterpret_cast<void**>(static_cast<std::byte*>(self) + 0xA4);
if (!sub_obj) {
WARN_ONCE("Skipping rotation update for object with null sub-object at +0xA4");
return;
}
object_rotation_update_hook.call_target(self, 0, param);
}
// Face mode panel (dialog 178) subclass
static WNDPROC g_face_panel_orig_wndproc = nullptr;
static HWND g_face_panel_hwnd = nullptr;
static LRESULT CALLBACK FacePanelSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_COMMAND) {
WORD id = LOWORD(wParam);
if (id == IDC_FACE_DELETE) { handle_face_delete(); return 0; }
if (id == IDC_FACE_DELETE_EXT) { handle_face_delete_ext(); return 0; }
if (id == IDC_FACE_SPLIT) { handle_face_split(); return 0; }
if (id == IDC_FACE_FLIP_NORMAL) { handle_face_flip_normal(); return 0; }
}
return CallWindowProcA(g_face_panel_orig_wndproc, hwnd, msg, wParam, lParam);
}
// Subclass the face mode panel when it becomes active.
// Called from CWnd_CreateDlg_injection when dialog 178 is created.
static void subclass_face_panel(HWND hdlg)
{
if (hdlg == g_face_panel_hwnd) return; // already subclassed
if (g_face_panel_hwnd && g_face_panel_orig_wndproc) {
SetWindowLongPtrA(g_face_panel_hwnd, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(g_face_panel_orig_wndproc));
}
g_face_panel_orig_wndproc = reinterpret_cast<WNDPROC>(
SetWindowLongPtrA(hdlg, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(FacePanelSubclassProc)));
g_face_panel_hwnd = hdlg;
}
// Vertex mode panel subclass — handles Delete and Bridge button clicks.
static WNDPROC g_vertex_panel_orig_wndproc = nullptr;
static HWND g_vertex_panel_hwnd = nullptr;
static LRESULT CALLBACK VertexPanelSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_COMMAND) {
WORD id = LOWORD(wParam);
if (id == IDC_VERTEX_DELETE) { handle_vertex_delete(); return 0; }
if (id == IDC_VERTEX_CAP) { handle_vertex_bridge(); return 0; }
}
return CallWindowProcA(g_vertex_panel_orig_wndproc, hwnd, msg, wParam, lParam);
}
static void subclass_vertex_panel(HWND hdlg)
{
if (hdlg == g_vertex_panel_hwnd) return;
if (g_vertex_panel_hwnd && g_vertex_panel_orig_wndproc) {
SetWindowLongPtrA(g_vertex_panel_hwnd, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(g_vertex_panel_orig_wndproc));
}
g_vertex_panel_orig_wndproc = reinterpret_cast<WNDPROC>(
SetWindowLongPtrA(hdlg, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(VertexPanelSubclassProc)));
g_vertex_panel_hwnd = hdlg;
}
// Group mode panel subclass — handles Mirror button click.
static WNDPROC g_group_panel_orig_wndproc = nullptr;
static HWND g_group_panel_hwnd = nullptr;
static void subclass_group_panel(HWND hdlg)
{
// Check if already subclassed (current WndProc is ours)
auto current = reinterpret_cast<WNDPROC>(GetWindowLongPtrA(hdlg, GWLP_WNDPROC));
if (current == GroupPanelSubclassProc) return;
g_group_panel_orig_wndproc = reinterpret_cast<WNDPROC>(
SetWindowLongPtrA(hdlg, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(GroupPanelSubclassProc)));
g_group_panel_hwnd = hdlg;
}
// Called from CFormView_PostCreate_injection when dialog 231 is created
static void subclass_group_panel_post_create(HWND hwnd)
{
subclass_group_panel(hwnd);
}
static LRESULT CALLBACK GroupPanelSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_COMMAND) {
WORD id = LOWORD(wParam);
if (id == IDC_GROUP_MIRROR) {
handle_group_mirror();
return 0;
}
}
return CallWindowProcA(g_group_panel_orig_wndproc, hwnd, msg, wParam, lParam);
}