madden-franchise 2.3.3 → 2.3.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.
@@ -68,7 +68,7 @@ class FranchiseFileField extends EventEmitter {
68
68
  this.emit('change');
69
69
  };
70
70
 
71
- setUnformattedValueWithoutChangeEvent(unformattedValue) {
71
+ setUnformattedValueWithoutChangeEvent(unformattedValue, suppressErrors) {
72
72
  if (!utilService.isString(unformattedValue)) { throw new Error(`Argument must be of type string. You passed in a ${typeof unformattedValue}.`); }
73
73
  else if (!utilService.stringOnlyContainsBinaryDigits(unformattedValue)) { throw new Error(`Argument must only contain binary digits 1 and 0. If you would like to set the value, please set the 'value' attribute instead.`)}
74
74
  else {
@@ -82,7 +82,7 @@ class FranchiseFileField extends EventEmitter {
82
82
  }
83
83
 
84
84
  // check for 'allowed' error - this will be true if the unformatted value is invalid.
85
- if (this._offset.enum && value === unformattedValue.padStart(this._offset.length, '0')) {
85
+ if (this._offset.enum && value === unformattedValue.padStart(this._offset.length, '0') && !suppressErrors) {
86
86
  throw new Error(`Argument is not a valid unformatted value for this field. You passed in ${value}.`)
87
87
  }
88
88
 
@@ -25,6 +25,27 @@ class FranchiseFileTable extends EventEmitter {
25
25
  };
26
26
 
27
27
  get hexData () {
28
+ this.updateBuffer();
29
+ return this.data;
30
+ };
31
+
32
+ set schema (schema) {
33
+ // console.time('set schema');
34
+ this._schema = schema;
35
+ const modifiedHeaderAttributes = this.strategy.parseHeaderAttributesFromSchema(schema, this.data, this.header);
36
+
37
+ this.header.headerSize = modifiedHeaderAttributes.headerSize;
38
+ this.header.record1Size = modifiedHeaderAttributes.record1Size;
39
+ this.header.table1StartIndex = modifiedHeaderAttributes.table1StartIndex;
40
+ this.header.table2StartIndex = modifiedHeaderAttributes.table2StartIndex;
41
+ // console.timeEnd('set schema');
42
+ };
43
+
44
+ get schema () {
45
+ return this._schema;
46
+ };
47
+
48
+ updateBuffer() {
28
49
  // need to check table2 data first because it may change offsets of the legit records.
29
50
  const table2Data = this.strategy.getTable2BinaryData(this.table2Records, this.data.slice(this.header.table2StartIndex));
30
51
 
@@ -62,23 +83,21 @@ class FranchiseFileTable extends EventEmitter {
62
83
  bufferArrays = bufferArrays.concat(table2Data);
63
84
 
64
85
  this.data = Buffer.concat(bufferArrays);
65
- return this.data;
66
86
  };
67
87
 
68
- set schema (schema) {
69
- // console.time('set schema');
70
- this._schema = schema;
71
- const modifiedHeaderAttributes = this.strategy.parseHeaderAttributesFromSchema(schema, this.data, this.header);
88
+ setNextRecordToUse(index, resetEmptyRecordMap) {
89
+ // We need to update the table header to use this row next
90
+ this.header.nextRecordToUse = index;
72
91
 
73
- this.header.headerSize = modifiedHeaderAttributes.headerSize;
74
- this.header.record1Size = modifiedHeaderAttributes.record1Size;
75
- this.header.table1StartIndex = modifiedHeaderAttributes.table1StartIndex;
76
- this.header.table2StartIndex = modifiedHeaderAttributes.table2StartIndex;
77
- // console.timeEnd('set schema');
78
- };
92
+ // And finally update the buffer to reflect this change
93
+ this.data.writeUInt32BE(index, this.header.headerOffset - 4);
79
94
 
80
- get schema () {
81
- return this._schema;
95
+ // Recalculate the empty record map if the option is set and the
96
+ // records have already been read.
97
+ if (resetEmptyRecordMap && this.recordsRead) {
98
+ this.updateBuffer();
99
+ this.emptyRecords = this._parseEmptyRecords();
100
+ }
82
101
  };
83
102
 
84
103
  // 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.
@@ -154,6 +173,7 @@ class FranchiseFileTable extends EventEmitter {
154
173
  }
155
174
 
156
175
  const that = this;
176
+
157
177
  record.on('change', function (changedOffset) {
158
178
  this.isChanged = true;
159
179
 
@@ -215,7 +235,7 @@ class FranchiseFileTable extends EventEmitter {
215
235
  if (!previousEmptyReference) {
216
236
  // If no previous empty record exists and a next record exists, we need to update the header to
217
237
  // point to this record as the next record to use.
218
- updateNextRecordToUseHeaderAndBuffer(emptyRecordReference.next);
238
+ that.setNextRecordToUse(emptyRecordReference.next);
219
239
  }
220
240
  }
221
241
 
@@ -223,20 +243,32 @@ class FranchiseFileTable extends EventEmitter {
223
243
  // Then there are no more empty references in the table
224
244
  // Update the table header nextRecordToUse back to the table record capacity
225
245
  if (!previousEmptyReference && !nextEmptyReference) {
226
- updateNextRecordToUseHeaderAndBuffer(that.header.recordCapacity);
246
+ that.setNextRecordToUse(that.header.recordCapacity);
227
247
  }
228
248
  }
229
249
  }
250
+ else {
251
+ // The field was not empty, let's check if it is now
252
+ const referenceData = utilService.getReferenceData(this._data.slice(0, 32));
253
+ if (referenceData.tableId === 0) {
254
+ // In this case, the record is now empty. Add an entry to the empty record map
255
+ // onRecordEmpty(this);
256
+ }
257
+ }
230
258
  }
231
259
 
232
260
  that.emit('change');
233
261
  });
234
262
 
235
263
  record.on('empty', function () {
264
+ onRecordEmpty(this);
265
+ });
266
+
267
+ function onRecordEmpty(record) {
236
268
  // First, check if the record is already empty. If so, don't do anything...
237
269
  // If not empty, then we need to empty it.
238
- if (!this.isEmpty) {
239
- this.isChanged = true;
270
+ if (!record.isEmpty) {
271
+ record.isChanged = true;
240
272
  const lastEmptyRecordMapEntry = Array.from(that.emptyRecords).pop();
241
273
 
242
274
  // When we empty a record, we need to check if another empty record exists in the table.
@@ -248,11 +280,11 @@ class FranchiseFileTable extends EventEmitter {
248
280
 
249
281
  that.emptyRecords.set(lastEmptyRecordIndex, {
250
282
  previous: lastEmptyRecordMapEntry[1].previous,
251
- next: this.index
283
+ next: record.index
252
284
  });
253
285
 
254
286
  // Then we need to update the current record index to point to the record capacity.
255
- that.emptyRecords.set(this.index, {
287
+ that.emptyRecords.set(record.index, {
256
288
  previous: lastEmptyRecordIndex,
257
289
  next: that.header.recordCapacity
258
290
  });
@@ -263,32 +295,24 @@ class FranchiseFileTable extends EventEmitter {
263
295
 
264
296
  // And update both record's data. This will set the unformatted and formatted values
265
297
  // without emitting an event
266
- changeRecordBuffers(lastEmptyRecordIndex, this.index);
267
- changeRecordBuffers(this.index, that.header.recordCapacity);
298
+ changeRecordBuffers(lastEmptyRecordIndex, record.index);
299
+ changeRecordBuffers(record.index, that.header.recordCapacity);
268
300
  }
269
301
  else {
270
302
  // In this case, the record that was emptied is the first empty record in the table
271
- that.emptyRecords.set(this.index, {
303
+ that.emptyRecords.set(record.index, {
272
304
  previous: null,
273
305
  next: that.header.recordCapacity
274
306
  });
275
307
 
276
308
  // Finally update the table header and buffer so that the game uses this new empty
277
309
  // record as the next record to use (or fill)
278
- updateNextRecordToUseHeaderAndBuffer(this.index);
279
- changeRecordBuffers(this.index, that.header.recordCapacity);
310
+ that.setNextRecordToUse(record.index);
311
+ changeRecordBuffers(record.index, that.header.recordCapacity);
280
312
  }
281
313
 
282
314
  that.emit('change');
283
315
  }
284
- });
285
-
286
- function updateNextRecordToUseHeaderAndBuffer(nextRecordToUse) {
287
- // We need to update the table header to use this row next
288
- that.header.nextRecordToUse = nextRecordToUse;
289
-
290
- // And finally update the buffer to reflect this change
291
- that.data.writeUInt32BE(nextRecordToUse, that.header.headerOffset - 4);
292
316
  };
293
317
 
294
318
  function changeRecordBuffers(index, emptyRecordReference) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "madden-franchise",
3
- "version": "2.3.3",
3
+ "version": "2.3.4",
4
4
  "description": "Tools to read a madden franchise file and get data from it",
5
5
  "main": "FranchiseFile.js",
6
6
  "scripts": {