Files
aego/app/edit/edit.go
T
sanspie 50be5c04bc
ci-platforms / darwin amd64 smoke (push) Skipped
GitHub / linux amd64 native GitHub-hosted native CI: success
ci-platforms / windows amd64 smoke (push) Skipped
GitHub / windows amd64 native GitHub-hosted native CI: success
GitHub / darwin arm64 native GitHub-hosted native CI: success
GitHub / darwin amd64 native GitHub-hosted native CI: success
ci / checks (push) Failing after 1h21m34s
ci / native-authoring (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
github-native / gitea-pending (push) Skipped
github-native / darwin amd64 native (push) Skipped
github-native / linux amd64 native (push) Skipped
github-native / windows amd64 native (push) Skipped
github-native / darwin arm64 native (push) Skipped
github-native / gitea-result (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 12m4s
ci / release (push) Skipped
Stage 12.5: Art Workspace and asset authoring
Add dockable Art tools, animation and previews, guarded asset workflows,
and the Web Asset Studio built on the shared Art Core. Expand English and
Russian documentation, interactive Try scenes, examples and editor icons.

Add audio waveform navigation, source analysis, guarded previews and a
browser Worker mixer. Preserve source changes and recovery with bounded
checks, and strengthen native, browser and interoperability CI.

Prepare version 0.12.5 and manually built native artifact candidates.
Fix the production site image demo build dependency.
Reuse immutable scene indices when capturing keyframes.
Wait for real Persist save/load completion in component tests.
Stabilize browser Game View captures outside pointer hover.
Aseprite interchange and persistent Audio EDL recovery remain limited;
the artifact workflow does not publish releases or change stable.
2026-11-15 10:37:50 +03:00

399 lines
9.4 KiB
Go

package edit
import (
"aego/app/agentui/backend"
"aego/engine/i18n"
"errors"
"os"
"os/signal"
"runtime/debug"
"runtime/pprof"
"time"
"aego/app"
"aego/app/assembly"
"aego/app/assembly/builtin"
"aego/app/assembly/editors"
"aego/app/crash"
"aego/app/edit/agentbridge"
"aego/asset"
"aego/asset/pipeline"
"aego/core/log"
"aego/editor/core/document"
"aego/editor/panels/scripteditor"
"aego/editor/shell"
"aego/engine"
"aego/engine/base"
"aego/engine/module"
"aego/platform"
"aego/project"
"aego/scene"
"aego/script/langsvc"
"aego/sdk"
)
var ErrRestart = errors.New("editor restart requested")
type Config struct {
Headless bool
Hidden bool
Debug bool
Frames int
Width int
Height int
NoWatch bool
Supervised bool
CPUProfile string
Recent string
Profile string
Logger *log.Logger
Catalog assembly.Catalog
OpenPath string
NativeNote string
LauncherTab string
// ExampleLink is an untrusted OS protocol request. The launcher displays
// the parsed HTTPS origin and waits for confirmation before any network IO.
ExampleLink string
// RemoteProject is set by the confirmed remote-example launcher. It is
// retained for the session, even if project.aego changes after startup.
RemoteProject bool
// AgentBackend overrides the saved provider for tests/replay; headless nil means no panel.
AgentBackend backend.Backend
}
type docAttach struct {
id document.ID
scene *scene.Scene
detach func()
}
type driver struct {
*shell.App
s *Session
}
func (d *driver) Update(dt float64) {
d.s.syncDocs()
d.s.spike()
d.App.Update(dt)
}
type Session struct {
*app.Host
App *shell.App
Scripts *scriptHost
cfg Config
log *log.ChannelLogger
stop func()
unsub func()
docs []docAttach
driver *driver
mods []sdk.Module
bridge *agentbridge.Bridge
ring *crash.Ring
crashScene bool
spikeMs float64
lastSpike float64
}
func Run(p *project.Project, cfg Config, mods ...sdk.Module) error {
shotsDir := os.Getenv(EnvShots)
cfg = screenshotConfig(cfg, shotsDir)
if cfg.CPUProfile != "" {
f, err := os.Create(cfg.CPUProfile)
if err != nil {
return err
}
if err := pprof.StartCPUProfile(f); err != nil {
f.Close()
return err
}
defer func() {
pprof.StopCPUProfile()
f.Close()
}()
}
s, err := Open(p, cfg, mods...)
if err != nil {
return err
}
defer func() {
if rec := recover(); rec != nil {
s.crash("panic", "editor", rec, debug.Stack(), "")
panic(rec)
}
}()
if shotsDir != "" {
return s.shots(shotsDir)
}
if err := s.Run(); err != nil {
return err
}
if s.App.Restarting() {
return ErrRestart
}
return nil
}
func Open(p *project.Project, cfg Config, mods ...sdk.Module) (*Session, error) {
if cfg.RemoteProject && p.Native {
return nil, errors.New(i18n.Tr("launcher.remote.native_blocked", "Remote examples cannot start a native Go project."))
}
platform.SetAppMetadata(platform.AppMetadata{Name: "Aego Editor", Version: engine.Version, Identifier: "ru.akarpov.aego.editor"})
if cfg.Width <= 0 {
cfg.Width = 1600
}
if cfg.Height <= 0 {
cfg.Height = 900
}
made, dirsErr := p.EnsureDirs()
cat := cfg.Catalog
if cat == nil {
cat = editors.With(builtin.Default())
}
h, err := app.Build(p, app.Config{
Engine: engine.Config{
Title: p.Name + " — Aego Editor",
Width: cfg.Width,
Height: cfg.Height,
Headless: cfg.Headless,
Hidden: cfg.Hidden,
Debug: cfg.Debug,
AppID: p.Module + ".editor",
Trace: true,
},
Logger: cfg.Logger,
Debug: cfg.Debug,
LoopScope: engine.ScopeFrame,
LocateBindings: true,
Docs: true,
Editor: true,
Catalog: cat,
Profile: cfg.Profile,
NoExternalImportTools: cfg.RemoteProject,
}, sdk.AdaptAll(mods)...)
if err != nil {
return nil, err
}
s := &Session{Host: h, cfg: cfg, log: h.Log.With(shell.ChannelEditor)}
loadMessages(h.Modules, s.log)
i18n.Default().SetDev(cfg.Debug)
s.mods = mods
s.ring = crash.NewRing(500)
h.Log.AddSink(s.ring)
if st, ok := module.SectionValue[base.Settings](h.Sections, base.Section); ok {
s.crashScene, s.spikeMs = st.CrashScene, st.SpikeMs
}
h.Engine.Jobs().SetPanicHandler(func(rec any, stack []byte) { s.crash("panic", "worker", rec, stack, "") })
touchRecent(cfg, p.Root, p.Name, s.log)
s.Scripts = &scriptHost{
bindings: h.Bindings,
schema: h.Types.Schema(),
pipeline: h.Pipeline,
importer: app.ScriptImporter(h.Importers),
ws: h.Workspace,
log: h.Log,
modules: h.Modules,
fault: s.fault,
panicked: s.scriptPanic,
}
lang := langsvc.New(h.Workspace, h.Engine.Jobs())
if p.Native {
lang.SetNativeRoot(p.NativeDir())
}
if err := s.Scripts.Reload(); err != nil {
s.log.Error("cannot publish script types", log.F("err", err.Error()))
}
s.unsub = h.Loader.Subscribe(func(r asset.Reloaded) {
if r.Kind != asset.ArtScript {
return
}
if err := s.Scripts.Reload(); err != nil {
s.log.Error("cannot publish script types", log.F("err", err.Error()))
}
})
s.App = shell.New(shell.Options{
Project: p,
Pipeline: h.Pipeline,
Loader: h.Loader,
Types: h.Types,
Renderer: h.Renderer,
Log: h.Log,
Modules: h.Modules,
Sections: h.Sections,
Problems: h.Problems,
Settings: h.Settings,
Hooks: h.Modules.Editor,
RenderStats: h.RenderStats,
Supervised: cfg.Supervised,
Paced: !cfg.Headless,
Scripts: s.Scripts,
Code: scripteditor.NewHost(lang),
CreatedDirs: made,
Help: h.Docs,
RunSnippet: snippets(),
DirsErr: dirsErr,
NewProject: func(dir, template string) (string, error) { return CreateProject(dir, template, "", "") },
Relaunch: Relaunch,
OpenExampleLink: OpenExampleLink,
OpenPath: cfg.OpenPath,
NativeNote: cfg.NativeNote,
RunTests: s.runTests,
Recent: cfg.Recent,
PrefsDir: prefsDir(cfg),
LogsDir: LogsDir(),
Crash: func(where string, rec any, stack []byte) { s.crash("panic", where, rec, stack, "") },
Exporter: newExporter(p, h.Pipeline, func() []string {
var dirty []string
if s.App != nil {
for _, doc := range s.App.Context().Docs.Scripts() {
if doc.Dirty() {
dirty = append(dirty, doc.Path)
}
}
for _, doc := range s.App.Context().Docs.AssetDocuments() {
if doc.Binding.Dirty != nil && doc.Binding.Dirty() {
dirty = append(dirty, doc.Path)
}
}
}
return dirty
}),
Agent: s.agentHost(cfg),
Proxy: s.startProxy,
Updates: s.updates(cfg),
})
s.driver = &driver{App: s.App, s: s}
return s, nil
}
func (s *Session) attachedTo(d *document.SceneDocument) bool {
for _, a := range s.docs {
if a.id == d.ID && a.scene == d.Scene() {
return true
}
}
return false
}
func live(docs []*document.SceneDocument, a docAttach) bool {
for _, d := range docs {
if d.ID == a.id && d.Scene() == a.scene {
return true
}
}
return false
}
func (s *Session) syncDocs() {
ctx := s.App.Context()
if ctx == nil || s.Modules == nil {
return
}
docs := ctx.Docs.Docs()
n := 0
for _, a := range s.docs {
if live(docs, a) {
s.docs[n] = a
n++
continue
}
a.detach()
}
s.docs = s.docs[:n]
for _, d := range docs {
if s.attachedTo(d) {
continue
}
det, err := s.Modules.Attach(&module.AttachContext{World: s.Engine.MainWorld(), Scene: d.Scene(), Log: s.Log, Editor: true})
if err != nil {
s.log.Error("modules could not attach to the scene", log.F("path", d.Path), log.F("err", err.Error()))
det = func() {}
}
s.docs = append(s.docs, docAttach{id: d.ID, scene: d.Scene(), detach: det})
}
}
func (s *Session) applyBatch(b *pipeline.Batch) {
if ctx := s.App.Context(); ctx != nil {
ctx.ApplyBatch(b)
return
}
for id := range b.Failed {
s.Loader.RejectSource(id)
}
if len(b.Imported) > 0 {
if err := s.Loader.Swap(b.Imported, s.Pipeline.Artifacts()); err != nil {
s.log.Error("hot reload failed, keeping the previous assets", log.F("err", err.Error()))
return
}
}
for _, id := range b.Removed {
s.Loader.Drop(id)
}
}
func (s *Session) Step() bool {
return s.Engine.Step(s.driver)
}
func (s *Session) Run() error {
defer s.Close()
if !s.cfg.NoWatch && s.cfg.Frames == 0 {
stop, err := s.Pipeline.Watch(150*time.Millisecond, func(b *pipeline.Batch) {
s.Engine.Jobs().OnMain(func() { s.applyBatch(b) })
})
if err != nil {
return err
}
s.stop = stop
}
interrupts := make(chan os.Signal, 1)
signal.Notify(interrupts, os.Interrupt)
defer signal.Stop(interrupts)
go func() {
if _, ok := <-interrupts; ok {
s.Engine.Jobs().OnMain(s.Engine.Quit)
}
}()
if s.cfg.Frames > 0 {
for i := 0; i < s.cfg.Frames; i++ {
if !s.Engine.Step(s.driver) {
break
}
}
return s.Engine.Close()
}
return s.Engine.Run(s.driver)
}
func (s *Session) Close() {
for _, pr := range i18n.Default().TakeProblems() {
s.log.Warn(pr.Message, log.F("code", pr.Code), log.F("key", pr.Key))
}
if s.stop != nil {
s.stop()
s.stop = nil
}
if s.unsub != nil {
s.unsub()
s.unsub = nil
}
for i := len(s.docs) - 1; i >= 0; i-- {
s.docs[i].detach()
}
s.docs = nil
if s.bridge != nil {
s.bridge.Close()
s.bridge = nil
}
if s.App != nil {
if closer, ok := s.App.AgentExporter().(interface{ Close() }); ok {
closer.Close()
}
}
s.Host.Close()
}