Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/TSMapEditor/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public static class Constants
public static float DepthRenderStep = 0;

public const string ClipboardMapDataFormatValue = "ScenarioEditorCopiedMapData";
public const string ClipboardTriggerActionEventFormatValue = "ScenarioEditorCopiedTriggerData";
public const string ClipboardTriggerFormatValue = "ScenarioEditorCopiedTrigger";
public const string UserDataFolder = "UserData";

public const char NewTheaterGenericLetter = 'G';
Expand Down
13 changes: 13 additions & 0 deletions src/TSMapEditor/Models/Tag.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Rampastring.Tools;
using System.IO;

namespace TSMapEditor.Models
{
Expand All @@ -23,5 +24,17 @@ public void WriteToIniSection(IniSection iniSection)
}

public string GetDisplayString() => Name + " (" + ID + ")";

public void Serialize(MemoryStream memoryStream)
{
StreamHelpers.WriteUnicodeString(memoryStream, Name);
StreamHelpers.WriteInt(memoryStream, Repeating);
}

public void Deserialize(MemoryStream memoryStream)
{
Name = StreamHelpers.ReadUnicodeString(memoryStream);
Repeating = StreamHelpers.ReadInt(memoryStream);
}
}
}
97 changes: 97 additions & 0 deletions src/TSMapEditor/Models/Trigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Rampastring.Tools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using TSMapEditor.CCEngine;
using TSMapEditor.Misc;
using TSMapEditor.Models.Enums;
Expand Down Expand Up @@ -256,5 +258,100 @@ public static Trigger ParseTrigger(string id, string data)
Hard = Conversions.BooleanFromString(parts[6], true),
};
}

public void Serialize(MemoryStream memoryStream)
{
StreamHelpers.WriteUnicodeString(memoryStream, HouseType);
StreamHelpers.WriteUnicodeString(memoryStream, Name);

StreamHelpers.WriteBool(memoryStream, Disabled);
StreamHelpers.WriteBool(memoryStream, Easy);
StreamHelpers.WriteBool(memoryStream, Normal);
StreamHelpers.WriteBool(memoryStream, Hard);

StreamHelpers.WriteUnicodeString(memoryStream, EditorColor);

StreamHelpers.WriteInt(memoryStream, Conditions.Count);
foreach (var condition in Conditions)
{
condition.Serialize(memoryStream);
}

StreamHelpers.WriteInt(memoryStream, Actions.Count);
foreach (var action in Actions)
{
action.Serialize(memoryStream);
}
}

public void Deserialize(MemoryStream memoryStream)
{
HouseType = StreamHelpers.ReadUnicodeString(memoryStream);
Name = StreamHelpers.ReadUnicodeString(memoryStream);

Disabled = StreamHelpers.ReadBool(memoryStream);
Easy = StreamHelpers.ReadBool(memoryStream);
Normal = StreamHelpers.ReadBool(memoryStream);
Hard = StreamHelpers.ReadBool(memoryStream);

EditorColor = StreamHelpers.ReadUnicodeString(memoryStream);

int conditionCount = StreamHelpers.ReadInt(memoryStream);
Conditions = new List<TriggerCondition>(conditionCount);
for (int i = 0; i < conditionCount; i++)
{
var condition = new TriggerCondition();
condition.Deserialize(memoryStream);
Conditions.Add(condition);
}

int actionCount = StreamHelpers.ReadInt(memoryStream);
Actions = new List<TriggerAction>(actionCount);
for (int i = 0; i < actionCount; i++)
{
var action = new TriggerAction();
action.Deserialize(memoryStream);
Actions.Add(action);
}
}

public static void CopyToClipboard(Trigger trigger, Tag tag)
{
if (trigger == null || tag == null)
return;

byte[] bytes;
using var memoryStream = new MemoryStream();

trigger.Serialize(memoryStream);
tag.Serialize(memoryStream);

bytes = memoryStream.ToArray();
Clipboard.SetData(Constants.ClipboardTriggerFormatValue, bytes);
}

public static (Trigger, Tag) GetTriggerAndTagFromClipboard(Map map)
{
if (!HasTriggerInClipboard())
return (null, null);

var bytes = (byte[])Clipboard.GetData(Constants.ClipboardTriggerFormatValue);
using var memoryStream = new MemoryStream(bytes);

var trigger = new Trigger(map.GetNewUniqueInternalId());
trigger.Deserialize(memoryStream);

var tag = new Tag();
tag.ID = map.GetNewUniqueInternalId();
tag.Deserialize(memoryStream);
tag.Trigger = trigger;

return (trigger, tag);
}

public static bool HasTriggerInClipboard()
{
return Clipboard.ContainsData(Constants.ClipboardTriggerFormatValue);
}
}
}
21 changes: 21 additions & 0 deletions src/TSMapEditor/Models/TriggerAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.IO;

