Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Allow passing path to Steam files to Crankshaft, and expose it to plu…
Browse files Browse the repository at this point in the history
…gins
  • Loading branch information
coolavery committed May 13, 2022
1 parent e69fde9 commit e899574
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 24 deletions.
8 changes: 5 additions & 3 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,33 @@ func BundleSharedScripts() (string, error) {
var evalScriptTemplate string

// BuildEvalScriptFromFile gets a script from a file and builds an eval script with it.
func BuildEvalScriptFromFile(serverPort string, uiMode cdp.UIMode, scriptPath string) (string, error) {
func BuildEvalScriptFromFile(serverPort string, uiMode cdp.UIMode, scriptPath string, steamPath string) (string, error) {
scriptBytes, err := ioutil.ReadFile(scriptPath)
if err != nil {
return "", fmt.Errorf(`Failed to read injected script at "%s": %w`, scriptPath, err)
}

script := string(scriptBytes)

return BuildEvalScript(serverPort, uiMode, script)
return BuildEvalScript(serverPort, uiMode, script, steamPath)
}

// BuildEvalScript builds a script to be evaluated in the Steam target context.
func BuildEvalScript(serverPort string, uiMode cdp.UIMode, script string) (string, error) {
func BuildEvalScript(serverPort string, uiMode cdp.UIMode, script string, steamPath string) (string, error) {
evalTmpl := template.Must(template.New("eval").Parse(evalScriptTemplate))
var evalScript bytes.Buffer
if err := evalTmpl.Execute(&evalScript, struct {
Version string
InjectedScript string
ServerPort string
UIMode cdp.UIMode
SteamDir string
}{
Version: VERSION,
InjectedScript: script,
ServerPort: serverPort,
UIMode: uiMode,
SteamDir: steamPath,
}); err != nil {
return "", fmt.Errorf("Failed to execute eval script template: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions build/eval.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ document.querySelectorAll('[data-smm-id]').forEach((node) => node.remove());

window.smmServerPort = '{{ .ServerPort }}';
window.smmUIMode = '{{ .UIMode }}';
window.csSteamDir = '{{ .SteamDir }}';

{{ .InjectedScript }}
6 changes: 3 additions & 3 deletions cmd/crankshaft/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
}

func run() error {
debugPort, serverPort, skipPatching, dataDir, pluginsDir, logsDir := config.ParseFlags()
debugPort, serverPort, skipPatching, dataDir, pluginsDir, logsDir, steamPath := config.ParseFlags()

// Ensure data directory exists
if err := os.MkdirAll(dataDir, 0700); err != nil {
Expand Down Expand Up @@ -72,7 +72,7 @@ func run() error {
waitAndPatch := func() {
cdp.WaitForConnection(debugPort)
cdp.WaitForLibraryEl(debugPort)
patcher.Patch(debugPort, serverPort)
patcher.Patch(debugPort, serverPort, steamPath)
}

if len(os.Getenv("DISPLAY")) != 0 {
Expand Down Expand Up @@ -119,7 +119,7 @@ func run() error {
ws.ServeWs(hub, w, r)
})

rpcServer := rpc.HandleRpc(debugPort, serverPort, plugins, devmode.DevMode, hub)
rpcServer := rpc.HandleRpc(debugPort, serverPort, plugins, devmode.DevMode, hub, steamPath)

http.Handle("/rpc", handlers.CORS(
handlers.AllowedHeaders([]string{"Content-Type"}),
Expand Down
3 changes: 2 additions & 1 deletion config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func GetXdgStateHome() string {
return xdg.StateHome
}

func ParseFlags() (debugPort string, serverPort string, skipPatching bool, dataDir string, pluginsDir string, logsDir string) {
func ParseFlags() (debugPort string, serverPort string, skipPatching bool, dataDir string, pluginsDir string, logsDir string, steamPath string) {
dataHome := GetXdgDataHome()
stateHome := GetXdgStateHome()

Expand All @@ -25,6 +25,7 @@ func ParseFlags() (debugPort string, serverPort string, skipPatching bool, dataD
dataDir = *flag.String("data-dir", path.Join(dataHome, "crankshaft"), "Crankshaft data directory")
pluginsDir = *flag.String("plugins-dir", path.Join(dataHome, "crankshaft", "plugins"), "Directory to load plugins from")
logsDir = *flag.String("logs-dir", path.Join(stateHome, "crankshaft", "logs"), "Directory to write logs to")
steamPath = *flag.String("steam-path", path.Join(dataHome, "Steam"), "Path to Steam files")
flag.Parse()
return
}
11 changes: 2 additions & 9 deletions patcher/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ package patcher

import (
"path"

"git.sr.ht/~avery/crankshaft/pathutil"
"github.com/adrg/xdg"
)

func getSteamUiPath() string {
return pathutil.SubstituteHomeDir(path.Join(xdg.DataHome, "Steam", "steamui"))
}

// Patch patches necessary Steam resources.
func Patch(debugPort string, serverPort string) {
PatchJS(getSteamUiPath(), debugPort, serverPort)
func Patch(debugPort string, serverPort string, steamPath string) {
PatchJS(path.Join(steamPath, "steamui"), debugPort, serverPort)
}
13 changes: 7 additions & 6 deletions rpc/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ type InjectService struct {
serverPort string
plugins *plugins.Plugins
devMode bool
steamPath string
}

func NewInjectService(debugPort, serverPort string, plugins *plugins.Plugins, devMode bool) *InjectService {
return &InjectService{debugPort, serverPort, plugins, devMode}
func NewInjectService(debugPort, serverPort string, plugins *plugins.Plugins, devMode bool, steamPath string) *InjectService {
return &InjectService{debugPort, serverPort, plugins, devMode, steamPath}
}

type InjectArgs struct{}
Expand Down Expand Up @@ -82,9 +83,9 @@ func (service *InjectService) Inject(r *http.Request, req *InjectArgs, res *Inje

var libraryEvalScript string
if service.devMode {
libraryEvalScript, err = build.BuildEvalScriptFromFile(service.serverPort, steamClient.UiMode, ".build/library.js")
libraryEvalScript, err = build.BuildEvalScriptFromFile(service.serverPort, steamClient.UiMode, ".build/library.js", service.steamPath)
} else {
libraryEvalScript, err = build.BuildEvalScript(service.serverPort, steamClient.UiMode, libraryScript)
libraryEvalScript, err = build.BuildEvalScript(service.serverPort, steamClient.UiMode, libraryScript, service.steamPath)
}
if err != nil {
log.Println(err)
Expand All @@ -101,9 +102,9 @@ func (service *InjectService) Inject(r *http.Request, req *InjectArgs, res *Inje
if steamClient.UiMode == cdp.UIModeDeck {
var menuEvalScript string
if service.devMode {
menuEvalScript, err = build.BuildEvalScriptFromFile(service.serverPort, steamClient.UiMode, ".build/menu.js")
menuEvalScript, err = build.BuildEvalScriptFromFile(service.serverPort, steamClient.UiMode, ".build/menu.js", service.steamPath)
} else {
menuEvalScript, err = build.BuildEvalScript(service.serverPort, steamClient.UiMode, menuScript)
menuEvalScript, err = build.BuildEvalScript(service.serverPort, steamClient.UiMode, menuScript, service.steamPath)
}
if err != nil {
log.Println(err)
Expand Down
4 changes: 2 additions & 2 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
rpcJson "github.com/gorilla/rpc/v2/json"
)

func HandleRpc(debugPort, serverPort string, plugins *plugins.Plugins, devMode bool, hub *ws.Hub) *rpc.Server {
func HandleRpc(debugPort, serverPort string, plugins *plugins.Plugins, devMode bool, hub *ws.Hub, steamPath string) *rpc.Server {
server := rpc.NewServer()
server.RegisterCodec(rpcJson.NewCodec(), "application/json")
server.RegisterService(network.NewNetworkService(), "NetworkService")
server.RegisterService(new(FSService), "")
server.RegisterService(inject.NewInjectService(debugPort, serverPort, plugins, devMode), "InjectService")
server.RegisterService(inject.NewInjectService(debugPort, serverPort, plugins, devMode, steamPath), "InjectService")
server.RegisterService(NewPluginsService(plugins), "PluginsService")
server.RegisterService(NewIPCService(hub), "IPCService")
return server
Expand Down

0 comments on commit e899574

Please sign in to comment.