Skip to content

Commit a80d259

Browse files
Move configuration to $HOME/.config/gospeccy, reduce the number of snapshot paths, add 'setDownloadPath(string)' script function
1 parent 1ceb603 commit a80d259

File tree

5 files changed

+75
-108
lines changed

5 files changed

+75
-108
lines changed

ChangeLog

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
option, wos* script functions). Info about a WorldOfSpectrum file
88
includes publication type, machine type, and rating.
99

10-
* New script functions: vars(), wosFind(string), wosDowload(WOS),
10+
* New script functions: vars(), wosFind(string), wosDownload(WOS),
1111
wosLoad(WOS)
1212

1313
* Evaluating an expression in the console prints the result of the

README.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,23 @@ Configuration approach. To enjoy it, you should create the following
135135
folder structure:
136136

137137
<pre>
138-
mkdir -p $HOME/.gospeccy/sna # Snapshots folder
139-
mkdir -p $HOME/.gospeccy/roms # System roms folder
140-
mkdir -p $HOME/.gospeccy/scripts # Scripts folder
138+
mkdir -p $HOME/.config/gospeccy/roms # System roms folder
139+
mkdir -p $HOME/.config/gospeccy/scripts # Scripts folder
141140
</pre>
142141

143-
Then put your snapshots, system rom or scripts in the
144-
corresponding folder. After this, to load
145-
`$HOME/.gospeccy/sna/somegame.z80` simply execute:
142+
In the scripts folder, create file `config_local.go` with the following
143+
contents:
146144

147145
<pre>
148-
gospeccy somegame.z80
146+
// Search path for snapshots, scripts, etc
147+
addSearchPath("/home/user/gospeccy")
149148
</pre>
150149

151-
The same applies to `load()` and `script()` functions in the
152-
interactive console.
150+
After this, to load `/home/user/gospeccy/somegame.z80` simply execute:
151+
152+
<pre>
153+
gospeccy somegame.z80
154+
</pre>
153155

154156
# Screenshots
155157

src/interpreter/functions.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ func wrapper_addSearchPath(t *eval.Thread, in []eval.Value, out []eval.Value) {
131131
spectrum.AddCustomSearchPath(path)
132132
}
133133

