Skip to content

Commit 10dcfc4

Browse files
committed
Initial version
0 parents  commit 10dcfc4

20 files changed

+861
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.exe
3+
xplane-gateway-downloader

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# X-Plane Gateway Downloader
2+
3+
With this console application you can download airport updates from the X-Plane Gateway directly into your CustomScenery folder.
4+
5+
## Commands
6+
```
7+
$ xplane-gateway-downloader --help
8+
NAME:
9+
X-Plane Gateway Downloader - Download airports from X-Plane Gateway with ease
10+
11+
USAGE:
12+
xplane-gateway-downloader [global options] command [command options] [arguments...]
13+
14+
DESCRIPTION:
15+
Update airport sceneries from the X-Plane Gateway
16+
17+
COMMANDS:
18+
install Install a new airport scenery pack
19+
update Update all installed airport scenery packs
20+
uninstall Uninstall an installed airport scenery pack
21+
config Configure the application
22+
help, h Shows a list of commands or help for one command
23+
24+
GLOBAL OPTIONS:
25+
--help, -h show help (default: false)
26+
```
27+
28+
### Configuration
29+
30+
Before you can manage new airports you have to set up the application.
31+
32+
```
33+
$ xplane-gateway-downloader config --help
34+
NAME:
35+
xplane-gateway-downloader config - Configure the application
36+
37+
USAGE:
38+
xplane-gateway-downloader config [command options] [arguments...]
39+
40+
OPTIONS:
41+
--custom-scenery-folder path, --csf path The path to CustomScenery folder of x-plane
42+
--x-plane-version version, -v version Set the current version of x-plane
43+
```
44+
45+
Run the following commands and replace the placeholder.
46+
47+
- Set the path to the CustomScenery folder in the x-plane game folder. The path must contain the ending slashes!
48+
- **Windows:** (use double backslash due to character escaping)
49+
```
50+
xplane-gateway-downloader config --custom-scenery-folder C:\\path\\to\\X-Plane 11\\Custom Scenery\\
51+
```
52+
- **Linux:**
53+
```
54+
xplane-gateway-downloader config --custom-scenery-folder /path/to/Custom Scenery/
55+
```
56+
57+
- Set your X-Plane game version. This will allow the application to not download airports if there is no newer version available and therefore save disk space.
58+
Most of you will be at the newest version which currently is 11.55. If not, change the version in the command below.
59+
```
60+
xplane-gateway-downloader config --x-plane-version 11.55
61+
```
62+
63+
### Install an airport
64+
65+
```
66+
$ xplane-gateway-downloader install --help
67+
NAME:
68+
xplane-gateway-downloader install - Install a new airport scenery pack
69+
70+
USAGE:
71+
xplane-gateway-downloader install [command options] [arguments...]
72+
73+
OPTIONS:
74+
--icao ICAO, -i ICAO Install an airport by ICAO code
75+
```
76+
77+
Example: ``xplane-gateway-downloader install --icao EDDF``
78+
79+
### Update all installed airports
80+
81+
```
82+
$ xplane-gateway-downloader update --help
83+
NAME:
84+
xplane-gateway-downloader update - Update all installed airport scenery packs
85+
86+
USAGE:
87+
xplane-gateway-downloader update [command options] [arguments...]
88+
89+
OPTIONS:
90+
--help, -h show help (default: false)
91+
```
92+
93+
Example: ``xplane-gateway-downloader update``
94+
95+
### Uninstall an installed airport
96+
97+
```
98+
$ xplane-gateway-downloader uninstall --help
99+
NAME:
100+
xplane-gateway-downloader uninstall - Uninstall an installed airport scenery pack
101+
102+
USAGE:
103+
xplane-gateway-downloader uninstall [command options] [arguments...]
104+
105+
OPTIONS:
106+
--icao ICAO, -i ICAO Uninstall an airport by ICAO code
107+
```
108+
109+
Example: ``xplane-gateway-downloader uninstall --icao EDDF``

