Skip to content

Commit bb746c1

Browse files
committed
Post code review fixes, automatically select pasted entities
1 parent 9c5c94c commit bb746c1

File tree

3 files changed

+215
-189
lines changed

3 files changed

+215
-189
lines changed

src/TSMapEditor/StreamHelpers.cs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,35 @@ public class StreamHelperReadException : Exception
99
public StreamHelperReadException(string message) : base(message)
1010
{
1111
}
12-
}
12+
}
1313

1414
public static class StreamHelpers
1515
{
1616
public static int ReadInt(Stream stream)
1717
{
18-
byte[] buffer = new byte[8];
18+
Span<byte> buffer = stackalloc byte[4];
1919

20-
if (stream.Read(buffer, 0, 4) != 4)
20+
if (stream.Read(buffer) != sizeof(int))
2121
throw new StreamHelperReadException("Failed to read integer from stream: end of stream");
2222

23-
return BitConverter.ToInt32(buffer, 0);
23+
return BitConverter.ToInt32(buffer);
2424
}
2525

2626
public static long ReadLong(Stream stream)
2727
{
28-
byte[] buffer = new byte[8];
28+
Span<byte> buffer = stackalloc byte[8];
2929

30-
if (stream.Read(buffer, 0, 8) != 8)
30+
if (stream.Read(buffer) != sizeof(int))
3131
throw new StreamHelperReadException("Failed to read integer from stream: end of stream");
3232

33-
return BitConverter.ToInt64(buffer, 0);
33+
return BitConverter.ToInt64(buffer);
3434
}
3535

3636
public static string ReadASCIIString(Stream stream)
3737
{
3838
int length = ReadInt(stream);
39-
byte[] stringBuffer = new byte[length];
40-
if (stream.Read(stringBuffer, 0, length) != length)
39+
Span<byte> stringBuffer = stackalloc byte[length];
40+
if (stream.Read(stringBuffer) != length)
4141
throw new StreamHelperReadException("Failed to read string from stream: end of stream");
4242

4343
if (length == -1)
@@ -49,11 +49,13 @@ public static string ReadASCIIString(Stream stream)
4949

5050
public static byte[] ASCIIStringToBytes(string str)
5151
{
52-
byte[] buffer = new byte[sizeof(int) + str.Length];
53-
Array.Copy(BitConverter.GetBytes(str.Length), buffer, sizeof(int));
54-
byte[] stringBytes = Encoding.ASCII.GetBytes(str);
55-
Array.Copy(stringBytes, 0, buffer, sizeof(int), stringBytes.Length);
56-
return buffer;
52+
int length = sizeof(int) + str.Length;
53+
Span<byte> span = stackalloc byte[length];
54+
55+
BitConverter.TryWriteBytes(span, str.Length);
56+
Encoding.ASCII.GetBytes(str, span.Slice(sizeof(int)));
57+
58+
return span.ToArray();
5759
}
5860

5961
public static string ReadUnicodeString(Stream stream)
@@ -63,22 +65,23 @@ public static string ReadUnicodeString(Stream stream)
6365
if (length == -1)
6466
return null;
6567

66-
byte[] stringBuffer = new byte[length];
67-
if (stream.Read(stringBuffer, 0, length) != length)
68+
Span<byte> stringBuffer = stackalloc byte[length];
69+
if (stream.Read(stringBuffer) != length)
6870
throw new StreamHelperReadException("Failed to read Unicode string from stream: end of stream");
6971

7072
string result = Encoding.UTF8.GetString(stringBuffer);
7173
return result;
7274
}
7375

7476
public static byte[] UnicodeStringToBytes(string str)
75-
{
76-
byte[] buffer = new byte[sizeof(int) + str.Length];
77-
78-
Array.Copy(BitConverter.GetBytes(str.Length), buffer, sizeof(int));
79-
byte[] stringBytes = Encoding.UTF8.GetBytes(str);
80-
Array.Copy(stringBytes, 0, buffer, sizeof(int), stringBytes.Length);
81-
return buffer;
77+
{
78+
int length = sizeof(int) + str.Length;
79+
Span<byte> span = stackalloc byte[length];
80+
81+
BitConverter.TryWriteBytes(span, str.Length);
82+
Encoding.Unicode.GetBytes(str, span.Slice(sizeof(int)));
83+
84+
return span.ToArray();
8285
}
8386

8487
public static bool ReadBool(Stream stream)
@@ -98,15 +101,13 @@ public static void WriteUShort(Stream stream, ushort shortNum)
98101

99102
public static void WriteUnicodeString(Stream stream, string str)
100103
{
101-
byte[] bytes;
102-
103104
if (string.IsNullOrEmpty(str))
104105
{
105106
stream.Write(BitConverter.GetBytes(-1));
106107
}
107108
else
108109
{
109-
bytes = Encoding.UTF8.GetBytes(str);
110+
byte[] bytes = Encoding.UTF8.GetBytes(str);
110111
stream.Write(BitConverter.GetBytes(bytes.Length));
111112
stream.Write(bytes);
112113
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using System;
2+
using System.IO;
3+
using System.Windows.Forms;
4+
using TSMapEditor.Models;
5+
6+
namespace TSMapEditor.UI.Windows
7+
{
8+
public class CopiedTriggerData
9+
{
10+
public TriggerCondition CopiedTriggerEvent = null;
11+
public TriggerAction CopiedTriggerAction = null;
12+
13+
public void CopyToClipboard()
14+
{
15+
if (CopiedTriggerEvent == null && CopiedTriggerAction == null)
16+
return;
17+
18+
using var memoryStream = new MemoryStream();
19+
20+
if (CopiedTriggerEvent == null)
21+
{
22+
memoryStream.WriteByte(0);
23+
}
24+
else
25+
{
26+
memoryStream.WriteByte(1);
27+
CopiedTriggerEvent.Serialize(memoryStream);
28+
}
29+
30+
if (CopiedTriggerAction == null)
31+
{
32+
memoryStream.WriteByte(0);
33+
}
34+
else
35+
{
36+
memoryStream.WriteByte(1);
37+
CopiedTriggerAction.Serialize(memoryStream);
38+
}
39+
40+
byte[] bytes = memoryStream.ToArray();
41+
Clipboard.SetData(Constants.ClipboardTriggerActionEventFormatValue, bytes);
42+
}
43+
44+
public TriggerAction GetTriggerActionFromClipboard()
45+
{
46+
if (!Clipboard.ContainsData(Constants.ClipboardTriggerActionEventFormatValue))
47+
return null;
48+
49+
var bytes = (byte[])Clipboard.GetData(Constants.ClipboardTriggerActionEventFormatValue);
50+
51+
using var memoryStream = new MemoryStream(bytes);
52+
53+
SkipTriggerEventDataInStream(memoryStream);
54+
55+
int hasTriggerAction = memoryStream.ReadByte();
56+
if (hasTriggerAction <= 0)
57+
return null;
58+
59+
var triggerAction = new TriggerAction();
60+
triggerAction.Deserialize(memoryStream);
61+
62+
return triggerAction;
63+
}
64+
65+
public TriggerCondition GetTriggerEventFromClipboard()
66+
{
67+
if (!Clipboard.ContainsData(Constants.ClipboardTriggerActionEventFormatValue))
68+
return null;
69+
70+
var bytes = (byte[])Clipboard.GetData(Constants.ClipboardTriggerActionEventFormatValue);
71+
72+
using var memoryStream = new MemoryStream(bytes);
73+
int hasTriggerEvent = memoryStream.ReadByte();
74+
if (hasTriggerEvent <= 0)
75+
return null;
76+
77+
var triggerEvent = new TriggerCondition();
78+
triggerEvent.Deserialize(memoryStream);
79+
80+
return triggerEvent;
81+
}
82+
83+
private void SkipTriggerEventDataInStream(MemoryStream memoryStream)
84+
{
85+
int hasTriggerEvent = memoryStream.ReadByte();
86+
if (hasTriggerEvent <= 0)
87+
return;
88+
89+
memoryStream.Position += 4; // skip condition index
90+
91+
Span<byte> buffer = stackalloc byte[4];
92+
for (int i = 0; i < TriggerCondition.MAX_PARAM_COUNT; i++) // skip params
93+
{
94+
memoryStream.Read(buffer);
95+
int stringLength = BitConverter.ToInt32(buffer);
96+
if (stringLength <= 0)
97+
continue;
98+
99+
memoryStream.Position += stringLength;
100+
}
101+
}
102+
103+
public bool HasTriggerActionDataInClipboard()
104+
{
105+
return HasTriggerActionOrEventData(true);
106+
}
107+
108+
public bool HasTriggerEventDataInClipboard()
109+
{
110+
return HasTriggerActionOrEventData(false);
111+
}
112+
113+
private bool HasTriggerActionOrEventData(bool skipEvent)
114+
{
115+
if (!Clipboard.ContainsData(Constants.ClipboardTriggerActionEventFormatValue))
116+
return false;
117+
118+
byte[] bytes = (byte[])Clipboard.GetData(Constants.ClipboardTriggerActionEventFormatValue);
119+
120+
using var memoryStream = new MemoryStream(bytes);
121+
122+
if (skipEvent)
123+
SkipTriggerEventDataInStream(memoryStream);
124+
125+
int hasTriggerEventOrActionFlag = memoryStream.ReadByte();
126+
return hasTriggerEventOrActionFlag == 1;
127+
}
128+
129+
public void ClearValuesIfClipboardEmpty()
130+
{
131+
if (Clipboard.ContainsData(Constants.ClipboardTriggerActionEventFormatValue))
132+
return;
133+
134+
CopiedTriggerAction = null;
135+
CopiedTriggerEvent = null;
136+
}
137+
138+
public void SetCopiedTriggerEvent(TriggerCondition triggerEvent)
139+
{
140+
if (triggerEvent == null)
141+
return;
142+
143+
ClearValuesIfClipboardEmpty();
144+
CopiedTriggerEvent = triggerEvent;
145+
}
146+
147+
public void SetCopiedTriggerAction(TriggerAction triggerAction)
148+
{
149+
if (triggerAction == null)
150+
return;
151+
152+
ClearValuesIfClipboardEmpty();
153+
CopiedTriggerAction = triggerAction;
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)