Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadProjConfig #264

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package spx

import (
"encoding/json"
"io"
"syscall"

spxfs "github.com/goplus/spx/fs"
)
Expand All @@ -39,6 +41,20 @@ func loadJson(ret interface{}, fs spxfs.Dir, file string) (err error) {
return json.NewDecoder(f).Decode(ret)
}

func loadProjConfig(proj *projConfig, fs spxfs.Dir, index interface{}) (err error) {
switch v := index.(type) {
case io.Reader:
err = json.NewDecoder(v).Decode(proj)
case string:
err = loadJson(&proj, fs, v)
case nil:
err = loadJson(&proj, fs, "index.json")
default:
return syscall.EINVAL
}
return
}

// -------------------------------------------------------------------------------------

type Config struct {
Expand Down
45 changes: 21 additions & 24 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@
package spx

import (
"encoding/json"
"flag"
"fmt"
"image"
"image/color"
"io"
"log"
"math/rand"
"os"
"reflect"
"sync/atomic"
"syscall"
"time"
"unsafe"

Expand Down Expand Up @@ -147,9 +144,20 @@ func Gopt_Game_Run(game Gamer, resource interface{}, gameConf ...*Config) {
}

var conf Config
if gameConf != nil { // TODO(xsw): load from index.json
var proj projConfig
if gameConf != nil {
conf = *gameConf[0]
err = loadProjConfig(&proj, fs, conf.Index)
} else {
// load Config from index.json
if err = loadProjConfig(&proj, fs, nil); err == nil {
conf = *proj.Run
}
}
if err != nil {
panic(err)
}

if !conf.DontParseFlags {
f := flag.CommandLine
verbose := f.Bool("v", false, "print verbose information")
Expand Down Expand Up @@ -201,7 +209,7 @@ func Gopt_Game_Run(game Gamer, resource interface{}, gameConf ...*Config) {
}
}
}
if err := g.endLoad(v, conf.Index); err != nil {
if err := g.endLoad(v, &proj); err != nil {
panic(err)
}

Expand Down Expand Up @@ -330,22 +338,7 @@ type initer interface {
Main()
}

func (p *Game) loadIndex(g reflect.Value, index interface{}) (err error) {
var proj projConfig
switch v := index.(type) {
case io.Reader:
err = json.NewDecoder(v).Decode(&proj)
case string:
err = loadJson(&proj, p.fs, v)
case nil:
err = loadJson(&proj, p.fs, "index.json")
default:
return syscall.EINVAL
}
if err != nil {
return
}

func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
if scenes := proj.getScenes(); len(scenes) > 0 {
p.baseObj.init("", scenes, proj.getSceneIndex())
p.worldWidth_ = proj.Map.Width
Expand Down Expand Up @@ -404,11 +397,11 @@ func (p *Game) loadIndex(g reflect.Value, index interface{}) (err error) {
return
}

func (p *Game) endLoad(g reflect.Value, index interface{}) (err error) {
func (p *Game) endLoad(g reflect.Value, proj *projConfig) (err error) {
if debugLoad {
log.Println("==> EndLoad")
}
return p.loadIndex(g, index)
return p.loadIndex(g, proj)
}

func Gopt_Game_Reload(game Gamer, index interface{}) (err error) {
Expand All @@ -423,7 +416,11 @@ func Gopt_Game_Reload(game Gamer, index interface{}) (err error) {
}
}
}
return g.loadIndex(v, index)
var proj projConfig
if err = loadProjConfig(&proj, g.fs, index); err != nil {
return
}
return g.loadIndex(v, &proj)
}

// -----------------------------------------------------------------------------
Expand Down
Loading