Worms4Editor/LibW4M/Data/Highscores/HighscoreCollective.cs

71 lines
2.7 KiB
C#

using LibXom.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibW4M.Data.Highscores
{
public class HighscoreCollective : SaveDataCollective
{
public HighscoreCollective(W4SaveFile fileBelongs, XomContainer mainContainer) : base(fileBelongs, mainContainer)
{
}
public override void Create()
{
HighscoreData highscore = new HighscoreData(this.fileBelongs, this.fileBelongs.CreateContainer("HighScoreData"), false);
this.collectiveEntries.Add(highscore);
}
public override void Load()
{
// highscore data is stored after teams data section of TeamDataCollective.
int[] decompressedCollective = mainContainer.Decompress();
int teamsLen = decompressedCollective[0];
int highscoreStart = teamsLen + 1;
int highscoresLen = decompressedCollective[highscoreStart];
for (int i = 0; i < highscoresLen; i++)
{
HighscoreData highscoreData = new HighscoreData(fileBelongs, fileBelongs.LookupContainerById(decompressedCollective[highscoreStart + i + 1]));
collectiveEntries.Add(highscoreData);
}
}
public override void Save()
{
// Decompress the collective
int[] decompressedCollective = mainContainer.Decompress();
// get total length of teams collective
int teamsLen = decompressedCollective[0];
// Highscore collective starts right *after* the teams collective.
int highscoreStart = teamsLen + 1;
// get the current number of highscores.
int highscoresLen = this.Length;
// copy teams data from collective.
int[] teamsData = new int[teamsLen + 1];
Array.Copy(decompressedCollective, teamsData, teamsData.Length);
// copy teams data back to collective.
int[] newCollective = new int[teamsData.Length + highscoresLen + 1];
Array.Copy(teamsData, newCollective, teamsData.Length);
// populate highscores collective
newCollective[highscoreStart] = highscoresLen;
for (int i = 0; i < highscoresLen; i++)
{
HighscoreData highscoreData = collectiveEntries[i] as HighscoreData;
highscoreData.Save();
newCollective[highscoreStart + i + 1] = highscoreData.mainContainer.Id;
}
// Compress whole collective and update the underlying container.
mainContainer.CompressAndUpdate(newCollective);
}
}
}