namespace TSMapEditor.Models
{
Expand Down Expand Up @@ -52,5 +53,25 @@ public static TriggerAction ParseFromArray(string[] array, int startIndex)

return triggerAction;
}

public void Serialize(MemoryStream memoryStream)
{
StreamHelpers.WriteInt(memoryStream, ActionIndex);

foreach (var parameter in Parameters)
{
StreamHelpers.WriteUnicodeString(memoryStream, parameter);
}
}

public void Deserialize(MemoryStream memoryStream)
{
ActionIndex = StreamHelpers.ReadInt(memoryStream);

for (int i = 0; i < Parameters.Length; i++)
{
Parameters[i] = StreamHelpers.ReadUnicodeString(memoryStream);
}
}
}
}
21 changes: 21 additions & 0 deletions src/TSMapEditor/Models/TriggerCondition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Rampastring.Tools;
using System;
using System.IO;
using TSMapEditor.CCEngine;

namespace TSMapEditor.Models
Expand Down Expand Up @@ -88,5 +89,25 @@ public static TriggerCondition ParseFromArray(string[] array, int startIndex, in

return triggerCondition;
}

public void Serialize(MemoryStream memoryStream)
{
StreamHelpers.WriteInt(memoryStream, ConditionIndex);

foreach (var parameter in Parameters)
{
StreamHelpers.WriteUnicodeString(memoryStream, parameter);
}
}

public void Deserialize(MemoryStream memoryStream)
{
ConditionIndex = StreamHelpers.ReadInt(memoryStream);

for (int i = 0; i < Parameters.Length; i++)
{
Parameters[i] = StreamHelpers.ReadUnicodeString(memoryStream);
}
}
}
}
86 changes: 22 additions & 64 deletions src/TSMapEditor/Mutations/Classes/PasteTerrainMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using TSMapEditor.GameMath;
using TSMapEditor.Models;
using TSMapEditor.UI;
Expand Down Expand Up @@ -40,8 +39,6 @@ public abstract class CopiedMapEntry
public Point2D Offset { get; protected set; }
public abstract CopiedEntryType EntryType { get; }

private byte[] buffer;

protected CopiedMapEntry()
{
}
Expand All @@ -51,55 +48,16 @@ protected CopiedMapEntry(Point2D offset)
Offset = offset;
}

protected int ReadInt(Stream stream)
{
if (stream.Read(buffer, 0, 4) != 4)
throw new CopiedMapDataSerializationException("Failed to read integer from stream: end of stream");

return BitConverter.ToInt32(buffer, 0);
}

protected long ReadLong(Stream stream)
{
if (stream.Read(buffer, 0, 8) != 8)
throw new CopiedMapDataSerializationException("Failed to read integer from stream: end of stream");

return BitConverter.ToInt64(buffer, 0);
}

protected string ReadASCIIString(Stream stream)
{
int length = ReadInt(stream);
byte[] stringBuffer = new byte[length];
if (stream.Read(stringBuffer, 0, length) != length)
throw new CopiedMapDataSerializationException("Failed to read string from stream: end of stream");

string result = Encoding.ASCII.GetString(stringBuffer);
return result;
}

protected byte[] ASCIIStringToBytes(string str)
{
byte[] buffer = new byte[sizeof(int) + str.Length];
Array.Copy(BitConverter.GetBytes(str.Length), buffer, sizeof(int));
byte[] stringBytes = Encoding.ASCII.GetBytes(str);
Array.Copy(stringBytes, 0, buffer, sizeof(int), stringBytes.Length);
return buffer;
}

/// <summary>
/// Reads all of the map entry's data from a stream.
/// </summary>
/// <param name="stream">The stream to read the data from.</param>
public void ReadData(Stream stream)
{
buffer = new byte[8];

int x = ReadInt(stream);
int y = ReadInt(stream);
int x = StreamHelpers.ReadInt(stream);
int y = StreamHelpers.ReadInt(stream);
Offset = new Point2D(x, y);
ReadCustomData(stream);
buffer = null; // Free memory
}

/// <summary>
Expand Down Expand Up @@ -159,7 +117,7 @@ protected override byte[] GetCustomData()