app/config.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
"github.com/urfave/cli/v2"
6+
"github.com/xEtarusx/xplane-gateway-downloader/config"
7+
"github.com/xEtarusx/xplane-gateway-downloader/downloader"
8+
"os"
9+
)
10+
11+
func ActionConfig(c *cli.Context) error {
12+
customSceneryFolder := c.String("custom-scenery-folder")
13+
xPlaneVersion := c.String("x-plane-version")
14+
15+
if customSceneryFolder != "" {
16+
handleCustomSceneryFolder(customSceneryFolder)
17+
}
18+
19+
if xPlaneVersion != "" {
20+
handleXPlaneVersion(xPlaneVersion)
21+
}
22+
23+
return nil
24+
}
25+
26+
func handleCustomSceneryFolder(path string) {
27+
if _, err := os.Stat(path); os.IsNotExist(err) {
28+
// path/to/custom-scenery-folder does not exist
29+
fmt.Println("The path does not exist. Please make sure that it's pointing to your CustomScenery folder in the X-Plane game folder and ends with a slash")
30+
return
31+
}
32+
33+
// Persist the CustomScenery folder path in config.json
34+
config.GlobalConfig.CustomSceneryFolder = path
35+
}
36+
37+
func handleXPlaneVersion(version string) {
38+
releases, err := downloader.GetReleaseData()
39+
if err != nil {
40+
fmt.Println("Could not download release list from gateway")
41+
fmt.Println(err)
42+
return
43+
}
44+
45+
for _, release := range releases {
46+
if release.Version != version {
47+
continue
48+
}
49+
50+
// Persist the X-Plane version in config.json
51+
config.GlobalConfig.XPlaneVersion = version
52+
fmt.Printf("Version X-Plane %s successfully saved\n", version)
53+
54+
// Download list of all sceneries released with version
55+
fmt.Print("Downloading list of released sceneries ... ")
56+
sceneriesList, err := downloader.GetReleaseSceneryData(version)
57+
if err != nil {
58+
fmt.Println("failed")
59+
fmt.Printf("Could not download list of released sceneries from gateway for version %s\n", version)
60+
fmt.Println(err)
61+
return
62+
}
63+
64+
config.GlobalConfig.ReleasedSceneryPacksWithVersion[version] = sceneriesList
65+
fmt.Println("done")
66+
67+
return
68+
}
69+
70+
fmt.Printf("The version %s is invalid\n\n", version)
71+
fmt.Println("Valid versions:")
72+
73+
for _, release := range releases {
74+
fmt.Printf("%s (released %s)\n", release.Version, release.Date.Format("2006-01-02"))
75+
}
76+
}

app/helper.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package app
2+
3+
import (
4+
"archive/zip"
5+
"bytes"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
func GetSceneryPackFolderName(icao string) string {
12+
return icao + "_Scenery_Pack"
13+
}
14+
15+
// WriteZipToFolder Write bytes from a zip archive into destination path
16+
func WriteZipToFolder(zipContent []byte, destinationPath string) error {
17+
// create a zip reader
18+
zipReader, err := zip.NewReader(bytes.NewReader(zipContent), int64(len(zipContent)))
19+
if err != nil {
20+
return err
21+
}
22+
23+
// loop through all the files of the zip archive
24+
for _, zipFile := range zipReader.File {
25+
fpath := filepath.Join(destinationPath, zipFile.Name)
26+
27+
if zipFile.FileInfo().IsDir() {
28+
// Make Folder
29+
_ = os.MkdirAll(fpath, os.ModePerm)
30+
continue
31+
}
32+
33+
// Make File
34+
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
35+
return err
36+
}
37+
38+
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, zipFile.Mode())
39+
if err != nil {
40+
return err
41+
}
42+
43+
rc, err := zipFile.Open()
44+
if err != nil {
45+
return err
46+
}
47+
48+
_, err = io.Copy(outFile, rc)
49+
50+
// Close the file without defer to close before next iteration of loop
51+
_ = outFile.Close()
52+
_ = rc.Close()
53+
}
54+
55+
return nil
56+
}

