-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInstructionProcessor.cs
752 lines (704 loc) · 31.8 KB
/
InstructionProcessor.cs
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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Input;
namespace LBEE_TranslationPatch
{
public static class InstructionProcessor
{
public static HashSet<char> CharCollection = new HashSet<char>();
public static Dictionary<string, Dictionary<int, int>> ScriptCommandRedirectors = new();
public static int GetCmdHeaderLength(byte[] command)
{
int SpecByte = command[3];
return Math.Min(SpecByte, 2) * 2 + 4;
}
public static string PostProcessText(string In)
{
// 这两个字体在绘制的时候有问题,由于字体太小,所以直接绘制到了字符的顶端
// 通过调整绘制的位置大概可以解决问题,但这里先替换为相近的字符,暂时规避问题。
return In.Replace('—', 'ー').Replace('-', 'ー').Replace('·', '・');
}
public static int GetStrLength(byte[] Command,int StartIndex)
{
int index = StartIndex;
while (Command[index] != 0 || Command[index + 1] != 0)
{
index += 2;
}
return index - StartIndex;
}
public static int GetSingleByteStrLength(byte[] Command, int StartIndex)
{
int index = StartIndex;
while (Command[index] != 0)
{
index++;
}
return index - StartIndex;
}
public static Dictionary<byte, Func<byte[], JObject?>> InstructionGetMapping = new ()
{
{ 0x1F, MESSAGE_GET },
{ 0x21, SELECT_GET },
{ 0x19, VARSTR_SET_GET },
{ 0x5A, TASK_GET },
{ 0x5C, BATTLE_GET },
{ 0x69, SAYAVOICETEXT_GET }
};
public static Dictionary<byte, Func<byte[], JObject, byte[]?>> InstructionSetMapping = new ()
{
{ 0x1F, MESSAGE_SET },
{ 0x21, SELECT_SET },
{ 0x19, VARSTR_SET_SET },
{ 0x5A, TASK_SET },
{ 0x5C, BATTLE_SET },
{ 0x69, SAYAVOICETEXT_SET }
};
public static Dictionary<byte, Func<List<LucaCommand>, int, LucaCommand[]?>> AssignCmdMapping = new ()
{
{ 14, TAIL4Ptr_ASSIGN_CMD }, // GOTO
{ 16, TAIL4Ptr_ASSIGN_CMD }, // GOSUB
{ 17, TAIL4Ptr_ASSIGN_CMD }, // IFY
{ 18, TAIL4Ptr_ASSIGN_CMD }, // IFN
{ 20, JUMP_ASSIGN_CMD }, // JUMP
{ 21, FARCALL_ASSIGN_CMD }, // FARCALL
{ 15, ONGOTO_ASSIGN_CMD } // ONGOTO
};
public static Dictionary<byte, Action<LucaCommand,LucaCommand[]>> FixPtrMapping = new()
{
{ 14, TAIL4Ptr_FIX_PTR },
{ 16, TAIL4Ptr_FIX_PTR },
{ 17, TAIL4Ptr_FIX_PTR },
{ 18, TAIL4Ptr_FIX_PTR },
{ 20, JUMP_FIX_PTR },
{ 21, FARCALL_FIX_PTR },
{ 15, ONGOTO_FIX_PTR }
};
public static JObject? MESSAGE_GET(byte[] command)
{
int index = GetCmdHeaderLength(command)+2;
int strALength = GetStrLength(command,index);
int strBLength = GetStrLength(command, index + strALength + 2);
var outObj = new JObject
{
["JP"] = Encoding.Unicode.GetString(command, index,strALength),
["EN"] = Encoding.Unicode.GetString(command, index+ strALength + 2, strBLength),
};
outObj["Translation"] = outObj["EN"];
return outObj;
}
public static byte[]? MESSAGE_SET(byte[] command, JObject inJsonObj)
{
int index = GetCmdHeaderLength(command)+2;
int strStart = index + GetStrLength(command, index) + 2;
int strEnd = GetStrLength(command, strStart) + strStart;
List<byte> newCommand = new List<byte>(command[..strStart]);
string Translation = PostProcessText(inJsonObj["Translation"]?.Value<string>()??"");
string EN = inJsonObj["EN"]?.Value<string>()??"";
if(Translation!=EN)
{
foreach(var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
}
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
newCommand.AddRange(command.Skip(strEnd));
// 不需要修正指令长度,交由上层修复
return newCommand.ToArray();
}
public static JObject? VARSTR_SET_GET(byte[] command)
{
JObject TrasnlationObj = new JObject();
int index = GetCmdHeaderLength(command) + 2; // Header+ID
TrasnlationObj["Text"] = Encoding.Unicode.GetString(command[index..(index+GetStrLength(command, index))]);
TrasnlationObj["Translation"] = TrasnlationObj["Text"];
return TrasnlationObj;
}
public static byte[]? VARSTR_SET_SET(byte[] command, JObject inJsonObj)
{
int index = GetCmdHeaderLength(command) + 2; // Header+ID
string Translation = PostProcessText(inJsonObj["Translation"]?.Value<string>() ?? "");
List<byte> newCommand = new List<byte>();
newCommand.AddRange(command[..index]);
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
newCommand.Add(0);
newCommand.Add(0);
foreach (var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
return newCommand.ToArray();
}
public static JObject? SELECT_GET(byte[] command)
{
JObject TrasnlationObj = new JObject();
int index = GetCmdHeaderLength(command) + 4*2; // Header+ID+VAR123
int StrLength = GetStrLength(command, index);
TrasnlationObj["JP"] = Encoding.Unicode.GetString(command[index..(index + StrLength)]);
index += StrLength + 2;
StrLength = GetStrLength(command, index);
TrasnlationObj["EN"] = Encoding.Unicode.GetString(command[index..(index + StrLength)]);
TrasnlationObj["Translation"] = TrasnlationObj["EN"];
return TrasnlationObj;
}
public static byte[]? SELECT_SET(byte[] command, JObject inJsonObj)
{
int index = GetCmdHeaderLength(command) + 4*2; // Header+ID
int StrLength = GetStrLength(command, index); // Jp
index += StrLength + 2;
StrLength = GetStrLength(command, index);
string Translation = PostProcessText(inJsonObj["Translation"]?.Value<string>() ?? "");
List<byte> newCommand = new List<byte>();
newCommand.AddRange(command[..index]);
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
newCommand.AddRange(command.Skip(index + StrLength));
foreach (var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
return newCommand.ToArray();
}
public static JObject? TASK_GET(byte[] command)
{
JObject TrasnlationObj = new JObject();
int index = GetCmdHeaderLength(command); // Header
int TaskID = command[index] + command[index + 1] * 256;
index += 2;
string? msgStr_jp1 = null;
string? msgStr_en1 = null;
string? msgStr_jp2 = null;
string? msgStr_en2 = null;
if (command.Length <= index)
{
return null;
}
if (TaskID == 4)
{
int TaskVar1 = command[index]+command[index+1]*256;
index += 2;
if (command.Length <= index)
{
return null;
}
if (TaskVar1 == 0 || TaskVar1 == 4 || TaskVar1 == 5 || TaskVar1 == 6)
{
index += 2; // TaskVar2
if (TaskVar1 == 6)
{
index += 2; //TaskVar3
}
int strLength = GetStrLength(command, index);
msgStr_jp1 = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength+2; // Include \0;
msgStr_en1 = Encoding.Unicode.GetString(command[index..(index + GetStrLength(command, index))]);
}
else if (TaskVar1 == 1)
{
index += 2 * 3; // TaskVar2,3,4
int strLength = GetStrLength(command, index);
msgStr_jp1 = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength+2;
strLength = GetStrLength(command, index);
msgStr_en1 = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength + 2;
strLength = GetStrLength(command, index);
msgStr_jp2 = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength+2;
msgStr_en2 = Encoding.Unicode.GetString(command[index..(index + GetStrLength(command, index))]);
}
}
else if (TaskID == 54)
{
// 只有英文?有点怪
int strLength = GetStrLength(command, index);
msgStr_en1 = Encoding.Unicode.GetString(command[index..(index + strLength)]);
}
else if (TaskID == 69)
{
index += 2;
int strLength = GetStrLength(command, index);
msgStr_jp1 = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength + 2;
strLength = GetStrLength(command, index);
msgStr_en1 = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength + 2;
strLength = GetStrLength(command, index);
msgStr_jp2 = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength + 2;
msgStr_en2 = Encoding.Unicode.GetString(command[index..(index + GetStrLength(command, index))]);
}
if (msgStr_en1 == null && msgStr_jp1 == null &&
msgStr_en2 == null && msgStr_jp2 == null)
{
return null;
}
if (msgStr_jp1 != null)
{
TrasnlationObj["JP1"] = msgStr_jp1;
}
if (msgStr_jp2 != null)
{
TrasnlationObj["JP2"] = msgStr_jp2;
}
if (msgStr_en1 != null)
{
TrasnlationObj["EN1"] = msgStr_en1;
TrasnlationObj["Translation1"] = msgStr_en1;
}
if (msgStr_en2 != null)
{
TrasnlationObj["EN2"] = msgStr_en2;
TrasnlationObj["Translation2"] = msgStr_en2;
}
return TrasnlationObj;
}
public static byte[]? TASK_SET(byte[] command, JObject inJsonObj)
{
int index = GetCmdHeaderLength(command); // Header
int TaskID = command[index] + command[index + 1] * 256;
index += 2;
if (command.Length <= index)
{
return null;
}
if (TaskID == 4)
{
int TaskVar1 = command[index] + command[index + 1] * 256;
index += 2;
if (command.Length <= index)
{
return null;
}
if (TaskVar1 == 0 || TaskVar1 == 4 || TaskVar1 == 5 || TaskVar1 == 6)
{
var newCommand = new List<byte>();
index += 2; // TaskVar2
if (TaskVar1 == 6)
{
index += 2; //TaskVar3
}
index += GetStrLength(command, index) + 2;
newCommand.AddRange(command[..index]);
int strLength = GetStrLength(command, index);
string Translation = PostProcessText(inJsonObj["Translation1"]?.Value<string>() ?? "");
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
newCommand.AddRange(command.Skip(index+strLength));
return newCommand.ToArray();
}
else if (TaskVar1 == 1)
{
var newCommand = new List<byte>();
index += 2 * 3; // TaskVar2,3,4
index += GetStrLength(command, index) + 2;
newCommand.AddRange(command[..index]); //str1
int strLength = GetStrLength(command, index);
string Translation = PostProcessText(inJsonObj["Translation1"]?.Value<string>() ?? "");
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
index += strLength + 2; // str2
strLength = GetStrLength(command, index);
// -2包含str2的\0
newCommand.AddRange(command[(index-2)..(index + strLength + 2)]);
index += strLength + 2; //str3
strLength = GetStrLength(command, index);
string Translation2 = PostProcessText(inJsonObj["Translation2"]?.Value<string>() ?? "");
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation2));
newCommand.AddRange(command.Skip(index + strLength)); //str4
foreach (var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
foreach (var newChar in Translation2.ToCharArray())
{
CharCollection.Add(newChar);
}
return newCommand.ToArray();
}
}
else if (TaskID == 54)
{
// 只有英文?有点怪
int strLength = GetStrLength(command, index);
string Translation = PostProcessText(inJsonObj["Translation1"]?.Value<string>() ?? "");
foreach (var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
List<byte> newCommand = new List<byte>(command[..index]);
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
newCommand.AddRange(command.Skip(index + strLength));
return newCommand.ToArray();
}
else if (TaskID == 69)
{
var newCommand = new List<byte>();
index += 2;
index += GetStrLength(command, index) + 2;
newCommand.AddRange(command[..index]); //str1
int strLength = GetStrLength(command, index);
string Translation = PostProcessText(inJsonObj["Translation1"]?.Value<string>() ?? "");
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
index += strLength + 2; // str2
strLength = GetStrLength(command, index);
newCommand.AddRange(command[(index-2)..(index + strLength + 2)]);
index += strLength + 2; //str3
strLength = GetStrLength(command, index);
string Translation2 = PostProcessText(inJsonObj["Translation2"]?.Value<string>() ?? "");
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation2));
newCommand.AddRange(command.Skip(index + strLength)); //str4
foreach (var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
foreach (var newChar in Translation2.ToCharArray())
{
CharCollection.Add(newChar);
}
return newCommand.ToArray();
}
return null;
}
public static JObject? BATTLE_GET(byte[] command)
{
JObject TrasnlationObj = new JObject();
int index = GetCmdHeaderLength(command); // Header+ID
int BattleID = command[index] + command[index+1] * 256;
string? msgStr_jp = null;
string? msgStr_en = null;
index += 2;
if(index >= command.Length)
{
return null;
}
/*if(BattleID == 300)
{
// LucaSystem中认为BattleID为300的指令只有日文文本,不确定是否需要翻译
// 英文语言不全?先返回空白,不做进一步处理
return null;
}*/
else if (BattleID == 101)
{
index += 2; //Skip Var1
int Var2 = command[index] + command[index + 1] * 256;
if (Var2 == 0)
{
index += 4; //Skip Var2,3
int strLength = GetSingleByteStrLength(command, index);
index += strLength + 1; // Skip ExprStr
strLength = GetStrLength(command, index);
msgStr_jp = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength + 2;
strLength = GetStrLength(command, index);
msgStr_en = Encoding.Unicode.GetString(command[index..(index + strLength)]);
}
else
{
// 当下的var2就是文本
int strLength = GetStrLength(command, index);
msgStr_jp = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength + 2;
strLength = GetStrLength(command, index);
msgStr_en = Encoding.Unicode.GetString(command[index..(index + strLength)]);
}
}
else if (BattleID == 102)
{
int Var1 = command[index] + command[index + 1] * 256;
if (Var1 == 0)
{
index += 4; //Skip Var1,2
int strLength = GetSingleByteStrLength(command, index);
index += strLength + 1; // 跳过ExprStr,ExprStr为单字节字符串
strLength = GetStrLength(command, index);
msgStr_jp = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength + 2;
strLength = GetStrLength(command, index);
msgStr_en = Encoding.Unicode.GetString(command[index..(index + strLength)]);
}
else
{
// 当下的Var1就是文本
int strLength = GetStrLength(command, index);
msgStr_jp = Encoding.Unicode.GetString(command[index..(index + strLength)]);
index += strLength + 2;
strLength = GetStrLength(command, index);
msgStr_en = Encoding.Unicode.GetString(command[index..(index + strLength)]);
}
}
if (msgStr_en == null && msgStr_jp == null)
{
return null;
}
if (msgStr_jp != null)
{
TrasnlationObj["JP"] = msgStr_jp;
}
if (msgStr_en != null)
{
TrasnlationObj["EN"] = msgStr_en;
TrasnlationObj["Translation"] = msgStr_en;
}
return TrasnlationObj;
}
public static byte[]? BATTLE_SET(byte[] command, JObject inJsonObj)
{
JObject TrasnlationObj = new JObject();
int index = GetCmdHeaderLength(command); // Header+ID
int BattleID = command[index] + command[index+1] * 256;
index += 2;
if (index >= command.Length)
{
return null;
}
/*if (BattleID == 300)
{
return command;
}*/
else if (BattleID == 101)
{
index += 2; //Skip Var1
int Var2 = command[index] + command[index + 1] * 256;
string Translation = PostProcessText(inJsonObj["Translation"]?.Value<string>() ?? "");
foreach (var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
List<byte>? newCommand = null;
if (Var2 == 0)
{
index += 4; //Skip Var2,3
int strLength = GetSingleByteStrLength(command, index);
index += strLength + 1; // Skip ExprStr
strLength = GetStrLength(command, index);
index += strLength + 2; // Skip JP
newCommand = new List<byte>(command[..index]);
strLength = GetStrLength(command, index);
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
index += strLength;
}
else
{
// 当下的var2就是文本
int strLength = GetStrLength(command, index);
index += strLength + 2;
newCommand = new List<byte>(command[..index]);
strLength = GetStrLength(command, index);
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
index += strLength;
}
newCommand.AddRange(command.Skip(index));
return newCommand.ToArray();
}
else if (BattleID == 102)
{
string Translation = PostProcessText(inJsonObj["Translation"]?.Value<string>() ?? "");
foreach (var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
List<byte>? newCommand = null;
int Var1 = command[index] + command[index + 1] * 256;
if (Var1 == 0)
{
index += 4; //Skip Var1,2
int strLength = GetSingleByteStrLength(command, index);
index += strLength + 1; // Skip ExprStr
strLength = GetStrLength(command, index);
index += strLength + 2;
newCommand = new List<byte>(command[..index]);
strLength = GetStrLength(command, index);
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
index += strLength;
}
else
{
// 当下的Var1就是文本
int strLength = GetStrLength(command, index);
index += strLength + 2;
newCommand = new List<byte>(command[..index]);
strLength = GetStrLength(command, index);
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
index += strLength;
}
newCommand.AddRange(command.Skip(index));
return newCommand.ToArray();
}
return null;
}
public static JObject? SAYAVOICETEXT_GET(byte[] command)
{
JObject TrasnlationObj = new JObject();
int index = GetCmdHeaderLength(command) + 2; // Header+ID
int StrLength = GetStrLength(command, index);
TrasnlationObj["JP"] = Encoding.Unicode.GetString(command[index..(index + StrLength)]);
index += StrLength + 2;
StrLength = GetStrLength(command, index);
TrasnlationObj["EN"] = Encoding.Unicode.GetString(command[index..(index + StrLength)]);
TrasnlationObj["Translation"] = TrasnlationObj["EN"];
return TrasnlationObj;
}
public static byte[]? SAYAVOICETEXT_SET(byte[] command, JObject inJsonObj)
{
int index = GetCmdHeaderLength(command) + 2; // Header+ID
int StrLength = GetStrLength(command, index); // Jp
index += StrLength + 2;
StrLength = GetStrLength(command, index);
string Translation = PostProcessText(inJsonObj["Translation"]?.Value<string>() ?? "");
List<byte> newCommand = new List<byte>();
newCommand.AddRange(command[..index]);
newCommand.AddRange(Encoding.Unicode.GetBytes(Translation));
newCommand.AddRange(command.Skip(index + StrLength));
foreach (var newChar in Translation.ToCharArray())
{
CharCollection.Add(newChar);
}
return newCommand.ToArray();
}
public static int LittleEndian2Int(byte[] InBytes)
{
int result = 0;
for (int i = 0; i < 4; i++)
{
result |= InBytes[i] << (8 * i);
}
return result;
}
public static void Int2LittleEndian(byte[] InBytes, int Offset, int Value)
{
for (int i = 0; i < 4; i++)
{
InBytes[Offset + i] = (byte)((Value >> (8 * i)) & 0xFF);
}
}
// 用于修正指令中的指针
// 感觉在有了CommandRedirectors之后是不需要Assign的步骤了,但是为了避免出Bug,旧代码就不动了
// 感觉屎山正在慢慢堆积。。
public static LucaCommand[]? TAIL4Ptr_ASSIGN_CMD(List<LucaCommand> InAllCommands,int CmdIndex)
{
var CurCmd = InAllCommands[CmdIndex];
if(CurCmd.Command==null)
{
return null;
}
int TargetCmdPtr = LittleEndian2Int(CurCmd.Command[^4..]);
for (int i = 0; i < InAllCommands.Count; i++)
{
if (InAllCommands[i].CmdPtr == TargetCmdPtr)
{
return new LucaCommand[] { InAllCommands[i] };
}
}
return null;
}
public static void TAIL4Ptr_FIX_PTR(LucaCommand CurCmd, LucaCommand[] InCommands)
{
if (CurCmd.Command != null && InCommands.Length>0)
{
Int2LittleEndian(CurCmd.Command, CurCmd.Command.Length - 4, InCommands[0].CmdPtr);
}
}
public static LucaCommand[]? FARCALL_ASSIGN_CMD(List<LucaCommand> InAllCommands, int CmdIndex)
{
// 这里只是一个占位符,即使不再需要ASSIGN_CMD,但还是传回一个非null值,确保接下来能正常执行FIX_PTR
return new LucaCommand[0];
}
public static void FARCALL_FIX_PTR(LucaCommand CurCmd, LucaCommand[] InCommands)
{
if (CurCmd.Command == null)
{
return;
}
int index = GetCmdHeaderLength(CurCmd.Command);
int ExpLength = GetSingleByteStrLength(CurCmd.Command, index + 2);
string TargetScript = Encoding.ASCII.GetString(CurCmd.Command[(index + 2)..(index + 2 + ExpLength)]).ToLower();
int SourceCmdPtr = LittleEndian2Int(CurCmd.Command[(index + 2 + ExpLength + 1)..(index + 2 + ExpLength + 1 + 4)]);
if (!ScriptCommandRedirectors.ContainsKey(TargetScript))
{
Console.Error.WriteLine("Error: Failed to find redirectors for script " + TargetScript);
Environment.Exit(-1);
}
var TargetRedirectors = ScriptCommandRedirectors[TargetScript];
if(!TargetRedirectors.ContainsKey(SourceCmdPtr))
{
Console.Error.WriteLine("Error: Failed to find redirectors for script " + TargetScript + " SourcePtr " + SourceCmdPtr);
Environment.Exit(-1);
}
Int2LittleEndian(CurCmd.Command, index + 2 + ExpLength + 1, TargetRedirectors[SourceCmdPtr]);
}
public static LucaCommand[]? JUMP_ASSIGN_CMD(List<LucaCommand> InAllCommands, int CmdIndex)
{
return new LucaCommand[0];
}
// 与FARCALL_FIX_PTR相同,但Header和脚本名之间少了2个字节的变量。
public static void JUMP_FIX_PTR(LucaCommand CurCmd, LucaCommand[] InCommands)
{
if (CurCmd.Command == null)
{
return;
}
int index = GetCmdHeaderLength(CurCmd.Command);
int ExpLength = GetSingleByteStrLength(CurCmd.Command, index);
if (index + ExpLength + 1 >= CurCmd.GetCmdLength())
{
// 这个JUMP可能不带参数,直接返回
return;
}
string TargetScript = Encoding.ASCII.GetString(CurCmd.Command[index..(index + ExpLength)]).ToLower();
int SourceCmdPtr = LittleEndian2Int(CurCmd.Command[(index + ExpLength + 1)..(index + ExpLength + 1 + 4)]);
if (!ScriptCommandRedirectors.ContainsKey(TargetScript))
{
Console.Error.WriteLine("Error: Failed to find redirectors for script " + TargetScript);
Environment.Exit(-1);
}
var TargetRedirectors = ScriptCommandRedirectors[TargetScript];
if (!TargetRedirectors.ContainsKey(SourceCmdPtr))
{
Console.Error.WriteLine("Error: Failed to find redirectors for script " + TargetScript + " SourcePtr " + SourceCmdPtr);
Environment.Exit(-1);
}
Int2LittleEndian(CurCmd.Command, index + ExpLength + 1, TargetRedirectors[SourceCmdPtr]);
}
public static LucaCommand[]? ONGOTO_ASSIGN_CMD(List<LucaCommand> InAllCommands, int CmdIndex)
{
return new LucaCommand[0];
}
// 原来ONGOTO的含义不是当跳转结束后XXX,而是当XXX时跳转
// 感觉当初的思路完全错误了。
public static void ONGOTO_FIX_PTR(LucaCommand CurCmd, LucaCommand[] InCommands)
{
if (CurCmd.Command == null)
{
return;
}
int index = GetCmdHeaderLength(CurCmd.Command);
int ExpLength = GetSingleByteStrLength(CurCmd.Command, index);
byte[] PtrArray = CurCmd.Command.Skip(index + ExpLength + 1).ToArray();
if(PtrArray.Length % 4!=0)
{
// 怎么会不能被整除呢?
Console.Error.WriteLine("Error: ONGOTO PtrArray length is not multiple of 4,Script may contains error,Exit!");
Environment.Exit(-1);
}
var CurScript = Program.ScriptNameContext.Peek();
var TargetRedirectors = ScriptCommandRedirectors[CurScript];
for (int i = 0; i < PtrArray.Length; i += 4)
{
byte[] PtrByteArray = PtrArray[i..(i + 4)];
// 这里其实应该是uint的,但鉴于脚本里不可能出现大于2G的指针,所以用int还是uint都无所谓了
int SourceCmdPtr = BitConverter.IsLittleEndian ?
BitConverter.ToInt32(PtrArray, i) :
BitConverter.ToInt32(PtrArray.Reverse().ToArray(), i);
if (!TargetRedirectors.ContainsKey(SourceCmdPtr))
{
Console.Error.WriteLine("Error: Failed to find redirectors for script " + CurScript + " SourcePtr " + SourceCmdPtr);
Environment.Exit(-1);
}
Int2LittleEndian(CurCmd.Command, index + ExpLength + 1 + i, TargetRedirectors[SourceCmdPtr]);
}
}
}
}