Skip to content

Commit eb624ce

Browse files
authored
Add options for copying and pasting triggers, trigger events and trigger actions
1 parent 5d50fba commit eb624ce

File tree

9 files changed

+561
-72
lines changed

9 files changed

+561
-72
lines changed

src/TSMapEditor/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public static class Constants
110110
public static float DepthRenderStep = 0;
111111

112112
public const string ClipboardMapDataFormatValue = "ScenarioEditorCopiedMapData";
113+
public const string ClipboardTriggerActionEventFormatValue = "ScenarioEditorCopiedTriggerData";
114+
public const string ClipboardTriggerFormatValue = "ScenarioEditorCopiedTrigger";
113115
public const string UserDataFolder = "UserData";
114116

115117
public const char NewTheaterGenericLetter = 'G';

src/TSMapEditor/Models/Tag.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Rampastring.Tools;
2+
using System.IO;
23

34
namespace TSMapEditor.Models
45
{
@@ -23,5 +24,17 @@ public void WriteToIniSection(IniSection iniSection)
2324
}
2425

2526
public string GetDisplayString() => Name + " (" + ID + ")";
27+
28+
public void Serialize(MemoryStream memoryStream)
29+
{
30+
StreamHelpers.WriteUnicodeString(memoryStream, Name);
31+
StreamHelpers.WriteInt(memoryStream, Repeating);
32+
}
33+
34+
public void Deserialize(MemoryStream memoryStream)
35+
{
36+
Name = StreamHelpers.ReadUnicodeString(memoryStream);
37+
Repeating = StreamHelpers.ReadInt(memoryStream);
38+
}
2639
}
2740
}

src/TSMapEditor/Models/Trigger.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Rampastring.Tools;
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Windows.Forms;
57
using TSMapEditor.CCEngine;
68
using TSMapEditor.Misc;
79
using TSMapEditor.Models.Enums;
@@ -256,5 +258,100 @@ public static Trigger ParseTrigger(string id, string data)
256258
Hard = Conversions.BooleanFromString(parts[6], true),
257259
};
258260
}
261+
262+
public void Serialize(MemoryStream memoryStream)
263+
{
264+
StreamHelpers.WriteUnicodeString(memoryStream, HouseType);
265+
StreamHelpers.WriteUnicodeString(memoryStream, Name);
266+
267+
StreamHelpers.WriteBool(memoryStream, Disabled);
268+
StreamHelpers.WriteBool(memoryStream, Easy);
269+
StreamHelpers.WriteBool(memoryStream, Normal);
270+
StreamHelpers.WriteBool(memoryStream, Hard);
271+
272+
StreamHelpers.WriteUnicodeString(memoryStream, EditorColor);
273+
274+
StreamHelpers.WriteInt(memoryStream, Conditions.Count);
275+
foreach (var condition in Conditions)
276+
{
277+
condition.Serialize(memoryStream);
278+
}
279+
280+
StreamHelpers.WriteInt(memoryStream, Actions.Count);
281+
foreach (var action in Actions)
282+
{
283+
action.Serialize(memoryStream);
284+
}
285+
}
286+
287+
public void Deserialize(MemoryStream memoryStream)
288+
{
289+
HouseType = StreamHelpers.ReadUnicodeString(memoryStream);
290+
Name = StreamHelpers.ReadUnicodeString(memoryStream);
291+
292+
Disabled = StreamHelpers.ReadBool(memoryStream);
293+
Easy = StreamHelpers.ReadBool(memoryStream);
294+
Normal = StreamHelpers.ReadBool(memoryStream);
295+
Hard = StreamHelpers.ReadBool(memoryStream);
296+
297+
EditorColor = StreamHelpers.ReadUnicodeString(memoryStream);
298+
299+
int conditionCount = StreamHelpers.ReadInt(memoryStream);
300+
Conditions = new List<TriggerCondition>(conditionCount);
301+
for (int i = 0; i < conditionCount; i++)
302+
{
303+
var condition = new TriggerCondition();
304+
condition.Deserialize(memoryStream);
305+
Conditions.Add(condition);
306+
}
307+
308+
int actionCount = StreamHelpers.ReadInt(memoryStream);
309+
Actions = new List<TriggerAction>(actionCount);
310+
for (int i = 0; i < actionCount; i++)
311+
{
312+
var action = new TriggerAction();
313+
action.Deserialize(memoryStream);
314+
Actions.Add(action);
315+
}
316+
}
317+
318+
public static void CopyToClipboard(Trigger trigger, Tag tag)
319+
{
320+
if (trigger == null || tag == null)
321+
return;
322+
323+
byte[] bytes;
324+
using var memoryStream = new MemoryStream();
325+
326+
trigger.Serialize(memoryStream);
327+
tag.Serialize(memoryStream);
328+
329+
bytes = memoryStream.ToArray();
330+
Clipboard.SetData(Constants.ClipboardTriggerFormatValue, bytes);
331+
}
332+
333+
public static (Trigger, Tag) GetTriggerAndTagFromClipboard(Map map)
334+
{
335+
if (!HasTriggerInClipboard())
336+
return (null, null);
337+
338+
var bytes = (byte[])Clipboard.GetData(Constants.ClipboardTriggerFormatValue);
339+
using var memoryStream = new MemoryStream(bytes);
340+
341+
var trigger = new Trigger(map.GetNewUniqueInternalId());
342+
trigger.Deserialize(memoryStream);
343+
344+
var tag = new Tag();
345+
tag.ID = map.GetNewUniqueInternalId();
346+
tag.Deserialize(memoryStream);
347+
tag.Trigger = trigger;
348+
349+
return (trigger, tag);
350+
}
351+
352+
public static bool HasTriggerInClipboard()
353+
{
354+
return Clipboard.ContainsData(Constants.ClipboardTriggerFormatValue);
355+
}
259356
}
260357
}

