121 lines
4.3 KiB
C#
121 lines
4.3 KiB
C#
using LibW4M.Data.Highscores;
|
|
using LibXom.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LibW4M.Data.Stats
|
|
{
|
|
public class TeamStatsCollective : SaveDataCollective
|
|
{
|
|
public List<XomString> TeamNames = new List<XomString>();
|
|
|
|
public TeamStatsCollective(W4SaveFile fileBelongs, XomContainer mainContainer) : base(fileBelongs, mainContainer)
|
|
{
|
|
}
|
|
|
|
public override void Load()
|
|
{
|
|
// team stats are stored *after* the regular stats
|
|
|
|
int[] decompressedCollective = mainContainer.Decompress();
|
|
int statsLen = decompressedCollective[0];
|
|
|
|
|
|
int teamStatsStart = statsLen + 1;
|
|
int teamStatsLen = decompressedCollective[teamStatsStart];
|
|
|
|
// And then the team name this is associated with, is after that
|
|
int teamNamesStart = (statsLen + 1) + (teamStatsLen + 1);
|
|
int teamNamesLen = decompressedCollective[teamNamesStart];
|
|
|
|
for(int i = 0; i < teamNamesLen; i++)
|
|
TeamNames.Add(fileBelongs.LookupStringFromId(decompressedCollective[(teamNamesStart + 1) + i]));
|
|
|
|
for(int i = 0; i < teamStatsLen; i++)
|
|
{
|
|
StatsContainerData stats = new StatsContainerData(this.fileBelongs, fileBelongs.LookupContainerById(decompressedCollective[(teamStatsStart + 1) + i]));
|
|
|
|
if (i < TeamNames.Count)
|
|
stats.teamName = TeamNames[i];
|
|
|
|
this.collectiveEntries.Add(stats);
|
|
}
|
|
}
|
|
|
|
public override void Delete(SaveDataEntry entry)
|
|
{
|
|
for (int i = 0; i < this.Length; i++)
|
|
{
|
|
SaveDataEntry saveDataEntry = this[i];
|
|
if (saveDataEntry.Equals(entry))
|
|
{
|
|
collectiveEntries.RemoveAt(i);
|
|
if(i > 0 && i < TeamNames.Count)
|
|
TeamNames.RemoveAt(i);
|
|
saveDataEntry.DeleteEntries();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void Save()
|
|
{
|
|
// Decompress the collective
|
|
int[] decompressedCollective = mainContainer.Decompress();
|
|
|
|
// get total length of stats
|
|
int statsLen = decompressedCollective[0];
|
|
|
|
// Team Stats collective starts right *after* the stats collective.
|
|
int teamStatsStart = statsLen + 1;
|
|
int teamStatsLen = this.Length;
|
|
|
|
int teamNamesStart = (statsLen + 1) + (teamStatsLen + 1);
|
|
int teamNamesLen = this.TeamNames.Count;
|
|
|
|
// copy regular stats data from collective.
|
|
int[] statsData = new int[statsLen + 1];
|
|
Array.Copy(decompressedCollective, statsData, statsData.Length);
|
|
|
|
// copy regular stats data back to collective.
|
|
int[] newCollective = new int[statsData.Length + (1 + teamStatsLen) + (1 + TeamNames.Count)];
|
|
Array.Copy(statsData, newCollective, statsData.Length);
|
|
|
|
// populate team stats collective
|
|
newCollective[teamStatsStart] = teamStatsLen;
|
|
|
|
for(int i = 0; i < teamStatsLen; i++)
|
|
{
|
|
StatsContainerData teamStatsData = collectiveEntries[i] as StatsContainerData;
|
|
if (teamStatsData is null) continue;
|
|
teamStatsData.Save();
|
|
newCollective[teamStatsStart + i + 1] = teamStatsData.mainContainer.Id;
|
|
}
|
|
|
|
|
|
// add team names too
|
|
newCollective[teamNamesStart] = teamNamesLen;
|
|
for (int i = 0; i < teamNamesLen; i++)
|
|
newCollective[teamNamesStart + i + 1] = this.TeamNames[i].Id;
|
|
|
|
// Compress whole collective and update the underlying container.
|
|
mainContainer.CompressAndUpdate(newCollective);
|
|
|
|
}
|
|
|
|
public override void Create()
|
|
{
|
|
// create stat container data
|
|
StatsContainerData stat = new StatsContainerData(this.fileBelongs, this.fileBelongs.CreateContainer("StatsContainer"), false);
|
|
// set team name
|
|
stat.teamName = this.fileBelongs.LookupString("Untitled Statistics");
|
|
this.TeamNames.Add(stat.teamName);
|
|
// add to collective
|
|
this.collectiveEntries.Add(stat);
|
|
}
|
|
}
|
|
}
|