madden-franchise 3.0.2 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/FranchiseFile.js CHANGED
@@ -270,6 +270,10 @@ class FranchiseFile extends EventEmitter {
270
270
  return this.tables[index];
271
271
  };
272
272
 
273
+ getTableByUniqueId (id) {
274
+ return this.tables.find((table) => { return table.header && table.header.uniqueId === id; });
275
+ };
276
+
273
277
  getReferencedRecord (referenceValue) {
274
278
  const reference = utilService.getReferenceData(referenceValue);
275
279
  return this.getTableById(reference.tableId).records[reference.rowNumber];
@@ -428,12 +428,20 @@ class FranchiseFileTable extends EventEmitter {
428
428
  const changedRecordWasEmpty = emptyRecordReference !== null && emptyRecordReference !== undefined;
429
429
 
430
430
  if (changedRecordWasEmpty) {
431
- // Check if the record's first four bytes still have a reference to the 0th table.
432
- // If so, then the record is still considered empty.
431
+ // 1/5/23: Assume all changes to a field make the record not empty. Leaving comments
432
+ // below if this needs to be reverted in the future.
433
433
 
434
- // We need to check the buffer because the first field is not always a reference.
435
- // const referenceData = utilService.getReferenceDataFromBuffer(object.data.slice(0, 4));
436
- // if (referenceData.tableId !== 0 || referenceData.rowNumber > this.header.recordCapacity) {
434
+
435
+ // Check if the record's first four bytes still have a reference to the 0th table.
436
+ // If so, then the record is still considered empty.
437
+
438
+ // We need to check the buffer because the first field is not always a reference.
439
+ // const referenceData = utilService.getReferenceDataFromBuffer(object.data.slice(0, 4));
440
+ // if (referenceData.tableId !== 0 || referenceData.rowNumber > this.header.recordCapacity) {
441
+
442
+ // if the record contains any string values, point the string values to
443
+ // their correct offsets
444
+ this.strategy.recalculateStringOffsets(this, object);
437
445
 
438
446
  // Delete the empty record entry because it is no longer empty
439
447
  this.emptyRecords.delete(object.index);
@@ -491,7 +499,16 @@ class FranchiseFileTable extends EventEmitter {
491
499
  }
492
500
  else if (object instanceof FranchiseFileTable2Field) {
493
501
  object.isChanged = true;
494
- this.emit('change');
502
+
503
+ // When a table2 field changes, we need to check if the record is empty. If so, we need to mark it as not empty.
504
+ if (object.fieldReference) {
505
+ this.onEvent('change', object.fieldReference._parent);
506
+ }
507
+ else {
508
+ // Only emit change here if the field reference is empty.
509
+ // the onEvent call will emit a change in the above condition.
510
+ this.emit('change');
511
+ }
495
512
  }
496
513
  };
497
514
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "madden-franchise",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "description": "Tools to read a madden franchise file and get data from it",
5
5
  "main": "FranchiseFile.js",
6
6
  "scripts": {
@@ -64,6 +64,7 @@ M19TableHeaderStrategy.parseHeader = (data) => {
64
64
  'isArray': isArray,
65
65
  'tableId': tableId,
66
66
  'tablePad1': tablePad1,
67
+ 'uniqueId': tablePad1,
67
68
  'tableUnknown1': tableUnknown1,
68
69
  'tableUnknown2': tableUnknown2,
69
70
  'data1Id': data1Id,
@@ -73,6 +73,7 @@ M20TableHeaderStrategy.parseHeader = (data) => {
73
73
  'isArray': isArray,
74
74
  'tableId': tableId,
75
75
  'tablePad1': tablePad1,
76
+ 'uniqueId': tablePad1,
76
77
  'tableUnknown1': tableUnknown1,
77
78
  'tableUnknown2': tableUnknown2,
78
79
  'data1Id': data1Id,
@@ -33,4 +33,28 @@ FranchiseTableStrategy.getMandatoryOffsets = (offsets) => {
33
33
  return [];
34
34
  };
35
35
 
36
+ FranchiseTableStrategy.recalculateStringOffsets = (table, record) => {
37
+ // First, calculate length allocated per record in table2
38
+ const byteLengthPerRecord = table.offsetTable.filter((offsetEntry) => {
39
+ return offsetEntry.type === 'string';
40
+ }).reduce((accum, cur) => {
41
+ return accum + cur.maxLength;
42
+ }, 0);
43
+
44
+ // Then, go through each string field sorted by offset index, and assign offsets to the table2 fields
45
+ let runningOffset = 0;
46
+
47
+ record.fieldsArray.filter((field) => {
48
+ return field.offset.type === 'string';
49
+ }).sort((a, b) => {
50
+ return a.offset.index - b.offset.index;
51
+ }).forEach((field) => {
52
+ if (field.secondTableField) {
53
+ field.secondTableField.offset = (record.index * byteLengthPerRecord) + runningOffset;
54
+ }
55
+
56
+ runningOffset += field.offset.maxLength;
57
+ });
58
+ };
59
+
36
60
  module.exports = FranchiseTableStrategy;
@@ -8,5 +8,6 @@ M19TableStrategy.parseHeaderAttributesFromSchema = M19TableHeaderStrategy.parseH
8
8
 
9
9
  M19TableStrategy.getTable2BinaryData = FranchiseTableStrategy.getTable2BinaryData;
10
10
  M19TableStrategy.getMandatoryOffsets = FranchiseTableStrategy.getMandatoryOffsets;
11
+ M19TableStrategy.recalculateStringOffsets = FranchiseTableStrategy.recalculateStringOffsets;
11
12
 
12
13
  module.exports = M19TableStrategy;
@@ -8,5 +8,6 @@ M20TableStrategy.parseHeaderAttributesFromSchema = M20TableHeaderStrategy.parseH
8
8
 
9
9
  M20TableStrategy.getTable2BinaryData = FranchiseTableStrategy.getTable2BinaryData;
10
10
  M20TableStrategy.getMandatoryOffsets = FranchiseTableStrategy.getMandatoryOffsets;
11
+ M20TableStrategy.recalculateStringOffsets = FranchiseTableStrategy.recalculateStringOffsets;
11
12
 
12
13
  module.exports = M20TableStrategy;
@@ -8,5 +8,6 @@ M19FTCTableStrategy.parseHeaderAttributesFromSchema = M19TableHeaderStrategy.par
8
8
 
9
9
  M19FTCTableStrategy.getTable2BinaryData = FTCTableStrategy.getTable2BinaryData;
10
10
  M19FTCTableStrategy.getMandatoryOffsets = FTCTableStrategy.getMandatoryOffsets;
11
+ M19FTCTableStrategy.recalculateStringOffsets = () => {};
11
12
 
12
13
  module.exports = M19FTCTableStrategy;
@@ -8,5 +8,6 @@ M20FTCTableStrategy.parseHeaderAttributesFromSchema = M20TableHeaderStrategy.par
8
8
 
9
9
  M20FTCTableStrategy.getTable2BinaryData = FTCTableStrategy.getTable2BinaryData;
10
10
  M20FTCTableStrategy.getMandatoryOffsets = FTCTableStrategy.getMandatoryOffsets;
11
+ M20FTCTableStrategy.recalculateStringOffsets = () => {};
11
12
 
12
13
  module.exports = M20FTCTableStrategy;