/
rbxclip.go
47 lines (43 loc) · 1.14 KB
/
rbxclip.go
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
// Package rbxclip reads and writes Roblox instances to and from the
// clipboard. Data written to the clipboard is compatible with Roblox Studio.
package rbxclip
import (
"bytes"
"github.com/robloxapi/rbxfile"
"github.com/robloxapi/rbxfile/rbxl"
)
// Get reads instances from the clipboard, decoding them to a rbxfile.Root. A
// nil value is returned if the clipboard contains no instances, or is unable
// to read them.
func Get() *rbxfile.Root {
b, err := get()
if err != nil {
return nil
}
buf := bytes.NewBuffer(b)
root, err := rbxl.DeserializeModel(buf)
if err != nil {
return nil
}
return root
}
// Has returns whether or not there are instances in the clipboard. Returns
// false if the call fails.
func Has() bool {
b, _ := has()
return b
}
// Set writes instances to the clipboard, encoding from a rbxfile.Root.
// Passing nil will simply clear the clipboard. Returns whether or not the
// call was successful.
func Set(root *rbxfile.Root) bool {
if root == nil {
return clear() == nil
}
var buf bytes.Buffer
if err := rbxl.SerializeModel(&buf, root); err != nil {
return false
}
clear()
return set(buf.Bytes()) == nil
}