Skip to content

Commit

Permalink
Allow add operation in patcher to upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
KalopsiaTwilight committed Jul 29, 2024
1 parent 07498af commit 2d2814e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
14 changes: 7 additions & 7 deletions DBCD/DBCDStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public interface IDBCDStorage : IEnumerable<DynamicKeyValuePair<int>>, IDictiona
void Save(string filename);
void Export(string fileName);
void Import(string fileName);
void AddEmpty();
void AddEmpty(int? id = null);
void RemoveFromStorage(int key);
Type GetRowType();
}
Expand Down Expand Up @@ -248,7 +248,7 @@ public void Export(string filename)
}
}

public void AddEmpty()
public void AddEmpty(int? id = null)
{
var lastItem = Values.LastOrDefault();
if (lastItem == null)
Expand Down Expand Up @@ -279,11 +279,11 @@ public void AddEmpty()
stringField.SetValue(toAdd, string.Empty);
}

var id = lastItem.ID + 1;
var idField = typeof(T).GetField("ID");
idField?.SetValue(toAdd, id);
Add(id, new DBCDRow(id, toAdd, fieldAccessor));
db2Storage.Add(id, toAdd);
var toSetId = id ?? Values.Max(x => x.ID) + 1;
var idField = typeof(T).GetField(fieldNames.FirstOrDefault() ?? "ID");
idField?.SetValue(toAdd, toSetId);
Add(toSetId, new DBCDRow(toSetId, toAdd, fieldAccessor));
db2Storage.Add(toSetId, toAdd);
}

public void RemoveFromStorage(int key)
Expand Down
19 changes: 11 additions & 8 deletions DBXPatching.Core/DBXPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,17 @@ private DBXPatchingOperationResult ApplyAddRecordInstructions(AddRecordInstructi
var result = OpenDb(instruction.Filename, out var records);
if (result.ResultCode != PatchingResultCode.OK) { return result; }

records!.AddEmpty();
var row = records.Values.LastOrDefault();

DBCDRow? row;
if (instruction.RecordId != null && records!.Keys.Any(x => x == instruction.RecordId))
{
row = records[instruction.RecordId.Value];
} else
{
records!.AddEmpty(instruction.RecordId);
row = records.Values.LastOrDefault();
}

if (row == null)
{
return new DBXPatchingOperationResult()
Expand All @@ -151,12 +160,6 @@ private DBXPatchingOperationResult ApplyAddRecordInstructions(AddRecordInstructi
};
}

if (instruction.RecordId.HasValue)
{
row.ID = instruction.RecordId.Value;
row[instruction.Filename, row.GetDynamicMemberNames().First()] = instruction.RecordId.Value;
}

if (!string.IsNullOrEmpty(instruction.RecordIdReference))
{
row.ID = _referenceIds[instruction.RecordIdReference];
Expand Down

0 comments on commit 2d2814e

Please sign in to comment.