Skip to content

Commit

Permalink
Use buffered file writer
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Sep 22, 2023
1 parent 1969302 commit 8dd9e9c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
39 changes: 39 additions & 0 deletions pkg/os/os.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package os

import (
"bufio"
"bytes"
"errors"
"io"
"io/fs"
"os"
"os/signal"
"os/user"
"syscall"
)

const ReadChunk = 1024

var ErrNotExist = os.ErrNotExist

func Exists(path string) bool {
Expand Down Expand Up @@ -45,3 +50,37 @@ func GetUserHome() (string, error) {
func WriteFile(name string, data []byte, perm os.FileMode) error {
return os.WriteFile(name, data, perm)
}

func ReadFile(name string) (dat []byte, err error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()

r := bufio.NewReader(f)
buf := bytes.NewBuffer(make([]byte, 0))
chunk := make([]byte, ReadChunk)

c := 0
for {
if c, err = r.Read(chunk); err != nil {
break
}
buf.Write(chunk[:c])
}

if err == io.EOF {
err = nil
}

return buf.Bytes(), err
}

func StatSize(path string) (int64, error) {
fi, err := os.Stat(path)
if err != nil {
return 0, err
}
return fi.Size(), nil
}
45 changes: 25 additions & 20 deletions pkg/worker/caged/libretro/nanoarch/nanoarch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package nanoarch
import (
"errors"
"fmt"
"os"
"runtime"
"strings"
"sync/atomic"
"time"
"unsafe"

"github.com/giongto35/cloud-game/v3/pkg/logger"
"github.com/giongto35/cloud-game/v3/pkg/os"
"github.com/giongto35/cloud-game/v3/pkg/worker/caged/libretro/graphics"
"github.com/giongto35/cloud-game/v3/pkg/worker/caged/libretro/image"
"github.com/giongto35/cloud-game/v3/pkg/worker/caged/libretro/repo/arch"
Expand Down Expand Up @@ -115,10 +115,9 @@ func init() { Nan0.reserved <- struct{}{} }

func NewNano(localPath string) *Nanoarch {
nano := &Nan0
nano.cSaveDirectory = C.CString(localPath + string(os.PathSeparator) + "legacy_save")
nano.cSystemDirectory = C.CString(localPath + string(os.PathSeparator) + "system")
nano.cSaveDirectory = C.CString(localPath + "/legacy_save")
nano.cSystemDirectory = C.CString(localPath + "/system")
nano.cUserName = C.CString("retro")

return nano
}

Expand Down Expand Up @@ -209,29 +208,33 @@ func (n *Nanoarch) CoreLoad(meta Metadata) {
}

func (n *Nanoarch) LoadGame(path string) error {
fi, err := os.Stat(path)
if err != nil {
return err
}
fileSize := fi.Size()

n.log.Debug().Msgf("ROM size: %v", byteCountBinary(fileSize))

fPath := C.CString(path)
defer C.free(unsafe.Pointer(fPath))
gi := C.struct_retro_game_info{path: fPath, size: C.size_t(fileSize)}
game := C.struct_retro_game_info{}

if !bool(n.sysInfo.need_fullpath) {
big := bool(n.sysInfo.need_fullpath) // big ROMs are loaded by cores later
if big {
size, err := os.StatSize(path)
if err != nil {
return err
}
game.size = C.size_t(size)
} else {
bytes, err := os.ReadFile(path)
if err != nil {
return err
}
dataPtr := unsafe.Pointer(C.CBytes(bytes))
gi.data = dataPtr
defer C.free(dataPtr)
// !to pin in 1.21
ptr := unsafe.Pointer(C.CBytes(bytes))
game.data = ptr
game.size = C.size_t(len(bytes))
defer C.free(ptr)
}
fp := C.CString(path)
defer C.free(unsafe.Pointer(fp))
game.path = fp

n.log.Debug().Msgf("ROM - big: %v, size: %v", big, byteCountBinary(int64(game.size)))

if ok := C.bridge_retro_load_game(retroLoadGame, &gi); !ok {
if ok := C.bridge_retro_load_game(retroLoadGame, &game); !ok {
return fmt.Errorf("core failed to load ROM: %v", path)
}

Expand Down Expand Up @@ -467,6 +470,7 @@ func SaveRAM() State {
func RestoreSaveRAM(st State) {
if len(st) > 0 {
if memory := ptSaveRAM(); memory != nil {
//noinspection GoRedundantConversion
copy(unsafe.Slice((*byte)(memory.ptr), memory.size), st)
}
}
Expand Down Expand Up @@ -573,6 +577,7 @@ func coreVideoRefresh(data unsafe.Pointer, width, height uint, packed uint) {

var data_ []byte
if data != C.RETRO_HW_FRAME_BUFFER_VALID {
//noinspection GoRedundantConversion
data_ = unsafe.Slice((*byte)(data), bytes)
} else {
// if Libretro renders frame with OpenGL context
Expand Down
2 changes: 1 addition & 1 deletion pkg/worker/caged/libretro/storage.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package libretro

import (
"os"
"path/filepath"
"strings"

"github.com/giongto35/cloud-game/v3/pkg/os"
"github.com/giongto35/cloud-game/v3/pkg/worker/compression/zip"
)

Expand Down
3 changes: 2 additions & 1 deletion pkg/worker/cloud/cloudstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/giongto35/cloud-game/v3/pkg/os"
)

// !to replace all with unified s3 api
Expand Down

0 comments on commit 8dd9e9c

Please sign in to comment.