Worms4Editor/LibW4M/Data/Teams/TeamsCollective.cs

60 lines
2.3 KiB
C#

using LibW4M.Data.WeaponFactory;
using LibXom.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibW4M.Data.Teams
{
public class TeamsCollective : SaveDataCollective
{
public TeamsCollective(W4SaveFile fileBelongs, XomContainer mainContainer) : base(fileBelongs, mainContainer)
{
}
public override void Load()
{
int[] decompressedCollective = mainContainer.Decompress();
int numTeams = decompressedCollective[0];
for (int i = 0; i < numTeams; i++)
{
TeamData team = new TeamData(this.fileBelongs, this.fileBelongs.xomFile.GetContainerById(decompressedCollective[i + 1]));
this.collectiveEntries.Add(team);
}
}
public override void Save()
{
// Annoyingly, in the actual XOM Highscore Data is stored alongside teamdata in the collective,
// this means to save properly, we need to copy the highscore data and write it back after,
// For consistancy and my sanity, i have abstracted this away, so highscore data has its own collective.
int[] decompressedCollective = mainContainer.Decompress();
int oldLen = decompressedCollective[0];
// Copy HighScore data.
int[] highScoreData = new int[(decompressedCollective.Length - (oldLen + 1))];
Array.ConstrainedCopy(decompressedCollective, oldLen + 1, highScoreData, 0, highScoreData.Length);
// Recalculate collective size and offsets
int sz = this.Length;
int[] collective = new int[(sz + 1) + (highScoreData.Length)];
collective[0] = sz;
for (int i = 0; i < sz; i++)
{
TeamData team = collectiveEntries[i] as TeamData;
team.Save();
collective[i + 1] = team.mainContainer.Id;
}
// Copy highscore data back to the collective.
Array.ConstrainedCopy(highScoreData, 0, collective, sz + 1, highScoreData.Length);
// Compress whole collective and update the underlying container.
mainContainer.CompressAndUpdate(collective);
}
}
}