-
Notifications
You must be signed in to change notification settings - Fork 6
/
game.go
161 lines (130 loc) · 3.46 KB
/
game.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package manager
import (
"../utils"
"github.com/pyk/byten"
"html"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
type RepositoryGameList struct {
// XMLName xml.Name `xml:"game_list"`
GameList []RepositoryGame `xml:"game"`
}
type RepositoryGame struct {
// XMLName xml.Name `xml:"game"`
Name string `xml:"name"`
Title string `xml:"title"`
Version string `xml:"version"`
Url string `xml:"url"`
Size int `xml:"size"`
Lang string `xml:"lang"`
Descurl string `xml:"descurl"`
Author string `xml:"author"`
Description string `xml:"description"`
Image string `xml:"image"`
Langs []string `xml:"langs>lang"`
Date string `xml:"date"`
Timestamp int64 `xml:"-"`
InstalledVersion string `xml:"-"`
RepositoryName string `xml:"-"`
Installed bool `xml:"-"`
OnlyInstalled bool `xml:"-"`
//IsUpdateExist bool `xml:"-"`
Languages []string `xml:"-"`
Id string `xml:"-"`
}
type Game RepositoryGame
func generateGameId(repository string, g *Game) string {
return repository + "/" + g.Name + "/" + strings.Join(g.Languages, "_")
}
func (g *Game) addGameAdditionalData(repositoryName string) {
date, e := time.Parse("2006-01-02", g.Date)
if e == nil {
g.Timestamp = date.Unix()
}
if len(g.Langs) > 0 {
g.Languages = g.Langs
} else {
g.Languages = strings.Split(g.Lang, ",")
}
g.RepositoryName = repositoryName
g.Title = html.UnescapeString(g.Title)
if g.Description != "" {
g.Description = html.UnescapeString(g.Description)
}
g.Id = generateGameId(repositoryName, g)
}
func (g *Game) HumanSize() string {
if g.Size > 0 {
return byten.Size(int64(g.Size))
}
return ""
}
func (g *Game) HumanVersion() string {
if g.IsUpdateAvailable() {
return g.InstalledVersion + " (" + g.Version + ")"
}
return g.Version
}
func (g *Game) IsUpdateAvailable() bool {
return g.InstalledVersion != "" && g.InstalledVersion != g.Version
}
func ReadLocalGameInfo(path string, info os.FileInfo) Game {
var e error
// Eval possible symlink
gameName := info.Name()
evalPath, e := filepath.EvalSymlinks(filepath.Join(path, info.Name()))
if e == nil {
newInfo, e := os.Stat(evalPath)
if e == nil {
info = newInfo
}
}
if !info.IsDir() { // IDF
gameName = strings.TrimSuffix(info.Name(), filepath.Ext(info.Name()))
}
game := Game{
Name: gameName,
Title: gameName,
Installed: true,
}
if info.IsDir() {
game, _ = appendGameInfo(game, path, info)
}
game.addGameAdditionalData("")
return game
}
func appendGameInfo(game Game, path string, info os.FileInfo) (newGame Game, e error) {
// TODO: idf
newGame = game
mainLuaFilePath := filepath.Join(path, info.Name(), "main.lua") // STEAD2 main file path
if !utils.PathExist(mainLuaFilePath) {
mainLuaFilePath = filepath.Join(path, info.Name(), "main3.lua") // STEAD3 main file path
}
file, e := ioutil.ReadFile(mainLuaFilePath)
if e != nil {
return
}
// Title
r, e := regexp.Compile("(?i)--\\s*\\$Name:\\s*(.*)\\$")
if e == nil {
matches := r.FindStringSubmatch(string(file))
if len(matches) > 1 {
newGame.Title = matches[1]
}
}
// Version
r, e = regexp.Compile("(?i)--\\s*\\$Version:\\s*(.*)\\$")
if e == nil {
matches := r.FindStringSubmatch(string(file))
if len(matches) > 1 {
newGame.InstalledVersion = matches[1]
newGame.Version = matches[1]
}
}
return
}