src/TSMapEditor/Models/TriggerAction.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Globalization;
3+
using System.IO;
34

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

5354
return triggerAction;
5455
}
56+
57+
public void Serialize(MemoryStream memoryStream)
58+
{
59+
StreamHelpers.WriteInt(memoryStream, ActionIndex);
60+
61+
foreach (var parameter in Parameters)
62+
{
63+
StreamHelpers.WriteUnicodeString(memoryStream, parameter);
64+
}
65+
}
66+
67+
public void Deserialize(MemoryStream memoryStream)
68+
{
69+
ActionIndex = StreamHelpers.ReadInt(memoryStream);
70+
71+
for (int i = 0; i < Parameters.Length; i++)
72+
{
73+
Parameters[i] = StreamHelpers.ReadUnicodeString(memoryStream);
74+
}
75+
}
5576
}
5677
}

src/TSMapEditor/Models/TriggerCondition.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Rampastring.Tools;
22
using System;
3+
using System.IO;
34
using TSMapEditor.CCEngine;
45

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

8990
return triggerCondition;
9091
}
92+
93+
public void Serialize(MemoryStream memoryStream)
94+
{
95+
StreamHelpers.WriteInt(memoryStream, ConditionIndex);
96+
97+
foreach (var parameter in Parameters)
98+
{
99+
StreamHelpers.WriteUnicodeString(memoryStream, parameter);
100+
}
101+
}
102+
103+
public void Deserialize(MemoryStream memoryStream)
104+
{
105+
ConditionIndex = StreamHelpers.ReadInt(memoryStream);
106+
107+
for (int i = 0; i < Parameters.Length; i++)
108+
{
109+
Parameters[i] = StreamHelpers.ReadUnicodeString(memoryStream);
110+
}
111+
}
91112
}
92113
}

src/TSMapEditor/Mutations/Classes/PasteTerrainMutation.cs

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections.Generic;
44
using System.IO;
5-
using System.Text;
65
using TSMapEditor.GameMath;
76
using TSMapEditor.Models;
87
using TSMapEditor.UI;
@@ -40,8 +39,6 @@ public abstract class CopiedMapEntry
4039
public Point2D Offset { get; protected set; }
4140
public abstract CopiedEntryType EntryType { get; }
4241