app/install.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
"github.com/urfave/cli/v2"
6+
"github.com/xEtarusx/xplane-gateway-downloader/config"
7+
"github.com/xEtarusx/xplane-gateway-downloader/downloader"
8+
"os"
9+
"path"
10+
"strings"
11+
)
12+
13+
func ActionInstall(c *cli.Context) error {
14+
icao := strings.ToUpper(c.String("icao"))
15+
16+
if icao == "" {
17+
fmt.Println("--icao parameter cannot be empty")
18+
return nil
19+
}
20+
21+
// Check if airport is already installed locally
22+
if config.GlobalConfig.IsAirportInstalled(icao) {
23+
fmt.Printf("Airport %s already installed", icao)
24+
return nil
25+
}
26+
27+
// Check if the folder {ICAO}_Scenery_Pack already exist in CustomScenery folder
28+
if _, err := os.Stat(path.Join(config.GlobalConfig.CustomSceneryFolder, GetSceneryPackFolderName(icao))); !os.IsNotExist(err) {
29+
fmt.Printf("The folder %s already exists in your CustomScenery folder but not in the local config. Aborting\n", GetSceneryPackFolderName(icao))
30+
return nil
31+
}
32+
33+
airport, err := downloader.GetAirportData(icao)
34+
if err != nil {
35+
return err
36+
}
37+
38+
// Check if the user has set their x-plane version
39+
if config.GlobalConfig.IsXPlaneVersionSet() {
40+
// Check if the airport scenery is already included in the x-plane release
41+
if config.GlobalConfig.IsSceneryPackIncluded(airport.RecommendedSceneryId) {
42+
fmt.Printf("There is no newer version of this airport available. X-Plane %v contains the latest update.\n", config.GlobalConfig.XPlaneVersion)
43+
return nil
44+
}
45+
}
46+
47+
scenery, err := downloader.GetSceneryData(airport.RecommendedSceneryId)
48+
if err != nil {
49+
return err
50+
}
51+
52+
// extract the {ICAO}_Scenery_Pack.zip form the downloaded zip archive
53+
sceneryPackBytes, err := scenery.ExtractSceneryPackZip(icao)
54+
if err != nil {
55+
return err
56+
}
57+
58+
// delete old scenery folder
59+
err = os.RemoveAll(path.Join(config.GlobalConfig.CustomSceneryFolder, GetSceneryPackFolderName(icao)))
60+
if err != nil {
61+
return err
62+
}
63+
64+
// create new scenery folder
65+
err = WriteZipToFolder(sceneryPackBytes, config.GlobalConfig.CustomSceneryFolder)
66+
if err != nil {
67+
return err
68+
}
69+
70+
fmt.Printf("%s was successfully installed\n", airport.ICAO)
71+
72+
// Store the scenery approved date for information purpose in the airport
73+
airport.SceneryApprovedDate = scenery.DateApproved
74+
75+
// Store airport in config
76+
config.GlobalConfig.SaveAirport(airport)
77+
78+
return nil
79+
}

app/uninstall.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
"github.com/urfave/cli/v2"
6+
"github.com/xEtarusx/xplane-gateway-downloader/config"
7+
"os"
8+
"path"
9+
"strings"
10+
)
11+
12+
func ActionUninstall(c *cli.Context) error {
13+
icao := strings.ToUpper(c.String("icao"))
14+
15+
if icao == "" {
16+
fmt.Println("--icao parameter cannot be empty")
17+
return nil
18+
}
19+
20+
// Check if airport is not installed locally
21+
if !config.GlobalConfig.IsAirportInstalled(icao) {
22+
fmt.Printf("Airport %s is not installed", icao)
23+
return nil
24+
}
25+
26+
// delete scenery folder
27+
err := os.RemoveAll(path.Join(config.GlobalConfig.CustomSceneryFolder, GetSceneryPackFolderName(icao)))
28+
if err != nil {
29+
return err
30+
}
31+
32+
// delete airport from local config
33+
config.GlobalConfig.AirportConfig = config.GlobalConfig.RemoveAirport(icao)
34+
35+
return nil
36+
}

0 commit comments

Comments
 (0)