134+
// Signature: func setDownloadPath(path string)
135+
func wrapper_setDownloadPath(t *eval.Thread, in []eval.Value, out []eval.Value) {
136+
path := in[0].(eval.StringValue).Get(t)
137+
spectrum.SetDownloadPath(path)
138+
}
139+
134140
func load(path string) {
135141
var program interface{}
136142
program, err := formats.ReadProgram(path)
@@ -457,7 +463,14 @@ func defineFunctions(w *eval.World) {
457463
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_addSearchPath, functionSignature)
458464
defineFunction("addSearchPath", funcType, funcValue)
459465
help_keys = append(help_keys, "addSearchPath(path string)")
460-
help_vals = append(help_vals, "Append to the paths searched when loading snapshots")
466+
help_vals = append(help_vals, "Append to the paths searched when loading snapshots, scripts, etc")
467+
}
468+
{
469+
var functionSignature func(string)
470+
funcType, funcValue := eval.FuncFromNativeTyped(wrapper_setDownloadPath, functionSignature)
471+
defineFunction("setDownloadPath", funcType, funcValue)
472+
help_keys = append(help_keys, "setDownloadPath(path string)")
473+
help_vals = append(help_vals, `Set path where to download files (""=default path)`)
461474
}
462475
{
463476
var functionSignature func() string

src/spectrum/helpers.go

+48-96
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,48 @@ import (
4444
"sync"
4545
)
4646

47-
var DefaultUserDir = path.Join(os.Getenv("HOME"), ".gospeccy")
48-
var distDir string
47+
var DefaultUserDir = path.Join(os.Getenv("HOME"), ".config", "gospeccy")
48+
var srcDir string
4949

5050
func init() {
5151
gopaths := os.Getenv("GOPATH")
5252
gopath0 := strings.Split(gopaths, string(os.PathListSeparator))[0]
53-
distDir = path.Join(gopath0, "src", "github.com", "remogatto", "gospeccy")
53+
srcDir = path.Join(gopath0, "src", "github.com", "remogatto", "gospeccy")
5454
}
5555

5656
var customSearchPaths []string
57-
var customSearchPaths_mutex sync.RWMutex
57+
var downloadPath string
58+
var mutex sync.RWMutex
5859

5960
func AddCustomSearchPath(path string) {
60-
customSearchPaths_mutex.Lock()
61-
{
62-
customSearchPaths = append(customSearchPaths, path)
61+
mutex.Lock()
62+
customSearchPaths = append(customSearchPaths, path)
63+
mutex.Unlock()
64+
}
65+
66+
func DownloadPath() string {
67+
mutex.RLock()
68+
p := downloadPath
69+
mutex.RUnlock()
70+
71+
if p == "" {
72+
p = path.Join(DefaultUserDir, "snapshots")
6373
}
64-
customSearchPaths_mutex.Unlock()
74+
return p
75+
}
76+
77+
func SetDownloadPath(path string) {
78+
mutex.Lock()
79+
downloadPath = path
80+
mutex.Unlock()
6581
}
6682

6783
func searchForValidPath(paths []string, fileName string) (string, error) {
6884
for _, dir := range paths {
6985
if _, err := os.Lstat(dir); err == nil {
7086
_, err = filepath.EvalSymlinks(dir)
7187
if err != nil {
72-
return "", errors.New("path \"" + dir + "\" contains one or more invalid symbolic links")
88+
return "", errors.New("path \"" + dir + "\" contains an invalid symbolic link")
7389
}
7490
}
7591

@@ -83,115 +99,49 @@ func searchForValidPath(paths []string, fileName string) (string, error) {
8399
}
84100

85101
func appendCustomSearchPaths(paths *[]string) {
86-
customSearchPaths_mutex.RLock()
87-
{
88-
*paths = append(*paths, customSearchPaths...)
89-
}
90-
customSearchPaths_mutex.RUnlock()
91-
}
92-
93-
// Return a valid path for the specified snapshot,
94-
// or the original filename if the search did not find anything.
95-
//
96-
// An error is returned if the search could not proceed.
97-
//
98-
// The search is performed in this order:
99-
// 1. ./
100-
// 2. $HOME/.gospeccy/sna/
101-
func SnaPath(fileName string) (string, error) {
102-
var (
103-
currDir = ""
104-
userDir = path.Join(DefaultUserDir, "sna")
105-
)
106-
107-
var paths []string
108-
paths = append(paths, currDir, userDir)
109-
appendCustomSearchPaths(&paths)
110-
111-
return searchForValidPath(paths, fileName)
102+
mutex.RLock()
103+
*paths = append(*paths, customSearchPaths...)
104+
mutex.RUnlock()
112105
}
113106

114-
// Return a valid path for the specified tape file,
115-
// or the original filename if the search did not find anything.
116-
//
117-
// An error is returned if the search could not proceed.
118-
//
119-
// The search is performed in this order:
120-
// 1. ./
121-
// 2. $HOME/.gospeccy/tape/
122-
func TapePath(fileName string) (string, error) {
123-
var (
124-
currDir = ""
125-
userDir = path.Join(DefaultUserDir, "tape")
126-
)
127-
128-
var paths []string
129-
paths = append(paths, currDir, userDir)
130-
appendCustomSearchPaths(&paths)
131-
132-
return searchForValidPath(paths, fileName)
133-
}
134-
135-
// Return a valid path for the specified zip file,
107+
// Return a valid path for the file based on its extension,
136108
// or the original filename if the search did not find anything.
137109
//
138110
// An error is returned if the search could not proceed.
139111
//
140112
// The search is performed in this order:
141113
// 1. ./
142-
// 2. $HOME/.gospeccy/zip/
143-
func ZipPath(fileName string) (string, error) {
144-
var (
145-
currDir = ""
146-
userDir = path.Join(DefaultUserDir, "zip")
147-
)
148-
114+
// 2. $GOPATH/src/github.com/remogatto/gospeccy/snapshots/
115+
// 3. Custom search paths
116+
// 4. Download path
117+
func ProgramPath(fileName string) (string, error) {
149118
var paths []string
150-
paths = append(paths, currDir, userDir)
119+
paths = append(paths, "")
120+
paths = append(paths, path.Join(srcDir, "snapshots"))
151121
appendCustomSearchPaths(&paths)
152-
122+
paths = append(paths, DownloadPath())
153123
return searchForValidPath(paths, fileName)
154124
}
155125

156-
// Return a valid path for the file based on its extension,
157-
// or the original filename if the search did not find anything.
158-
//
159-
// An error is returned if the search could not proceed.
160-
func ProgramPath(fileName string) (string, error) {
161-
ext := strings.ToLower(path.Ext(fileName))
162-
163-
switch ext {
164-
case ".sna", ".z80":
165-
return SnaPath(fileName)
166-
167-
case ".tap":
168-
return TapePath(fileName)
169-
170-
case ".zip":
171-
return ZipPath(fileName)
172-
}
173-
174-
return fileName, nil
175-
}
176-
177126
// Returns a valid path for the 48k system ROM,
178127
// or the original filename if the search did not find anything.
179128
//
180129
// An error is returned if the search could not proceed.
181130
//
182131
// The search is performed in this order:
183132
// 1. ./roms/
184-
// 2. $HOME/.gospeccy/roms/
133+
// 2. $HOME/.config/gospeccy/roms/
185134
// 3. $GOPATH/src/github.com/remogatto/gospeccy/roms/
135+
// 4. Custom search paths
186136
func SystemRomPath(fileName string) (string, error) {
187137
var (
188138
currDir = "roms"
189139
userDir = path.Join(DefaultUserDir, "roms")
190-
distDir = path.Join(distDir, "roms")
140+
srcDir = path.Join(srcDir, "roms")
191141
)
192142

193143
var paths []string
194-
paths = append(paths, currDir, userDir, distDir)
144+
paths = append(paths, currDir, userDir, srcDir)
195145
appendCustomSearchPaths(&paths)
196146

197147
return searchForValidPath(paths, fileName)
@@ -204,17 +154,18 @@ func SystemRomPath(fileName string) (string, error) {
204154
//
205155
// The search is performed in this order:
206156
// 1. ./scripts/
207-
// 2. $HOME/.gospeccy/scripts/
157+
// 2. $HOME/.config/gospeccy/scripts/
208158
// 3. $GOPATH/src/github.com/remogatto/gospeccy/scripts/
159+
// 4. Custom search paths
209160
func ScriptPath(fileName string) (string, error) {
210161
var (
211162
currDir = "scripts"
212163
userDir = path.Join(DefaultUserDir, "scripts")
213-
distDir = path.Join(distDir, "scripts")
164+
srcDir = path.Join(srcDir, "scripts")
214165
)
215166

216167
var paths []string
217-
paths = append(paths, currDir, userDir, distDir)
168+
paths = append(paths, currDir, userDir, srcDir)
218169
appendCustomSearchPaths(&paths)
219170

220171
return searchForValidPath(paths, fileName)
@@ -227,17 +178,18 @@ func ScriptPath(fileName string) (string, error) {
227178
//
228179
// The search is performed in this order:
229180
// 1. ./fonts/
230-
// 2. $HOME/.gospeccy/fonts/
181+
// 2. $HOME/.config/gospeccy/fonts/
231182
// 3. $GOPATH/src/github.com/remogatto/gospeccy/fonts/
183+
// 4. Custom search paths
232184
func FontPath(fileName string) (string, error) {
233185
var (
234186
currDir = "fonts"
235187
userDir = path.Join(DefaultUserDir, "fonts")
236-
distDir = path.Join(distDir, "fonts")
188+
srcDir = path.Join(srcDir, "fonts")
237189
)
238190

239191
var paths []string
240-
paths = append(paths, currDir, userDir, distDir)
192+
paths = append(paths, currDir, userDir, srcDir)
241193
appendCustomSearchPaths(&paths)
242194

243195
return searchForValidPath(paths, fileName)

src/spectrum/wos.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func WosQuery(app *Application, query string) ([]WosRecord, error) {
181181
// An URL can be obtained by calling function WosQuery.
182182
func WosGet(app *Application, stdout io.Writer, url string) (string, error) {
183183
filename := path.Base(url)
184-
dir := path.Join(DefaultUserDir, "zip")
184+
dir := DownloadPath()
185185
filePath := path.Join(dir, filename)
186186
ftpURL := url[6:len(url)]
187187

0 commit comments

Comments
 (0)