forked from cleolibrary/CLEO4
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathText.cpp
More file actions
763 lines (621 loc) · 26.3 KB
/
Text.cpp
File metadata and controls
763 lines (621 loc) · 26.3 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
#include "plugin.h"
#include "CLEO.h"
#include "CLEO_Utils.h"
#include "ScriptDrawing.h"
#include "CTextManager.h"
#include <CGame.h>
#include <CHud.h>
#include <CMessages.h>
#include <CModelInfo.h>
#include <CTheScripts.h>
#include <CMissionCleanup.h>
#include <CTxdStore.h>
#include <CText.h>
#include <shlwapi.h>
using namespace CLEO;
using namespace plugin;
class Text
{
public:
static ScriptDrawing scriptDrawing;
static CTextManager textManager;
static const size_t MessageQueueSize = 8; // matches CMessages::BriefMessages queue size
static const size_t BriefSize = 20; // matches CMessages::PreviousBriefs count
static const size_t BigStylesCount = 7; // matches CMessages::BIGMessages count
static size_t queueIdx;
static size_t briefIdx;
static char messageQueue[MessageQueueSize][400];
static char bigMessages[BigStylesCount][MAX_STR_LEN + 1];
static char messageNow[400];
static char briefs[BriefSize][400];
static WORD genericLabelCounter;
bool m_initialized = false;
MemPatch m_patchCTextGet;
Text()
{
if (!PluginCheckCleoVersion()) return;
// register opcodes
CLEO_RegisterOpcode(0x0390, opcode_0390); // load_texture_dictionary
CLEO_RegisterOpcode(0x0391, opcode_0391); // remove_texture_dictionary
CLEO_RegisterOpcode(0x0ACA, opcode_0ACA); // print_help_string
CLEO_RegisterOpcode(0x0ACB, opcode_0ACB); // print_big_string
CLEO_RegisterOpcode(0x0ACC, opcode_0ACC); // print_string
CLEO_RegisterOpcode(0x0ACD, opcode_0ACD); // print_string_now
CLEO_RegisterOpcode(0x0ACE, opcode_0ACE); // print_help_formatted
CLEO_RegisterOpcode(0x0ACF, opcode_0ACF); // print_big_formatted
CLEO_RegisterOpcode(0x0AD0, opcode_0AD0); // print_formatted
CLEO_RegisterOpcode(0x0AD1, opcode_0AD1); // print_formatted_now
CLEO_RegisterOpcode(0x0AD3, opcode_0AD3); // string_format
CLEO_RegisterOpcode(0x0AD4, opcode_0AD4); // scan_string
CLEO_RegisterOpcode(0x0ADB, opcode_0ADB); // get_name_of_vehicle_model
CLEO_RegisterOpcode(0x0ADE, opcode_0ADE); // get_text_label_string
CLEO_RegisterOpcode(0x0ADF, opcode_0ADF); // add_text_label
CLEO_RegisterOpcode(0x0AE0, opcode_0AE0); // remove_text_label
CLEO_RegisterOpcode(0x0AED, opcode_0AED); // string_float_format
CLEO_RegisterOpcode(0x2600, opcode_2600); // is_text_empty
CLEO_RegisterOpcode(0x2601, opcode_2601); // is_text_equal
CLEO_RegisterOpcode(0x2602, opcode_2602); // is_text_in_text
CLEO_RegisterOpcode(0x2603, opcode_2603); // is_text_prefix
CLEO_RegisterOpcode(0x2604, opcode_2604); // is_text_sufix
CLEO_RegisterOpcode(0x2605, opcode_2605); // display_text_formatted
CLEO_RegisterOpcode(0x2606, opcode_2606); // load_fxt
CLEO_RegisterOpcode(0x2607, opcode_2607); // unload_fxt
CLEO_RegisterOpcode(0x2608, opcode_2608); // get_text_length
CLEO_RegisterOpcode(0x2609, opcode_2609); // add_text_label_formatted
// register event callbacks
CLEO_RegisterCallback(eCallbackId::GameBegin, OnGameBegin);
CLEO_RegisterCallback(eCallbackId::GameProcessBefore, OnGameProcessBefore);
CLEO_RegisterCallback(eCallbackId::GameEnd, OnGameEnd);
CLEO_RegisterCallback(eCallbackId::ScriptProcessBefore, OnScriptBeforeProcess);
CLEO_RegisterCallback(eCallbackId::ScriptProcessAfter, OnScriptAfterProcess);
CLEO_RegisterCallback(eCallbackId::ScriptUnregister, OnScriptUnregister);
CLEO_RegisterCallback(eCallbackId::ScriptDraw, OnScriptDraw);
CLEO_RegisterCallback(eCallbackId::DrawingFinished, OnDrawingFinished);
}
~Text()
{
CLEO_UnregisterCallback(eCallbackId::GameBegin, OnGameBegin);
CLEO_UnregisterCallback(eCallbackId::GameProcessBefore, OnGameProcessBefore);
CLEO_UnregisterCallback(eCallbackId::GameEnd, OnGameEnd);
CLEO_UnregisterCallback(eCallbackId::ScriptProcessBefore, OnScriptBeforeProcess);
CLEO_UnregisterCallback(eCallbackId::ScriptProcessAfter, OnScriptAfterProcess);
CLEO_UnregisterCallback(eCallbackId::ScriptUnregister, OnScriptUnregister);
CLEO_UnregisterCallback(eCallbackId::ScriptDraw, OnScriptDraw);
CLEO_UnregisterCallback(eCallbackId::DrawingFinished, OnDrawingFinished);
m_patchCTextGet.Apply(); // undo hook
}
static void __stdcall OnGameBegin(DWORD saveSlot) { textManager.LoadFxts(); }
static void __stdcall OnGameProcessBefore() { genericLabelCounter = 0; }
static void __stdcall OnGameEnd() { textManager.Clear(); }
static bool __stdcall OnScriptBeforeProcess(CLEO::CRunningScript* pScript)
{
scriptDrawing.ScriptProcessingBegin(pScript);
return true;
}
static void __stdcall OnScriptAfterProcess(CLEO::CRunningScript* pScript)
{
scriptDrawing.ScriptProcessingEnd(pScript);
}
static void __stdcall OnScriptUnregister(CLEO::CRunningScript* pScript) { scriptDrawing.ScriptUnregister(pScript); }
static void __stdcall OnScriptDraw(bool beforeFade) { scriptDrawing.Draw(beforeFade); }
static void WINAPI OnDrawingFinished()
{
// late initialization
if (!instance.m_initialized)
{
instance.m_patchCTextGet = MemPatchJump(gaddrof(CText::Get), &HOOK_CTextGet);
instance.m_initialized = true;
}
}
// hook of game's CText::Get
static const char* __fastcall HOOK_CTextGet(CText* text, int dummy, const char* gxt)
{
if ((gxt[0] == '\0') || (gxt[0] == ' ')) return "";
// check CLEO's texts
auto result = Text::textManager.LocateFxt(gxt);
if (result != nullptr) return result;
// call original function
instance.m_patchCTextGet.Apply(); // restore original function code
result = text->Get(gxt);
MemPatchJump(gaddrof(CText::Get), &HOOK_CTextGet); // reinstall our hook
return result;
}
static bool CanTextInThisSlotBeAddedToBrief(const char* slot)
{
// content dedup: exits early if another entry already holds the same text.
for (size_t i = 0; i < BriefSize; i++)
{
const auto pText = CMessages::PreviousBriefs[i].m_pText;
if (pText == nullptr) break; // end of used entries
if (pText == slot) continue; // stale self-reference, skip to avoid a false positive duplicate
if (strcmp(pText, slot) == 0) return false; // content duplicate, exit
}
// stale pointer eviction: prevents the game's pointer-based dedup from rejecting the insertion
for (size_t i = 0; i < BriefSize; i++)
{
auto& pText = CMessages::PreviousBriefs[i].m_pText;
if (pText == slot)
{
pText = nullptr;
break;
}
}
return true;
}
static void AddToBriefHistory(const char* text)
{
if (CTheScripts::bAddNextMessageToPreviousBriefs)
{
briefIdx = (briefIdx + 1) % BriefSize;
auto& briefSlot = briefs[briefIdx];
strncpy_s(briefSlot, text, sizeof(briefSlot) - 1);
if (CanTextInThisSlotBeAddedToBrief(briefSlot))
{
CMessages::AddToPreviousBriefArray(briefSlot, -1, -1, -1, -1, -1, -1, 0);
}
}
CTheScripts::bAddNextMessageToPreviousBriefs = true;
}
static void AddToMessageQueue(CLEO::CRunningScript* pScript, const char* text, int time)
{
queueIdx = (queueIdx + 1) % MessageQueueSize;
auto& messageSlot = messageQueue[queueIdx];
strncpy_s(messageSlot, text, sizeof(messageSlot) - 1);
bool addToBrief = false;
if (!IsLegacyScript(pScript))
{
if (CTheScripts::bAddNextMessageToPreviousBriefs)
{
addToBrief = CanTextInThisSlotBeAddedToBrief(messageSlot);
}
CTheScripts::bAddNextMessageToPreviousBriefs = true;
}
CMessages::AddMessage(messageSlot, time, false, addToBrief);
}
// 0390=1,load_texture_dictionary %1s%
static OpcodeResult __stdcall opcode_0390(CLEO::CRunningScript* thread)
{
if (!thread->IsCustom())
{
return CLEO_CallNativeOpcode(thread, 0x0390); // call original opcode
}
OPCODE_READ_PARAM_STRING(txdName);
if (instance.scriptDrawing.IsTxdLoaded(thread, txdName))
{
instance.scriptDrawing.MakeActiveTxd(thread, txdName);
}
else
{
std::string path = "models\\txd\\";
path += txdName;
path += ".txd";
auto slot = CTxdStore::FindTxdSlot("script");
if (slot == -1) slot = CTxdStore::AddTxdSlot("script");
CTxdStore::LoadTxd(slot, path.c_str());
CTxdStore::AddRef(slot);
instance.scriptDrawing.StoreTxd(thread, txdName);
}
return OR_CONTINUE;
}
// 0391=0,remove_texture_dictionary
static OpcodeResult __stdcall opcode_0391(CLEO::CRunningScript* thread)
{
if (thread->IsCustom())
{
// CLEO unloads all textures when the custom script ends.
// See https://github.com/cleolibrary/CLEO5/wiki/Script-TXD-Issues
return OR_CONTINUE;
}
return CLEO_CallNativeOpcode(thread, 0x0391);
}
// 0ACA=1,print_help_string %1d%
static OpcodeResult __stdcall opcode_0ACA(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(text);
CHud::SetHelpMessage(text, true, false, false);
if (!IsLegacyScript(thread)) AddToBriefHistory(text);
return OR_CONTINUE;
}
// 0ACB=3,show_styled_text %1d% time %2d% style %3d%
static OpcodeResult __stdcall opcode_0ACB(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(text);
auto time = OPCODE_READ_PARAM_INT();
auto style = OPCODE_READ_PARAM_INT();
auto styleIdx = std::clamp(style, 0, (int)BigStylesCount - 1);
auto& bigMessage = bigMessages[styleIdx];
strncpy_s(bigMessage, text, sizeof(bigMessage) - 1);
CMessages::AddBigMessage(bigMessage, time, style - 1);
return OR_CONTINUE;
}
// 0ACC=2,print_string %1d% time %2d%
static OpcodeResult __stdcall opcode_0ACC(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(text);
auto time = OPCODE_READ_PARAM_INT();
AddToMessageQueue(thread, text, time);
return OR_CONTINUE;
}
// 0ACD=2,print_string_now %1d% time %2d%
static OpcodeResult __stdcall opcode_0ACD(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(text);
auto time = OPCODE_READ_PARAM_INT();
strncpy_s(messageNow, text, sizeof(messageNow) - 1);
CMessages::AddMessageJumpQ(messageNow, time, false, false);
if (!IsLegacyScript(thread)) AddToBriefHistory(messageNow);
return OR_CONTINUE;
}
// 0ACE=-1,print_help_formatted %1d%
static OpcodeResult __stdcall opcode_0ACE(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING_FORMATTED(text);
CHud::SetHelpMessage(text, true, false, false);
if (!IsLegacyScript(thread)) AddToBriefHistory(text);
return OR_CONTINUE;
}
// 0ACF=-1,print_big_formatted %1d% time %2d% style %3d%
static OpcodeResult __stdcall opcode_0ACF(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(format);
auto time = OPCODE_READ_PARAM_INT();
auto style = OPCODE_READ_PARAM_INT();
OPCODE_READ_PARAMS_FORMATTED(format, text);
auto styleIdx = std::clamp(style, 0, (int)BigStylesCount - 1);
auto& bigMessage = bigMessages[styleIdx];
strncpy_s(bigMessage, text, sizeof(bigMessage) - 1);
CMessages::AddBigMessage(bigMessage, time, style - 1);
return OR_CONTINUE;
}
// 0AD0=-1,print_formatted %1d% time %2d%
static OpcodeResult __stdcall opcode_0AD0(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(format);
auto time = OPCODE_READ_PARAM_INT();
OPCODE_READ_PARAMS_FORMATTED(format, text);
AddToMessageQueue(thread, text, time);
return OR_CONTINUE;
}
// 0AD1=-1,print_formatted_now %1d% time %2d%
static OpcodeResult __stdcall opcode_0AD1(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(format);
auto time = OPCODE_READ_PARAM_INT();
OPCODE_READ_PARAMS_FORMATTED(format, text);
strncpy_s(messageNow, text, sizeof(messageNow) - 1);
CMessages::AddMessageJumpQ(messageNow, time, false, false);
if (!IsLegacyScript(thread)) AddToBriefHistory(messageNow);
return OR_CONTINUE;
}
// 0AD3=-1,string %1d% format %2d% ...
static OpcodeResult __stdcall opcode_0AD3(CLEO::CRunningScript* thread)
{
auto result = OPCODE_READ_PARAM_OUTPUT_VAR_STRING();
OPCODE_READ_PARAM_STRING_FORMATTED(text);
OPCODE_WRITE_PARAM_VAR_STRING(result, text);
return OR_CONTINUE;
}
// 0AD4=-1,%3d% = scan_string %1d% format %2d% //IF and SET
static OpcodeResult __stdcall opcode_0AD4(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(src);
OPCODE_READ_PARAM_STRING(format);
auto readCount = OPCODE_READ_PARAM_OUTPUT_VAR_INT(); // store_to
// format string tokens processing helper
const char* formatPos = format;
auto GetNextTokenType = [&]() {
while (true)
{
if (*formatPos == '\0') return DT_END;
if (*formatPos == '%') // formatting sequence start
{
if (formatPos[1] == '%' || // escaped % character
formatPos[1] == '*') // sscanf non-capturing token
{
formatPos += 2;
continue;
}
while (true)
{
formatPos++; // next char
if (*formatPos == '\0')
{
return DT_INVALID;
}
// width specification
if (*formatPos >= '0' && *formatPos <= '9')
{
formatPos++;
continue;
}
switch (*formatPos)
{
case 's':
return DT_STRING;
default:
return DT_VAR; // any32. TODO: actually parse and verify other types?
}
}
}
formatPos++;
}
};
// collect provided by caller store_to variables
size_t outputParamCount = 0;
SCRIPT_VAR* outputParams[35];
struct StringParamDesc
{
bool used = false;
StringParamBufferInfo target;
std::string str;
} stringParams[35];
for (int i = 0; i < 35; i++)
{
auto paramType = thread->PeekDataType();
auto formatTokenType = GetNextTokenType();
if (formatTokenType == DT_INVALID)
{
SUSPEND("Invalid string format sequence ('%s')", format);
}
if (paramType == DT_END)
{
if (formatTokenType != DT_END)
{
SUSPEND_COMPAT("More tokens than variables in format string %s", format);
}
break;
}
if (formatTokenType == DT_END)
{
SUSPEND_COMPAT("More variables than tokens in format string %s", format);
}
if (IsVarString(paramType))
{
if (!IsStrictValidation(thread))
{
// older CLEOs did not care about string variable size limitations
// just give pointer to the variable's data and allow overflow depending on input data
outputParams[i] = CLEO_GetPointerToScriptVariable(thread);
}
else
{
stringParams[i].used = true;
stringParams[i].target = OPCODE_READ_PARAM_OUTPUT_VAR_STRING();
stringParams[i].str.resize(MAX_STR_LEN); // temp storage
outputParams[i] = (SCRIPT_VAR*)stringParams[i].str.data();
}
}
else if (formatTokenType == DT_STRING) // `%s`
{
auto ptr = OPCODE_READ_PARAM_PTR();
outputParams[i] = (SCRIPT_VAR*)ptr;
}
else
{
outputParams[i] = OPCODE_READ_PARAM_OUTPUT_VAR_ANY32();
}
outputParamCount++;
}
CLEO_SkipUnusedVarArgs(thread); // and var args terminator
#pragma warning(suppress : 4996) // sscanf_s would expect additional arg after each %s arg
*readCount = sscanf(
src, format, outputParams[0], outputParams[1], outputParams[2], outputParams[3], outputParams[4],
outputParams[5], outputParams[6], outputParams[7], outputParams[8], outputParams[9], outputParams[10],
outputParams[11], outputParams[12], outputParams[13], outputParams[14], outputParams[15], outputParams[16],
outputParams[17], outputParams[18], outputParams[19], outputParams[20], outputParams[21], outputParams[22],
outputParams[23], outputParams[24], outputParams[25], outputParams[26], outputParams[27], outputParams[28],
outputParams[29], outputParams[30], outputParams[31], outputParams[32], outputParams[33], outputParams[34]
);
// transfer string params to target variables
for (auto& p : stringParams)
{
if (p.used) OPCODE_WRITE_PARAM_VAR_STRING(p.target, p.str.c_str());
}
OPCODE_CONDITION_RESULT(outputParamCount == *readCount);
return OR_CONTINUE;
}
// 0ADB=2,%2d% = car_model %1d% name
static OpcodeResult __stdcall opcode_0ADB(CLEO::CRunningScript* thread)
{
auto modelIndex = OPCODE_READ_PARAM_UINT();
CVehicleModelInfo* model;
// if 1.0 US, prefer GetModelInfo function - makes it compatible with fastman92's limit adjuster
if (CLEO_GetGameVersion() == CLEO::GV_US10)
model = plugin::CallAndReturn<CVehicleModelInfo*, 0x403DA0, int>(modelIndex);
else
model = reinterpret_cast<CVehicleModelInfo*>(CModelInfo::ms_modelInfoPtrs[modelIndex]);
auto str = std::string(std::string_view(model->m_szGameName, sizeof(model->m_szGameName))); // to proper cstr
OPCODE_WRITE_PARAM_STRING(str.c_str());
return OR_CONTINUE;
}
// 0ADE=2,%2d% = text_by_GXT_entry %1d%
static OpcodeResult __stdcall opcode_0ADE(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING_LEN(gxt, 7); // GXT labels can be max 7 character long
auto txt = textManager.Get(gxt);
if (IsVarString(thread->PeekDataType()))
{
OPCODE_WRITE_PARAM_STRING(txt);
}
else
{
OPCODE_WRITE_PARAM_PTR(txt); // address of the text
}
return OR_CONTINUE;
}
// 0ADF=2,add_dynamic_GXT_entry %1d% text %2d%
static OpcodeResult __stdcall opcode_0ADF(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING_LEN(gxt, 7); // GXT labels can be max 7 character long
OPCODE_READ_PARAM_STRING(txt);
textManager.AddFxt(gxt, txt);
return OR_CONTINUE;
}
// 0AE0=1,remove_dynamic_GXT_entry %1d%
static OpcodeResult __stdcall opcode_0AE0(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING_LEN(gxt, 7); // GXT labels can be max 7 character long
textManager.RemoveFxt(gxt);
return OR_CONTINUE;
}
// 0AED=3,%3d% = float %1d% to_string_format %2d%
static OpcodeResult __stdcall opcode_0AED(CLEO::CRunningScript* thread)
{
// this opcode is useless now
auto val = OPCODE_READ_PARAM_FLOAT();
OPCODE_READ_PARAM_STRING(format);
char result[32];
sprintf_s(result, sizeof(result), format, val);
OPCODE_WRITE_PARAM_STRING(result);
return OR_CONTINUE;
}
// 2600=1, is_text_empty %1s%
static OpcodeResult __stdcall opcode_2600(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(str);
OPCODE_CONDITION_RESULT(str[0] == '\0');
return OR_CONTINUE;
}
// 2601=3, is_text_equal %1s% another %2s% ignore_case %3d%
static OpcodeResult __stdcall opcode_2601(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(a);
OPCODE_READ_PARAM_STRING(b);
auto ignoreCase = OPCODE_READ_PARAM_BOOL();
auto result = ignoreCase ? _stricmp(a, b) : strcmp(a, b);
OPCODE_CONDITION_RESULT(result == 0);
return OR_CONTINUE;
}
// 2602=3, is_text_in_text %1s% sub_text %2s% ignore_case %3d%
static OpcodeResult __stdcall opcode_2602(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(str);
OPCODE_READ_PARAM_STRING(substr);
auto ignoreCase = OPCODE_READ_PARAM_BOOL();
if (substr[0] == '\0')
{
OPCODE_CONDITION_RESULT(true);
return OR_CONTINUE;
}
auto result = ignoreCase ? StrStrIA(str, substr) : strstr(str, substr);
OPCODE_CONDITION_RESULT(result != nullptr);
return OR_CONTINUE;
}
// 2603=3, is_text_prefix %1s% prefix %2s% ignore_case %3d%
static OpcodeResult __stdcall opcode_2603(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(str);
OPCODE_READ_PARAM_STRING(prefix);
auto ignoreCase = OPCODE_READ_PARAM_BOOL();
auto prefixLen = strlen(prefix);
auto result = ignoreCase ? _strnicmp(str, prefix, prefixLen) : strncmp(str, prefix, prefixLen);
OPCODE_CONDITION_RESULT(result == 0);
return OR_CONTINUE;
}
// 2604=3, is_text_sufix %1s% sufix %2s% ignore_case %3d%
static OpcodeResult __stdcall opcode_2604(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(str);
OPCODE_READ_PARAM_STRING(sufix);
auto ignoreCase = OPCODE_READ_PARAM_BOOL();
auto strLen = strlen(str);
auto sufixLen = strlen(sufix);
if (sufixLen > strLen)
{
OPCODE_CONDITION_RESULT(false);
return OR_CONTINUE;
}
auto offset = strLen - sufixLen;
auto result = ignoreCase ? _stricmp(str + offset, sufix) : strcmp(str + offset, sufix);
OPCODE_CONDITION_RESULT(result == 0);
return OR_CONTINUE;
}
// 2605=-1,display_text_formatted offset_left %1d% offset_top %2d% format %3d% args
static OpcodeResult __stdcall opcode_2605(CLEO::CRunningScript* thread)
{
auto posX = OPCODE_READ_PARAM_FLOAT();
auto posY = OPCODE_READ_PARAM_FLOAT();
OPCODE_READ_PARAM_STRING_FORMATTED(text);
if (CTheScripts::NumberOfIntroTextLinesThisFrame >= 0x60) // GTA SA CTheScripts::IntroTextLines capacity
{
LOG_WARNING(thread, "Display text limit (%d) exceeded in script %s", 0x60, ScriptInfoStr(thread).c_str());
return OR_CONTINUE;
}
// new generic GXT label
// includes unprintable character, to ensure there will be no collision with user GXT labels
char gxt[9] = {0x01, 'C', 'L', 'E', 0x00, 0x00, 0x00, 0x00, 0x00}; // enough space for even worst case scenario
_itoa_s(genericLabelCounter, gxt + 4, sizeof(gxt) - 4, 36); // 0xFFFF -> "1ekf"
genericLabelCounter++;
textManager.AddFxt(gxt, text);
auto& draw = CTheScripts::IntroTextLines[CTheScripts::NumberOfIntroTextLinesThisFrame];
memcpy(&draw.xPosition, &posX, sizeof(draw.xPosition)); // invalid type in Plugin SDK. Just copy memory
memcpy(&draw.yPosition, &posY, sizeof(draw.yPosition));
strcpy_s(draw.text, gxt);
CTheScripts::NumberOfIntroTextLinesThisFrame++;
return OR_CONTINUE;
}
// 2606=1, load_fxt %1d%
static OpcodeResult __stdcall opcode_2606(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
size_t added = 0;
try
{
std::ifstream stream(filename);
added = textManager.ParseFxtFile(stream, true, false);
}
catch (std::exception& ex)
{
LOG_WARNING(0, "Loading of FXT file '%s' failed: \n%s", filename, ex.what());
}
OPCODE_CONDITION_RESULT(added != 0);
return OR_CONTINUE;
}
// 2607=1, unload_fxt %1d%
static OpcodeResult __stdcall opcode_2607(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
size_t removed = 0;
try
{
std::ifstream stream(filename);
removed = textManager.ParseFxtFile(stream, true, true);
}
catch (std::exception& ex)
{
LOG_WARNING(0, "Unloading of FXT file '%s' failed: \n%s", filename, ex.what());
}
OPCODE_CONDITION_RESULT(removed != 0);
return OR_CONTINUE;
}
// 2608=3,get_text_length %1d% store_to %2d%
static OpcodeResult __stdcall opcode_2608(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING(str);
auto result = strlen(str);
OPCODE_WRITE_PARAM_INT(result);
return OR_CONTINUE;
}
// 2609=-1,add_text_label_formatted %1d% args %2d%
static OpcodeResult __stdcall opcode_2609(CLEO::CRunningScript* thread)
{
OPCODE_READ_PARAM_STRING_LEN(gxt, 7); // GXT labels can be max 7 character long
OPCODE_READ_PARAM_STRING_FORMATTED(str);
textManager.AddFxt(gxt, str);
return OR_CONTINUE;
}
} instance;
ScriptDrawing Text::scriptDrawing;
CTextManager Text::textManager;
size_t Text::queueIdx = 0;
size_t Text::briefIdx = 0;
char Text::messageQueue[Text::MessageQueueSize][400];
char Text::briefs[Text::BriefSize][400];
char Text::bigMessages[Text::BigStylesCount][MAX_STR_LEN + 1];
char Text::messageNow[400];
WORD Text::genericLabelCounter;
// exports
extern "C" __declspec(dllexport) RwTexture* GetScriptTexture(CLEO::CRunningScript* script, DWORD slot)
{
return instance.scriptDrawing.GetScriptTexture(script, slot);
}