Skip to content

Commit a17ce91

Browse files
committed
Handle clipboard returning a MemoryStream object
1 parent d6f0d91 commit a17ce91

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/TSMapEditor/UI/CursorActions/PasteTerrainCursorAction.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
using Rampastring.XNAUI;
99
using Microsoft.Xna.Framework;
1010
using System.Linq;
11-
using TSMapEditor.UI.Controls;
11+
using System.IO;
12+
using TSMapEditor.UI.Windows;
1213

1314
namespace TSMapEditor.UI.CursorActions
1415
{
@@ -96,7 +97,35 @@ public override void OnActionEnter()
9697
return;
9798
}
9899

99-
byte[] data = (byte[])System.Windows.Forms.Clipboard.GetData(Constants.ClipboardMapDataFormatValue);
100+
byte[] data;
101+
102+
object clipboardContents = System.Windows.Forms.Clipboard.GetData(Constants.ClipboardMapDataFormatValue);
103+
if (clipboardContents == null)
104+
{
105+
Logger.Log($"WARNING: {nameof(PasteTerrainCursorAction)}: Clipboard data is null");
106+
ExitAction();
107+
return;
108+
}
109+
110+
if (clipboardContents is byte[] cpBytes)
111+
{
112+
data = cpBytes;
113+
}
114+
else if (clipboardContents is MemoryStream ms)
115+
{
116+
// We write clipboard contents as a byte array, but for some reason, reading from the clipboard
117+
// can sometimes give us a MemoryStream in some cases, based on error logs received from users.
118+
Logger.Log($"WARNING: {nameof(PasteTerrainCursorAction)}: Reading clipboard contents as MemoryStream");
119+
data = ms.ToArray();
120+
}
121+
else
122+
{
123+
string typeName = clipboardContents.GetType().Name;
124+
Logger.Log($"WARNING: {nameof(PasteTerrainCursorAction)}: Unknown clipboard object type {typeName}");
125+
EditorMessageBox.Show(CursorActionTarget.WindowManager, "Paste Error", $"Failed to read data from clipboard: unknown clipboard object type {typeName}", MessageBoxButtons.OK);
126+
ExitAction();
127+
return;
128+
}
100129

101130
try
102131
{

0 commit comments

Comments
 (0)