protected override void ReadCustomData(Stream stream)
{
TileIndex = ReadInt(stream);
TileIndex = StreamHelpers.ReadInt(stream);
SubTileIndex = (byte)stream.ReadByte();
HeightOffset = (byte)stream.ReadByte();
}
Expand All @@ -184,7 +142,7 @@ public CopiedOverlayEntry(Point2D offset, string overlayTypeName, int frameIndex

protected override byte[] GetCustomData()
{
byte[] nameBytes = ASCIIStringToBytes(OverlayTypeName);
byte[] nameBytes = StreamHelpers.ASCIIStringToBytes(OverlayTypeName);
byte[] buffer = new byte[ nameBytes.Length + sizeof(int)];
Array.Copy(nameBytes, buffer, nameBytes.Length);
Array.Copy(BitConverter.GetBytes(FrameIndex), 0, buffer, nameBytes.Length, sizeof(int));
Expand All @@ -193,8 +151,8 @@ protected override byte[] GetCustomData()

protected override void ReadCustomData(Stream stream)
{
OverlayTypeName = ReadASCIIString(stream);
FrameIndex = ReadInt(stream);
OverlayTypeName = StreamHelpers.ReadASCIIString(stream);
FrameIndex = StreamHelpers.ReadInt(stream);
}
}

Expand All @@ -215,13 +173,13 @@ public CopiedSmudgeEntry(Point2D offset, string smudgeTypeName) : base(offset)

protected override byte[] GetCustomData()
{
byte[] nameBytes = ASCIIStringToBytes(SmudgeTypeName);
byte[] nameBytes = StreamHelpers.ASCIIStringToBytes(SmudgeTypeName);
return nameBytes;
}

protected override void ReadCustomData(Stream stream)
{
SmudgeTypeName = ReadASCIIString(stream);
SmudgeTypeName = StreamHelpers.ReadASCIIString(stream);
}
}

Expand All @@ -240,12 +198,12 @@ public CopiedObjectEntry(Point2D offset, string objectTypeName) : base(offset)

protected override byte[] GetCustomData()
{
return ASCIIStringToBytes(ObjectTypeName);
return StreamHelpers.ASCIIStringToBytes(ObjectTypeName);
}

protected override void ReadCustomData(Stream stream)
{
ObjectTypeName = ReadASCIIString(stream);
ObjectTypeName = StreamHelpers.ReadASCIIString(stream);
}
}

Expand Down Expand Up @@ -285,9 +243,9 @@ public CopiedTechnoEntry(Point2D offset, string objectTypeName, string ownerName

protected override byte[] GetCustomData()
{
byte[] objectTypeBuffer = ASCIIStringToBytes(ObjectTypeName);
byte[] ownerBuffer = ASCIIStringToBytes(OwnerHouseName);
byte[] missionBuffer = ASCIIStringToBytes(Mission);
byte[] objectTypeBuffer = StreamHelpers.ASCIIStringToBytes(ObjectTypeName);
byte[] ownerBuffer = StreamHelpers.ASCIIStringToBytes(OwnerHouseName);
byte[] missionBuffer = StreamHelpers.ASCIIStringToBytes(Mission);
byte[] result = new byte[sizeof(int) + sizeof(int) + 1 + objectTypeBuffer.Length + ownerBuffer.Length + missionBuffer.Length];
Array.Copy(BitConverter.GetBytes(HP), 0, result, 0, sizeof(int));
Array.Copy(BitConverter.GetBytes(Veterancy), 0, result, sizeof(int), sizeof(int));
Expand All @@ -300,12 +258,12 @@ protected override byte[] GetCustomData()

protected override void ReadCustomData(Stream stream)
{
HP = ReadInt(stream);
Veterancy = ReadInt(stream);
HP = StreamHelpers.ReadInt(stream);
Veterancy = StreamHelpers.ReadInt(stream);
Facing = (byte)stream.ReadByte();
ObjectTypeName = ReadASCIIString(stream);
OwnerHouseName = ReadASCIIString(stream);
Mission = ReadASCIIString(stream);
ObjectTypeName = StreamHelpers.ReadASCIIString(stream);
OwnerHouseName = StreamHelpers.ReadASCIIString(stream);
Mission = StreamHelpers.ReadASCIIString(stream);
}
}

Expand Down Expand Up @@ -362,7 +320,7 @@ protected override byte[] GetCustomData()
protected override void ReadCustomData(Stream stream)
{
base.ReadCustomData(stream);
SubCell = (SubCell)ReadInt(stream);
SubCell = (SubCell)StreamHelpers.ReadInt(stream);
}
}

Expand All @@ -379,11 +337,11 @@ public byte[] Serialize()

using (var memoryStream = new MemoryStream())
{
memoryStream.Write(BitConverter.GetBytes(Width));
memoryStream.Write(BitConverter.GetBytes(Height));
StreamHelpers.WriteUShort(memoryStream, Width);
StreamHelpers.WriteUShort(memoryStream, Height);

// Write entry count
memoryStream.Write(BitConverter.GetBytes(CopiedMapEntries.Count));
StreamHelpers.WriteInt(memoryStream, CopiedMapEntries.Count);

// Write entries
foreach (var entry in CopiedMapEntries)
Expand Down
Loading
Loading