Worms4Editor/LibW4M/Data/Highscores/HighscoreCollective.cs

71 lines
2.7 KiB
C#
Raw Normal View History

2023-01-15 03:46:18 +00:00
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()
{
2023-04-01 12:57:34 +00:00
HighscoreData highscore = new HighscoreData(this.fileBelongs, this.fileBelongs.CreateContainer("HighScoreData"), false);
this.collectiveEntries.Add(highscore);
}
2023-01-15 03:46:18 +00:00
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++)
{
2023-02-16 04:12:22 +00:00
HighscoreData highscoreData = new HighscoreData(fileBelongs, fileBelongs.LookupContainerById(decompressedCollective[highscoreStart + i + 1]));
2023-01-15 03:46:18 +00:00
collectiveEntries.Add(highscoreData);
}
}
public override void Save()
{
// Decompress the collective
int[] decompressedCollective = mainContainer.Decompress();
// get total length of teams collective
int teamsLen = decompressedCollective[0];
2023-02-25 06:46:12 +00:00
2023-01-15 03:46:18 +00:00
// Highscore collective starts right *after* the teams collective.
int highscoreStart = teamsLen + 1;
// get the current number of highscores.
2023-02-25 06:46:12 +00:00
int highscoresLen = this.Length;
2023-01-15 03:46:18 +00:00
// 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);
}
}
}