-
Notifications
You must be signed in to change notification settings - Fork 3
/
gui.cpp
1012 lines (825 loc) · 23.7 KB
/
gui.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
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
// generated by Fast Light User Interface Designer (fluid) version 1.0302
#include "winheaders.h"
#include "gui.h"
#include "spelunky.h"
#include "patches.h"
#include "seeder.h"
#include "offline.h"
#include "gamedetect.h"
#include "watermark.h"
#include "anticrash.h"
#include "info.h"
#include "derandom.h"
#include "version.h"
#include "oneplayer_only.h"
#include "game_state_detector.h"
#include "updates.h"
#include "daily.h"
#include "remove_daily.h"
#include "custom_hud.h"
#include "patch_group.h"
#include "registry.h"
#include "mods.h"
#include "gui_netplay.h"
#include "syllabic.h"
#include "netplay_connection.h"
#include "frzsave_patch.h"
#include "rc_io.h"
#include "tile_editing.h"
#include "frozboards/session.h"
#include <FL/Fl_Check_Button.H>
#include <boost/assign.hpp>
#include <thread>
#include <memory>
#include <mutex>
#include <sstream>
#define FROZLUNKY_TITLE "Frozlunky " VERSION_STR
#define map_init boost::assign::map_list_of
typedef std::mutex mutex_type;
std::function<void(bool)> toggle_callback;
std::function<bool(const std::string&)> seed_change_callback;
std::thread start_thread;
Fl_Window* window = nullptr;
Fl_Double_Window* info_window = nullptr;
Fl_Check_Button* change_every_level = nullptr;
Fl_Input* input_seed = nullptr;
SeedChangeButton* seed_button = nullptr;
InfoButton* info_button = nullptr;
FrozboardsButton* lbs_button = nullptr;
DailyButton* daily_button = nullptr;
std::shared_ptr<Spelunky> spelunky;
std::shared_ptr<DerandomizePatch> dp;
std::shared_ptr<PatchGroup> patches;
std::shared_ptr<PatchGroup> special_patches;
std::shared_ptr<PatchGroup> all_patches;
std::shared_ptr<Seeder> seeder;
std::shared_ptr<GameChangeDetector> gcd;
std::shared_ptr<DailyInstance> daily;
std::shared_ptr<OnePlayerOnlyPatch> opop;
std::shared_ptr<GameHooks> info_hooks;
std::shared_ptr<CustomHudPatch> chp;
std::shared_ptr<RemoteCallPatch> rcp;
std::shared_ptr<StaticChunkPatch> stcp;
std::mutex daily_mutex;
std::mutex gsd_mutex;
std::shared_ptr<GameStateDetector> gsd;
std::vector<GameStateDetector::bind_id> froz_toggle_binds;
mutex_type state_mutex;
mutex_type gui_info_mutex;
int init_state = 0;
int window_init_state = -10;
bool allow_seed_change = true;
bool daily_available = false;
bool gui_visible = false;
const int states_can_disable_froz[] = {STATE_PLAYING, STATE_GAMEOVER_HUD, STATE_MAINMENU, STATE_LOBBY, STATE_TITLE, STATE_INTRO, STATE_CHARSELECT};
//////
// Unsafe
//////
std::shared_ptr<RemoteCallPatch> CurrentRCP() {
return rcp;
}
void request_soft_seed_lock() {
change_every_level->value(0);
change_every_level->redraw();
}
/////
//Special Mods
/////
DisplayModsButton* mods_button = nullptr;
void init_special_mods() {
Mods::Initialize(seeder, dp, info_hooks, chp);
Mods::SetVisibilityChangeCallback([](bool visible) {
if(!mods_button)
return;
if(visible || daily) {
mods_button->deactivate();
}
else {
mods_button->activate();
}
});
}
//////////
// LEVEL EDITOR
//////////
static ChunkEditorButton* editor_button;
int ChunkEditorButton::handle(int evt) {
if(evt == 2) {
TileEditing::ShowUI();
return 1;
}
return 0;
}
void init_level_editor() {
std::shared_ptr<TilePatch> tp(new TilePatch(spelunky));
stcp = std::make_shared<StaticChunkPatch>(info_hooks, tp, seeder);
Mods::ModsGroup()->add("stcp", stcp);
Mods::ModsGroup()->add("tp", tp);
TileEditing::Initialize(dp, info_hooks, seeder, stcp);
if(!TileEditing::Valid()) {
editor_button->deactivate();
editor_button->label("Level Editor Unavailable");
}
TileEditing::DisplayStateCallback([=](bool vis) {
if(vis) {
NetplayGUI::HideNetplayGUI();
Mods::HideModsGUI();
window->hide();
if(!stcp->is_active()) {
stcp->perform();
}
}
else {
stcp->undo();
seeder->seed(std::string(":") + Syllabic::MakePhoneticString(2));
window->show();
}
});
}
//////////
int DisplayModsButton::handle(int evt) {
if(evt == 2) {
Mods::ShowModsGUI();
}
return Fl_Button::handle(evt);
}
/////////
void undo_patches() {
if(gsd) {
int current_state = gsd->current_state();
auto end = std::end(states_can_disable_froz);
if(std::find(std::begin(states_can_disable_froz), end, current_state) == end) {
return;
}
}
if(patches) {
auto group = Mods::ModsGroup();
if(group) {
Mods::ModsGroup()->undo();
}
if(patches) {
patches->undo();
}
if(chp) {
chp->undo();
}
}
}
bool active_daily() {
return daily != nullptr;
}
void hide_gui(Fl_Widget* ignored = nullptr) {
gui_info_mutex.lock();
int children = window->children();
for(int i = 0; i < children; i++) {
auto child = window->child(i);
if(child != ignored) {
child->deactivate();
}
}
gui_visible = false;
gui_info_mutex.unlock();
window->redraw();
}
void show_gui(Fl_Widget* ignored = nullptr) {
gui_info_mutex.lock();
int children = window->children();
Fl_Widget* daily = dynamic_cast<Fl_Widget*>(daily_button);
for(int i = 0; i < children; i++) {
auto child = window->child(i);
if(child != ignored && !(!daily_available && child == daily)) {
child->activate();
}
}
gui_visible = true;
gui_info_mutex.unlock();
window->redraw();
}
DWORD __stdcall display_ended_daily_thread_func(void*) {
MessageBox(NULL, "Today's daily has ended.", "Frozboards", MB_OK);
return 0;
}
void create_session_and_start() {
daily_button->deactivate();
daily_button->label("Loading..");
Frozboards::Session::CreateSession(info_hooks->steam_id(), [](std::shared_ptr<Frozboards::Session> session, int code) {
if(session) {
session->set_invalidate_callback([]() {
if(daily) {
daily->force_end();
show_gui();
window->label(FROZLUNKY_TITLE);
daily_button->label("Spd Daily");
chp->set_text(L"");
}
DWORD thread;
CreateThread(NULL, 0, display_ended_daily_thread_func, NULL, NULL, &thread);
});
daily = std::shared_ptr<DailyInstance>(new DailyInstance(session, spelunky, seeder, dp, chp, patches, Mods::ModsGroup()));
daily_button->activate();
daily_button->label("End Daily");
}
else {
daily_button->activate();
daily_button->label("Spd Daily");
show_gui();
window->label(FROZLUNKY_TITLE);
if(code == SESSION_ERR_FAILED_REQ) {
MessageBoxA(NULL, "Oops! Frozlunky couldn't connect to Frozboards, try again later.", "Frozboards", MB_OK);
}
else if(code == SESSION_ERR_ALREADY_PLAYED) {
MessageBoxA(NULL, "You have already played today's daily.", "Frozboards", MB_OK);
}
}
});
}
bool attempt_create_daily(std::shared_ptr<Frozboards::Session> session) {
daily = std::shared_ptr<DailyInstance>(new DailyInstance(session, spelunky, seeder, dp, chp, patches, Mods::ModsGroup()));
int attempts = 0;
do {
daily = std::shared_ptr<DailyInstance>(new DailyInstance(session, spelunky, seeder, dp, chp, patches, Mods::ModsGroup()));
Sleep(200);
} while(!(daily->status() == DAILY_WAITING || daily->status() == DAILY_WAITING2) && attempts++ < 10);
return daily->status() == DAILY_WAITING || daily->status() == DAILY_WAITING2;
}
bool last_cycle_daily = false;
unsigned last_daily_status = DAILY_INVALID;
//returns true if daily in progress
bool daily_gui_update(Fl_Window* window)
{
if(active_daily())
{
if(!last_cycle_daily) {
hide_gui(daily_button);
}
daily->cycle();
unsigned status = daily->status();
switch(status) {
case DAILY_INVALID:
if(last_daily_status == DAILY_WAITING)
window->label(FROZLUNKY_TITLE);
daily.reset();
break;
case DAILY_WAITING:
case DAILY_WAITING2:
if(last_daily_status != DAILY_WAITING) {
window->label(FROZLUNKY_TITLE " - Start a game");
}
break;
case DAILY_INPROGRESS:
if(last_daily_status != DAILY_INPROGRESS)
window->label(FROZLUNKY_TITLE " - Daily in progress");
break;
case DAILY_COMPLETED:
if(last_daily_status != DAILY_COMPLETED) {
auto session = daily->get_session();
wchar_t fin[128];
std::swprintf(fin, sizeof(fin), L"Submitted Score: %.1f (Start a new game for a new attempt)", (float)daily->total_score());
chp->unlock();
chp->set_text(fin);
if(!session->invalidated()) {
daily.reset();
if(!attempt_create_daily(session)) {
daily.reset();
MessageBox(NULL, "Failed to re-initialize session, the daily has been ended.", "Frozboards", MB_OK);
}
else {
daily->cycle();
}
}
else {
daily.reset();
}
}
break;
}
if(daily) {
daily->cycle();
}
last_daily_status = status;
last_cycle_daily = true;
return true;
}
else
{
//if the daily ends in any fashion we will always end up here once
if(last_cycle_daily) {
show_gui();
window->label(FROZLUNKY_TITLE);
daily_button->label("Spd Daily");
chp->set_text(L"");
}
last_daily_status = DAILY_INVALID;
last_cycle_daily = false;
return false;
}
}
int DailyButton::handle(int evt) {
if(evt == 2) {
std::string steam_id = info_hooks->steam_id();
if(steam_id == "") {
MessageBox(NULL, "Frozlunky did not find a suitable Steam account to bind to Frozboards.", "Frozboards Error", MB_OK);
return Fl_Button::handle(evt);
}
//abort daily
if(daily) {
daily->force_end();
if(MessageBox(NULL, "Would you like to view your runs?", "Frozboards", MB_YESNO) == IDYES) {
ShellExecute(NULL, "open", (std::string(FROZBOARDS_URL "/user.htm?sid=")+info_hooks->steam_id()).c_str(), NULL, NULL, SW_SHOWNORMAL);
}
}
else {
//if this is accessed then opop is guaranteed to not be null
if(opop->controller_count() > 1) {
MessageBox(NULL, "You currently have multiple players active, please play with only one player.", "Frozboards", MB_OK);
return Fl_Button::handle(evt);
}
if(seeder->is_locked()) {
MessageBox(NULL, "A daily cannot be started due to a conflicting active mod.", "Frozboards", MB_OK);
return Fl_Button::handle(evt);
}
this->label("...");
this->deactivate();
Mods::HideModsGUI();
hide_gui(daily_button);
if(!chp->valid()) {
MessageBox(NULL, "Custom HUDs are not available (this is not supposed to happen). You will not see your score in-game.", "Frozboards", MB_OK);
}
create_session_and_start();
}
}
return Fl_Button::handle(evt);
}
int FrozboardsButton::handle(int evt) {
if(evt == 2) {
ShellExecute(NULL, "open", FROZBOARDS_URL, NULL, NULL, SW_SHOWNORMAL);
}
return Fl_Button::handle(evt);
}
int InfoButton::handle(int evt) {
if(evt == 2) {
ShellExecute(NULL, "open", FROZLUNKY_URL, NULL, NULL, SW_SHOWNORMAL);
}
return Fl_Button::handle(evt);
}
void cancel_froz_disable() {
if(froz_toggle_binds.size() > 0) {
for(GameStateDetector::bind_id id : froz_toggle_binds) {
gsd->request_unbind(id);
}
froz_toggle_binds.clear();
}
}
void queue_froz_disable(std::function<void()> disable) {
cancel_froz_disable();
//called by gui cycle thread eventually
auto bind = [disable](int,int,GameStateDetector::bind_id) {
disable();
cancel_froz_disable();
window->redraw();
};
for(int allowed_state : states_can_disable_froz) {
froz_toggle_binds.push_back(gsd->bind(allowed_state, bind, true));
}
}
int ToggleButton::handle(int evt)
{
if(active_daily())
return Fl_Button::handle(evt);
if(evt == 2 && spelunky && gsd)
{
Fl_Window* window = this->window();
//TODO implement all_patches and change these patches checks to all_patches (so we can have special patches too)
if(patches->is_active()) { //disable frozlunky
gsd_mutex.lock();
if(!froz_toggle_binds.empty())
{ //cancel froz disable if queued
cancel_froz_disable();
this->label("Disable");
mods_button->activate();
}
else
{
queue_froz_disable([this]() {
patches->push_state();
Mods::ModsGroup()->push_state();
undo_patches();
if(!patches->is_active()) {
hide_gui(this);
this->label("Enable");
}
else {
this->label("(Failed)");
patches->pop_state();
Mods::ModsGroup()->pop_state();
}
this->window()->redraw();
});
this->label("Waiting");
Mods::HideModsGUI();
mods_button->deactivate();
}
gsd_mutex.unlock();
window->redraw();
}
else { //enable frozlunky
gsd_mutex.lock();
if(!froz_toggle_binds.empty()) {
cancel_froz_disable();
this->label("Enable");
}
else {
queue_froz_disable([this]() {
patches->pop_state();
Mods::ModsGroup()->pop_state();
seeder->seed(seeder->get_seed());
show_gui();
this->label("Disable");
this->window()->redraw();
});
this->label("Waiting");
}
window->redraw();
gsd_mutex.unlock();
}
}
return Fl_Button::handle(evt);
}
int SeedChangeButton::handle(int evt) {
if(evt == 2 && spelunky && input_seed != nullptr) {
Fl_Window* window = this->window();
std::string seed = input_seed->value();
window->label((std::string(FROZLUNKY_TITLE " - ") + seed).c_str());
seeder->seed(seed);
window->redraw();
}
return Fl_Button::handle(evt);
}
///////////////////////////////
// NETPLAY
//////////////////////////////
Fl_Button* netplay_button = nullptr;
bool netplay_visible = false;
int NetplayButton::handle(int evt) {
if(evt == 2) {
if(netplay_visible) {
NetplayGUI::HideNetplayGUI();
}
else {
NetplayGUI::DisplayNetplayGUI();
}
}
return Fl_Button::handle(evt);
}
void configure_netplay() {
NetplayGUI::NetplayChangeCallback([](bool val)
{
auto mods = Mods::ModsGroup();
if(val) {
mods->push_state();
mods->undo();
mods->lock();
if(NetplayGUI::PlayerID() == 0) {
hide_gui(input_seed);
seed_button->activate();
change_every_level->activate();
allow_seed_change = true;
}
else {
hide_gui();
allow_seed_change = false;
}
if(!netplay_visible) {
netplay_button->activate();
}
}
else {
mods->unlock();
mods->pop_state();
allow_seed_change = true;
show_gui();
}
});
NetplayGUI::VisibilityChangeCallback([](bool visible) {
netplay_visible = visible;
if(visible) {
netplay_button->deactivate();
}
else {
netplay_button->activate();
}
});
}
/////////////////////////////
/**
Creates the window!
*/
Fl_Window* make_window()
{
Fl_Window* w;
{ Fl_Window* o = new Fl_Window(256, 150, FROZLUNKY_TITLE);
w = o;
window = o;
o->begin();
{ Fl_Input* o = new Fl_Input(45, 10, 205, 25, "Seed:");
o->textfont(4);
o->deactivate();
input_seed = o;
} // Fl_Input* o
{
change_every_level = new Fl_Check_Button(5, 35, 230, 25, "Change seed on game end");
std::string cel_save = Registry::GetValue("ChangeSeed");
if(!cel_save.empty()) {
try {
change_every_level->value(std::stoi(cel_save));
}
catch(std::exception&) {
change_every_level->value(1);
Registry::SetValue("ChangeSeed", "1");
}
}
else {
change_every_level->value(1);
Registry::SetValue("ChangeSeed", "1");
}
change_every_level->callback([](Fl_Widget* cel_widget) {
Fl_Check_Button* fcel = dynamic_cast<Fl_Check_Button*>(cel_widget);
Registry::SetValue("ChangeSeed", std::to_string(fcel->value()));
});
change_every_level->deactivate();
} // Fl_Check_Button* o
{ (lbs_button = new FrozboardsButton(5, 90, 35, 25, "LBs"))->deactivate();
} //Fl_Button* o
{ (mods_button = new DisplayModsButton(5, 90, 120, 25, "Mods"))->deactivate();
}
{ (seed_button = new SeedChangeButton(130, 90, 120, 25, "Seed it!"))->deactivate();
} // Fl_Button* o
{ (info_button = new InfoButton(5, 60, 35, 25, "?"))->deactivate();
} // Fl_Button* o
{ (daily_button = new DailyButton(45, 60, 70, 25, "Loading.."));
} //Fl_Button* o
lbs_button->deactivate();
lbs_button->hide();
daily_button->deactivate();
daily_button->hide();
info_button->deactivate();
info_button->hide();
{ (new ToggleButton(130, 60, 120, 25, "Disable"))->deactivate();
} // Fl_Button* o
{ (netplay_button = new NetplayButton(5, 60, 120, 25, "Netplay"))->deactivate();
} // Fl_Button* o
{ editor_button = new ChunkEditorButton(5, 120, 246, 25, "Switch to Level Editor");
}
o->end();
} // Fl_Window* o
return w;
}
//this must be set before showing window
void set_toggle_callback(decltype(toggle_callback) callback) {
toggle_callback = callback;
}
//this must be set before showing window
void set_seed_change_callback(decltype(seed_change_callback) callback) {
seed_change_callback = callback;
}
bool is_int(const std::string& str) {
try {
std::stoi(str);
return true;
}
catch(std::exception e) {
return false;
}
}
void set_guimode_seed(const std::string& seed) {
if(seeder) {
seeder->seed(seed);
}
}
/*
std::string random_str(const char* seed = nullptr) {
if(seed == nullptr) {
srand((unsigned int)time(nullptr));
}
else {
srand(std::hash<std::string>()(std::string(seed)));
}
std::stringstream str;
int len = 14;
while(len > 0) {
str << (char)((rand()%(0x7A-0x61))+0x61);
len--;
}
return str.str();
}
*/
std::string random_str(const char* seed = nullptr) {
unsigned iseed;
if(seed == nullptr) {
iseed = (unsigned int)time(nullptr);
}
else {
iseed = std::hash<std::string>()(std::string(seed));
}
return Syllabic::MakePhoneticString(iseed, 2);
}
DWORD __stdcall loop(void* wind)
{
while(true)
{
Fl_Window* window = (Fl_Window*)wind;
state_mutex.lock();
if(window_init_state != init_state) {
if(init_state == 0) {
window->label(FROZLUNKY_TITLE " - Initializing...");
hide_gui();
window->redraw();
}
else if(init_state == 1) {
show_gui();
daily_button->deactivate();
netplay_button->deactivate();
window->redraw();
}
else {
MessageBox(NULL, "Failed to initialize Frozlunky, try restarting Spelunky or checking for a Frozlunky update at " UPDATE_DOWNLOAD_URL, "Error", MB_OK);
if(window) {
delete window;
}
return 0;
}
window_init_state = init_state;
}
if(init_state == 1 && !daily_gui_update(window) && allow_seed_change && !seeder->is_locked())
{
if(change_every_level->value() == 1 && gcd->game_changed())
{
std::string seed = input_seed->value();
auto rand_mark = seed.find(":");
if(rand_mark != std::string::npos) {
seed = seed.substr(0, rand_mark);
}
std::string seeder_seed = seeder->get_seed();
const char* rand_seed = nullptr;
if(seeder_seed != "") {
rand_seed = seeder_seed.c_str();
}
std::string res = seed + ":" + random_str(rand_seed);
set_guimode_seed(res);
}
//this will not get called if a daily is in progress, daily overrides user-level importance
if(gsd) {
gsd_mutex.lock();
gsd->cycle();
gsd_mutex.unlock();
}
}
//destroy
if(!spelunky->alive()) {
delete window;
exit(0);
return 0;
}
state_mutex.unlock();
Sleep(32);
}
return 0;
}
int gui_operate(std::shared_ptr<Spelunky> spelunk, char* icon)
{
spelunky = spelunk;
DBG_EXPR(std::cout << "[GUI] Initializing operations on Spelunky located at base: " << spelunk->base_directory() << std::endl);
Fl_Window* gui_window = make_window();
window = gui_window;
start_thread = std::thread([]() {
try {
patches = std::make_shared<PatchGroup>(spelunky);
patches->add("op", std::make_shared<OfflinePatch>(spelunky));
patches->add("dp", dp = std::make_shared<DerandomizePatch>(spelunky));
patches->add("wp", std::make_shared<WatermarkPatch>(spelunky));
patches->add("acp", std::make_shared<AnticrashPatch>(spelunky));
patches->add("rdp", std::make_shared<RemoveDailyPatch>(spelunky));
#ifdef DEBUG_MODE
#define PATCH_ASSERT(NAME) if(!patches->get(NAME)->valid()) { throw std::exception(#NAME " patch invalid."); }
#else
#define PATCH_ASSERT(NAME) if(!patches->get(NAME)->valid()) { throw std::exception(); }
#endif
PATCH_ASSERT("dp");
PATCH_ASSERT("wp");
PATCH_ASSERT("acp");
#undef PATCH_ASSERT
opop = std::make_shared<OnePlayerOnlyPatch>(spelunky, dp);
patches->perform();
seeder = std::make_shared<Seeder>(dp);
seeder->on_seed_change([](const std::string& seed) {
if(!daily && input_seed != nullptr) {
input_seed->value(seed.c_str());
window->copy_label((std::string(FROZLUNKY_TITLE " - ") + seed).c_str());
input_seed->redraw();
window->redraw();
}
return true;
});
gsd = std::make_shared<GameStateDetector>(dp);
info_hooks = std::make_shared<GameHooks>(spelunky, dp);
gcd = std::make_shared<GameChangeDetector>(dp, info_hooks);
chp = std::make_shared<CustomHudPatch>(dp);
chp->perform();
patches->add("chp", chp);
init_special_mods();
init_level_editor();
bool allow_netplay = true;
//change save paths to frzlunky_save.*
{
rcp = std::make_shared<RemoteCallPatch>(spelunky);
rcp->perform();
std::shared_ptr<FrzSavePatch> fsp = std::make_shared<FrzSavePatch>(spelunky);
if(!fsp->valid()) {
DBG_EXPR(std::cout << "[GUI] Warning: Failed to initialize FrzSavePatch" << std::endl);
allow_netplay = false;
}
else {
fsp->perform();
if(!rcp->enqueue_call(std::make_shared<RCSave>(dp)->make(), []() {
DBG_EXPR(std::cout << "[GUI] Successfully performed force save." << std::endl);
}))
{
DBG_EXPR(std::cout << "[GUI] Failed to enqueue call to force save." << std::endl);
}
}
}
state_mutex.lock();
init_state = 1;
state_mutex.unlock();
set_guimode_seed(std::string(":")+random_str()); //init with random seed
netplay_button->deactivate();
netplay_button->label("Init..");
Updates::easy_update_check([=](bool result) {
if(!result) {
DailyInstance::Available(spelunky, dp, [](bool available) {
gui_info_mutex.lock();
daily_available = available;
if(available && gui_visible) {
daily_button->label("Spd Daily");
daily_button->activate();
}
else {
daily_button->label("No Daily");
daily_button->deactivate();
}
gui_info_mutex.unlock();
window->redraw();
});
if(allow_netplay && NetplayConnection::Initialize()) {
std::cout << "Initialized " << std::endl;
if(NetplayGUI::Init(spelunky, dp, seeder, info_hooks)) {
configure_netplay();
netplay_button->label("Netplay");
netplay_button->activate();
window->redraw();
}
else {
netplay_button->label("Failed");
netplay_button->deactivate();
}
}
else {
netplay_button->label("Failed");
netplay_button->deactivate();
}
}
else {
daily_button->label("Outdated");
daily_button->deactivate();
netplay_button->label("Outdated");
netplay_button->deactivate();
}
window->redraw();
});
}
catch(std::exception e) {
state_mutex.lock();
init_state = -1;
state_mutex.unlock();
std::cout << "Error: " << e.what() << std::endl;
}
});
if(icon != nullptr) {
window->icon(icon);
}
window->show();
window->callback([](Fl_Widget* wind) {
undo_patches();
Mods::HideModsGUI();
if(info_window != nullptr) {
delete (Fl_Double_Window*)info_window;
}