@@ -44,32 +44,48 @@ import (
44
44
"sync"
45
45
)
46
46
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
49
49
50
50
func init () {
51
51
gopaths := os .Getenv ("GOPATH" )
52
52
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" )
54
54
}
55
55
56
56
var customSearchPaths []string
57
- var customSearchPaths_mutex sync.RWMutex
57
+ var downloadPath string
58
+ var mutex sync.RWMutex
58
59
59
60
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" )
63
73
}
64
- customSearchPaths_mutex .Unlock ()
74
+ return p
75
+ }
76
+
77
+ func SetDownloadPath (path string ) {
78
+ mutex .Lock ()
79
+ downloadPath = path
80
+ mutex .Unlock ()
65
81
}
66
82
67
83
func searchForValidPath (paths []string , fileName string ) (string , error ) {
68
84
for _ , dir := range paths {
69
85
if _ , err := os .Lstat (dir ); err == nil {
70
86
_ , err = filepath .EvalSymlinks (dir )
71
87
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 " )
73
89
}
74
90
}
75
91
@@ -83,115 +99,49 @@ func searchForValidPath(paths []string, fileName string) (string, error) {
83
99
}
84
100
85
101
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 ()
112
105
}
113
106
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,
136
108
// or the original filename if the search did not find anything.
137
109
//
138
110
// An error is returned if the search could not proceed.
139
111
//
140
112
// The search is performed in this order:
141
113
// 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 ) {
149
118
var paths []string
150
- paths = append (paths , currDir , userDir )
119
+ paths = append (paths , "" )
120
+ paths = append (paths , path .Join (srcDir , "snapshots" ))
151
121
appendCustomSearchPaths (& paths )
152
-
122
+ paths = append ( paths , DownloadPath ())
153
123
return searchForValidPath (paths , fileName )
154
124
}
155
125
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
-
177
126
// Returns a valid path for the 48k system ROM,
178
127
// or the original filename if the search did not find anything.
179
128
//
180
129
// An error is returned if the search could not proceed.
181
130
//
182
131
// The search is performed in this order:
183
132
// 1. ./roms/
184
- // 2. $HOME/.gospeccy/roms/
133
+ // 2. $HOME/.config/ gospeccy/roms/
185
134
// 3. $GOPATH/src/github.com/remogatto/gospeccy/roms/
135
+ // 4. Custom search paths
186
136
func SystemRomPath (fileName string ) (string , error ) {
187
137
var (
188
138
currDir = "roms"
189
139
userDir = path .Join (DefaultUserDir , "roms" )
190
- distDir = path .Join (distDir , "roms" )
140
+ srcDir = path .Join (srcDir , "roms" )
191
141
)
192
142
193
143
var paths []string
194
- paths = append (paths , currDir , userDir , distDir )
144
+ paths = append (paths , currDir , userDir , srcDir )
195
145
appendCustomSearchPaths (& paths )
196
146
197
147
return searchForValidPath (paths , fileName )
@@ -204,17 +154,18 @@ func SystemRomPath(fileName string) (string, error) {
204
154
//
205
155
// The search is performed in this order:
206
156
// 1. ./scripts/
207
- // 2. $HOME/.gospeccy/scripts/
157
+ // 2. $HOME/.config/ gospeccy/scripts/
208
158
// 3. $GOPATH/src/github.com/remogatto/gospeccy/scripts/
159
+ // 4. Custom search paths
209
160
func ScriptPath (fileName string ) (string , error ) {
210
161
var (
211
162
currDir = "scripts"
212
163
userDir = path .Join (DefaultUserDir , "scripts" )
213
- distDir = path .Join (distDir , "scripts" )
164
+ srcDir = path .Join (srcDir , "scripts" )
214
165
)
215
166
216
167
var paths []string
217
- paths = append (paths , currDir , userDir , distDir )
168
+ paths = append (paths , currDir , userDir , srcDir )
218
169
appendCustomSearchPaths (& paths )
219
170
220
171
return searchForValidPath (paths , fileName )
@@ -227,17 +178,18 @@ func ScriptPath(fileName string) (string, error) {
227
178
//
228
179
// The search is performed in this order:
229
180
// 1. ./fonts/
230
- // 2. $HOME/.gospeccy/fonts/
181
+ // 2. $HOME/.config/ gospeccy/fonts/
231
182
// 3. $GOPATH/src/github.com/remogatto/gospeccy/fonts/
183
+ // 4. Custom search paths
232
184
func FontPath (fileName string ) (string , error ) {
233
185
var (
234
186
currDir = "fonts"
235
187
userDir = path .Join (DefaultUserDir , "fonts" )
236
- distDir = path .Join (distDir , "fonts" )
188
+ srcDir = path .Join (srcDir , "fonts" )
237
189
)
238
190
239
191
var paths []string
240
- paths = append (paths , currDir , userDir , distDir )
192
+ paths = append (paths , currDir , userDir , srcDir )
241
193
appendCustomSearchPaths (& paths )
242
194
243
195
return searchForValidPath (paths , fileName )
0 commit comments