using System.IO;
using System.Text;
using Syroot.IO;
using Syroot.Maths;
using Syroot.Worms.Core;
namespace Syroot.Worms.Gen2.Worms2
{
///
/// Represents map configuration stored by the land generator in LAND.DAT files.
/// Used by W2. S. https://worms2d.info/Land_Data_file.
///
public class LandData : ILoadableFile
{
// ---- CONSTANTS ----------------------------------------------------------------------------------------------
private const int _signature = 0x1A444E4C; // "LND", 0x1A
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
///
/// Initializes a new instance of the class.
///
public LandData()
{
}
///
/// Initializes a new instance of the class, loading the data from the given
/// .
///
/// The to load the data from.
public LandData(Stream stream)
{
Load(stream);
}
///
/// Initializes a new instance of the class, loading the data from the given file.
///
/// The name of the file to load the data from.
public LandData(string fileName)
{
Load(fileName);
}
// ---- PROPERTIES ---------------------------------------------------------------------------------------------
///
/// Gets or sets the size of the landscape in pixels.
///
public Vector2 Size { get; set; }
///
/// Gets or sets a value indicating whether an indestructible top border will be enabled.
///
public bool TopBorder { get; set; }
///
/// Gets or sets an array of coordinates at which objects can be placed.
///
public Vector2[] ObjectLocations { get; set; }
///
/// Gets or sets the visual foreground image.
///
public Image Foreground { get; set; }
///
/// Gets or sets the collision mask of the landscape.
///
public Image CollisionMask { get; set; }
///
/// Gets or sets the visual background image.
///
public Image Background { get; set; }
///
/// Gets or sets an image of unknown use.
///
public Image UnknownImage { get; set; }
///
/// Gets or sets the path to the land image file.
///
public string LandTexturePath { get; set; }
///
/// Gets or sets the path to the Water.dir file.
///
public string WaterDirPath { get; set; }
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
///
/// Loads the data from the given .
///
/// The to load the data from.
public void Load(Stream stream)
{
using (BinaryDataReader reader = new BinaryDataReader(stream, Encoding.ASCII, true))
{
// Read the header.
if (reader.ReadInt32() != _signature)
{
throw new InvalidDataException("Invalid LND file signature.");
}
int fileLength = reader.ReadInt32();
// Read the data.
Size = reader.ReadStruct();
TopBorder = reader.ReadBoolean(BinaryBooleanFormat.NonZeroDword);
// Read the possible object coordinate array.
ObjectLocations = new Vector2[reader.ReadInt32()];
for (int i = 0; i < ObjectLocations.Length; i++)
{
ObjectLocations[i] = reader.ReadStruct();
}
int unknown = reader.ReadInt32(); // Seems 0.
// Read the image data.
Foreground = new Image(stream);
CollisionMask = new Image(stream);
Background = new Image(stream);
UnknownImage = new Image(stream);
// Read the file paths.
LandTexturePath = reader.ReadString(BinaryStringFormat.ByteLengthPrefix);
WaterDirPath = reader.ReadString(BinaryStringFormat.ByteLengthPrefix);
}
}
///
/// Loads the data from the given file.
///
/// The name of the file to load the data from.
public void Load(string fileName)
{
using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Load(stream);
}
}
}
}