Worms4Editor/LibW4M/Data/Stats/StatsContainerData.cs

91 lines
2.5 KiB
C#
Raw Normal View History

2023-02-23 09:43:15 +00:00
using LibXom.Data;
using LibXom.Streams;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibW4M.Data.Stats
{
public class StatsContainerData : SaveDataEntry
{
2023-03-04 16:10:07 +00:00
public int[] Values;
2023-02-25 06:46:12 +00:00
internal XomString? teamName;
2023-04-01 12:57:34 +00:00
public StatsContainerData(W4SaveFile fileBelongs, XomContainer mainContainer, bool load=true) : base(fileBelongs, mainContainer, load)
2023-02-23 09:43:15 +00:00
{
}
2023-03-04 16:10:07 +00:00
public bool IsRenamable
{
get
{
return (teamName is not null);
}
}
public void ChangeName(string newName)
{
if (!IsRenamable) return;
if (teamName.Value.Equals(newName)) return;
for(int i = 0; i < this.fileBelongs.TeamStatsCollective.Length; i++)
{
StatsContainerData? stat = this.fileBelongs.TeamStatsCollective[i] as StatsContainerData;
if (stat is null) continue;
2023-03-07 07:49:53 +00:00
if (stat.Uuid.Equals(this.Uuid))
2023-03-04 16:10:07 +00:00
{
if (i > this.fileBelongs.TeamStatsCollective.TeamNames.Count) break;
XomString xStr = this.fileBelongs.LookupString(newName);
// update in both places
this.fileBelongs.TeamStatsCollective.TeamNames[i] = xStr;
this.teamName = xStr;
}
}
}
2023-02-25 06:46:12 +00:00
public override string FriendlyName
{
get
{
2023-03-04 16:10:07 +00:00
if (IsRenamable) return teamName.Value;
return String.Join(" - ", Values);
2023-02-25 06:46:12 +00:00
}
}
2023-02-23 09:43:15 +00:00
public override void Load()
{
using (XomStreamReader reader = new XomStreamReader(new MemoryStream(this.mainContainer.GetData())))
{
reader.Skip(3);
Values = reader.ReadInt32Array();
}
}
2023-04-01 12:57:34 +00:00
internal override void loadDefaults()
{
this.Values = new int[24];
}
2023-02-23 09:43:15 +00:00
public override void Save()
{
using (MemoryStream ms = new MemoryStream())
{
using (XomStreamWriter writer = new XomStreamWriter(ms))
{
writer.Skip(3);
writer.WriteInt32Array(Values);
ms.Seek(0x00, SeekOrigin.Begin);
mainContainer.SetData(ms.ToArray());
}
}
}
}
}