Skip to content

Commit

Permalink
优化结构
Browse files Browse the repository at this point in the history
  • Loading branch information
AnxysUaen authored and AnxysUaen committed Nov 25, 2023
1 parent 28b32f8 commit 1417e6b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
53 changes: 29 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ type NeedData struct {
cname string
}

type LibraryVdf struct {
Libraryfolders map[string]struct {
Apps map[string]string `json:"apps"`
} `json:"libraryfolders"`
}

func getDiskList() []string {
diskList := []string{}
for i := 67; i <= 90; i++ {
Expand Down Expand Up @@ -95,6 +101,9 @@ func getIconFile(appId string, icon string) []byte {
func scanSteamInstallation() {
if _, err := os.Stat(path.Join(steamInstalledFolder, "steam.exe")); err != nil {
fmt.Print("自动检测Steam安装目录:")
} else {
fmt.Println("Test传入固定路径")
return
}
diskList := getDiskList()
for _, disk := range diskList {
Expand Down Expand Up @@ -125,33 +134,29 @@ func scanSteamInstallation() {
}

func scanSteamGameId() {
libraryVdf := path.Join(steamInstalledFolder, "steamapps/libraryfolders.vdf")
if _, err := os.Stat(libraryVdf); err != nil {
fmt.Printf("ERROR! [%s]读取失败", libraryVdf)
libraryVdfPath := path.Join(steamInstalledFolder, "steamapps/libraryfolders.vdf")
if _, err := os.Stat(libraryVdfPath); err != nil {
fmt.Printf("ERROR! [%s]读取失败", libraryVdfPath)
return
}
libraryVdfFile, err := os.Open(libraryVdfPath)
if err != nil {
fmt.Printf("ERROR! [%s]读取失败", libraryVdfPath)
return
}
defer libraryVdfFile.Close()
if vdfMap, err := vdf.NewParser(libraryVdfFile).Parse(); err != nil {
fmt.Printf("ERROR! 解析配置文件错误")
} else {
if file, err := os.Open(libraryVdf); err != nil {
fmt.Printf("ERROR! [%s]读取失败", libraryVdf)
} else {
defer file.Close()
if vdfMap, err := vdf.NewParser(file).Parse(); err != nil {
fmt.Printf("ERROR! 解析配置文件错误")
return
} else {
if libMap, ok := vdfMap["libraryfolders"].(map[string]interface{}); ok {
for _, curLibrary := range libMap {
curLibIDList := []string{}
if curLibraryMap, ok := curLibrary.(map[string]interface{}); ok {
if appIdMap, ok := curLibraryMap["apps"].(map[string]interface{}); ok {
for appKey := range appIdMap {
curLibIDList = append(curLibIDList, fmt.Sprintf("%v", appKey))
}
installedGameIDList = append(installedGameIDList, curLibIDList...)
}
}
}
}
vdfJson, _ := json.Marshal(vdfMap)
var vdfData LibraryVdf
json.Unmarshal(vdfJson, &vdfData)
for _, curLibrary := range vdfData.Libraryfolders {
curLibIDList := []string{}
for appKey := range curLibrary.Apps {
curLibIDList = append(curLibIDList, fmt.Sprintf("%v", appKey))
}
installedGameIDList = append(installedGameIDList, curLibIDList...)
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"testing"
)

var ScanSteamInstallation = scanSteamInstallation
var ScanSteamGameId = scanSteamGameId

func TestScanSteamInstallation(t *testing.T) {
steamInstalledFolder = "./test_files"
ScanSteamInstallation()
}

func TestScanSteamGameId(t *testing.T) {
steamInstalledFolder = "./test_files"
ScanSteamGameId()
fmt.Println(installedGameIDList)
}

0 comments on commit 1417e6b

Please sign in to comment.