-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathkmachine.cpp
More file actions
1407 lines (1239 loc) · 46.4 KB
/
kmachine.cpp
File metadata and controls
1407 lines (1239 loc) · 46.4 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 "kmachine.h"
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <thread>
#include <list>
#include <set>
#define MINIAUDIO_IMPLEMENTATION
// NOTE - this is needed, because on macOS, there is a file called `MacTypes.h`
// inside it, it defines something named `Ptr`
// Our `Ptr` is not namespaced, so there is ambiguity.
//
// Second fix is because miniaudio redefines functions in the stdlib based on bad pre-processor
// assumptions AppleClang apparently does not define POSIX macros, leading to future ambiguity
namespace MiniAudioLib {
#if defined(__APPLE__)
#if !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200809L
#include "third-party/miniaudio.h"
#undef _POSIX_C_SOURCE
#else
// It should work if it's defined, but for some reason it didn't this is the unlikely branch
// but lets maintain the original value
#define NOT_REAL_OLD_POSIX_C_SOURCE _POSIX_C_SOURCE
#include "third-party/miniaudio.h"
#define _POSIX_C_SOURCE NOT_REAL_OLD_POSIX_C_SOURCE
#undef NOT_REAL_OLD_POSIX_C_SOURCE
#endif
#else
#include "third-party/miniaudio.h"
#endif
} // namespace MiniAudioLib
#include "common/global_profiler/GlobalProfiler.h"
#include "common/log/log.h"
#include "common/symbols.h"
#include "common/util/FileUtil.h"
#include "common/util/Timer.h"
#include "common/util/font/font_utils.h"
#include "common/util/string_util.h"
#include "game/external/discord.h"
#include "game/graphics/display.h"
#include "game/graphics/gfx.h"
#include "game/graphics/screenshot.h"
#include "game/kernel/common/Ptr.h"
#include "game/kernel/common/kernel_types.h"
#include "game/kernel/common/kprint.h"
#include "game/kernel/common/kscheme.h"
#include "game/mips2c/mips2c_table.h"
#include "game/sce/libcdvd_ee.h"
#include "game/sce/libpad.h"
#include "game/sce/libscf.h"
#include "game/sce/sif_ee.h"
/*!
* Where does OVERLORD load its data from?
*/
OverlordDataSource isodrv;
// Get IOP modules from DVD or from dsefilesv
u32 modsrc;
// Reboot IOP with IOP kernel from DVD/CD on boot
u32 reboot_iop;
const char* init_types[] = {"fakeiso", "deviso", "iso_cd"};
u8 pad_dma_buf[2 * SCE_PAD_DMA_BUFFER_SIZE];
// added
u32 vif1_interrupt_handler = 0;
u32 vblank_interrupt_handler = 0;
Timer ee_clock_timer;
MiniAudioLib::ma_engine maEngine;
std::map<std::string, std::list<MiniAudioLib::ma_sound*>> maSoundMap;
std::set<MiniAudioLib::ma_sound*> pausedSounds;
MiniAudioLib::ma_sound* mainMusicSound;
void kmachine_init_globals_common() {
memset(pad_dma_buf, 0, sizeof(pad_dma_buf));
isodrv = fakeiso; // changed. fakeiso is the only one that works in opengoal.
modsrc = 1;
reboot_iop = 1;
vif1_interrupt_handler = 0;
vblank_interrupt_handler = 0;
ee_clock_timer = Timer();
#ifdef _WIN32 // only do this on windows, because it only works on windows?
MiniAudioLib::ma_engine_uninit(&maEngine);
#endif
MiniAudioLib::ma_engine_init(NULL, &maEngine);
}
/*!
* Initialize the CD Drive
* DONE, EXACT
*/
void InitCD() {
lg::info("Initializing CD drive. This may take a while...");
ee::sceCdInit(SCECdINIT);
ee::sceCdMmode(SCECdDVD);
while (ee::sceCdDiskReady(0) == SCECdNotReady) {
lg::debug("Drive not ready... insert a disk!");
}
lg::debug("Disk type {}\n", ee::sceCdGetDiskType());
}
/*!
* Initialize the GS and display the splash screen.
* Not yet implemented. TODO
*/
void InitVideo() {}
/*!
* Flush caches. Does all the memory, regardless of what you specify
*/
void CacheFlush(void* mem, int size) {
(void)mem;
(void)size;
// FlushCache(0);
// FlushCache(2);
}
/*!
* Open a new controller pad.
* Set the new_pad flag to 1 and state to 0.
* Prints an error if it fails to open.
*/
u64 CPadOpen(u64 cpad_info, s32 pad_number) {
auto cpad = Ptr<CPadInfo>(cpad_info).c();
if (cpad->cpad_file == 0) {
// not open, so we will open it
cpad->cpad_file =
ee::scePadPortOpen(pad_number, 0, pad_dma_buf + pad_number * SCE_PAD_DMA_BUFFER_SIZE);
if (cpad->cpad_file < 1) {
MsgErr("dkernel: !open cpad #%d (%d)\n", pad_number, cpad->cpad_file);
}
cpad->new_pad = 1;
cpad->state = 0;
}
return cpad_info;
}
// Mutex to synchronize access to activeMusics
std::mutex activeMusicsMutex;
// Declare a mutex for synchronizing access to mainMusicInstance
std::mutex mainMusicMutex;
// Function to stop all instances of specific sound by filepath
void stopMP3(u32 filePathu32) {
std::string filePath = Ptr<String>(filePathu32).c()->data();
std::cout << "Trying to stop file: " << filePath << std::endl;
std::lock_guard<std::mutex> lock(activeMusicsMutex);
auto it = maSoundMap.find(filePath);
if (it == maSoundMap.end()) {
std::cerr << "Couldn't find sound to stop: " << filePath << std::endl;
} else {
// stop all instances of this sound
for (auto* sound : it->second) {
if (MiniAudioLib::ma_sound_stop(sound) != MiniAudioLib::MA_SUCCESS) {
std::cerr << "Failed to stop sound: " << filePath << std::endl;
}
// let the thread finish and handle ma_sound_uninit
}
// clear list of sounds for this filepath
it->second.clear();
}
}
// Function to stop all currently playing sounds.
void stopAllSounds() {
for (auto& pair : maSoundMap) {
// stop all instances of this sound
for (auto* sound : pair.second) {
MiniAudioLib::ma_sound_stop(sound);
}
pair.second.clear();
}
maSoundMap.clear();
}
// Function to pause all currently playing sounds.
void pauseSoundFiles() {
std::lock_guard<std::mutex> lock(activeMusicsMutex);
for (auto& pair : maSoundMap) {
for (auto* sound : pair.second) {
if (sound && MiniAudioLib::ma_sound_is_playing(sound)) {
MiniAudioLib::ma_sound_stop(sound);
pausedSounds.insert(sound);
}
}
}
}
// Function to resume all paused sounds.
void resumeSoundFiles() {
std::lock_guard<std::mutex> lock(activeMusicsMutex);
for (auto* sound : pausedSounds) {
if (sound && !MiniAudioLib::ma_sound_is_playing(sound)) {
MiniAudioLib::ma_sound_start(sound);
}
}
pausedSounds.clear();
}
// Function to get the names of currently playing files.
std::vector<std::string> getPlayingFileNames() {
std::vector<std::string> playingFileNames;
for (const auto& pair : maSoundMap) {
playingFileNames.push_back(pair.first);
}
return playingFileNames;
}
u64 playMP3_internal(u32 filePathu32, u32 volume, bool isMainMusic) {
std::string filePath = Ptr<String>(filePathu32).c()->data();
std::string fullFilePath = fs::path(file_util::get_jak_project_dir() / "custom_assets" /
game_version_names[g_game_version] / "audio" / filePath).string();
if (!file_util::file_exists(fullFilePath)) {
// file doesn't exist, let GOAL side know we didn't find it
return bool_to_symbol(false);
}
std::thread thread([=]() {
std::cout << "Playing file: " << filePath << std::endl;
MiniAudioLib::ma_result result;
MiniAudioLib::ma_sound* sound = new MiniAudioLib::ma_sound;
result = MiniAudioLib::ma_sound_init_from_file(&maEngine, fullFilePath.c_str(), 0, NULL, NULL,
sound);
if (result != MiniAudioLib::MA_SUCCESS) {
std::cout << "Failed to load: " << filePath << std::endl;
delete sound;
return;
}
MiniAudioLib::ma_sound_set_volume(sound, ((float)volume) / 100.0);
if (isMainMusic) {
MiniAudioLib::ma_sound_set_looping(sound, MA_TRUE);
mainMusicMutex.lock();
mainMusicSound = sound;
mainMusicMutex.unlock();
}
MiniAudioLib::ma_sound_start(sound);
if (!isMainMusic) {
std::lock_guard<std::mutex> lock(activeMusicsMutex);
if (maSoundMap.find(filePath) == maSoundMap.end()) {
maSoundMap.insert(std::make_pair(filePath, std::list<MiniAudioLib::ma_sound*>()));
}
maSoundMap[filePath].push_back(sound);
}
// sleep/loop until we're no longer main music, or non-looping sound is stopped/ends
while (mainMusicSound == sound || MiniAudioLib::ma_sound_is_playing(sound) || pausedSounds.count(sound) > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
MiniAudioLib::ma_sound_stop(sound);
MiniAudioLib::ma_sound_uninit(sound);
std::cout << "Finished playing file: " << filePath << std::endl;
if (!isMainMusic) {
std::lock_guard<std::mutex> lock(activeMusicsMutex);
if (maSoundMap.find(filePath) != maSoundMap.end()) {
maSoundMap[filePath].remove(sound);
}
pausedSounds.erase(sound);
}
delete sound;
});
thread.detach();
return bool_to_symbol(true);
}
u64 playMP3(u32 filePathu32, u32 volume) {
return playMP3_internal(filePathu32, volume, false);
}
// Function to stop the Main Music.
void stopMainMusic() {
mainMusicMutex.lock();
if (mainMusicSound && MiniAudioLib::ma_sound_is_playing(mainMusicSound)) {
std::cout << "Stopping Main Music..." << std::endl;
MiniAudioLib::ma_sound_stop(mainMusicSound);
mainMusicSound = NULL;
std::cout << "Stopped Main Music " << std::endl;
}
mainMusicMutex.unlock();
}
// Function to play the Main Music.
void playMainMusic(u32 filePathu32, u32 volume) {
stopMainMusic();
std::cout << "Playing Main Music" << std::endl;
playMP3_internal(filePathu32, volume, true);
}
void pauseMainMusic() {
mainMusicMutex.lock();
if (mainMusicSound && MiniAudioLib::ma_sound_is_playing(mainMusicSound)) {
MiniAudioLib::ma_sound_stop(mainMusicSound);
}
mainMusicMutex.unlock();
}
void resumeMainMusic() {
mainMusicMutex.lock();
if (mainMusicSound && !MiniAudioLib::ma_sound_is_playing(mainMusicSound)) {
MiniAudioLib::ma_sound_start(mainMusicSound);
}
mainMusicMutex.unlock();
}
// Function to change the volume of the Main Music.
void changeMainMusicVolume(u32 volume) {
mainMusicMutex.lock();
if (mainMusicSound) {
MiniAudioLib::ma_sound_set_volume(mainMusicSound, ((float)volume) / 100.0);
}
mainMusicMutex.unlock();
}
/*!
* Not checked super carefully for jak 2, but looks the same
*/
u64 CPadGetData(u64 cpad_info) {
using namespace ee;
auto cpad = Ptr<CPadInfo>(cpad_info).c();
auto pad_state = scePadGetState(cpad->number, 0);
if (pad_state == scePadStateDiscon) {
cpad->state = 0;
}
cpad->valid = pad_state | 0x80;
switch (cpad->state) {
// case 99: // functional
default: // controller is functioning as normal
if (pad_state == scePadStateStable || pad_state == scePadStateFindCTP1) {
scePadRead(cpad->number, 0, (u8*)cpad);
// ps2 controllers would send an enabled bit if the button was NOT pressed, but we don't do
// that here. removed code that flipped the bits.
if (cpad->change_time) {
scePadSetActDirect(cpad->number, 0, cpad->direct);
}
cpad->valid = pad_state;
}
break;
case 0: // unavailable
if (pad_state == scePadStateStable || pad_state == scePadStateFindCTP1) {
auto pad_mode = scePadInfoMode(cpad->number, 0, InfoModeCurID, 0);
if (pad_mode != 0) {
auto vibration_mode = scePadInfoMode(cpad->number, 0, InfoModeCurExID, 0);
if (vibration_mode > 0) {
// vibration supported
pad_mode = vibration_mode;
}
if (pad_mode == 4) {
// controller mode
cpad->state = 40;
} else if (pad_mode == 7) {
// dualshock mode
cpad->state = 70;
} else {
// who knows mode
cpad->state = 90;
}
}
}
break;
case 40: // controller mode - check for extra modes
// cpad->change_time = 0;
cpad->change_time = 0;
if (scePadInfoMode(cpad->number, 0, InfoModeIdTable, -1) == 0) {
// no controller modes
cpad->state = 90;
return cpad_info;
}
cpad->state = 41;
case 41: // controller mode - change to dualshock mode!
// try to enter the 2nd controller mode (dualshock for ds2's)
if (scePadSetMainMode(cpad->number, 0, 1, 3) == 1) {
cpad->state = 42;
}
break;
case 42: // controller mode change check
if (scePadGetReqState(cpad->number, 0) == scePadReqStateFailed) {
// failed to change to DS2
cpad->state = 41;
}
if (scePadGetReqState(cpad->number, 0) == scePadReqStateComplete) {
// change successful. go back to the beginning.
cpad->state = 0;
}
break;
case 70: // dualshock mode - check vibration
// get number of actuators (2 for DS2)
if (scePadInfoAct(cpad->number, 0, -1, 0) < 1) {
// no actuators means no vibration. skip to end!
// cpad->change_time = 0;
cpad->change_time = 0;
cpad->state = 99;
} else {
// we have actuators to use.
// cpad->change_time = 1; // remember to update pad times.
cpad->change_time = 1;
cpad->state = 75;
}
break;
case 75: // set actuator vib param info
if (scePadSetActAlign(cpad->number, 0, cpad->align) != 0) {
if (scePadInfoPressMode(cpad->number, 0) == 1) {
// pressure buttons supported
cpad->state = 76;
} else {
// no pressure buttons, done with controller setup
cpad->state = 99;
}
}
break;
case 76: // enter pressure mode
if (scePadEnterPressMode(cpad->number, 0) == 1) {
cpad->state = 78;
}
break;
case 78: // pressure mode request check
if (scePadGetReqState(cpad->number, 0) == scePadReqStateFailed) {
cpad->state = 76;
}
if (scePadGetReqState(cpad->number, 0) == scePadReqStateComplete) {
cpad->state = 99;
}
break;
case 90:
break; // unsupported controller. too bad!
}
return cpad_info;
}
// should make sure this works the same way in jak 2
void InstallHandler(u32 handler_idx, u32 handler_func) {
switch (handler_idx) {
case 3:
vblank_interrupt_handler = handler_func;
break;
case 5:
vif1_interrupt_handler = handler_func;
break;
default:
lg::error("unknown handler: {}\n", handler_idx);
ASSERT(false);
}
}
// nothing used this in jak1, hopefully same for 2
void InstallDebugHandler() {
ASSERT(false);
}
/*!
* Get length of a file.
*/
s32 klength(u64 fs) {
auto file_stream = Ptr<FileStream>(fs).c();
if ((file_stream->flags ^ 1) & 1) {
// first flag bit not set. This means no errors
auto end_seek = ee::sceLseek(file_stream->file, 0, SCE_SEEK_END);
auto reset_seek = ee::sceLseek(file_stream->file, 0, SCE_SEEK_SET);
if (reset_seek < 0 || end_seek < 0) {
// seeking failed, flag it
file_stream->flags |= 1;
}
return end_seek;
} else {
return 0;
}
}
/*!
* Seek a file stream.
*/
s32 kseek(u64 fs, s32 offset, s32 where) {
s32 result = -1;
auto file_stream = Ptr<FileStream>(fs).c();
if ((file_stream->flags ^ 1) & 1) {
result = ee::sceLseek(file_stream->file, offset, where);
if (result < 0) {
file_stream->flags |= 1;
}
}
return result;
}
/*!
* Read from a file stream.
*/
s32 kread(u64 fs, u64 buffer, s32 size) {
s32 result = -1;
auto file_stream = Ptr<FileStream>(fs).c();
if ((file_stream->flags ^ 1) & 1) {
result = ee::sceRead(file_stream->file, Ptr<u8>(buffer).c(), size);
if (result < 0) {
file_stream->flags |= 1;
}
}
return result;
}
/*!
* Write to a file stream.
*/
s32 kwrite(u64 fs, u64 buffer, s32 size) {
s32 result = -1;
auto file_stream = Ptr<FileStream>(fs).c();
if ((file_stream->flags ^ 1) & 1) {
result = ee::sceWrite(file_stream->file, Ptr<u8>(buffer).c(), size);
if (result < 0) {
file_stream->flags |= 1;
}
}
return result;
}
/*!
* Close a file stream.
*/
u64 kclose(u64 fs) {
auto file_stream = Ptr<FileStream>(fs).c();
if ((file_stream->flags ^ 1) & 1) {
ee::sceClose(file_stream->file);
file_stream->file = -1;
}
file_stream->flags = 0;
return fs;
}
// TODO dma_to_iop
void dma_to_iop() {
ASSERT(false);
}
u64 DecodeLanguage() {
return masterConfig.language;
}
u64 DecodeAspect() {
return masterConfig.aspect;
}
u64 DecodeVolume() {
return masterConfig.volume;
}
// NOTE: this is originally hardcoded, and returns different values depending on the disc region.
// it returns 0 for NTSC-U, 1 for PAL and 2 for NTSC-J
u64 DecodeTerritory() {
return GAME_TERRITORY_SCEA;
}
u64 DecodeTimeout() {
return masterConfig.timeout;
}
u64 DecodeInactiveTimeout() {
return masterConfig.inactive_timeout;
}
void DecodeTime(u32 ptr) {
Ptr<ee::sceCdCLOCK> clock(ptr);
// in jak2, if this fails, they do a sceScfGetLocalTimefromRTC
sceCdReadClock(clock.c());
}
void vif_interrupt_callback(int bucket_id) {
// added for the PC port for faking VIF interrupts from the graphics system.
if (vif1_interrupt_handler && MasterExit == RuntimeExitStatus::RUNNING) {
call_goal(Ptr<Function>(vif1_interrupt_handler), bucket_id, 0, 0, s7.offset, g_ee_main_mem);
}
}
/// PC PORT FUNCTIONS BEGIN
u32 offset_of_s7() {
return s7.offset;
}
inline bool symbol_to_bool(const u32 symptr) {
return symptr != s7.offset;
}
inline u64 bool_to_symbol(const bool val) {
return val ? static_cast<u64>(s7.offset) + true_symbol_offset(g_game_version) : s7.offset;
}
u64 pc_filter_debug_string(u32 str_ptr, u32 dist_ptr) {
auto str = std::string(Ptr<String>(str_ptr).c()->data());
float dist;
memcpy(&dist, &dist_ptr, 4);
// Check distance first
if (Gfx::g_debug_settings.text_check_range) {
if (dist / 4096.F > Gfx::g_debug_settings.text_max_range) {
return bool_to_symbol(true);
}
}
// Get the current filters
const auto& filters = Gfx::g_debug_settings.text_filters;
if (filters.empty()) {
// there are no filters, exit early
return bool_to_symbol(false);
}
// Currently very dumb contains check
for (const auto& filter : filters) {
if (filter.type == DebugTextFilter::Type::CONTAINS) {
if (!str.empty() && !filter.content.empty() && !str_util::contains(str, filter.content)) {
return bool_to_symbol(true);
}
} else if (filter.type == DebugTextFilter::Type::NOT_CONTAINS) {
if (!str.empty() && !filter.content.empty() && str_util::contains(str, filter.content)) {
return bool_to_symbol(true);
}
} else if (filter.type == DebugTextFilter::Type::REGEX) {
if (str_util::valid_regex(filter.content) &&
std::regex_match(str, std::regex(filter.content))) {
return bool_to_symbol(true);
}
}
}
return bool_to_symbol(false);
}
CommonPCPortFunctionWrappers g_pc_port_funcs;
u64 read_ee_timer() {
u64 ns = ee_clock_timer.getNs();
return (ns * 3) / 10;
}
void pc_memmove(u32 dst, u32 src, u32 size) {
memmove(Ptr<u8>(dst).c(), Ptr<u8>(src).c(), size);
}
void send_gfx_dma_chain(u32 /*bank*/, u32 chain) {
if (Gfx::GetCurrentRenderer()) {
Gfx::GetCurrentRenderer()->send_chain(g_ee_main_mem, chain);
}
}
void pc_texture_upload_now(u32 page, u32 mode) {
if (Gfx::GetCurrentRenderer()) {
Gfx::GetCurrentRenderer()->texture_upload_now(Ptr<u8>(page).c(), mode, s7.offset);
}
}
void pc_texture_relocate(u32 dst, u32 src, u32 format) {
if (Gfx::GetCurrentRenderer()) {
Gfx::GetCurrentRenderer()->texture_relocate(dst, src, format);
}
}
u64 pc_get_mips2c(u32 name) {
const char* n = Ptr<String>(name).c()->data();
return Mips2C::gLinkedFunctionTable.get(n);
}
u64 pc_get_display_id() {
if (Display::GetMainDisplay()) {
return Display::GetMainDisplay()->get_display_manager()->get_active_display_index();
}
return 0;
}
void pc_set_display_id(u64 display_id) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_display_manager()->enqueue_set_display_id(display_id);
}
}
u64 pc_get_display_name(u32 id, u32 str_dest_ptr) {
std::string name = "";
if (Display::GetMainDisplay()) {
name = Display::GetMainDisplay()->get_display_manager()->get_connected_display_name(id);
}
if (name.empty()) {
return bool_to_symbol(false);
}
if (g_game_version == GameVersion::Jak1) {
// The Jak 1 font has only caps
name = str_util::to_upper(name).c_str();
}
// Encode the string to the game font
const auto encoded_name = get_font_bank_from_game_version(g_game_version)
->convert_utf8_to_game(str_util::titlize(name).c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), encoded_name.c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::titlize(encoded_name).c_str());
return bool_to_symbol(true);
}
u32 pc_get_display_mode() {
auto display_mode = game_settings::DisplaySettings::DisplayMode::Windowed;
if (Display::GetMainDisplay()) {
display_mode = Display::GetMainDisplay()->get_display_manager()->get_display_mode();
}
switch (display_mode) {
case game_settings::DisplaySettings::DisplayMode::Borderless:
return g_pc_port_funcs.intern_from_c("borderless").offset;
case game_settings::DisplaySettings::DisplayMode::Fullscreen:
return g_pc_port_funcs.intern_from_c("fullscreen").offset;
default:
return g_pc_port_funcs.intern_from_c("windowed").offset;
}
}
void pc_set_display_mode(u32 symptr, u64 window_width, u64 window_height) {
if (!Display::GetMainDisplay()) {
return;
}
if (symptr == g_pc_port_funcs.intern_from_c("windowed").offset || symptr == s7.offset) {
Display::GetMainDisplay()->get_display_manager()->enqueue_set_window_display_mode(
game_settings::DisplaySettings::DisplayMode::Windowed, window_width, window_height);
} else if (symptr == g_pc_port_funcs.intern_from_c("borderless").offset) {
Display::GetMainDisplay()->get_display_manager()->enqueue_set_window_display_mode(
game_settings::DisplaySettings::DisplayMode::Borderless, window_width, window_height);
} else if (symptr == g_pc_port_funcs.intern_from_c("fullscreen").offset) {
Display::GetMainDisplay()->get_display_manager()->enqueue_set_window_display_mode(
game_settings::DisplaySettings::DisplayMode::Fullscreen, window_width, window_height);
}
}
u64 pc_get_display_count() {
if (Display::GetMainDisplay()) {
return Display::GetMainDisplay()->get_display_manager()->num_connected_displays();
}
return 0;
}
void pc_get_active_display_size(u32 w_ptr, u32 h_ptr) {
if (!Display::GetMainDisplay()) {
return;
}
if (w_ptr) {
auto w_out = Ptr<s64>(w_ptr).c();
if (w_out) {
*w_out = Display::GetMainDisplay()->get_display_manager()->get_screen_width();
}
}
if (h_ptr) {
auto h_out = Ptr<s64>(h_ptr).c();
if (h_out) {
*h_out = Display::GetMainDisplay()->get_display_manager()->get_screen_height();
}
}
}
s64 pc_get_active_display_refresh_rate() {
if (Display::GetMainDisplay()) {
return Display::GetMainDisplay()->get_display_manager()->get_active_display_refresh_rate();
}
return 0;
}
void pc_get_window_size(u32 w_ptr, u32 h_ptr) {
if (!Display::GetMainDisplay()) {
return;
}
if (w_ptr) {
auto w = Ptr<s64>(w_ptr).c();
if (w) {
*w = Display::GetMainDisplay()->get_display_manager()->get_window_width();
}
}
if (h_ptr) {
auto h = Ptr<s64>(h_ptr).c();
if (h) {
*h = Display::GetMainDisplay()->get_display_manager()->get_window_height();
}
}
}
void pc_get_window_scale(u32 x_ptr, u32 y_ptr) {
if (!Display::GetMainDisplay()) {
return;
}
if (x_ptr) {
auto x = Ptr<float>(x_ptr).c();
if (x) {
*x = Display::GetMainDisplay()->get_display_manager()->get_window_scale_x();
}
}
if (y_ptr) {
auto y = Ptr<float>(y_ptr).c();
if (y) {
*y = Display::GetMainDisplay()->get_display_manager()->get_window_scale_y();
}
}
}
void pc_set_window_size(u64 width, u64 height) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_display_manager()->enqueue_set_window_size(width, height);
}
}
s64 pc_get_num_resolutions(u32 for_windowed) {
if (Display::GetMainDisplay()) {
return Display::GetMainDisplay()->get_display_manager()->get_num_resolutions(
symbol_to_bool(for_windowed));
}
return 0;
}
void pc_get_resolution(u32 id, u32 for_windowed, u32 w_ptr, u32 h_ptr) {
if (Display::GetMainDisplay()) {
auto res = Display::GetMainDisplay()->get_display_manager()->get_resolution(
id, symbol_to_bool(for_windowed));
auto w = Ptr<s64>(w_ptr).c();
if (w) {
*w = res.width;
}
auto h = Ptr<s64>(h_ptr).c();
if (h) {
*h = res.height;
}
}
}
u64 pc_is_supported_resolution(u64 width, u64 height) {
if (Display::GetMainDisplay()) {
return bool_to_symbol(
Display::GetMainDisplay()->get_display_manager()->is_supported_resolution(width, height));
}
return bool_to_symbol(false);
}
u64 pc_get_controller_name(u32 id, u32 str_dest_ptr) {
std::string name = "";
if (Display::GetMainDisplay()) {
name = Display::GetMainDisplay()->get_input_manager()->get_controller_name(id);
}
if (name.empty()) {
return bool_to_symbol(false);
}
if (g_game_version == GameVersion::Jak1) {
// The Jak 1 font has only caps
name = str_util::to_upper(name).c_str();
}
// Encode the string to the game font
const auto encoded_name = get_font_bank_from_game_version(g_game_version)
->convert_utf8_to_game(str_util::titlize(name).c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), encoded_name.c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::titlize(encoded_name).c_str());
return bool_to_symbol(true);
}
u64 pc_get_current_bind(s32 bind_assignment_info, u32 str_dest_ptr) {
if (!Display::GetMainDisplay()) {
// TODO - return something that lets the runtime use a translatable string if unknown
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::to_upper("unknown").c_str());
return bool_to_symbol(true);
}
auto info = bind_assignment_info ? Ptr<BindAssignmentInfo>(bind_assignment_info).c() : NULL;
auto port = (int)info->port;
auto device_type = (int)info->device_type;
auto for_button = info->buttons != s7.offset;
auto input_idx = (int)info->input_idx;
auto analog_min_range = info->analog_min_range != s7.offset;
if (Display::GetMainDisplay()) {
auto name = Display::GetMainDisplay()->get_input_manager()->get_current_bind(
port, (InputDeviceType)device_type, for_button, input_idx, analog_min_range);
if (name.empty()) {
return bool_to_symbol(false);
}
if (g_game_version == GameVersion::Jak1) {
// The Jak 1 font has only caps
name = str_util::to_upper(name).c_str();
}
// Encode the string to the game font
const auto encoded_name = get_font_bank_from_game_version(g_game_version)
->convert_utf8_to_game(str_util::titlize(name).c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), encoded_name.c_str());
return bool_to_symbol(true);
}
// TODO - return something that lets the runtime use a translatable string if unknown
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::to_upper("unknown").c_str());
return bool_to_symbol(true);
}
u64 pc_get_controller_count() {
if (Display::GetMainDisplay()) {
return Display::GetMainDisplay()->get_input_manager()->get_num_controllers();
}
return 0;
}
u64 pc_get_controller_index(u32 port) {
if (Display::GetMainDisplay()) {
return Display::GetMainDisplay()->get_input_manager()->get_controller_index(port);
}
return 0;
}
void pc_set_controller(u32 controller_id, u32 port) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_input_manager()->set_controller_for_port(controller_id, port);
}
}
u32 pc_get_keyboard_enabled() {
if (Display::GetMainDisplay()) {
return bool_to_symbol(Display::GetMainDisplay()->get_input_manager()->is_keyboard_enabled());
}
return bool_to_symbol(false);
}
void pc_set_keyboard_enabled(u32 sym_val) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_input_manager()->enable_keyboard(symbol_to_bool(sym_val));
}
}
void pc_set_mouse_options(u32 enabled, u32 control_camera, u32 control_movement) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_input_manager()->enqueue_update_mouse_options(
symbol_to_bool(enabled), symbol_to_bool(control_camera), symbol_to_bool(control_movement));
}
}
void pc_set_mouse_camera_sens(u32 xsens, u32 ysens) {
float xsens_val;
memcpy(&xsens_val, &xsens, 4);
float ysens_val;
memcpy(&ysens_val, &ysens, 4);
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_input_manager()->set_camera_sens(xsens_val, ysens_val);
}
}
void pc_ignore_background_controller_events(u32 sym_val) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_input_manager()->enqueue_ignore_background_controller_events(
symbol_to_bool(sym_val));
}
}
u64 pc_current_controller_has_led() {
if (Display::GetMainDisplay()) {
return bool_to_symbol(Display::GetMainDisplay()->get_input_manager()->controller_has_led(0));
}
return bool_to_symbol(false);
}
u64 pc_current_controller_has_rumble() {
if (Display::GetMainDisplay()) {
return bool_to_symbol(Display::GetMainDisplay()->get_input_manager()->controller_has_rumble(0));
}
return bool_to_symbol(false);
}
void pc_set_controller_led(const int port, const u8 red, const u8 green, const u8 blue) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_input_manager()->enqueue_set_controller_led(port, red, green,
blue);
}
}
u64 pc_waiting_for_bind() {
if (Display::GetMainDisplay()) {
return bool_to_symbol(Display::GetMainDisplay()->get_input_manager()->get_waiting_for_bind());
}
return bool_to_symbol(false);
}
void pc_set_waiting_for_bind(InputDeviceType device_type,
u32 for_analog,
u32 for_minimum_analog,
s32 input_idx) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_input_manager()->set_wait_for_bind(
device_type, symbol_to_bool(for_analog), symbol_to_bool(for_minimum_analog), input_idx);
}