Skip to content

Commit

Permalink
Optimize details of ispx (#816)
Browse files Browse the repository at this point in the history
* optimize details of ispx

* use [email protected]

* TODO for igop main
  • Loading branch information
nighca authored Aug 29, 2024
1 parent 9876343 commit 79e229b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tools/ispx/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/goplus/builder/ispx
go 1.21

require (
github.com/goplus/igop v0.27.0
github.com/goplus/igop v0.27.1
github.com/goplus/reflectx v1.2.2
github.com/goplus/spx v1.0.1-0.20240828022121-d1b083ef69a3
github.com/hajimehoshi/ebiten/v2 v2.7.8
Expand Down
2 changes: 2 additions & 0 deletions tools/ispx/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/goplus/gop v1.2.6 h1:kog3c5Js+8EopqmI4+CwueXsqibnBwYVt5q5N7juRVY=
github.com/goplus/gop v1.2.6/go.mod h1:uREWbR1MrFaviZ4Mbx4ZCcAYDoqzO0iv1Qo6Np0Xx4E=
github.com/goplus/igop v0.27.0 h1:4Wk5CIdm3FI1w6d0Y8GO2IC9sgs4wwYdpizCgRdcfYs=
github.com/goplus/igop v0.27.0/go.mod h1:V8Kf/b4nrw0OPPodwnOYZPCpXvU+hqzuhSAXIToT0ME=
github.com/goplus/igop v0.27.1 h1:nLfk+2a8TZ1XvMb6XZnD6f1DlJ6Owj5We1gt6BMKqaI=
github.com/goplus/igop v0.27.1/go.mod h1:V8Kf/b4nrw0OPPodwnOYZPCpXvU+hqzuhSAXIToT0ME=
github.com/goplus/mod v0.13.10 h1:5Om6KOvo31daN7N30kWU1vC5zhsJPM+uPbcEN/FnlzE=
github.com/goplus/mod v0.13.10/go.mod h1:HDuPZgpWiaTp3PUolFgsiX+Q77cbUWB/mikVHfYND3c=
github.com/goplus/reflectx v1.2.2 h1:T1p20OIH/HcnAvQQNnDLwl6AZOjU34icsfc6migD6L8=
Expand Down
16 changes: 10 additions & 6 deletions tools/ispx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"archive/zip"
"bytes"
"log"
"reflect"
"syscall/js"

_ "github.com/goplus/builder/ispx/pkg/github.com/goplus/spx"
Expand Down Expand Up @@ -48,6 +47,9 @@ func main() {

var mode igop.Mode
ctx := igop.NewContext(mode)

// Register patch for spx to support functions with generic type like `Gopt_Game_Gopx_GetWidget`.
// See details in https://github.com/goplus/builder/issues/765#issuecomment-2313915805
err = gopbuild.RegisterPackagePatch(ctx, "github.com/goplus/spx", `
package spx
Expand All @@ -67,13 +69,15 @@ func Gopt_Game_Gopx_GetWidget[T any](sg ShapeGetter, name string) *T {
if err != nil {
log.Fatalln("Failed to register package patch:", err)
}

source, err := gopbuild.BuildFSDir(ctx, fs, "")
if err != nil {
log.Fatalln("Failed to build Go+ source:", err)
}

// Definition of `Gamer` here should be the same as `Gamer` in `github.com/goplus/spx`
// otherwise, it produces: "fatal error: unreachable method called. linker bug?"
// TODO: consider better solution to avoid replacing `Gopy_Game_Main` and `Gopy_Game_Run`, see details in https://github.com/goplus/builder/issues/824
type Gamer interface {
initGame(sprites []spx.Spriter) *spx.Game
}
Expand All @@ -84,15 +88,15 @@ func Gopt_Game_Gopx_GetWidget[T any](sg ShapeGetter, name string) *T {
}

igop.RegisterExternal("github.com/goplus/spx.Gopt_Game_Main", func(game Gamer, sprites ...spx.Spriter) {
g := game.initGame(sprites)
game.initGame(sprites)
if me, ok := game.(interface{ MainEntry() }); ok {
me.MainEntry()
}
v := reflect.ValueOf(g).Elem().FieldByName("isRunned")
if v.IsValid() && v.Bool() {
return
if me, ok := game.(interface{ IsRunned() bool }); ok {
if !me.IsRunned() {
gameRun(game.(spx.Gamer), "assets")
}
}
gameRun(game.(spx.Gamer), "assets")
})

igop.RegisterExternal("github.com/goplus/spx.Gopt_Game_Run", gameRun)
Expand Down

0 comments on commit 79e229b

Please sign in to comment.