2024-10-15 13:51:37 +02:00
|
|
|
// Copyright 2024 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
2024-10-15 14:28:38 +02:00
|
|
|
"errors"
|
2024-10-15 13:51:37 +02:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ebitengineFileDir string
|
|
|
|
ebitengineFileDirOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2024-10-16 04:11:00 +02:00
|
|
|
type CallerType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CallerTypeNone CallerType = iota
|
|
|
|
CallerTypeRegular
|
|
|
|
CallerTypeInternal
|
|
|
|
)
|
|
|
|
|
2024-10-15 13:51:37 +02:00
|
|
|
// FirstCaller returns the file and line number of the first caller outside of Ebitengine.
|
2024-10-16 04:11:00 +02:00
|
|
|
func FirstCaller() (file string, line int, callerType CallerType) {
|
2024-10-15 13:51:37 +02:00
|
|
|
ebitengineFileDirOnce.Do(func() {
|
|
|
|
cmd := exec.Command("go", "list", "-f", "{{.Dir}}", "github.com/hajimehoshi/ebiten/v2")
|
|
|
|
out, err := cmd.Output()
|
2024-10-15 14:28:38 +02:00
|
|
|
if errors.Is(err, exec.ErrNotFound) {
|
|
|
|
return
|
|
|
|
}
|
2024-10-15 13:51:37 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("debug: go list -f {{.Dir}} failed: %v", err))
|
|
|
|
}
|
|
|
|
ebitengineFileDir = filepath.ToSlash(strings.TrimSpace(string(out)))
|
|
|
|
})
|
|
|
|
|
2024-10-15 14:28:38 +02:00
|
|
|
// Go command is not found.
|
|
|
|
if ebitengineFileDir == "" {
|
2024-10-16 04:11:00 +02:00
|
|
|
return "", 0, CallerTypeNone
|
2024-10-15 14:28:38 +02:00
|
|
|
}
|
|
|
|
|
2024-10-15 13:51:37 +02:00
|
|
|
// Relying on a caller stacktrace is very fragile, but this is fine as this is only for debugging.
|
|
|
|
var ebitenPackageReached bool
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// The file should be with a slash, but just in case, convert it.
|
|
|
|
file = filepath.ToSlash(file)
|
|
|
|
|
|
|
|
if !ebitenPackageReached {
|
|
|
|
if path.Dir(file) == ebitengineFileDir {
|
|
|
|
ebitenPackageReached = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-16 04:11:00 +02:00
|
|
|
if path.Dir(file) == ebitengineFileDir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(path.Dir(file), ebitengineFileDir+"/") && !strings.HasPrefix(path.Dir(file), ebitengineFileDir+"/examples/") {
|
|
|
|
if strings.HasPrefix(path.Dir(file), ebitengineFileDir+"/internal/") {
|
|
|
|
return file, line, CallerTypeInternal
|
|
|
|
}
|
2024-10-15 13:51:37 +02:00
|
|
|
continue
|
|
|
|
}
|
2024-10-16 04:11:00 +02:00
|
|
|
return file, line, CallerTypeRegular
|
2024-10-15 13:51:37 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 04:11:00 +02:00
|
|
|
return "", 0, CallerTypeNone
|
2024-10-15 13:51:37 +02:00
|
|
|
}
|