43-
private byte[] buffer;
44-
4542
protected CopiedMapEntry()
4643
{
4744
}
@@ -51,55 +48,16 @@ protected CopiedMapEntry(Point2D offset)
5148
Offset = offset;
5249
}
5350

54-
protected int ReadInt(Stream stream)
55-
{
56-
if (stream.Read(buffer, 0, 4) != 4)
57-
throw new CopiedMapDataSerializationException("Failed to read integer from stream: end of stream");
58-
59-
return BitConverter.ToInt32(buffer, 0);
60-
}
61-
62-
protected long ReadLong(Stream stream)
63-
{
64-
if (stream.Read(buffer, 0, 8) != 8)
65-
throw new CopiedMapDataSerializationException("Failed to read integer from stream: end of stream");
66-
67-
return BitConverter.ToInt64(buffer, 0);
68-
}
69-
70-
protected string ReadASCIIString(Stream stream)
71-
{
72-
int length = ReadInt(stream);
73-
byte[] stringBuffer = new byte[length];
74-
if (stream.Read(stringBuffer, 0, length) != length)
75-
throw new CopiedMapDataSerializationException("Failed to read string from stream: end of stream");
76-
77-
string result = Encoding.ASCII.GetString(stringBuffer);
78-
return result;
79-
}
80-
81-
protected byte[] ASCIIStringToBytes(string str)
82-
{
83-
byte[] buffer = new byte[sizeof(int) + str.Length];
84-
Array.Copy(BitConverter.GetBytes(str.Length), buffer, sizeof(int));
85-
byte[] stringBytes = Encoding.ASCII.GetBytes(str);
86-
Array.Copy(stringBytes, 0, buffer, sizeof(int), stringBytes.Length);
87-
return buffer;
88-
}
89-
9051
/// <summary>
9152
/// Reads all of the map entry's data from a stream.
9253
/// </summary>
9354
/// <param name="stream">The stream to read the data from.</param>
9455
public void ReadData(Stream stream)
9556
{
96-
buffer = new byte[8];
97-
98-
int x = ReadInt(stream);
99-
int y = ReadInt(stream);
57+
int x = StreamHelpers.ReadInt(stream);
58+
int y = StreamHelpers.ReadInt(stream);
10059
Offset = new Point2D(x, y);
10160
ReadCustomData(stream);
102-
buffer = null; // Free memory
10361
}
10462

10563
/// <summary>
@@ -159,7 +117,7 @@ protected override byte[] GetCustomData()
159117

160118
protected override void ReadCustomData(Stream stream)
161119
{
162-
TileIndex = ReadInt(stream);
120+
TileIndex = StreamHelpers.ReadInt(stream);
163121
SubTileIndex = (byte)stream.ReadByte();
164122
HeightOffset = (byte)stream.ReadByte();
165123
}
@@ -184,7 +142,7 @@ public CopiedOverlayEntry(Point2D offset, string overlayTypeName, int frameIndex
184142

185143
protected override byte[] GetCustomData()
186144
{
187-
byte[] nameBytes = ASCIIStringToBytes(OverlayTypeName);
145+
byte[] nameBytes = StreamHelpers.ASCIIStringToBytes(OverlayTypeName);
188146
byte[] buffer = new byte[ nameBytes.Length + sizeof(int)];
189147
Array.Copy(nameBytes, buffer, nameBytes.Length);
190148
Array.Copy(BitConverter.GetBytes(FrameIndex), 0, buffer, nameBytes.Length, sizeof(int));
@@ -193,8 +151,8 @@ protected override byte[] GetCustomData()
193151

194152
protected override void ReadCustomData(Stream stream)
195153
{
196-
OverlayTypeName = ReadASCIIString(stream);
197-
FrameIndex = ReadInt(stream);
154+
OverlayTypeName = StreamHelpers.ReadASCIIString(stream);
155+
FrameIndex = StreamHelpers.ReadInt(stream);
198156
}
199157
}
200158

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

216174
protected override byte[] GetCustomData()
217175
{
218-
byte[] nameBytes = ASCIIStringToBytes(SmudgeTypeName);
176+
byte[] nameBytes = StreamHelpers.ASCIIStringToBytes(SmudgeTypeName);
219177
return nameBytes;
220178
}
221179

222180
protected override void ReadCustomData(Stream stream)
223181
{
224-
SmudgeTypeName = ReadASCIIString(stream);
182+
SmudgeTypeName = StreamHelpers.ReadASCIIString(stream);
225183
}
226184
}
227185

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

