renaming types

This commit is contained in:
Alex Yatskov 2019-01-08 18:45:19 -08:00
parent a02379c18b
commit 9878545952
6 changed files with 73 additions and 85 deletions

View File

@ -7,7 +7,7 @@ import (
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
) )
type DatPalette struct { type Palette struct {
Colors [256]math.Color3b Colors [256]math.Color3b
} }
@ -17,13 +17,13 @@ type color struct {
R byte R byte
} }
func NewFromReader(reader io.Reader) (*DatPalette, error) { func NewFromReader(reader io.Reader) (*Palette, error) {
var colors [256]color var colors [256]color
if err := binary.Read(reader, binary.LittleEndian, &colors); err != nil { if err := binary.Read(reader, binary.LittleEndian, &colors); err != nil {
return nil, err return nil, err
} }
palette := new(DatPalette) palette := new(Palette)
for i, color := range colors { for i, color := range colors {
palette.Colors[i] = math.Color3b{R: color.R, G: color.G, B: color.B} palette.Colors[i] = math.Color3b{R: color.R, G: color.G, B: color.B}
} }
@ -31,8 +31,8 @@ func NewFromReader(reader io.Reader) (*DatPalette, error) {
return palette, nil return palette, nil
} }
func NewFromGrayscale() *DatPalette { func NewFromGrayscale() *Palette {
palette := new(DatPalette) palette := new(Palette)
for i := 0; i < 256; i++ { for i := 0; i < 256; i++ {
value := uint8(i) value := uint8(i)
palette.Colors[i] = math.Color3b{R: value, G: value, B: value} palette.Colors[i] = math.Color3b{R: value, G: value, B: value}

View File

@ -38,12 +38,12 @@ type Frame struct {
Data []byte Data []byte
} }
type Dc6Animation struct { type Animation struct {
Directions []Direction Directions []Direction
} }
func NewFromReader(reader io.ReadSeeker) (*Dc6Animation, error) { func NewFromReader(reader io.ReadSeeker) (*Animation, error) {
sprite := new(Dc6Animation) sprite := new(Animation)
var fileHead fileHeader var fileHead fileHeader
if err := binary.Read(reader, binary.LittleEndian, &fileHead); err != nil { if err := binary.Read(reader, binary.LittleEndian, &fileHead); err != nil {

View File

@ -20,23 +20,16 @@ import (
"unsafe" "unsafe"
) )
type File interface { type Archive struct {
Read(data []byte) (int, error) handle unsafe.Pointer
Seek(offset int64, whence int) (int64, error) paths map[string]string
Close() error
} }
type MpqArchive interface { func NewFromFile(path string) (*Archive, error) {
OpenFile(path string) (File, error)
GetPaths() []string
Close() error
}
func NewFromFile(path string) (MpqArchive, error) {
cs := C.CString(path) cs := C.CString(path)
defer C.free(unsafe.Pointer(cs)) defer C.free(unsafe.Pointer(cs))
a := new(archive) a := new(Archive)
if result := C.SFileOpenArchive(cs, 0, 0, (*C.HANDLE)(&a.handle)); result == 0 { if result := C.SFileOpenArchive(cs, 0, 0, (*C.HANDLE)(&a.handle)); result == 0 {
return nil, fmt.Errorf("failed to open archive (%d)", getLastError()) return nil, fmt.Errorf("failed to open archive (%d)", getLastError())
} }
@ -49,58 +42,7 @@ func NewFromFile(path string) (MpqArchive, error) {
return a, nil return a, nil
} }
type file struct { func (a *Archive) Close() error {
handle unsafe.Pointer
}
func (f *file) Read(data []byte) (int, error) {
var bytesRead int
if result := C.SFileReadFile(C.HANDLE(f.handle), unsafe.Pointer(&data[0]), C.ulong(len(data)), (*C.ulong)(unsafe.Pointer(&bytesRead)), nil); result == 0 {
lastError := getLastError()
if lastError == C.ERROR_HANDLE_EOF {
return bytesRead, io.EOF
}
return 0, fmt.Errorf("failed to read file (%d)", lastError)
}
return bytesRead, nil
}
func (f *file) Seek(offset int64, whence int) (int64, error) {
var method uint
switch whence {
case io.SeekStart:
method = C.FILE_BEGIN
case io.SeekCurrent:
method = C.FILE_CURRENT
case io.SeekEnd:
method = C.FILE_END
}
result := C.SFileSetFilePointer(C.HANDLE(f.handle), C.long(offset), nil, C.ulong(method))
if result == C.SFILE_INVALID_SIZE {
return 0, fmt.Errorf("failed to set file pointer (%d)", getLastError())
}
return int64(result), nil
}
func (f *file) Close() error {
if result := C.SFileCloseFile(C.HANDLE(f.handle)); result == 0 {
return fmt.Errorf("failed to close file (%d)", getLastError())
}
f.handle = nil
return nil
}
type archive struct {
handle unsafe.Pointer
paths map[string]string
}
func (a *archive) Close() error {
if result := C.SFileCloseArchive(C.HANDLE(a.handle)); result == 0 { if result := C.SFileCloseArchive(C.HANDLE(a.handle)); result == 0 {
return fmt.Errorf("failed to close archive (%d)", getLastError()) return fmt.Errorf("failed to close archive (%d)", getLastError())
} }
@ -110,7 +52,7 @@ func (a *archive) Close() error {
return nil return nil
} }
func (a *archive) OpenFile(path string) (File, error) { func (a *Archive) OpenFile(path string) (*File, error) {
if pathInt, ok := a.paths[path]; ok { if pathInt, ok := a.paths[path]; ok {
path = pathInt path = pathInt
} }
@ -118,7 +60,7 @@ func (a *archive) OpenFile(path string) (File, error) {
cs := C.CString(path) cs := C.CString(path)
defer C.free(unsafe.Pointer(cs)) defer C.free(unsafe.Pointer(cs))
file := new(file) file := new(File)
if result := C.SFileOpenFileEx(C.HANDLE(a.handle), cs, 0, (*C.HANDLE)(&file.handle)); result == 0 { if result := C.SFileOpenFileEx(C.HANDLE(a.handle), cs, 0, (*C.HANDLE)(&file.handle)); result == 0 {
return nil, fmt.Errorf("failed to open file (%d)", getLastError()) return nil, fmt.Errorf("failed to open file (%d)", getLastError())
} }
@ -126,7 +68,7 @@ func (a *archive) OpenFile(path string) (File, error) {
return file, nil return file, nil
} }
func (a *archive) GetPaths() []string { func (a *Archive) GetPaths() []string {
var extPaths []string var extPaths []string
for extPath := range a.paths { for extPath := range a.paths {
extPaths = append(extPaths, extPath) extPaths = append(extPaths, extPath)
@ -135,7 +77,7 @@ func (a *archive) GetPaths() []string {
return extPaths return extPaths
} }
func (a *archive) buildPathMap() error { func (a *Archive) buildPathMap() error {
f, err := a.OpenFile("(listfile)") f, err := a.OpenFile("(listfile)")
if err != nil { if err != nil {
return err return err
@ -161,6 +103,52 @@ func (a *archive) buildPathMap() error {
return nil return nil
} }
type File struct {
handle unsafe.Pointer
}
func (f *File) Read(data []byte) (int, error) {
var bytesRead int
if result := C.SFileReadFile(C.HANDLE(f.handle), unsafe.Pointer(&data[0]), C.ulong(len(data)), (*C.ulong)(unsafe.Pointer(&bytesRead)), nil); result == 0 {
lastError := getLastError()
if lastError == C.ERROR_HANDLE_EOF {
return bytesRead, io.EOF
}
return 0, fmt.Errorf("failed to read file (%d)", lastError)
}
return bytesRead, nil
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
var method uint
switch whence {
case io.SeekStart:
method = C.FILE_BEGIN
case io.SeekCurrent:
method = C.FILE_CURRENT
case io.SeekEnd:
method = C.FILE_END
}
result := C.SFileSetFilePointer(C.HANDLE(f.handle), C.long(offset), nil, C.ulong(method))
if result == C.SFILE_INVALID_SIZE {
return 0, fmt.Errorf("failed to set file pointer (%d)", getLastError())
}
return int64(result), nil
}
func (f *File) Close() error {
if result := C.SFileCloseFile(C.HANDLE(f.handle)); result == 0 {
return fmt.Errorf("failed to close file (%d)", getLastError())
}
f.handle = nil
return nil
}
func sanitizePath(path string) string { func sanitizePath(path string) string {
return strings.ToLower(strings.Replace(path, "\\", string(os.PathSeparator), -1)) return strings.ToLower(strings.Replace(path, "\\", string(os.PathSeparator), -1))
} }

View File

@ -3,7 +3,7 @@ package platform
import "github.com/FooSoft/lazarus/formats/mpq" import "github.com/FooSoft/lazarus/formats/mpq"
var fileState struct { var fileState struct {
mountPoints map[string]mpq.MpqArchive mountPoints map[string]*mpq.Archive
} }
type File struct { type File struct {

View File

@ -14,7 +14,7 @@ import (
"github.com/FooSoft/lazarus/formats/dc6" "github.com/FooSoft/lazarus/formats/dc6"
) )
func loadPalette(path string) (*dat.DatPalette, error) { func loadPalette(path string) (*dat.Palette, error) {
fp, err := os.Open(path) fp, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -23,7 +23,7 @@ func loadPalette(path string) (*dat.DatPalette, error) {
return dat.NewFromReader(fp) return dat.NewFromReader(fp)
} }
func loadSprite(path string) (*dc6.Dc6Animation, error) { func loadSprite(path string) (*dc6.Animation, error) {
fp, err := os.Open(path) fp, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -32,7 +32,7 @@ func loadSprite(path string) (*dc6.Dc6Animation, error) {
return dc6.NewFromReader(fp) return dc6.NewFromReader(fp)
} }
func extractSprite(spritePath string, palette *dat.DatPalette, targetDir string) error { func extractSprite(spritePath string, palette *dat.Palette, targetDir string) error {
sprite, err := loadSprite(spritePath) sprite, err := loadSprite(spritePath)
if err != nil { if err != nil {
return err return err

View File

@ -15,7 +15,7 @@ import (
"github.com/FooSoft/lazarus/platform/imgui" "github.com/FooSoft/lazarus/platform/imgui"
) )
func loadPalette(path string) (*dat.DatPalette, error) { func loadPalette(path string) (*dat.Palette, error) {
fp, err := os.Open(path) fp, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -24,7 +24,7 @@ func loadPalette(path string) (*dat.DatPalette, error) {
return dat.NewFromReader(fp) return dat.NewFromReader(fp)
} }
func loadAnimation(path string) (*dc6.Dc6Animation, error) { func loadAnimation(path string) (*dc6.Animation, error) {
fp, err := os.Open(path) fp, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -34,8 +34,8 @@ func loadAnimation(path string) (*dc6.Dc6Animation, error) {
} }
type scene struct { type scene struct {
animation *dc6.Dc6Animation animation *dc6.Animation
palette *dat.DatPalette palette *dat.Palette
texture graphics.Texture texture graphics.Texture
directionIndex int directionIndex int
@ -145,7 +145,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
var palette *dat.DatPalette var palette *dat.Palette
if len(*palettePath) > 0 { if len(*palettePath) > 0 {
palette, err = loadPalette(*palettePath) palette, err = loadPalette(*palettePath)
if err != nil { if err != nil {