Worms4Editor/LibW4M/W4SaveFile.cs

149 lines
4.8 KiB
C#

using LibW4M.Data;
using LibW4M.Data.Highscores;
using LibW4M.Data.Schemes;
using LibW4M.Data.Teams;
using LibW4M.Data.WeaponFactory;
using LibXom;
using LibXom.Blocks;
using LibXom.Data;
using System.Runtime.InteropServices;
namespace LibW4M
{
public class W4SaveFile
{
internal List<SaveDataEntry> collectives = new List<SaveDataEntry>();
private WeaponsCollective weaponFactoryCollective;
private TeamsCollective teamDataColective;
private SchemesCollective schemesCollective;
private HighscoreCollective highscoreCollective;
public WeaponsCollective WeaponFactoryCollective
{
get
{
return weaponFactoryCollective;
}
}
public TeamsCollective TeamDataColective
{
get
{
return teamDataColective;
}
}
public HighscoreCollective HighscoreDataCollective
{
get
{
return highscoreCollective;
}
}
public SchemesCollective SchemeCollective
{
get
{
return schemesCollective;
}
}
internal XomFile xomFile;
internal WeaponData findWeaponWithContainerId(int containerId)
{
foreach(WeaponStore store in WeaponFactoryCollective)
{
if (store.Weapon.containerId == containerId) return store.Weapon;
if (store.Cluster.containerId == containerId) return store.Cluster;
}
return new WeaponData(this, xomFile.GetContainerById(containerId));
}
public void Save(string newXom)
{
foreach(SaveDataEntry entry in collectives)
{
entry.Save();
}
XomWriter.WriteXom(xomFile, newXom);
}
public void ExtractAllContainers(string path)
{
foreach(XomType type in xomFile.XomTypes)
{
string outfolder = Path.Combine(path, type.Name);
if (Directory.Exists(outfolder))
Directory.Delete(outfolder, true);
Directory.CreateDirectory(outfolder);
foreach(XomContainer container in type.Containers)
{
while (true)
{
try
{
File.WriteAllBytes(Path.ChangeExtension(Path.Combine(outfolder, container.Id.ToString("X2")), ".bin"), container.GetData());
break;
}
catch
{
continue;
}
}
}
}
}
public XomString[] StringArrayToXomStringArray(string[] strings)
{
XomString[] xstrings = new XomString[strings.Length];
for (int i = 0; i < xstrings.Length; i++) xstrings[i] = LookupString(strings[i]);
return xstrings;
}
public string[] XomStringArrayToStringArray(XomString[] xstrings)
{
string[] strings = new string[xstrings.Length];
for (int i = 0; i < strings.Length; i++) strings[i] = xstrings[i].Value;
return strings;
}
public int[] XomStringArrayToIntArray(XomString[] strs)
{
int[] ids = new int[strs.Length];
for (int i = 0; i < ids.Length; i++) ids[i] = strs[i].Id;
return ids;
}
public XomString[] IntArrayToXomStringArray(int[] stringIds)
{
XomString[] strings = new XomString[stringIds.Length];
for (int i = 0; i < strings.Length; i++) strings[i] = this.LookupStringFromId(stringIds[i]);
return strings;
}
public XomString LookupStringFromId(int id)
{
return xomFile.GetStringById(id);
}
public XomString LookupString(string value)
{
return xomFile.AddOrGetString(value);
}
public W4SaveFile(XomFile w4Save)
{
this.xomFile = w4Save;
// Read Weapon Factory
weaponFactoryCollective = new WeaponsCollective(this, this.xomFile.GetTypeByName("WeaponFactoryCollective").Containers.First());
// Abtract away teamdata storing both highscores and teams.
teamDataColective = new TeamsCollective(this, this.xomFile.GetTypeByName("TeamDataColective").Containers.First());
highscoreCollective = new HighscoreCollective(this, this.xomFile.GetTypeByName("TeamDataColective").Containers.First());
// Read Schemes / Game Styles
schemesCollective = new SchemesCollective(this, this.xomFile.GetTypeByName("SchemeColective").Containers.First());
}
}
}