From 79e229b0bfcc44e1b041ed97b9695711dcf76231 Mon Sep 17 00:00:00 2001 From: Hanxing Yang Date: Thu, 29 Aug 2024 09:54:22 +0800 Subject: [PATCH] Optimize details of ispx (#816) * optimize details of ispx * use igop@v0.27.1 * TODO for igop main --- tools/ispx/go.mod | 2 +- tools/ispx/go.sum | 2 ++ tools/ispx/main.go | 16 ++++++++++------ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tools/ispx/go.mod b/tools/ispx/go.mod index b8ab3cd21..a8895b2ca 100644 --- a/tools/ispx/go.mod +++ b/tools/ispx/go.mod @@ -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 diff --git a/tools/ispx/go.sum b/tools/ispx/go.sum index 1acce4cca..3d3af220e 100644 --- a/tools/ispx/go.sum +++ b/tools/ispx/go.sum @@ -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= diff --git a/tools/ispx/main.go b/tools/ispx/main.go index 5e8e120cf..dc11b2794 100644 --- a/tools/ispx/main.go +++ b/tools/ispx/main.go @@ -7,7 +7,6 @@ import ( "archive/zip" "bytes" "log" - "reflect" "syscall/js" _ "github.com/goplus/builder/ispx/pkg/github.com/goplus/spx" @@ -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 @@ -67,6 +69,7 @@ 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) @@ -74,6 +77,7 @@ func Gopt_Game_Gopx_GetWidget[T any](sg ShapeGetter, name string) *T { // 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 } @@ -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)