Skip to content

Commit 37d2491

Browse files
committed
refactor(studiorpc): redesign, add script status
1 parent 2e345ff commit 37d2491

File tree

3 files changed

+62
-74
lines changed

3 files changed

+62
-74
lines changed

cmd/vinegar/bootstrapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (a *app) newBootstrapper() *bootstrapper {
4141

4242
b := bootstrapper{
4343
app: a,
44-
rp: studiorpc.New(),
44+
rp: studiorpc.New(a.rbx),
4545
}
4646

4747
builder.GetObject("window").Cast(&b.win)

internal/studiorpc/discordrpc.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

internal/studiorpc/studiorpc.go

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,104 @@
22
package studiorpc
33

44
import (
5-
"fmt"
65
"log/slog"
7-
"regexp"
86
"strconv"
97
"strings"
8+
"time"
109

1110
"github.com/altfoxie/drpc"
1211
"github.com/sewnie/rbxweb"
1312
)
1413

1514
const appID = "1159891020956323923"
1615

17-
const (
18-
gameOpenEntry = "[FLog::LifecycleManager] Entered PlaceSessionScope:"
19-
gameCloseEntry = "[FLog::LifecycleManager] Exited PlaceSessionScope:"
20-
)
21-
22-
var gameOpenEntryPattern = regexp.MustCompile(`Entered PlaceSessionScope:'([0-9]+)'`)
23-
2416
type StudioRPC struct {
2517
presence drpc.Activity
2618
client *drpc.Client
2719
rbx *rbxweb.Client
2820

29-
placeID rbxweb.PlaceID
21+
place *rbxweb.PlaceDetail
3022
}
3123

32-
func New() *StudioRPC {
33-
c, _ := drpc.New("1159891020956323923")
24+
func New(c *rbxweb.Client) *StudioRPC {
3425
return &StudioRPC{
35-
client: c,
36-
rbx: rbxweb.NewClient(),
26+
client: drpc.New("1159891020956323923"),
27+
rbx: c,
28+
presence: drpc.Activity{
29+
Assets: &drpc.Assets{
30+
LargeImage: "studio",
31+
LargeText: "Vinegar",
32+
},
33+
},
3734
}
3835
}
3936

40-
// Handle implements the BinaryRichPresence interface
4137
func (s *StudioRPC) Handle(line string) error {
42-
if strings.Contains(line, gameOpenEntry) {
43-
return s.handleGameOpen(line)
44-
} else if strings.Contains(line, gameCloseEntry) {
45-
return s.handleGameClose()
38+
for _, handler := range []func(string) error{
39+
s.handleOpen,
40+
s.handleEdit,
41+
} {
42+
if err := handler(line); err != nil {
43+
return err
44+
}
4645
}
4746

4847
return nil
4948
}
5049

51-
func (s *StudioRPC) handleGameOpen(line string) error {
52-
m := gameOpenEntryPattern.FindStringSubmatch(line)
53-
if len(m) != 2 {
54-
return fmt.Errorf("log game join report entry is invalid")
50+
func (s *StudioRPC) handleOpen(line string) error {
51+
const entry = "[FLog::StudioKeyEvents] open place (identifier = "
52+
if !strings.HasPrefix(line, entry) {
53+
return nil
5554
}
5655

57-
pid, err := strconv.ParseInt(m[1], 10, 64)
56+
// open place (identifier = $id) [start]
57+
i, err := strconv.ParseInt(line[len(entry):len(line)-len(") [start]")], 10, 64)
5858
if err != nil {
5959
return err
6060
}
61+
place := rbxweb.PlaceID(i)
6162

62-
s.placeID = rbxweb.PlaceID(pid)
63+
slog.Info("studiorpc: Opened Place", "placeid", i)
6364

64-
slog.Info("Handled GameOpen", "placeid", s.placeID)
65+
s.place, err = s.rbx.GamesV1.GetPlaceDetail(place)
66+
if err != nil {
67+
slog.Error("studiorpc: Failed to fetch place detail", "err", err)
68+
}
6569

66-
return s.UpdateGamePresence()
67-
}
70+
s.presence.Timestamps = &drpc.Timestamps{
71+
Start: time.Now(),
72+
}
6873

69-
func (s *StudioRPC) handleGameClose() error {
70-
s.presence = drpc.Activity{}
71-
s.placeID = rbxweb.PlaceID(0)
74+
return nil
75+
}
7276

73-
slog.Info("Handled GameClose")
77+
func (s *StudioRPC) handleEdit(line string) error {
78+
const entry = "[FLog::RobloxDocManager] Setting current doc to type "
79+
if !strings.HasPrefix(line, entry) {
80+
return nil
81+
}
82+
slog.Info("studiorpc: Changed main view")
83+
switch line[len(entry):] {
84+
case "-1":
85+
s.place = nil
86+
s.presence.Details = "Idling"
87+
s.presence.State = ""
88+
s.presence.Assets.SmallImage = ""
89+
s.presence.Timestamps = &drpc.Timestamps{
90+
Start: time.Now(),
91+
}
92+
case "0":
93+
s.presence.Details = "Developing a place"
94+
s.presence.State = ""
95+
s.presence.Assets.SmallImage = "place"
96+
if s.place != nil {
97+
s.presence.Details = "Developing " + s.place.Name
98+
}
99+
case "2":
100+
s.presence.State = "Editing Script"
101+
s.presence.Assets.SmallImage = "script"
102+
}
74103

75104
return s.client.SetActivity(s.presence)
76105
}

0 commit comments

Comments
 (0)