-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstarsystem.go
57 lines (48 loc) · 1.31 KB
/
starsystem.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
package elite
import (
"bufio"
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
)
// StarSystemEvent is an event that contains the current star system.
// It may be a Location, FSDJump, or SupercruiseExit event.
type StarSystemEvent struct {
*JournalEntry
StarSystem string `json:"StarSystem,omitempty"`
}
// GetStarSystem returns the current star system.
func GetStarSystem() (string, error) {
return GetStarSystemFromPath(defaultLogPath)
}
// GetStarSystemFromPath returns the current star system using the specified log path.
func GetStarSystemFromPath(logPath string) (string, error) {
files, _ := ioutil.ReadDir(logPath)
found := false
var event StarSystemEvent
for i := len(files) - 1; i >= 0 && !found; i-- {
if !journalFilePattern.MatchString(files[i].Name()) {
continue
}
journalFile, err := os.Open(filepath.Join(logPath, files[i].Name()))
if err != nil {
return "", err
}
defer journalFile.Close()
scanner := bufio.NewScanner(journalFile)
for scanner.Scan() {
var tempEvent StarSystemEvent
json.Unmarshal([]byte(scanner.Text()), &tempEvent)
if tempEvent.Event == "FSDJump" || tempEvent.Event == "Location" {
event = tempEvent
found = true
}
}
}
if !found {
return "", errors.New("No location found in all log files")
}
return event.StarSystem, nil
}