Remove hash

This commit is contained in:
giongto35 2019-05-05 23:46:40 +08:00
parent 55229e33b6
commit fd7a847df6
4 changed files with 22 additions and 49 deletions

View file

@ -19,9 +19,6 @@ type Director struct {
Done chan struct{}
roomID string
// Hash represents a game state (roomID, gamePath).
// It is used for save file name
hash string
}
const FPS = 60
@ -33,7 +30,6 @@ func NewDirector(roomID string, imageChannel chan *image.RGBA, audioChannel chan
director.imageChannel = imageChannel
director.inputChannel = inputChannel
director.roomID = roomID
director.hash = ""
return &director
}
@ -97,24 +93,18 @@ L:
}
func (d *Director) PlayGame(path string) {
// Generate hash that is indentifier of a room (game path + roomID)
hash, err := hashFile(path, d.roomID)
if err != nil {
log.Fatalln(err)
}
d.hash = hash
console, err := nes.NewConsole(path)
if err != nil {
log.Fatalln(err)
}
// Set GameView as current view
d.SetView(NewGameView(console, path, hash, d.imageChannel, d.audioChannel, d.inputChannel))
d.SetView(NewGameView(console, path, d.roomID, d.imageChannel, d.audioChannel, d.inputChannel))
}
// SaveGame creates save events and doing extra step for load
func (d *Director) SaveGame(saveExtraFunc func() error) error {
if d.hash != "" {
d.view.Save(d.hash, saveExtraFunc)
if d.roomID != "" {
d.view.Save(d.roomID, saveExtraFunc)
return nil
} else {
return nil
@ -123,20 +113,15 @@ func (d *Director) SaveGame(saveExtraFunc func() error) error {
// LoadGame creates load events and doing extra step for load
func (d *Director) LoadGame() error {
if d.hash != "" {
d.view.Load(d.hash)
if d.roomID != "" {
d.view.Load(d.roomID)
return nil
} else {
return nil
}
}
// GetHash return hash
func (d *Director) GetHash() string {
return d.hash
}
// GetHashPath return the full path to hash file
func (d *Director) GetHashPath() string {
return savePath(d.hash)
return savePath(d.roomID)
}

View file

@ -40,8 +40,9 @@ const (
type GameView struct {
console *nes.Console
title string
hash string
// saveFile is the filename gameview save to
saveFile string
// equivalent to the list key pressed const above
keyPressed [NumKeys * 2]bool
@ -58,11 +59,11 @@ type job struct {
extraFunc func() error
}
func NewGameView(console *nes.Console, title, hash string, imageChannel chan *image.RGBA, audioChannel chan float32, inputChannel chan int) *GameView {
func NewGameView(console *nes.Console, title, saveFile string, imageChannel chan *image.RGBA, audioChannel chan float32, inputChannel chan int) *GameView {
gameview := &GameView{
console: console,
title: title,
hash: hash,
saveFile: saveFile,
keyPressed: [NumKeys * 2]bool{false},
imageChannel: imageChannel,
audioChannel: audioChannel,
@ -99,18 +100,17 @@ func (view *GameView) Enter() {
view.console.SetAudioSampleRate(SampleRate)
view.console.SetAudioChannel(view.audioChannel)
// load state if the hash file existed in the server (Join the old room)
if err := view.console.LoadState(savePath(view.hash)); err == nil {
// load state if the saveFile file existed in the server (Join the old room)
if err := view.console.LoadState(savePath(view.saveFile)); err == nil {
return
} else {
view.console.Reset()
}
//view.console.Reset()
// load sram
cartridge := view.console.Cartridge
if cartridge.Battery != 0 {
if sram, err := readSRAM(sramPath(view.hash)); err == nil {
if sram, err := readSRAM(sramPath(view.saveFile)); err == nil {
cartridge.SRAM = sram
}
}
@ -123,7 +123,7 @@ func (view *GameView) Exit() {
// save sram
cartridge := view.console.Cartridge
if cartridge.Battery != 0 {
writeSRAM(sramPath(view.hash), cartridge.SRAM)
writeSRAM(sramPath(view.saveFile), cartridge.SRAM)
}
}
@ -144,7 +144,7 @@ func (view *GameView) Update(t, dt float64) {
func (view *GameView) Save(hash string, extraSaveFunc func() error) {
// put saving event to queue, process in updateEvent
view.savingJob = &job{
path: savePath(view.hash),
path: savePath(view.saveFile),
extraFunc: extraSaveFunc,
}
}
@ -152,7 +152,7 @@ func (view *GameView) Save(hash string, extraSaveFunc func() error) {
func (view *GameView) Load(path string) {
// put saving event to queue, process in updateEvent
view.loadingJob = &job{
path: savePath(view.hash),
path: savePath(view.saveFile),
extraFunc: nil,
}
}

View file

@ -2,7 +2,6 @@
package emulator
import (
"crypto/md5"
"encoding/binary"
"fmt"
"image"
@ -10,7 +9,6 @@ import (
"image/draw"
"image/gif"
"image/png"
"io/ioutil"
"log"
"os"
"os/user"
@ -29,14 +27,13 @@ func init() {
homeDir = u.HomeDir
}
func sramPath(hash string) string {
return homeDir + "/.nes/sram/" + hash + ".dat"
// Public call to get savePath
func GetSavePath(roomID string) string {
return savePath(roomID)
}
// Public call to get savePath
func GetSavePath(path string, roomID string) string {
hash, _ := hashFile(path, roomID)
return savePath(hash)
func sramPath(hash string) string {
return homeDir + "/.nes/sram/" + hash + ".dat"
}
func savePath(hash string) string {
@ -51,15 +48,6 @@ func combineButtons(a, b [8]bool) [8]bool {
return result
}
// hashFile : signature of a room, maybe not need path
func hashFile(path string, roomID string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", md5.Sum(append(data, []byte(roomID)...))), nil
}
func copyImage(src image.Image) *image.RGBA {
dst := image.NewRGBA(src.Bounds())
draw.Draw(dst, dst.Rect, src, image.ZP, draw.Src)

View file

@ -68,7 +68,7 @@ func NewRoom(roomID, gamepath, gameName string, onlineStorage *storage.Client) *
path := gamepath + "/" + gameName
go func(path, roomID string) {
// Check room is on local or fetch from server
savepath := emulator.GetSavePath(path, roomID)
savepath := emulator.GetSavePath(roomID)
log.Println("Check ", savepath, " on local : ", room.isGameOnLocal(savepath))
if !room.isGameOnLocal(savepath) {
// Fetch room from GCP to server