Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
renaming types
Browse files Browse the repository at this point in the history
  • Loading branch information
FooSoft committed Jan 9, 2019
1 parent a02379c commit 9878545
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 85 deletions.
10 changes: 5 additions & 5 deletions formats/dat/dat.go
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/FooSoft/lazarus/math"
)

type DatPalette struct {
type Palette struct {
Colors [256]math.Color3b
}

Expand All @@ -17,22 +17,22 @@ type color struct {
R byte
}

func NewFromReader(reader io.Reader) (*DatPalette, error) {
func NewFromReader(reader io.Reader) (*Palette, error) {
var colors [256]color
if err := binary.Read(reader, binary.LittleEndian, &colors); err != nil {
return nil, err
}

palette := new(DatPalette)
palette := new(Palette)
for i, color := range colors {
palette.Colors[i] = math.Color3b{R: color.R, G: color.G, B: color.B}
}

return palette, nil
}

func NewFromGrayscale() *DatPalette {
palette := new(DatPalette)
func NewFromGrayscale() *Palette {
palette := new(Palette)
for i := 0; i < 256; i++ {
value := uint8(i)
palette.Colors[i] = math.Color3b{R: value, G: value, B: value}
Expand Down
6 changes: 3 additions & 3 deletions formats/dc6/dc6.go
Expand Up @@ -38,12 +38,12 @@ type Frame struct {
Data []byte
}

type Dc6Animation struct {
type Animation struct {
Directions []Direction
}

func NewFromReader(reader io.ReadSeeker) (*Dc6Animation, error) {
sprite := new(Dc6Animation)
func NewFromReader(reader io.ReadSeeker) (*Animation, error) {
sprite := new(Animation)

var fileHead fileHeader
if err := binary.Read(reader, binary.LittleEndian, &fileHead); err != nil {
Expand Down
124 changes: 56 additions & 68 deletions formats/mpq/mpq.go
Expand Up @@ -20,23 +20,16 @@ import (
"unsafe"
)

type File interface {
Read(data []byte) (int, error)
Seek(offset int64, whence int) (int64, error)
Close() error
}

type MpqArchive interface {
OpenFile(path string) (File, error)
GetPaths() []string
Close() error
type Archive struct {
handle unsafe.Pointer
paths map[string]string
}

func NewFromFile(path string) (MpqArchive, error) {
func NewFromFile(path string) (*Archive, error) {
cs := C.CString(path)
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 {
return nil, fmt.Errorf("failed to open archive (%d)", getLastError())
}
Expand All @@ -49,58 +42,7 @@ func NewFromFile(path string) (MpqArchive, error) {
return a, 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
}

type archive struct {
handle unsafe.Pointer
paths map[string]string
}

func (a *archive) Close() error {
func (a *Archive) Close() error {
if result := C.SFileCloseArchive(C.HANDLE(a.handle)); result == 0 {
return fmt.Errorf("failed to close archive (%d)", getLastError())
}
Expand All @@ -110,23 +52,23 @@ func (a *archive) Close() error {
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 {
path = pathInt
}

cs := C.CString(path)
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 {
return nil, fmt.Errorf("failed to open file (%d)", getLastError())
}

return file, nil
}

func (a *archive) GetPaths() []string {
func (a *Archive) GetPaths() []string {
var extPaths []string
for extPath := range a.paths {
extPaths = append(extPaths, extPath)
Expand All @@ -135,7 +77,7 @@ func (a *archive) GetPaths() []string {
return extPaths
}

func (a *archive) buildPathMap() error {
func (a *Archive) buildPathMap() error {
f, err := a.OpenFile("(listfile)")
if err != nil {
return err
Expand All @@ -161,6 +103,52 @@ func (a *archive) buildPathMap() error {
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 {
return strings.ToLower(strings.Replace(path, "\\", string(os.PathSeparator), -1))
}
Expand Down
2 changes: 1 addition & 1 deletion platform/file.go
Expand Up @@ -3,7 +3,7 @@ package platform
import "github.com/FooSoft/lazarus/formats/mpq"

var fileState struct {
mountPoints map[string]mpq.MpqArchive
mountPoints map[string]*mpq.Archive
}

type File struct {
Expand Down
6 changes: 3 additions & 3 deletions tools/dc6/dc6.go
Expand Up @@ -14,7 +14,7 @@ import (
"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)
if err != nil {
return nil, err
Expand All @@ -23,7 +23,7 @@ func loadPalette(path string) (*dat.DatPalette, error) {
return dat.NewFromReader(fp)
}

func loadSprite(path string) (*dc6.Dc6Animation, error) {
func loadSprite(path string) (*dc6.Animation, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
Expand All @@ -32,7 +32,7 @@ func loadSprite(path string) (*dc6.Dc6Animation, error) {
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)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions tools/viewer/viewer.go
Expand Up @@ -15,7 +15,7 @@ import (
"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)
if err != nil {
return nil, err
Expand All @@ -24,7 +24,7 @@ func loadPalette(path string) (*dat.DatPalette, error) {
return dat.NewFromReader(fp)
}

func loadAnimation(path string) (*dc6.Dc6Animation, error) {
func loadAnimation(path string) (*dc6.Animation, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
Expand All @@ -34,8 +34,8 @@ func loadAnimation(path string) (*dc6.Dc6Animation, error) {
}

type scene struct {
animation *dc6.Dc6Animation
palette *dat.DatPalette
animation *dc6.Animation
palette *dat.Palette
texture graphics.Texture

directionIndex int
Expand Down Expand Up @@ -145,7 +145,7 @@ func main() {
os.Exit(1)
}

var palette *dat.DatPalette
var palette *dat.Palette
if len(*palettePath) > 0 {
palette, err = loadPalette(*palettePath)
if err != nil {
Expand Down

0 comments on commit 9878545

Please sign in to comment.