mirror of
https://github.com/ivanizag/izapple2.git
synced 2024-11-20 00:32:05 +00:00
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package apple2
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/ivanizag/apple2/romdumps"
|
|
)
|
|
|
|
const (
|
|
internalPrefix = "<internal>/"
|
|
httpPrefix = "http://"
|
|
httpsPrefix = "https://"
|
|
)
|
|
|
|
func isInternalResource(filename string) bool {
|
|
return strings.HasPrefix(filename, internalPrefix)
|
|
}
|
|
|
|
func isHTTPResource(filename string) bool {
|
|
return strings.HasPrefix(filename, httpPrefix) ||
|
|
strings.HasPrefix(filename, httpsPrefix)
|
|
}
|
|
|
|
func loadResource(filename string) ([]uint8, error) {
|
|
var file io.Reader
|
|
if isInternalResource(filename) {
|
|
// load from embedded resource
|
|
resource := strings.TrimPrefix(filename, internalPrefix)
|
|
resourceFile, err := romdumps.Assets.Open(resource)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resourceFile.Close()
|
|
file = resourceFile
|
|
|
|
} else if isHTTPResource(filename) {
|
|
response, err := http.Get(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer response.Body.Close()
|
|
file = response.Body
|
|
|
|
} else {
|
|
diskFile, err := os.Open(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer diskFile.Close()
|
|
file = diskFile
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|