241199
protected override byte[] GetCustomData()
242200
{
243-
return ASCIIStringToBytes(ObjectTypeName);
201+
return StreamHelpers.ASCIIStringToBytes(ObjectTypeName);
244202
}
245203

246204
protected override void ReadCustomData(Stream stream)
247205
{
248-
ObjectTypeName = ReadASCIIString(stream);
206+
ObjectTypeName = StreamHelpers.ReadASCIIString(stream);
249207
}
250208
}
251209

@@ -285,9 +243,9 @@ public CopiedTechnoEntry(Point2D offset, string objectTypeName, string ownerName
285243

286244
protected override byte[] GetCustomData()
287245
{
288-
byte[] objectTypeBuffer = ASCIIStringToBytes(ObjectTypeName);
289-
byte[] ownerBuffer = ASCIIStringToBytes(OwnerHouseName);
290-
byte[] missionBuffer = ASCIIStringToBytes(Mission);
246+
byte[] objectTypeBuffer = StreamHelpers.ASCIIStringToBytes(ObjectTypeName);
247+
byte[] ownerBuffer = StreamHelpers.ASCIIStringToBytes(OwnerHouseName);
248+
byte[] missionBuffer = StreamHelpers.ASCIIStringToBytes(Mission);
291249
byte[] result = new byte[sizeof(int) + sizeof(int) + 1 + objectTypeBuffer.Length + ownerBuffer.Length + missionBuffer.Length];
292250
Array.Copy(BitConverter.GetBytes(HP), 0, result, 0, sizeof(int));
293251
Array.Copy(BitConverter.GetBytes(Veterancy), 0, result, sizeof(int), sizeof(int));
@@ -300,12 +258,12 @@ protected override byte[] GetCustomData()
300258

301259
protected override void ReadCustomData(Stream stream)
302260
{
303-
HP = ReadInt(stream);
304-
Veterancy = ReadInt(stream);
261+
HP = StreamHelpers.ReadInt(stream);
262+
Veterancy = StreamHelpers.ReadInt(stream);
305263
Facing = (byte)stream.ReadByte();
306-
ObjectTypeName = ReadASCIIString(stream);
307-
OwnerHouseName = ReadASCIIString(stream);
308-
Mission = ReadASCIIString(stream);
264+
ObjectTypeName = StreamHelpers.ReadASCIIString(stream);
265+
OwnerHouseName = StreamHelpers.ReadASCIIString(stream);
266+
Mission = StreamHelpers.ReadASCIIString(stream);
309267
}
310268
}
311269

@@ -362,7 +320,7 @@ protected override byte[] GetCustomData()
362320
protected override void ReadCustomData(Stream stream)
363321
{
364322
base.ReadCustomData(stream);
365-
SubCell = (SubCell)ReadInt(stream);
323+
SubCell = (SubCell)StreamHelpers.ReadInt(stream);
366324
}
367325
}
368326

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

380338
using (var memoryStream = new MemoryStream())
381339
{
382-
memoryStream.Write(BitConverter.GetBytes(Width));
383-
memoryStream.Write(BitConverter.GetBytes(Height));
340+
StreamHelpers.WriteUShort(memoryStream, Width);
341+
StreamHelpers.WriteUShort(memoryStream, Height);
384342

385343
// Write entry count
386-
memoryStream.Write(BitConverter.GetBytes(CopiedMapEntries.Count));
344+
StreamHelpers.WriteInt(memoryStream, CopiedMapEntries.Count);
387345

388346
// Write entries
389347
foreach (var entry in CopiedMapEntries)

0 commit comments

Comments
 (0)