-
Notifications
You must be signed in to change notification settings - Fork 8
/
ClassEditor.cs
1674 lines (1524 loc) · 75.2 KB
/
ClassEditor.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using DarkUI.Controls;
using DarkUI.Forms;
internal class ClassEditor
{
public List<string> Ecus_Definitions_Compatible = new List<string>();
public List<string> Ecus_Definitions_Compatible_filename = new List<string>();
//Variables for loaded rom definition
public List<string> DefinitionsLocationsX = new List<string>();
public List<string> DefinitionsLocationsY = new List<string>();
public List<string> DefinitionsLocationsTable = new List<string>();
public List<string> DefinitionsMathX = new List<string>();
public List<string> DefinitionsMathY = new List<string>();
public List<string> DefinitionsMathTable = new List<string>();
public List<string> DefinitionsMathXInverted = new List<string>();
public List<string> DefinitionsMathYInverted = new List<string>();
public List<string> DefinitionsMathTableInverted = new List<string>();
public List<string> DefinitionsFormatX = new List<string>();
public List<string> DefinitionsFormatY = new List<string>();
public List<string> DefinitionsFormatTable = new List<string>();
public List<bool> DefinitionsIsSingleByteX = new List<bool>();
public List<bool> DefinitionsIsSingleByteY = new List<bool>();
public List<bool> DefinitionsIsSingleByteTable = new List<bool>();
public List<string> DefinitionsName = new List<string>();
public List<string> DefinitionsUnit1 = new List<string>();
public List<string> DefinitionsUnit2 = new List<string>();
public List<string> DefinitionsTableSize = new List<string>();
public List<float> DefinitionsValueMin = new List<float>();
public List<float> DefinitionsValueMax = new List<float>();
public List<double> DefinitionsChangeAmount = new List<double>();
public List<string> DefinitionsHeaders = new List<string>();
public List<bool> DefinitionsIsXYInverted = new List<bool>();
public List<bool> DefinitionsIsTableInverted = new List<bool>();
public List<bool> DefinitionsIsReadOnly = new List<bool>();
public List<bool> DefinitionsIsUntested = new List<bool>();
public List<bool> DefinitionsIsNotDefined = new List<bool>();
public string DefinitionsChecksumLocation = "";
public string DefinitionsCurrentLoadedECU = "";
public long SelectedROMLocation;
public int SelectedTableSize;
public int SelectedTableIndexInDefinitions;
public bool IsTableLoadedCorrectly = false;
public bool CanReloadTablesValues = false;
public string string_ECU_Name;
public byte[] ROM_Bytes;
public string AllROMDifferences;
public string AllROMDifferencesRedo;
public bool ValuesChanged = false;
public bool IsSingleByteX = false;
public bool IsSingleByteY = false;
public bool IsSingleByteTable = false;
public int[] BufferValuesArray = new int[200];
public int[] BufferTableSize = new int[2];
public string BufferMath = "";
private string LastMathDoneCheck = "";
public string FileFormat = ""; //-> 1mb-fw, 1mb-full,
private Editortable Editortable_0;
internal ClassEditor(ref Editortable Editortable_1)
{
Editortable_0 = Editortable_1;
}
public float smethod_1()
{
return Editortable.float_0;
}
public string ValueIncDec(int RowIndex, int CellIndex, bool Increasing, bool Multiply4x)
{
float num = this.smethod_1();
string format = "0";
string text = Editortable_0.dataGridView_0.Rows[RowIndex].Cells[CellIndex].Value.ToString();
if (text.Contains(".") || text.Contains(","))
{
string[] SplittedCmd = new string[0];
if (text.Contains(".")) SplittedCmd = text.Split('.');
if (text.Contains(",")) SplittedCmd = text.Split(',');
int FormatLenght = SplittedCmd[1].Length;
if (FormatLenght == 0) format = "0.0";
if (FormatLenght == 1) format = "0.0";
if (FormatLenght == 2) format = "0.00";
if (FormatLenght == 3) format = "0.000";
if (FormatLenght == 4) format = "0.0000";
}
if (Multiply4x)
{
num *= 4f;
}
if (Increasing)
{
return (float.Parse(text) + num).ToString(format);
}
return (float.Parse(text) - num).ToString(format);
}
public void SetFileFormat(byte[] FilesBytes)
{
//SH7055 512Kb, SH7058 1Mb, SH72543 2Mb, SH7059 1.5Mb, MPC5554 2Mb, Bosch MED17.9.3 ECU 4Mb, TC179X 4Mb
if ((FilesBytes.Length - 1) == 0xF7FFF) FileFormat = "1mb-fw";
if ((FilesBytes.Length - 1) == 0xFFFFF) FileFormat = "1mb-full";
if ((FilesBytes.Length - 1) == 0x1EFFFF) FileFormat = "2mb-fw";
if ((FilesBytes.Length - 1) == 0x1FFFFF) FileFormat = "2mb-full";
if ((FilesBytes.Length - 1) == 0x26FFFF) FileFormat = "4mb-fw";
if ((FilesBytes.Length - 1) == 0x27FFFF) FileFormat = "4mb-full";
//if ((FilesBytes.Length - 1) == 0x3FFFFF) FileFormat = "4mb-full";
}
public void IncDecreaseSelection(bool Decreasing, bool HoldShift)
{
if (!Decreasing)
{
int num3 = 0;
int num4 = 0;
int j = 0;
while (j < Editortable_0.dataGridView_0.Rows.Count)
{
if (Editortable_0.dataGridView_0.Rows[j].Cells[num4].Selected)
{
Editortable_0.dataGridView_0.Rows[j].Cells[num4].Value = this.ValueIncDec(j, num4, true, HoldShift);
}
if (num4 == Editortable_0.dataGridView_0.Columns.Count - 1)
{
num4 = 0;
j++;
}
else
{
num4++;
}
num3++;
}
}
else
{
int num5 = 0;
int num6 = 0;
int k = 0;
while (k < Editortable_0.dataGridView_0.Rows.Count)
{
if (Editortable_0.dataGridView_0.Rows[k].Cells[num6].Selected)
{
Editortable_0.dataGridView_0.Rows[k].Cells[num6].Value = this.ValueIncDec(k, num6, false, HoldShift);
}
if (num6 == Editortable_0.dataGridView_0.Columns.Count - 1)
{
num6 = 0;
k++;
}
else
{
num6++;
}
num5++;
}
}
}
public void ShortcutsCommand(KeyEventArgs keyEventArgs_0, int int_232)
{
bool bool_ = false;
if (Control.ModifierKeys == Keys.Shift)
{
bool_ = true;
}
if (keyEventArgs_0.KeyCode == Keys.Delete || int_232 == 1)
{
int num = 0;
int num2 = 0;
int i = 0;
//if (Editortable_0.frmOBD2Scan_0 != null)
//{
while (i < Editortable_0.dataGridView_0.Rows.Count)
{
if (Editortable_0.dataGridView_0.Rows[i].Cells[num2].Selected)
{
Editortable_0.dataGridView_0.Rows[i].Cells[num2].Value = 0;
}
if (num2 == Editortable_0.dataGridView_0.Columns.Count - 1)
{
num2 = 0;
i++;
}
else
{
num2++;
}
num++;
}
//}
}
if (keyEventArgs_0.KeyCode == Keys.W || int_232 == 2)
{
IncDecreaseSelection(false, bool_);
}
if (keyEventArgs_0.KeyCode == Keys.S || int_232 == 3)
{
IncDecreaseSelection(true, bool_);
}
//Class40 class40_0 = new Class40();
//this.smethod_4(200).ContinueWith(new Action<Task>(this.<> c.<> 9.method_0));
//this.smethod_4(200, class40_0).ContinueWith(new Action<Task>(class40_0.method_0));
}
private Task smethod_4(int int_232, Class40 class40_0)
{
//Class40 class40_0 = new Class40();
class40_0.taskCompletionSource_0 = new TaskCompletionSource<object>();
new System.Threading.Timer(new TimerCallback(class40_0.method_0)).Change(int_232, -1);
return class40_0.taskCompletionSource_0.Task;
}
public void GetChanges()
{
long num = this.SelectedROMLocation;
int multiplier = 2;
if (this.IsSingleByteX || this.IsSingleByteY || this.IsSingleByteTable) multiplier = 1; //###############################
Editortable_0.GForm_Main_0.method_1("Checking for differences...");
//Get all Tables values
double[,] ReadBufferarray = new double[this.BufferTableSize[0], this.BufferTableSize[1]];
for (int i = 0; i < this.BufferTableSize[0]; i++) //10columns
{
for (int j = 0; j < this.BufferTableSize[1]; j++) //20rows
{
//calculate value inversed to make bytes
double ThisValue = double.Parse(Editortable_0.dataGridView_0.Rows[j].Cells[i].Value.ToString().Replace(',', '.'), CultureInfo.InvariantCulture);
ThisValue = DoMath(ThisValue, BufferMath, true, "Table");
ReadBufferarray[i, j] = (Int16)ThisValue;
}
}
//#############
double[] ValuesBufferarray = new double[this.SelectedTableSize];
for (int i = 0; i < this.BufferTableSize[0]; i++)
{
for (int j = 0; j < this.BufferTableSize[1]; j++)
{
ValuesBufferarray[i * this.BufferTableSize[1] + j] = ReadBufferarray[i, j];
}
}
//#############
byte[] BytesBufferarray = new byte[this.SelectedTableSize * multiplier];
for (int i = 0; i < this.SelectedTableSize; i++)
{
if (multiplier == 2)
{
byte[] ThisBytesToChange = BitConverter.GetBytes((Int16) ValuesBufferarray[i]);
BytesBufferarray[(i * 2)] = ThisBytesToChange[1];
BytesBufferarray[(i * 2) + 1] = ThisBytesToChange[0];
}
else
{
BytesBufferarray[i] = (byte) ValuesBufferarray[i];
}
}
//#############
byte[] array = new byte[this.SelectedTableSize * multiplier];
for (int i = 0; i < this.SelectedTableSize * multiplier; i++)
{
array[i] = this.ROM_Bytes[num + i];
//Apply Changes
this.ROM_Bytes[num + i] = BytesBufferarray[i];
}
int num3 = 0;
string text = null;
bool DiffDetected = false;
foreach (int num4 in BytesBufferarray)
{
if (num4.ToString() != array[num3].ToString())
{
string BufText = "Change at line: " + num3.ToString() + "[" + array[num3].ToString("X2") + "->" + num4.ToString("X2") + "] | At: 0x" + (this.SelectedROMLocation + num3).ToString("X");
text = text + BufText + Environment.NewLine;
Editortable_0.GForm_Main_0.method_1(BufText);
DiffDetected = true;
}
num3++;
}
if (!DiffDetected) Editortable_0.GForm_Main_0.method_1("No differences detected");
if (DiffDetected)
{
this.AllROMDifferencesRedo = "";
this.Editortable_0.redoToolStripMenuItem.Enabled = false;
}
this.AllROMDifferences = this.AllROMDifferences + text;
//this.string_3 = this.string_3 + "Address: " + this.SelectedROMLocation.ToString() + Environment.NewLine + text;
//this.string_3 = this.string_3 + "Table: " + TableSize + Environment.NewLine + "Address: " + this.SelectedROMLocation.ToString() + Environment.NewLine + text;
}
public void SaveROMBytes(string string_4)
{
//try
//{
if (this.ValuesChanged && this.SelectedTableSize != 0 && this.SelectedROMLocation != 0)
{
this.GetChanges();
}
this.ValuesChanged = false;
//################################################
byte[] SavingBytes = this.ROM_Bytes;
//Remove fake bootloader section if it's a partial firmware .bin file
if (!this.Editortable_0.IsFullBinary)
{
if (FileFormat == "1mb-fw")
{
byte[] BufferBytes = new byte[SavingBytes.Length - 0x8000];
for (int i = 0; i < BufferBytes.Length; i++) BufferBytes[i] = SavingBytes[i + 0x8000];
SavingBytes = BufferBytes;
}
if (FileFormat == "2mb-fw" || FileFormat == "4mb-fw")
{
byte[] BufferBytes = new byte[SavingBytes.Length - 0x10000];
for (int i = 0; i < BufferBytes.Length; i++) BufferBytes[i] = SavingBytes[i + 0x10000];
SavingBytes = BufferBytes;
}
}
//Fix Checksums
FixChecksums();
File.Create(string_4).Dispose();
File.WriteAllBytes(string_4, SavingBytes);
//Set LastFileOpened
string LastOpenFilePath = Application.StartupPath + @"\LastFileOpened.txt";
File.Create(LastOpenFilePath).Dispose();
File.WriteAllText(LastOpenFilePath, string_4);
//################################################
//Save rom differences changes to logs
string text = string_4 + "-logs.txt";
File.Create(text).Dispose();
File.WriteAllText(text, AllROMDifferences);
//################################################
//string text = string_4 + "~temp";
//string text2 = string_4 + "~temp2";
/*File.WriteAllBytes(text, this.byte_0);
File.WriteAllText(text2, this.string_2);
using (FileStream fileStream = new FileStream(string_4, FileMode.OpenOrCreate))
{
FlashGUI.smethod_1(this.string_0 + Environment.NewLine + this.string_1, fileStream);
}
using (ZipArchive zipArchive = ZipFile.Open(string_4, ZipArchiveMode.Update))
{
zipArchive.CreateEntryFromFile(text, this.string_1);
zipArchive.CreateEntryFromFile(text2, "CLOG");
}
File.Delete(text);
File.Delete(text2);*/
DarkMessageBox.Show("Successfully Saved File!.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
/*}
catch (Exception ex)
{
DarkMessageBox.Show("Failed to save file! error: " + Environment.NewLine + ex, "Save file failed", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}*/
}
public void FixChecksums()
{
if (!this.Editortable_0.IsFullBinary) this.ROM_Bytes = this.Editortable_0.GForm_Main_0.Class_Checksums_0.VerifyChecksumFWBin(this.ROM_Bytes);
if (this.Editortable_0.IsFullBinary) this.ROM_Bytes = this.Editortable_0.GForm_Main_0.Class_Checksums_0.VerifyChecksumFullBin(this.ROM_Bytes);
}
public void SetTableValues(int[] TableSize, long ROMLocationX, string TopLeftString, string RowHeaderString, string[] HeaderStringList, string ThisMathX, string ThisFormatX, bool IsXYInverted, long ROMLocationTable, string ThisMathTable, string ThisTableFormat, bool IsTableInverted, bool IsReadOnly)
{
try
{
this.SelectedTableSize = TableSize[0] * TableSize[1];
BufferValuesArray = new int[SelectedTableSize];
BufferTableSize = TableSize;
Editortable_0.dataGridView_0.Rows.Clear();
Editortable_0.dataGridView_0.Columns.Clear();
Editortable_0.dataGridView_0.RowTemplate.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
Editortable_0.dataGridView_0.TopLeftHeaderCell.Value = TopLeftString;
Editortable_0.dataGridView_0.AllowUserToAddRows = false;
//Correct the Table Orientation if Bad
/*if ((TableSize[1] == 1 && IsInverted) || (TableSize[0] == 1 && !IsInverted))
{
int Buf0 = TableSize[1];
int Buf1 = TableSize[0];
TableSize[0] = Buf0;
TableSize[1] = Buf1;
}*/
SelectedROMLocation = ROMLocationTable;
BufferMath = ThisMathTable;
//Apply Columns(Y)
if (IsXYInverted)
{
for (int i = 0; i < TableSize[1]; i++)
{
if (ROMLocationX != 0)
{
double num = 0;
if (IsSingleByteX) num = (double)this.GetSingleByteValue(ROMLocationX + i);
else num = (double)this.GetIntValue(ROMLocationX + i * 2);
string HeaderStr = "";
if (ThisFormatX != "") HeaderStr = DoMath(num, ThisMathX, false, "X").ToString(ThisFormatX);
if (ThisFormatX == "") HeaderStr = DoMath(num, ThisMathX, false, "X").ToString();
Editortable_0.dataGridView_0.Columns.Add(HeaderStr, HeaderStr);
}
else
{
Editortable_0.dataGridView_0.Columns.Add(RowHeaderString, RowHeaderString);
}
}
}
else
{
for (int j = 0; j < TableSize[0]; j++) Editortable_0.dataGridView_0.Columns.Add(HeaderStringList[j], HeaderStringList[j]);
}
int index = 0;
while (true)
{
if (index >= SelectedTableSize) // More than TableSize (ex: 10x20.. more than 200)
{
int[,] numArray2 = smethod_35<int>(BufferValuesArray, TableSize[0], TableSize[1]);
int rowIndex = 0;
while (true)
{
if ((rowIndex >= TableSize[1] && !IsXYInverted) || (rowIndex >= TableSize[0] && IsXYInverted)) //More than Y (make the 3D table)
{
int num10 = 0;
while (true)
{
if ((num10 >= TableSize[1] && !IsXYInverted) || (num10 >= TableSize[0] && IsXYInverted)) //Another More than Y (set X Header)
{
if (!IsXYInverted) SetBackColor(TableSize[0], Editortable.float_1[0], Editortable.float_1[1]);
if (IsXYInverted) SetBackColor(TableSize[1], Editortable.float_1[0], Editortable.float_1[1]);
break;
}
//Rows(X) Math
if (IsXYInverted)
{
string ThisHeaderVal = HeaderStringList[num10];
if (ThisHeaderVal == "") ThisHeaderVal = RowHeaderString;
Editortable_0.dataGridView_0.Rows[num10].HeaderCell.Value = ThisHeaderVal;
}
else
{
if (ROMLocationX != 0)
{
double num = 0;
if (IsSingleByteX) num = (double)this.GetSingleByteValue(ROMLocationX + num10);
else num = (double)this.GetIntValue(ROMLocationX + num10 * 2);
if (ThisFormatX != "") Editortable_0.dataGridView_0.Rows[num10].HeaderCell.Value = DoMath(num, ThisMathX, false, "X").ToString(ThisFormatX);
if (ThisFormatX == "") Editortable_0.dataGridView_0.Rows[num10].HeaderCell.Value = DoMath(num, ThisMathX, false, "X").ToString();
}
else
{
Editortable_0.dataGridView_0.Rows[num10].HeaderCell.Value = RowHeaderString;
}
}
num10++;
}
break;
}
//TableMath (Get full 1full row of value at a time)
string[] values = new string[0];
if (IsXYInverted)
{
values = new string[TableSize[1]];
for (int i = 0; i < TableSize[1]; i++)
{
if (ThisTableFormat.Contains("X"))
{
//Display Values in Hexadecimals
string Mathhh = DoMath((double)numArray2[rowIndex, i], ThisMathTable, false, "Table").ToString();
try
{
if (ThisTableFormat == "X4") values[i] = Int16.Parse(Mathhh).ToString(ThisTableFormat);
else if (ThisTableFormat == "X8") values[i] = Int32.Parse(Mathhh).ToString(ThisTableFormat);
else values[i] = int.Parse(Mathhh).ToString(ThisTableFormat);
}
catch
{
values[i] = Mathhh;
}
}
else
{
//Display Values in double/int
if (ThisTableFormat != "") values[i] = DoMath((double)numArray2[rowIndex, i], ThisMathTable, false, "Table").ToString(ThisTableFormat);
if (ThisTableFormat == "") values[i] = DoMath((double)numArray2[rowIndex, i], ThisMathTable, false, "Table").ToString();
}
}
}
else
{
values = new string[TableSize[0]];
for (int i = 0; i < TableSize[0]; i++)
{
if (ThisTableFormat.Contains("X"))
{
//Display Values in Hexadecimals
string Mathhh = DoMath((double)numArray2[i, rowIndex], ThisMathTable, false, "Table").ToString();
try
{
if (ThisTableFormat == "X4") values[i] = Int16.Parse(Mathhh).ToString(ThisTableFormat);
else if (ThisTableFormat == "X8") values[i] = Int32.Parse(Mathhh).ToString(ThisTableFormat);
else values[i] = int.Parse(Mathhh).ToString(ThisTableFormat);
}
catch
{
values[i] = Mathhh;
}
}
else
{
//Display Values in double/int
if (ThisTableFormat != "") values[i] = DoMath((double)numArray2[i, rowIndex], ThisMathTable, false, "Table").ToString(ThisTableFormat);
if (ThisTableFormat == "") values[i] = DoMath((double)numArray2[i, rowIndex], ThisMathTable, false, "Table").ToString();
}
}
}
Editortable_0.dataGridView_0.Rows.Insert(rowIndex, values);
rowIndex++;
}
break;
}
//Math perfomed just above
if (IsSingleByteTable) BufferValuesArray[index] = GetSingleByteValue(SelectedROMLocation + index);
else BufferValuesArray[index] = GetIntValue(SelectedROMLocation + (index * 2));
index++;
}
//##############################################################################################################
//Invert inner tables values X and Y
if (IsTableInverted)
{
int[,] numArray2 = smethod_35<int>(BufferValuesArray, TableSize[1], TableSize[0]);
for (int i = 0; i < Editortable_0.dataGridView_0.ColumnCount; i++)
{
for (int i2 = 0; i2 < Editortable_0.dataGridView_0.RowCount; i2++)
{
string valueinner = "";
if (IsXYInverted)
{
if (ThisTableFormat != "") valueinner = DoMath((double)numArray2[i, i2], ThisMathTable, false, "Table").ToString(ThisTableFormat);
if (ThisTableFormat == "") valueinner = DoMath((double)numArray2[i, i2], ThisMathTable, false, "Table").ToString();
}
else
{
if (ThisTableFormat != "") valueinner = DoMath((double)numArray2[i2, i], ThisMathTable, false, "Table").ToString(ThisTableFormat);
if (ThisTableFormat == "") valueinner = DoMath((double)numArray2[i2, i], ThisMathTable, false, "Table").ToString();
}
Editortable_0.dataGridView_0.Rows[i2].Cells[i].Value = valueinner;
}
}
}
//##############################################################################################################
Editortable_0.dataGridView_0.ReadOnly = IsReadOnly;
foreach (object obj in Editortable_0.dataGridView_0.Columns)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)obj;
dataGridViewColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
//dataGridViewColumn.Width = 50;
}
foreach (object obj2 in ((IEnumerable)Editortable_0.dataGridView_0.Rows))
{
DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
dataGridViewRow2.Height = 20;
}
this.SetBackColor(TableSize[0], Editortable.float_1[0], Editortable.float_1[1]);
this.IsTableLoadedCorrectly = true;
}
catch (Exception ex)
{
this.IsTableLoadedCorrectly = false;
DarkMessageBox.Show("Failed to load table. " + ex.ToString());
}
}
private int GetNearestMathIndex(string ThisMath)
{
int IndexOfNearest = -1;
int IndexOfMathDiv = ThisMath.IndexOf('/');
int IndexOfMathMul = ThisMath.IndexOf('*');
int IndexOfMathAdd = ThisMath.IndexOf('+');
//int IndexOfMathSub = ThisMath.IndexOf('-'); //don't check for sub, this is causing issue with negative number
if (IndexOfMathDiv == 0) return 0;
if (IndexOfMathMul == 0) return 0;
if (IndexOfMathAdd == 0) return 0;
if (IndexOfMathDiv == -1) IndexOfMathDiv = 99;
if (IndexOfMathMul == -1) IndexOfMathMul = 99;
if (IndexOfMathAdd == -1) IndexOfMathAdd = 99;
if (IndexOfMathDiv > 0 && IndexOfMathDiv < IndexOfMathMul && IndexOfMathDiv < IndexOfMathAdd) IndexOfNearest = IndexOfMathDiv;
if (IndexOfMathMul > 0 && IndexOfMathMul < IndexOfMathDiv && IndexOfMathMul < IndexOfMathAdd) IndexOfNearest = IndexOfMathMul;
if (IndexOfMathAdd > 0 && IndexOfMathAdd < IndexOfMathMul && IndexOfMathAdd < IndexOfMathDiv) IndexOfNearest = IndexOfMathAdd;
if (IndexOfNearest == 99) IndexOfNearest = -1;
return IndexOfNearest;
}
private char GetNextMath(string ThisMath)
{
int Thisindex = GetNearestMathIndex(ThisMath);
return ThisMath.Substring(Thisindex, 1)[0];
}
private double GetNextValue(string ThisMath)
{
double Value = 0;
int Nearestindex = GetNearestMathIndex(ThisMath);
if (Nearestindex == 0) ThisMath = ThisMath.Substring(1);
if (Nearestindex == -1)
{
Value = double.Parse(ThisMath.Replace(',', '.'), CultureInfo.InvariantCulture);
}
else
{
string ThisVarStr = ThisMath.Substring(0, Nearestindex);
Value = double.Parse(ThisVarStr.Replace(',', '.'), CultureInfo.InvariantCulture);
}
return Value;
}
public string InvertMathString(string ThisMath)
{
string ReturnStr = "";
List<double> ValuesList = new List<double>();
List<char> MathFuncList = new List<char>();
bool WeHaveVal1 = false;
while (ThisMath != "")
{
if (!WeHaveVal1) ValuesList.Add(GetNextValue(ThisMath));
MathFuncList.Add(GetNextMath(ThisMath));
ThisMath = ThisMath.Substring(GetNearestMathIndex(ThisMath) + 1);
ValuesList.Add(GetNextValue(ThisMath));
int NearestIndex = GetNearestMathIndex(ThisMath);
if (NearestIndex != -1) ThisMath = ThisMath.Substring(GetNearestMathIndex(ThisMath));
WeHaveVal1 = true;
if (!ThisMath.Contains("/") && !ThisMath.Contains("*") && !ThisMath.Contains("+"))
{
ThisMath = ""; //No remaining maths to perform
}
}
//Create inverted math function
for (int i = ValuesList.Count - 1; i >= 0; i--)
{
ReturnStr = ReturnStr + ValuesList[i];
if (i > 0)
{
if (MathFuncList[i - 1] == '*') ReturnStr = ReturnStr + "/";
if (MathFuncList[i - 1] == '/') ReturnStr = ReturnStr + "*";
if (MathFuncList[i - 1] == '+') ReturnStr = ReturnStr + "+-";
}
}
return ReturnStr;
}
public string SwipeMathFunc(string ThisMath)
{
string ReturnStr = "";
List<char> MathFuncList = new List<char>();
List<double> ValuesList = new List<double>();
bool WeHaveVal1 = false;
while (ThisMath != "")
{
if (!WeHaveVal1) ValuesList.Add(GetNextValue(ThisMath));
MathFuncList.Add(GetNextMath(ThisMath));
ThisMath = ThisMath.Substring(GetNearestMathIndex(ThisMath) + 1);
ValuesList.Add(GetNextValue(ThisMath));
int NearestIndex = GetNearestMathIndex(ThisMath);
if (NearestIndex != -1) ThisMath = ThisMath.Substring(GetNearestMathIndex(ThisMath));
WeHaveVal1 = true;
if (!ThisMath.Contains("/") && !ThisMath.Contains("*") && !ThisMath.Contains("+"))
{
ThisMath = ""; //No remaining maths to perform
}
}
//Create swiped math function
for (int i = 0; i < ValuesList.Count; i++)
{
if (i > 0)
{
if (i > 1) ReturnStr = ReturnStr + MathFuncList[i - 2].ToString();
else ReturnStr = ReturnStr + MathFuncList[MathFuncList.Count - 1].ToString();
}
ReturnStr = ReturnStr + ValuesList[i];
}
return ReturnStr;
}
public double DoMath(double ThisValueCheck, string ThisMath, bool Reverse, string Direction)
{
//Check if the reversed math function exist, if not we perform the reversed math function manually
bool ReversFinalForMath = Reverse;
if (Reverse)
{
if (Direction == "X" && DefinitionsMathXInverted[SelectedTableIndexInDefinitions] != "")
{
ThisMath = DefinitionsMathXInverted[SelectedTableIndexInDefinitions];
ReversFinalForMath = false;
}
if (Direction == "Y" && DefinitionsMathYInverted[SelectedTableIndexInDefinitions] != "")
{
ThisMath = DefinitionsMathYInverted[SelectedTableIndexInDefinitions];
ReversFinalForMath = false;
}
if (Direction == "Table" && DefinitionsMathTableInverted[SelectedTableIndexInDefinitions] != "")
{
ThisMath = DefinitionsMathTableInverted[SelectedTableIndexInDefinitions];
ReversFinalForMath = false;
}
}
//Perform Math
double ReturnVal = DoMathFinal(ThisValueCheck, ThisMath, ReversFinalForMath);
//################################################
//Confirm Math function in reverse
if (!Reverse)
{
bool PerformedNormalReverse = true;
//Has the reversed math function existing
if (Direction == "X" && DefinitionsMathXInverted[SelectedTableIndexInDefinitions] != "")
{
ThisMath = DefinitionsMathXInverted[SelectedTableIndexInDefinitions];
PerformedNormalReverse = false;
}
if (Direction == "Y" && DefinitionsMathYInverted[SelectedTableIndexInDefinitions] != "")
{
ThisMath = DefinitionsMathYInverted[SelectedTableIndexInDefinitions];
PerformedNormalReverse = false;
}
if (Direction == "Table" && DefinitionsMathTableInverted[SelectedTableIndexInDefinitions] != "")
{
ThisMath = DefinitionsMathTableInverted[SelectedTableIndexInDefinitions];
PerformedNormalReverse = false;
}
//Has NOT the reversed math function existing
double ReversedVal = DoMathFinal(ReturnVal, ThisMath, PerformedNormalReverse);
if (((int) ReversedVal).ToString() != ((int) ThisValueCheck).ToString()
&& ((int)ReversedVal + 1).ToString() != ((int)ThisValueCheck).ToString()
&& ((int)ReversedVal - 1).ToString() != ((int)ThisValueCheck).ToString())
{
if (LastMathDoneCheck != ThisMath)
{
Editortable_0.GForm_Main_0.method_1("Problem with inverted math: " + ThisMath + " | Values: " + ((int)ThisValueCheck).ToString() + " != " + ((int)ReversedVal).ToString());
LastMathDoneCheck = ThisMath;
}
//suggested to set 'ReadOnly' parameters when there is math problem.
//when there is math problem, it mean the inverted function of your math doesn't return the exact bytes values as within the binary.
//The problem come from the math inversion in the function bellow 'DoMathFinal'
//DefinitionsIsReadOnly
}
}
return ReturnVal;
}
//public double DoMath(double ThisValue, string ThisMath, bool Reverse)
public double DoMathFinal(double ThisValue, string ThisMath, bool Reverse)
{
double ReturnVal = ThisValue;
//No Math found, return value with no math calculation
if (ThisMath == "X" || ThisMath == "") return ReturnVal;
//Put X at the end in reverse
bool IsDivXValFirst = false;
if (Reverse)
{
if (ThisMath.Contains("X/")) IsDivXValFirst = true;
if (ThisMath[ThisMath.Length - 1] != 'X')
{
string XandMath = ThisMath.Substring(ThisMath.IndexOf('X'), 2);
ThisMath = ThisMath.Replace(XandMath, "") + XandMath[1].ToString() + XandMath[0].ToString();
}
}
ThisMath = ThisMath.Replace("X", ThisValue.ToString());
//########################################################
//double ValTest1 = GetNextValue(ThisMath);
char MathTestChar = GetNextMath(ThisMath);
string ThisMathTest = ThisMath.Substring(GetNearestMathIndex(ThisMath) + 1);
double ValTest2 = GetNextValue(ThisMathTest);
if (Reverse && MathTestChar == '*' && ValTest2 == 100)
{
IsDivXValFirst = true;
ThisMath = SwipeMathFunc(ThisMath);
}
//########################################################
if (Reverse) ThisMath = InvertMathString(ThisMath);
//Console.WriteLine("Math: " + ThisMath + " | Reversed: " + Reverse);
bool WeHaveVal1 = false;
double Val1 = 0;
while (ThisMath != "")
{
if (!WeHaveVal1) Val1 = GetNextValue(ThisMath);
char MathChar = GetNextMath(ThisMath);
ThisMath = ThisMath.Substring(GetNearestMathIndex(ThisMath) + 1);
double Val2 = GetNextValue(ThisMath);
if (MathChar == '*') ReturnVal = Val1 * Val2;
if (MathChar == '/') ReturnVal = Val1 / Val2;
if (MathChar == '+') ReturnVal = Val1 + Val2;
//Console.WriteLine("Doing: " + Val1 + MathChar.ToString() + Val2 + "=" + ReturnVal);
if (Reverse && MathChar == '*' && !IsDivXValFirst) ReturnVal = Val2 / Val1;
int NearestIndex = GetNearestMathIndex(ThisMath);
if (NearestIndex != -1) ThisMath = ThisMath.Substring(GetNearestMathIndex(ThisMath));
WeHaveVal1 = true;
Val1 = ReturnVal;
//Check for remaining maths
if (!ThisMath.Contains("/") && !ThisMath.Contains("*") && !ThisMath.Contains("+"))
{
ThisMath = ""; //No remaining maths to perform
}
}
return ReturnVal;
}
public string[] GetAdvancedHeader(int ValuesCount, long ThisLocation, string ThisMath, string HeaderFormat)
{
string[] strArray = new string[ValuesCount];
for (int i = 0; i < ValuesCount; i++)
{
int Valuue = 0;
if (IsSingleByteY) Valuue = GetSingleByteValue(ThisLocation + i);
else Valuue = GetIntValue(ThisLocation + (i * 2));
if (HeaderFormat == "") strArray[i] = DoMath((double) Valuue, ThisMath, false, "Y").ToString();
if (HeaderFormat != "") strArray[i] = DoMath((double) Valuue, ThisMath, false, "Y").ToString(HeaderFormat);
}
return strArray;
}
public byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
/*public Int16 ToInt16BE(byte[] TwoBytes)
{
Int16 k0 = BitConverter.ToInt16(TwoBytes, 0);
Int16 k1 = BitConverter.ToInt16(BitConverter.GetBytes(k0).Reverse().ToArray(), 0);
return k1;
}
public Int32 ToInt32BE(byte[] FourBytes)
{
Int32 k0 = BitConverter.ToInt32(FourBytes, 0);
Int32 k1 = BitConverter.ToInt32(BitConverter.GetBytes(k0).Reverse().ToArray(), 0);
return k1;
}*/
public long HexStringToInt(string hex)
{
string ThisStr = hex.Replace("0x", "");
if (ThisStr.Length == 1 || ThisStr.Length == 3 || ThisStr.Length == 5 || ThisStr.Length == 7)
{
ThisStr = "0" + ThisStr;
}
byte[] ThisBytes = StringToByteArray(ThisStr);
Array.Reverse(ThisBytes);
//Add Empty Bytes
if (ThisBytes.Length == 3)
{
byte[] buffArray = new byte[4];
buffArray[3] = 0;
for (int i = 0; i < ThisBytes.Length; i++) buffArray[i] = ThisBytes[i];
ThisBytes = buffArray;
}
if (ThisBytes.Length == 5)
{
byte[] buffArray = new byte[8];
buffArray[5] = 0;
buffArray[6] = 0;
buffArray[7] = 0;
for (int i = 0; i < ThisBytes.Length; i++) buffArray[i] = ThisBytes[i];
ThisBytes = buffArray;
}
if (ThisBytes.Length == 6)
{
byte[] buffArray = new byte[8];
buffArray[6] = 0;
buffArray[7] = 0;
for (int i = 0; i < ThisBytes.Length; i++) buffArray[i] = ThisBytes[i];
ThisBytes = buffArray;
}
if (ThisBytes.Length == 7)
{
byte[] buffArray = new byte[8];
buffArray[7] = 0;
for (int i = 0; i < ThisBytes.Length; i++) buffArray[i] = ThisBytes[i];
ThisBytes = buffArray;
}
if (ThisBytes.Length == 2) return BitConverter.ToUInt16(ThisBytes, 0);
if (ThisBytes.Length == 4) return BitConverter.ToUInt32(ThisBytes, 0);
if (ThisBytes.Length == 8) return BitConverter.ToInt64(ThisBytes, 0);
return 0;
}
public bool LoadROMbytes(string string_4)
{
if (File.Exists(string_4))
{
try
{
this.ROM_Bytes = File.ReadAllBytes(string_4);
//Console.WriteLine(Editortable_0.IsFullBinary);
//Console.WriteLine(FileFormat);
//Create fake bootloader section
if (!Editortable_0.IsFullBinary)
{
if (FileFormat == "1mb-fw")
{
byte[] BufferBytes = new byte[0x8000 + this.ROM_Bytes.Length];
for (int i = 0; i < 0x8000; i++) BufferBytes[i] = 0xff;
for (int i = 0; i < this.ROM_Bytes.Length; i++) BufferBytes[0x8000 + i] = this.ROM_Bytes[i];
this.ROM_Bytes = BufferBytes;
}
if (FileFormat == "2mb-fw" || FileFormat == "4mb-fw")
{
long ThisSize = (long)0x10000 + (long)this.ROM_Bytes.Length;
byte[] BufferBytes = new byte[ThisSize];
for (long i = 0; i < 0x10000; i++) BufferBytes[i] = 0xff;
for (long i = 0; i < this.ROM_Bytes.Length; i++) BufferBytes[0x10000 + i] = this.ROM_Bytes[i];
this.ROM_Bytes = BufferBytes;
}
}
//Get ECU filename (33 37 38 30 35 2D -> 37805- 'in ASCII chars') (37805-RRB-A140)
this.string_ECU_Name = "";
for (int i = 0; i < this.ROM_Bytes.Length; i++)
{