Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (

require github.com/coder/websocket v1.8.12

require github.com/AO-Underground/aolib/go/v2 v2.6.1
require github.com/AO-Underground/aolib/go/v2 v2.6.5

require (
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/AO-Underground/aolib/go/v2 v2.6.1 h1:Pn/BZ5desmvnCS21iZdVpuX6HpLSUhTva9To1YXJl8o=
github.com/AO-Underground/aolib/go/v2 v2.6.1/go.mod h1:8zK2xBlP1VOw+YZNhyv+slbFv7OP6B8AnDGRuFq7q3A=
github.com/AO-Underground/aolib/go/v2 v2.6.5 h1:1iUlL9LzDNkAo+4tgtqQQARKbJj6/FzHNl7X8d1x2so=
github.com/AO-Underground/aolib/go/v2 v2.6.5/go.mod h1:8zK2xBlP1VOw+YZNhyv+slbFv7OP6B8AnDGRuFq7q3A=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
Expand Down
8 changes: 6 additions & 2 deletions internal/assets/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ func (p *DecoderPool) runJob(req DecodeRequest) {
defer p.animGate.release()
}

// established is whether frame 0 went out as a stream's first chunk; the stream appends only frames after it.
established := false
if animated {
if first, err := DecodeImage(req.Data, false); err == nil {
// GIF/APNG can sniff "maybe" but decode static, and a static would upload the same texture twice.
Expand All @@ -673,6 +675,7 @@ func (p *DecoderPool) runJob(req DecodeRequest) {
if streamable {
// Streaming pages grow frame by frame; frame 0 is the first chunk.
first.Stream = true
established = true
}
req.OnDone(req.URL, first, nil)
} else {
Expand All @@ -681,8 +684,9 @@ func (p *DecoderPool) runJob(req DecodeRequest) {
}
}

// The definitely-animated CGO formats stream: frames land one at a time and the page plays what has arrived.
if streamable {
// The definitely-animated CGO formats stream: frames land one at a time and the page plays what has arrived. A file
// with the animation flag and one frame decodes as a still, as no stream ever sends its frame 0.
if streamable && established {
p.streamAnimated(req)
return
}
Expand Down
71 changes: 71 additions & 0 deletions internal/assets/webp_cgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package assets

import (
"encoding/binary"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -213,3 +214,73 @@ func BenchmarkDecodeWebPAnim_256x192_3f(b *testing.B) {
d.Release()
}
}

// oneFrameAnimatedWebP wraps a still lossy WebP in the animation chunks, as encoders that keep the ANIM flag for a single
// frame write it; webpmux and img2webp both collapse one frame to a still, so the fixture is built here.
func oneFrameAnimatedWebP(t *testing.T, still []byte, w, h int) []byte {
t.Helper()
const (
riffHeader = 12
vp8xAnimFlag = 0x02
frameMs = 150
)
if string(still[riffHeader:riffHeader+4]) != "VP8 " {
t.Fatalf("fixture chunk %q, want a lossy VP8 still", still[riffHeader:riffHeader+4])
}
frame := still[riffHeader:] // the VP8 chunk, header and padding included
u24 := func(b []byte, v int) []byte { return append(b, byte(v), byte(v>>8), byte(v>>16)) }
chunk := func(b []byte, kind string, body []byte) []byte {
b = append(b, kind...)
b = binary.LittleEndian.AppendUint32(b, uint32(len(body)))
b = append(b, body...)
if len(body)%2 == 1 {
b = append(b, 0)
}
return b
}
vp8x := u24(u24([]byte{vp8xAnimFlag, 0, 0, 0}, w-1), h-1)
anim := []byte{0, 0, 0, 0, 0, 0} // background colour, loop forever
anmf := u24(u24(u24(u24(u24(nil, 0), 0), w-1), h-1), frameMs)
anmf = append(append(anmf, 0), frame...)
body := chunk(chunk(chunk([]byte("WEBP"), "VP8X", vp8x), "ANIM", anim), "ANMF", anmf)
out := binary.LittleEndian.AppendUint32([]byte("RIFF"), uint32(len(body)))
return append(out, body...)
}

func TestAOneFrameAnimationDecodesToItsFrame(t *testing.T) {
const w, h = 256, 192
data := oneFrameAnimatedWebP(t, fixture(t, "sprite_256x192.webp"), w, h)
if Sniff(data) != FormatWebPAnim {
t.Fatalf("built %v, want an animated webp", Sniff(data))
}
const wait = 5 * time.Second
pool := NewDecoderPool(1)
defer pool.Close()
got := make(chan *Decoded, 4)
pool.Submit(DecodeRequest{
URL: "x", Data: data, Type: AssetTypeCharSprite, PlayAnimations: true,
OnDone: func(_ string, d *Decoded, err error) {
if err != nil {
t.Errorf("decode: %v", err)
d = &Decoded{}
}
got <- d
},
})
var frames int
var final *Decoded
for final == nil {
select {
case d := <-got:
frames += len(d.Frames)
if !d.Partial {
final = d
}
case <-time.After(wait):
t.Fatalf("no final page after %d frames", frames)
}
}
if final == nil || frames != 1 || final.Width != w || final.Height != h {
t.Fatalf("delivered %d frames, final %+v; want one %dx%d frame and a final page", frames, final, w, h)
}
}
2 changes: 2 additions & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ type Settings struct {
WardrobeIconPxVal int `json:"wardrobeIconPx,omitempty"`
// WardrobeNameAbove puts a grid card's name above its icon instead of below.
WardrobeNameAbove bool `json:"wardrobeNameAbove,omitempty"`
// WardrobeSingleClick makes one click on a character wear it, as double-click does.
WardrobeSingleClick bool `json:"wardrobeSingleClick,omitempty"`
// WardrobeAccentVal is the Wardrobe's highlight colour, packed 0xRRGGBB; 0 is the default blue.
WardrobeAccentVal int `json:"wardrobeAccent,omitempty"`
CustomChromeHex [7]string `json:"customChrome"`
Expand Down
3 changes: 3 additions & 0 deletions internal/config/settings_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions internal/courtroom/courtroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ const (
// missingSpritesCap bounds the 404'd base set (hard rule 4); past it SpriteWaitTimeout bounds any unrecorded miss.
// sequentialWaitLimit releases a strict-sequencing hold: a transient error or dropped warning never settles an asset.
sequentialWaitLimit = 10 * time.Second
missingSpritesCap = 512
// waitReaskInterval is how often a hold asks again for what it still waits on: an answer lost on the way, such as a
// dropped missing warning, would otherwise keep the message until the limit.
waitReaskInterval = time.Second
missingSpritesCap = 512
// missingDesksCap bounds the 404'd desk set (hard rule 4); past it the render-side release covers the layer.
missingDesksCap = 512
// blipVolumeFull is the unattenuated blip scale, used with no BlipVolumeFor callback.
Expand Down Expand Up @@ -324,9 +327,11 @@ type Courtroom struct {
// CatchUpLinger holds each fast-forwarded message before the next; default zero drains one per frame.
CatchUpLinger time.Duration

// waitLeft counts down an armed hold; waitFor is its message by pointer, so a new head re-arms.
waitLeft time.Duration
waitFor *protocol.ChatMessage
// waitLeft counts down an armed hold, and waitAskLeft to its next ask; waitFor is its message by pointer, so a new
// head re-arms.
waitLeft time.Duration
waitAskLeft time.Duration
waitFor *protocol.ChatMessage
// waitBlocker is the need holding the message, needs messageNeeds' reused list.
waitBlocker messageNeed
needs []messageNeed
Expand Down Expand Up @@ -591,7 +596,10 @@ func (c *Courtroom) waitHolds(msg *protocol.ChatMessage, behind int, dt time.Dur
// This runs every frame of a hold: the need that stopped the last check is one lookup, the full list 174 allocations.
// A release is always decided on a fresh list.
armed := c.waitFor == msg
if !armed || c.needSettled(c.waitBlocker) {
if armed {
c.waitAskLeft -= dt
}
if !armed || c.needSettled(c.waitBlocker) || c.waitAskLeft <= 0 {
needs := c.messageNeeds(msg)
blocker, blocked := c.firstUnsettled(needs)
if !blocked {
Expand All @@ -609,6 +617,7 @@ func (c *Courtroom) waitHolds(msg *protocol.ChatMessage, behind int, dt time.Dur
// Every fresh list, not only the first: a need that joins once char.ini lands (chat skin, blip) has no other
// requester until begin, which runs only after the gate releases.
c.requestNeeds(needs)
c.waitAskLeft = waitReaskInterval
}
c.waitLeft -= dt
if c.waitLeft <= 0 {
Expand Down
39 changes: 39 additions & 0 deletions internal/courtroom/messageneeds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,42 @@ func TestHeldNeedCoversTheHeldMessageOnly(t *testing.T) {
t.Error("a released message still counts as held")
}
}

// TestWaitGateAsksAgainWhileItHolds: a missing warning that never reaches the gate (a full hand-off drops it) leaves the
// blocker unsettled, so the hold asks again, which re-sends the warning, instead of waiting out the limit.
func TestWaitGateAsksAgainWhileItHolds(t *testing.T) {
const (
tick = 16 * time.Millisecond
wait = 5 * time.Second
)
room, _ := recordingRoom(t)
room.SequentialWait = true
room.Assets = assetFake{sprite: func(string) bool { return false }}
idle := room.urls.Emote("Phoenix", "normal", EmoteIdle)
warnedFor := func() bool {
deadline := time.After(wait)
for {
select {
case w := <-room.mgr.Warnings():
if w.Base == idle {
return true
}
case <-deadline:
return false
}
}
}

room.HandleEvent(Event{Kind: EventMessage, Message: waitMsg("Phoenix", "normal", "hi")})
room.Update(tick)
if !warnedFor() {
t.Fatal("the first ask never reported the sprite missing")
}
room.Update(waitReaskInterval) // the warning was lost: nothing settled the sprite
if room.QueueLen() != 1 {
t.Fatal("the message stopped waiting before the limit")
}
if !warnedFor() {
t.Fatal("the hold never asked again, so the lost warning was never sent again")
}
}
2 changes: 1 addition & 1 deletion internal/render/prescale.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func scaledPixels(n int32, s float32) int32 {

// drawScaled copies frame of page to dst through sf, a pre-scaled copy, and reports whether it drew; false leaves the
// draw to the caller (not SDL's software renderer, or too big to keep). src carries the colour mod to bake in, and
// page's applyScaleMode must have run, as its remembered filter is the one baked in. alpha fades the copy.
// page's ApplyScaleMode must have run, as its remembered filter is the one baked in. alpha fades the copy.
func (v *Viewport) drawScaled(ren *sdl.Renderer, sf *scaledFrame, page *TexturePage, frame int, src *sdl.Texture,
dst sdl.Rect, flip sdl.RendererFlip, modR, modG, modB, alpha uint8) bool {
if !v.prescale {
Expand Down
22 changes: 11 additions & 11 deletions internal/render/previewwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,27 +303,27 @@ func (p *PreviewWindow) Present() {
_ = p.ren.Clear()
if p.tex != nil {
winW, winH := p.win.GetSize()
dst := previewFitRect(winW, winH, p.texW, p.texH)
dst := FitRect(sdl.Rect{W: winW, H: winH}, p.texW, p.texH)
_ = p.ren.Copy(p.tex, nil, &dst)
}
_ = p.ren.Present() // the preview has its own renderer; a failed present only drops a preview frame
}

// previewFitRect fits src in win on both axes, centred, as the window can be narrower than the art.
func previewFitRect(winW, winH, srcW, srcH int32) sdl.Rect {
if winW <= 0 || winH <= 0 || srcW <= 0 || srcH <= 0 {
return sdl.Rect{W: winW, H: winH}
// FitRect fits src in box on both axes, centred, keeping its aspect; a source with no size fills the box.
func FitRect(box sdl.Rect, srcW, srcH int32) sdl.Rect {
if box.W <= 0 || box.H <= 0 || srcW <= 0 || srcH <= 0 {
return box
}
// Cross-multiply to compare the ratios using only integer math.
var w, h int32
if int64(winW)*int64(srcH) <= int64(winH)*int64(srcW) {
w = winW
h = int32((int64(srcH)*int64(winW) + int64(srcW)/2) / int64(srcW)) // rounded, not truncated
if int64(box.W)*int64(srcH) <= int64(box.H)*int64(srcW) {
w = box.W
h = int32((int64(srcH)*int64(box.W) + int64(srcW)/2) / int64(srcW)) // rounded, not truncated
} else {
h = winH
w = int32((int64(srcW)*int64(winH) + int64(srcH)/2) / int64(srcH))
h = box.H
w = int32((int64(srcW)*int64(box.H) + int64(srcH)/2) / int64(srcH))
}
return sdl.Rect{X: (winW - w) / 2, Y: (winH - h) / 2, W: w, H: h}
return sdl.Rect{X: box.X + (box.W-w)/2, Y: box.Y + (box.H-h)/2, W: w, H: h}
}

// HandleEvent handles this window's events; the caller routes by window id, so no id check is needed.
Expand Down
26 changes: 18 additions & 8 deletions internal/render/previewwindow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,9 @@ func TestClampPositionToDisplaysRecentresAnUnreachablePosition(t *testing.T) {
}
}

// --- previewFitRect (aspect-preserving, non-stretch Present) -----------------
// --- FitRect (aspect-preserving, non-stretch Present) -----------------

func TestPreviewFitRectPreservesAspectAndCentres(t *testing.T) {
func TestFitRectPreservesAspectAndCentres(t *testing.T) {
cases := []struct {
name string
winW, winH, srcW, srcH int32
Expand All @@ -586,9 +586,9 @@ func TestPreviewFitRectPreservesAspectAndCentres(t *testing.T) {
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := previewFitRect(c.winW, c.winH, c.srcW, c.srcH)
got := FitRect(sdl.Rect{W: c.winW, H: c.winH}, c.srcW, c.srcH)
if got.W != c.wantW || got.H != c.wantH {
t.Fatalf("previewFitRect(%d,%d,%d,%d) size = %dx%d, want %dx%d",
t.Fatalf("FitRect(%d,%d,%d,%d) size = %dx%d, want %dx%d",
c.winW, c.winH, c.srcW, c.srcH, got.W, got.H, c.wantW, c.wantH)
}
// Centred on both axes.
Expand All @@ -602,18 +602,28 @@ func TestPreviewFitRectPreservesAspectAndCentres(t *testing.T) {
}
}

// TestPreviewFitRectDegenerateInputsFillTheWindow pins the fallback: a
// TestFitRectDegenerateInputsFillTheWindow pins the fallback: a
// source with no usable size must not divide by zero or return a
// zero-area rect nobody can see — it fills the window instead.
func TestPreviewFitRectDegenerateInputsFillTheWindow(t *testing.T) {
func TestFitRectDegenerateInputsFillTheWindow(t *testing.T) {
for _, c := range []struct{ srcW, srcH int32 }{{0, 100}, {100, 0}, {-5, 100}} {
got := previewFitRect(320, 240, c.srcW, c.srcH)
got := FitRect(sdl.Rect{W: 320, H: 240}, c.srcW, c.srcH)
if got.W != 320 || got.H != 240 {
t.Errorf("previewFitRect(320,240,%d,%d) = %+v, want the whole 320x240 window", c.srcW, c.srcH, got)
t.Errorf("FitRect(320,240,%d,%d) = %+v, want the whole 320x240 window", c.srcW, c.srcH, got)
}
}
}

// TestFitRectKeepsTheBoxOffset: a square icon in the effects dropdown's 23x28 cell keeps its shape, inside the cell (#77).
func TestFitRectKeepsTheBoxOffset(t *testing.T) {
const cellX, cellY, cellW, cellH, iconSide = 40, 60, 23, 28, 64
got := FitRect(sdl.Rect{X: cellX, Y: cellY, W: cellW, H: cellH}, iconSide, iconSide)
want := sdl.Rect{X: cellX, Y: cellY + (cellH-cellW)/2, W: cellW, H: cellW}
if got != want {
t.Errorf("FitRect = %+v, want %+v", got, want)
}
}

func TestPreviewWindowOpenFailureStaysClosed(t *testing.T) {
want := errors.New("no window")
pw := NewPreviewWindow(func(string, int32, int32) (*sdl.Window, *sdl.Renderer, error) { return nil, nil, want })
Expand Down
10 changes: 5 additions & 5 deletions internal/render/scalemode.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func setTextureScaleMode(tex *sdl.Texture, mode sdl.ScaleMode) bool {
return tex != nil && tex.SetScaleMode(mode) == nil
}

// spriteScaleMode applies AO2's rule (animationlayer.cpp:229-273): smooth, but point-sampled when enlarged.
func (v *Viewport) spriteScaleMode(layer *courtroom.SpriteLayer, nativeH, targetH int32) sdl.ScaleMode {
switch courtroom.ResolveScalingMode(v.fx.UserScaling, layer.Scaling) {
// SpriteScaleMode applies AO2's rule (animationlayer.cpp:229-273): smooth, but point-sampled when enlarged.
func SpriteScaleMode(user, char courtroom.ScalingMode, nativeH, targetH int32) sdl.ScaleMode {
switch courtroom.ResolveScalingMode(user, char) {
case courtroom.ScalingPixel:
return sdl.ScaleModeNearest
case courtroom.ScalingSmooth:
Expand All @@ -26,8 +26,8 @@ func (v *Viewport) spriteScaleMode(layer *courtroom.SpriteLayer, nativeH, target
return sdl.ScaleModeLinear
}

// applyScaleMode sets every frame's filter, memoised so the steady state does no cgo; render thread only.
func (p *TexturePage) applyScaleMode(ren *sdl.Renderer, mode sdl.ScaleMode) {
// ApplyScaleMode sets every frame's filter, memoised so the steady state does no cgo; render thread only.
func (p *TexturePage) ApplyScaleMode(ren *sdl.Renderer, mode sdl.ScaleMode) {
if p == nil || ren == nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/render/scenery.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (v *Viewport) drawScenery(ren *sdl.Renderer, scene *courtroom.Scene, base s
return
}
v.fillRect = sceneryRect(vp, page.W, page.H, scene.SceneryStretch, v.panorama, v.origin)
page.applyScaleMode(ren, sceneryScaleMode(v.fx.UserScaling, scene.SceneryScaling, scene.SceneryStretch, page.H, v.fillRect.H))
page.ApplyScaleMode(ren, sceneryScaleMode(v.fx.UserScaling, scene.SceneryScaling, scene.SceneryStretch, page.H, v.fillRect.H))
tex := page.Frames[frame]
d := uint8(channelMax)
if dimPct < percentMax {
Expand Down
Loading
Loading