-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathDesktopClipboard.cs
101 lines (94 loc) · 2.81 KB
/
DesktopClipboard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Decompiled with JetBrains decompiler
// Type: StardewValley.DesktopClipboard
// Assembly: Stardew Valley, Version=1.5.6.22018, Culture=neutral, PublicKeyToken=null
// MVID: BEBB6D18-4941-4529-AC12-B54F0C61CC20
// Assembly location: C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Stardew Valley.dll
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using TextCopy;
namespace StardewValley
{
public class DesktopClipboard
{
public static bool _availabilityChecked;
public static bool _isAvailable;
public static bool IsAvailable
{
get
{
if (!DesktopClipboard._availabilityChecked)
{
DesktopClipboard._availabilityChecked = true;
string output = "";
DesktopClipboard._isAvailable = DesktopClipboard.GetText(ref output);
}
return DesktopClipboard._isAvailable;
}
}
public static bool GetText(ref string output)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
output = "";
output = ClipboardService.GetText();
return true;
}
return DesktopClipboard.externalGetText("xclip", "-o", ref output) || DesktopClipboard.externalGetText("pbpaste", "", ref output);
}
public static bool SetText(string text)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
ClipboardService.SetText(text);
return true;
}
return DesktopClipboard.externalSetText("xclip", "-selection clipboard", text) || DesktopClipboard.externalSetText("pbcopy", "", text);
}
private static bool externalSetText(string executable, string arguments, string text)
{
ProcessStartInfo startInfo = new ProcessStartInfo(executable, arguments)
{
RedirectStandardInput = true,
UseShellExecute = false
};
try
{
using (Process process = Process.Start(startInfo))
{
process.StandardInput.Write(text);
process.StandardInput.Close();
process.WaitForExit();
return process.ExitCode == 0;
}
}
catch (Exception ex)
{
return false;
}
}
private static bool externalGetText(string executable, string arguments, ref string output)
{
ProcessStartInfo startInfo = new ProcessStartInfo(executable, arguments)
{
RedirectStandardOutput = true,
UseShellExecute = false
};
try
{
using (Process process = Process.Start(startInfo))
{
string end = process.StandardOutput.ReadToEnd();
process.StandardOutput.Close();
process.WaitForExit();
output = process.ExitCode != 0 ? "" : end;
return true;
}
}
catch (Exception ex)
{
return false;
}
}
}
}