madden-franchise 2.2.6 → 2.3.2

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.
@@ -64,6 +64,11 @@ class FranchiseFileField extends EventEmitter {
64
64
  };
65
65
 
66
66
  set unformattedValue (unformattedValue) {
67
+ this.setUnformattedValueWithoutChangeEvent(unformattedValue);
68
+ this.emit('change');
69
+ };
70
+
71
+ setUnformattedValueWithoutChangeEvent(unformattedValue) {
67
72
  if (!utilService.isString(unformattedValue)) { throw new Error(`Argument must be of type string. You passed in a ${typeof unformattedValue}.`); }
68
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.`)}
69
74
  else {
@@ -89,10 +94,8 @@ class FranchiseFileField extends EventEmitter {
89
94
  else {
90
95
  this._unformattedValue = unformattedValue;
91
96
  }
92
-
93
- this.emit('change');
94
97
  }
95
- };
98
+ }
96
99
  };
97
100
 
98
101
  module.exports = FranchiseFileField;
@@ -11,6 +11,7 @@ class FranchiseFileRecord extends EventEmitter {
11
11
  this._fields = parseRecordFields(data, offsetTable);
12
12
  this.isChanged = false;
13
13
  this.arraySize = null;
14
+ this.isEmpty = false;
14
15
 
15
16
  const that = this;
16
17
  this._fields.forEach((field) => {
@@ -58,6 +59,19 @@ class FranchiseFileRecord extends EventEmitter {
58
59
  return Buffer.from(utilService.binaryBlockToDecimalBlock(this._data));
59
60
  };
60
61
 
62
+ get fields () {
63
+ return this._fields;
64
+ };
65
+
66
+ set data (data) {
67
+ this._data = data;
68
+
69
+ this._fields.forEach((field) => {
70
+ const unformattedValue = data.slice(field.offset.offset, field.offset.offset + field.offset.length);
71
+ field.setUnformattedValueWithoutChangeEvent(unformattedValue);
72
+ });
73
+ };
74
+
61
75
  getFieldByKey(key) {
62
76
  return this._fields.find((field) => { return field.key === key; });
63
77
  };
@@ -72,8 +86,9 @@ class FranchiseFileRecord extends EventEmitter {
72
86
  return field ? field.referenceData : null;
73
87
  };
74
88
 
75
- get fields () {
76
- return this._fields;
89
+ empty() {
90
+ this.emit('empty');
91
+ this.isEmpty = true;
77
92
  };
78
93
  };
79
94
 
@@ -1,4 +1,3 @@
1
- const assert = require('assert');
2
1
  const EventEmitter = require('events').EventEmitter;
3
2
  const utilService = require('./services/utilService');
4
3
  const FranchiseFileRecord = require('./FranchiseFileRecord');
@@ -22,6 +21,7 @@ class FranchiseFileTable extends EventEmitter {
22
21
  this.records = [];
23
22
  this.table2Records = [];
24
23
  this.arraySizes = [];
24
+ this.emptyRecords = new Map();
25
25
  };
26
26
 
27
27
  get hexData () {
@@ -39,8 +39,8 @@ class FranchiseFileTable extends EventEmitter {
39
39
 
40
40
  let arraySizeBuffer = Buffer.alloc(this.header.data1RecordCount * 4);
41
41
 
42
- this.arraySizes.forEach((arraySize) => {
43
- arraySizeBuffer.writeUInt32BE(arraySize);
42
+ this.arraySizes.forEach((arraySize, index) => {
43
+ arraySizeBuffer.writeUInt32BE(arraySize, (index * 4));
44
44
  });
45
45
 
46
46
  bufferArrays.push(arraySizeBuffer);
@@ -122,9 +122,10 @@ class FranchiseFileTable extends EventEmitter {
122
122
  } else {
123
123
  reject('Cannot read records: Schema is not defined.');
124
124
  }
125
+
126
+ this.emptyRecords = this._parseEmptyRecords();
125
127
 
126
128
  let offsetTableToUse = this.offsetTable;
127
-
128
129
  const mandatoryOffsetsToLoad = this.strategy.getMandatoryOffsets(this.offsetTable);
129
130
 
130
131
  if (attribsToLoad) {
@@ -148,12 +149,157 @@ class FranchiseFileTable extends EventEmitter {
148
149
  record.arraySize = this.arraySizes[index];
149
150
  }
150
151
 
152
+ if (this.emptyRecords.get(index)) {
153
+ record.isEmpty = true;
154
+ }
155
+
151
156
  const that = this;
152
157
  record.on('change', function (changedOffset) {
153
158
  this.isChanged = true;
154
- that.arraySizes[index] = this.arraySize;
159
+
160
+ if (that.isArray) {
161
+ that.arraySizes[index] = this.arraySize;
162
+ }
163
+
164
+ // When a record changes, we need to check if it was previously empty
165
+ // If so, we need to consider the record as no longer empty
166
+ // So we need to adjust the empty records
167
+
168
+ // Ex: Empty record list looks like this: A -> B -> C
169
+ // When B's value is changed, the records need updated to: A -> C
170
+ const emptyRecordReference = that.emptyRecords.get(this.index);
171
+ const changedRecordWasEmpty = emptyRecordReference !== null && emptyRecordReference !== undefined;
172
+
173
+ if (changedRecordWasEmpty) {
174
+
175
+ // Delete the empty record entry because it is no longer empty
176
+ that.emptyRecords.delete(this.index);
177
+
178
+ // Set the isEmpty back to false because it's no longer empty
179
+ this.isEmpty = false;
180
+
181
+ // Check if there is a previous empty record
182
+ const previousEmptyReference = that.emptyRecords.get(emptyRecordReference.previous);
183
+
184
+ if (previousEmptyReference) {
185
+ // Set the previous empty record to point to the old reference's next node
186
+ that.emptyRecords.set(emptyRecordReference.previous, {
187
+ previous: that.emptyRecords.get(emptyRecordReference.previous).previous,
188
+ next: emptyRecordReference.next
189
+ });
190
+
191
+ // change the table buffer and record buffer to reflect this change
192
+ changeRecordBuffers(emptyRecordReference.previous, emptyRecordReference.next);
193
+ }
194
+
195
+ // If there is a next empty reference, update the previous value accordingly to now point
196
+ // to the current record's previous index.
197
+ const nextEmptyReference = that.emptyRecords.get(emptyRecordReference.next);
198
+
199
+ if (nextEmptyReference) {
200
+ that.emptyRecords.set(emptyRecordReference.next, {
201
+ previous: emptyRecordReference.previous,
202
+ next: that.emptyRecords.get(emptyRecordReference.next).next
203
+ });
204
+
205
+ if (!previousEmptyReference) {
206
+ // If no previous empty record exists and a next record exists, we need to update the header to
207
+ // point to this record as the next record to use.
208
+ updateNextRecordToUseHeaderAndBuffer(emptyRecordReference.next);
209
+ }
210
+ }
211
+
212
+ // If there are no previous or next empty references
213
+ // Then there are no more empty references in the table
214
+ // Update the table header nextRecordToUse back to the table record capacity
215
+ if (!previousEmptyReference && !nextEmptyReference) {
216
+ updateNextRecordToUseHeaderAndBuffer(that.header.recordCapacity);
217
+ }
218
+ }
219
+
155
220
  that.emit('change');
156
221
  });
222
+
223
+ record.on('empty', function () {
224
+ // First, check if the record is already empty. If so, don't do anything...
225
+ // If not empty, then we need to empty it.
226
+ if (!this.isEmpty) {
227
+ this.isChanged = true;
228
+ const lastEmptyRecordMapEntry = Array.from(that.emptyRecords).pop();
229
+
230
+ // When we empty a record, we need to check if another empty record exists in the table.
231
+ if (lastEmptyRecordMapEntry !== null && lastEmptyRecordMapEntry !== undefined) {
232
+
233
+ // If an empty record already exists, we just need to get the last empty record
234
+ // and update its index to point to the current record that we want to empty.
235
+ const lastEmptyRecordIndex = lastEmptyRecordMapEntry[0];
236
+
237
+ that.emptyRecords.set(lastEmptyRecordIndex, {
238
+ previous: lastEmptyRecordMapEntry[1].previous,
239
+ next: this.index
240
+ });
241
+
242
+ // Then we need to update the current record index to point to the record capacity.
243
+ that.emptyRecords.set(this.index, {
244
+ previous: lastEmptyRecordIndex,
245
+ next: that.header.recordCapacity
246
+ });
247
+
248
+ // Finally, we need to update the buffers to reflect this data.
249
+ // First, place the new referenced index (will be the first 4 bytes)
250
+ // Next, fill the rest of the record with 0s (the last bytes of the record)
251
+
252
+ // And update both record's data. This will set the unformatted and formatted values
253
+ // without emitting an event
254
+ changeRecordBuffers(lastEmptyRecordIndex, this.index);
255
+ changeRecordBuffers(this.index, that.header.recordCapacity);
256
+ }
257
+ else {
258
+ // In this case, the record that was emptied is the first empty record in the table
259
+ that.emptyRecords.set(this.index, {
260
+ previous: null,
261
+ next: that.header.recordCapacity
262
+ });
263
+
264
+ // Finally update the table header and buffer so that the game uses this new empty
265
+ // record as the next record to use (or fill)
266
+ updateNextRecordToUseHeaderAndBuffer(this.index);
267
+ changeRecordBuffers(this.index, that.header.recordCapacity);
268
+ }
269
+
270
+ that.emit('change');
271
+ }
272
+ });
273
+
274
+ function updateNextRecordToUseHeaderAndBuffer(nextRecordToUse) {
275
+ // We need to update the table header to use this row next
276
+ that.header.nextRecordToUse = nextRecordToUse;
277
+
278
+ // And finally update the buffer to reflect this change
279
+ that.data.writeUInt32BE(nextRecordToUse, that.header.headerOffset - 4);
280
+ };
281
+
282
+ function changeRecordBuffers(index, emptyRecordReference) {
283
+ setBufferToEmptyRecordReference(index, emptyRecordReference);
284
+ setRecordInternalBuffer(index, emptyRecordReference);
285
+ };
286
+
287
+ function setBufferToEmptyRecordReference(index, emptyRecordReference) {
288
+ const recordStartIndex = that.header.table1StartIndex + (index * that.header.record1Size)
289
+ that.data.writeUInt32BE(emptyRecordReference, recordStartIndex);
290
+ that.data.fill(0, recordStartIndex + 4, recordStartIndex + that.header.record1Size);
291
+ };
292
+
293
+ function setRecordInternalBuffer(index, emptyRecordReference) {
294
+ let newData = utilService.dec2bin(emptyRecordReference, 32);
295
+
296
+ const recordSizeInBits = that.header.record1Size * 8;
297
+ if (recordSizeInBits > 32) {
298
+ newData += utilService.dec2bin(0, recordSizeInBits - 32);
299
+ }
300
+
301
+ that.records[index].data = newData;
302
+ };
157
303
  });
158
304
 
159
305
  this.table2Records.forEach((record, index) => {
@@ -173,6 +319,33 @@ class FranchiseFileTable extends EventEmitter {
173
319
  });
174
320
  };
175
321
 
322
+ _parseEmptyRecords() {
323
+ const firstEmptyRecord = this.header.nextRecordToUse;
324
+ const sizeOfEachRecord = this.header.record1Size;
325
+
326
+ let emptyRecords = new Map();
327
+
328
+ let previousEmptyRecordIndex = null;
329
+ let currentEmptyRecordIndex = firstEmptyRecord;
330
+
331
+ if (this.header.nextRecordToUse !== this.header.recordCapacity) {
332
+ while (currentEmptyRecordIndex !== this.header.recordCapacity) {
333
+ let nextEmptyRecordIndex = this.data.readUInt32BE(this.header.table1StartIndex + (currentEmptyRecordIndex * sizeOfEachRecord));
334
+
335
+ emptyRecords.set(currentEmptyRecordIndex, {
336
+ previous: previousEmptyRecordIndex,
337
+ next: nextEmptyRecordIndex
338
+ });
339
+
340
+ previousEmptyRecordIndex = currentEmptyRecordIndex;
341
+ currentEmptyRecordIndex = nextEmptyRecordIndex;
342
+ }
343
+ }
344
+
345
+ return emptyRecords;
346
+ };
347
+
348
+
176
349
  _parseTable2Values(data, header, records) {
177
350
  const that = this;
178
351
  const secondTableData = data.slice(header.table2StartIndex);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "madden-franchise",
3
- "version": "2.2.6",
3
+ "version": "2.3.2",
4
4
  "description": "Tools to read a madden franchise file and get data from it",
5
5
  "main": "FranchiseFile.js",
6
6
  "scripts": {
@@ -45,7 +45,7 @@ M19TableHeaderStrategy.parseHeader = (data) => {
45
45
  const data2RecordCapacity = data.readUInt32BE(headerOffset+48);
46
46
  const data2IndexEntries = data.readUInt32BE(headerOffset+52);
47
47
  const unknown4 = data.readUInt32BE(headerOffset+56);
48
- const data2RecordCount = data.readUInt32BE(headerOffset+60);
48
+ const nextRecordToUse = data.readUInt32BE(headerOffset+60);
49
49
 
50
50
  let offsetStart = 0xE4 + tableStoreLength;
51
51
  const hasSecondTable = tableTotalLength > table1Length;
@@ -95,10 +95,10 @@ M19TableHeaderStrategy.parseHeader = (data) => {
95
95
  'hasSecondTable': hasSecondTable,
96
96
  'table1StartIndex': tableStoreLength === 0 && !isArray ? headerSize : headerSize + (data1RecordCount * 4),
97
97
  'table2StartIndex': tableStoreLength === 0 && !isArray ? headerSize + (data1RecordCount * records1Size) : headerSize + (data1RecordCount * 4) + (data1RecordCount * records1Size),
98
- 'data2recordWords': data2RecordWords,
99
- 'data2RecordCapacity': data2RecordCapacity,
100
- 'data2IndexEntries': data2IndexEntries,
101
- 'data2RecordCount': data2RecordCount
98
+ 'recordWords': data2RecordWords,
99
+ 'recordCapacity': data2RecordCapacity,
100
+ 'numMembers': data2IndexEntries,
101
+ 'nextRecordToUse': nextRecordToUse
102
102
  };
103
103
  };
104
104
 
@@ -46,7 +46,7 @@ M20TableHeaderStrategy.parseHeader = (data) => {
46
46
  const data2RecordCapacity = data.readUInt32BE(headerOffset+48);
47
47
  const data2IndexEntries = data.readUInt32BE(headerOffset+52);
48
48
  const unknown4 = data.readUInt32BE(headerOffset+56);
49
- const data2RecordCount = data.readUInt32BE(headerOffset+60);
49
+ const nextRecordToUse = data.readUInt32BE(headerOffset+60);
50
50
 
51
51
  let offsetStart = 0xE8 + tableStoreLength;
52
52
  const hasSecondTable = tableTotalLength > table1Length;
@@ -104,10 +104,10 @@ M20TableHeaderStrategy.parseHeader = (data) => {
104
104
  'hasSecondTable': hasSecondTable,
105
105
  'table1StartIndex': table1StartIndex,
106
106
  'table2StartIndex': table2StartIndex,
107
- 'data2recordWords': data2RecordWords,
108
- 'data2RecordCapacity': data2RecordCapacity,
109
- 'data2IndexEntries': data2IndexEntries,
110
- 'data2RecordCount': data2RecordCount
107
+ 'recordWords': data2RecordWords,
108
+ 'recordCapacity': data2RecordCapacity,
109
+ 'numMembers': data2IndexEntries,
110
+ 'nextRecordToUse': nextRecordToUse
111
111
  };
112
112
  };
113
113