-
Notifications
You must be signed in to change notification settings - Fork 1
feat: implement game details for launching declared exe and args #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3217052
feat: fetching/storing product info
Gustash 56b20e3
feat: use game details for executable and args
Gustash 98a0ce3
chore: document SaveGameDetails
Gustash 051d601
fix: bad argument
Gustash 12d02bf
fix: address PR issues
Gustash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/gustash/freecarnival/logger" | ||
| ) | ||
|
|
||
| const productInfoUrl = "https://developers.indiegala.com/get_product_info" | ||
|
|
||
| type ProductInfo struct { | ||
| Status string `json:"status"` | ||
| Message string `json:"message"` | ||
|
|
||
| GameDetails *GameDetails `json:"product_data,omitempty"` | ||
| } | ||
|
|
||
| type GameDetails struct { | ||
| ExePath string `json:"exe_path,omitempty"` | ||
| Args string `json:"args,omitempty"` | ||
| Cwd string `json:"cwd,omitempty"` | ||
| } | ||
|
|
||
| // productInfoDir returns the directory for storing game details | ||
| func productInfoDir() (string, error) { | ||
| dir, err := configDir() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| productPath := filepath.Join(dir, "product") | ||
| if err := os.MkdirAll(productPath, 0o700); err != nil { | ||
| return "", err | ||
| } | ||
| return productPath, nil | ||
| } | ||
|
|
||
| // gameDetailsPath returns the file path to the specified game's details | ||
| func gameDetailsPath(slug string) (string, error) { | ||
| dir, err := productInfoDir() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Join(dir, fmt.Sprintf("%s.json", slug)), nil | ||
| } | ||
|
|
||
| // SaveGameDetails saves the provided GameDetails for the specified game slug to a file. | ||
| func SaveGameDetails(slug string, gameDetails *GameDetails) error { | ||
| path, err := gameDetailsPath(slug) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| data, err := json.MarshalIndent(gameDetails, "", " ") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return os.WriteFile(path, data, 0o600) | ||
| } | ||
|
|
||
| // FetchGameDetails gets game details from the API (e.g. exe_name, args...). | ||
| func FetchGameDetails(ctx context.Context, client *http.Client, slug string) (*GameDetails, error) { | ||
| return getProductInfoWithUrl(ctx, client, productInfoUrl, slug) | ||
| } | ||
|
|
||
| // getProductInfoWithUrl is an internal function that calls the server | ||
| // to fetch game details | ||
| func getProductInfoWithUrl(ctx context.Context, client *http.Client, targetURL, slug string) (*GameDetails, error) { | ||
| products, err := LoadLibrary() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| product := FindProductBySlug(products, slug) | ||
| if product == nil { | ||
| return nil, fmt.Errorf("couldn't find %s in library", slug) | ||
| } | ||
|
|
||
| endpoint, err := url.Parse(targetURL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| queryParams := url.Values{} | ||
| queryParams.Add("dev_id", product.Namespace) | ||
| queryParams.Add("prod_name", slug) | ||
|
|
||
| endpoint.RawQuery = queryParams.Encode() | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| req.Header.Set("User-Agent", "galaClient") | ||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| bodyBytes, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| bodyStr := string(bodyBytes) | ||
|
|
||
| var pi ProductInfo | ||
| if err := json.Unmarshal(bodyBytes, &pi); err != nil { | ||
| // If JSON decoding fails, treat as hard error | ||
| return nil, fmt.Errorf("failed to decode product info response: %w (body: %s)", err, bodyStr) | ||
| } | ||
|
|
||
| if resp.StatusCode == http.StatusOK && pi.Status == "success" { | ||
| if pi.GameDetails == nil { | ||
| return nil, fmt.Errorf("server didn't respond with game details: (body: %s)", bodyStr) | ||
| } | ||
|
|
||
| if err := SaveGameDetails(slug, pi.GameDetails); err != nil { | ||
| logger.Warn("Failed to save product info", "slug", slug, "error", err) | ||
Gustash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return pi.GameDetails, nil | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("fetching product info failed: %s (%s)", pi.Message, pi.Status) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.