madden-franchise 2.3.7 → 2.3.12

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.
@@ -86,11 +86,7 @@ class FranchiseFileTable extends EventEmitter {
86
86
  };
87
87
 
88
88
  setNextRecordToUse(index, resetEmptyRecordMap) {
89
- // We need to update the table header to use this row next
90
- this.header.nextRecordToUse = index;
91
-
92
- // And finally update the buffer to reflect this change
93
- this.data.writeUInt32BE(index, this.header.headerOffset - 4);
89
+ this._setNextRecordToUseBuffer(index);
94
90
 
95
91
  // Recalculate the empty record map if the option is set and the
96
92
  // records have already been read.
@@ -102,6 +98,90 @@ class FranchiseFileTable extends EventEmitter {
102
98
  this.emit('change');
103
99
  };
104
100
 
101
+ _setNextRecordToUseBuffer(index) {
102
+ // We need to update the table header to use this row next
103
+ this.header.nextRecordToUse = index;
104
+
105
+ // And finally update the buffer to reflect this change
106
+ this.data.writeUInt32BE(index, this.header.offsetStart - 4);
107
+ };
108
+
109
+ recalculateEmptyRecordReferences() {
110
+ // For this method, we are not going to assume any existing empty records.
111
+ // We're going through each record and checking if it is an empty reference.
112
+ // If so, we'll add it to the list. At the end we will check if there are any unreachable empty references
113
+ // and update those accordingly.
114
+ let emptyRecordReferenceIndicies = [];
115
+
116
+ this.records.forEach((record) => {
117
+ let isEmptyReference = false;
118
+ const firstFourBytesReference = utilService.getReferenceData(record._data.slice(0, 32));
119
+
120
+ if (firstFourBytesReference.tableId === 0 && firstFourBytesReference.rowNumber !== 0) {
121
+ // Could be a an empty record reference or a table2 field.
122
+ // Check for a table2 field reference.
123
+ const firstOffset = this.offsetTable[0];
124
+ if (firstOffset.type !== 'string') {
125
+ isEmptyReference = true;
126
+
127
+ // Save the row number that this record points to.
128
+ emptyRecordReferenceIndicies.push(firstFourBytesReference.rowNumber);
129
+ }
130
+ }
131
+
132
+ record.isEmpty = isEmptyReference;
133
+ });
134
+
135
+ // We need to determine the starting node.
136
+ // To do that, we need to find the empty record which no other empty record points to.
137
+ const unreachableRecords = this.records.filter((record) => { return record.isEmpty; }).filter((record) => {
138
+ return emptyRecordReferenceIndicies.indexOf(record.index) === -1;
139
+ });
140
+
141
+ // If there are more than 1 nodes which are not referenced, there is an issue
142
+ if (unreachableRecords.length > 1) {
143
+ const unreachableIndicies = unreachableRecords.map((record) => {
144
+ return record.index;
145
+ });
146
+
147
+ console.warn(`(${this.header.tableId}) ${this.name} - More than one unreachable records found: `
148
+ + `(${unreachableIndicies.join(', ')}). The game will most likely crash if you do not fix this problem. `
149
+ + `The nextRecordToUse has NOT been updated.`);
150
+ }
151
+ else {
152
+ let nextRecordToUse = this.header.recordCapacity;
153
+
154
+ if (unreachableRecords.length === 1) {
155
+ nextRecordToUse = unreachableRecords[0].index;
156
+ }
157
+
158
+ this._setNextRecordToUseBuffer(nextRecordToUse);
159
+ this.emptyRecords = this._parseEmptyRecords();
160
+ this.emit('change');
161
+ }
162
+ };
163
+
164
+ async replaceRawData(buf, shouldReadRecords) {
165
+ this.data = buf;
166
+
167
+ // Reset fields
168
+ this.recordsRead = false;
169
+ this.header = this.strategy.parseHeader(this.data);
170
+ this.name = this.header.name;
171
+ this.isArray = this.header.isArray;
172
+ this.loadedOffsets = [];
173
+ this.isChanged = false;
174
+ this.records = [];
175
+ this.table2Records = [];
176
+ this.arraySizes = [];
177
+ this.emptyRecords = new Map();
178
+
179
+ // Re-read records if desired
180
+ if (shouldReadRecords) {
181
+ return this.readRecords();
182
+ }
183
+ };
184
+
105
185
  // attribsToLoad is an array of attribute names (strings) to load. It is optional - if nothing is provided to the function it will load all attributes.
106
186
  readRecords (attribsToLoad) {
107
187
  return new Promise((resolve, reject) => {
@@ -144,8 +224,7 @@ class FranchiseFileTable extends EventEmitter {
144
224
  reject('Cannot read records: Schema is not defined.');
145
225
  }
146
226
 
147
- this.emptyRecords = this._parseEmptyRecords();
148
-
227
+
149
228
  let offsetTableToUse = this.offsetTable;
150
229
  const mandatoryOffsetsToLoad = this.strategy.getMandatoryOffsets(this.offsetTable);
151
230
 
@@ -153,18 +232,20 @@ class FranchiseFileTable extends EventEmitter {
153
232
  // get any new attributes to load plus the existing loaded offsets
154
233
  offsetTableToUse = offsetTableToUse.filter((attrib) => {
155
234
  return mandatoryOffsetsToLoad.includes(attrib.name)
156
- || attribsToLoad.includes(attrib.name)
157
- || this.loadedOffsets.find((offset) => { return offset.name === attrib.name; });
235
+ || attribsToLoad.includes(attrib.name)
236
+ || this.loadedOffsets.find((offset) => { return offset.name === attrib.name; });
158
237
  });
159
238
  }
160
-
239
+
161
240
  this.loadedOffsets = offsetTableToUse;
162
241
  this.records = readRecords(this.data, this.header, offsetTableToUse);
163
-
242
+
164
243
  if (this.header.hasSecondTable) {
165
244
  this._parseTable2Values(this.data, this.header, this.records);
166
245
  }
167
246
 
247
+ this.emptyRecords = this._parseEmptyRecords();
248
+
168
249
  this.records.forEach((record, index) => {
169
250
  if (this.isArray) {
170
251
  record.arraySize = this.arraySizes[index];
@@ -249,27 +330,6 @@ class FranchiseFileTable extends EventEmitter {
249
330
  }
250
331
  }
251
332
  }
252
- else {
253
- // The field was not empty, let's check if it is now
254
- const referenceData = utilService.getReferenceData(this._data.slice(0, 32));
255
- if (referenceData.tableId === 0) {
256
- // The record has a reference to table id 0, now we need to see if its referencing the 0th record or not
257
- // We have to be careful not to mistake a null reference for an empty reference to record 0
258
- if (referenceData.rowNumber !== 0) {
259
- // If the value is referencing a row number greater than 0, we need to treat this record as an empty reference.
260
- // onRecordEmpty(record);
261
- }
262
- else {
263
- // The record was updated to either be a null reference or an empty record reference to row 0.
264
- // First let's make sure we aren't editing row 0...if we are, then the value is a null reference
265
- // because a record cannot point to itself.
266
- if (this.index > 0) {
267
- // Now that we know that the edit isn't coming from row 0, let's check if row 0 is an empty reference.
268
- // If it is NOT an empty reference, then the updated value is not an empty reference.
269
- }
270
- }
271
- }
272
- }
273
333
  }
274
334
 
275
335
  that.emit('change');
@@ -379,9 +439,10 @@ class FranchiseFileTable extends EventEmitter {
379
439
  let previousEmptyRecordIndex = null;
380
440
  let currentEmptyRecordIndex = firstEmptyRecord;
381
441
 
382
- if (this.header.nextRecordToUse !== this.header.recordCapacity) {
442
+ if (firstEmptyRecord !== this.header.recordCapacity) {
383
443
  while (currentEmptyRecordIndex !== this.header.recordCapacity) {
384
- let nextEmptyRecordIndex = this.data.readUInt32BE(this.header.table1StartIndex + (currentEmptyRecordIndex * sizeOfEachRecord));
444
+ // let nextEmptyRecordIndex = this.data.readUInt32BE(this.header.table1StartIndex + (currentEmptyRecordIndex * sizeOfEachRecord));
445
+ let nextEmptyRecordIndex = utilService.getReferenceData(this.records[currentEmptyRecordIndex]._data.slice(0, 32)).rowNumber;
385
446
 
386
447
  emptyRecords.set(currentEmptyRecordIndex, {
387
448
  previous: previousEmptyRecordIndex,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "madden-franchise",
3
- "version": "2.3.7",
3
+ "version": "2.3.12",
4
4
  "description": "Tools to read a madden franchise file and get data from it",
5
5
  "main": "FranchiseFile.js",
6
6
  "scripts": {