Preserve current AVIF compression, local voice pipelines, practice workflows, UI API migration, locale updates, and packaging work. Native integration validation remains pending.
192 lines
6.3 KiB
Ziran
192 lines
6.3 KiB
Ziran
// Bounded downloadable sessions and their pre-start catalogue.
|
|
#import "json_scan"
|
|
#import "byte_text_linux"
|
|
|
|
AstralCueCapacity :: 32
|
|
|
|
AstralCue :: struct {
|
|
second: s32
|
|
key: [64]u8
|
|
}
|
|
|
|
AstralCourse :: struct {
|
|
// The selected practice ends after its final return and quiet period.
|
|
duration: s32
|
|
count: s32
|
|
cues: [AstralCueCapacity]AstralCue
|
|
}
|
|
|
|
AstralClock :: struct {
|
|
elapsed_seconds: s32
|
|
remainder_ms: s32
|
|
}
|
|
|
|
AstralInteger :: (data: string, at: s32, maximum: s32) -> s32 {
|
|
if at < 0 || at >= cast(s32)data.count || data[at] < cast(u8)48 || data[at] > cast(u8)57 {
|
|
return -1
|
|
}
|
|
number := ReadNumber(data, at)
|
|
if !number.valid || number.value < 0.0 || number.value > cast(float64)maximum ||
|
|
number.value != cast(float64)cast(s32)number.value {
|
|
return -1
|
|
}
|
|
return cast(s32)number.value
|
|
}
|
|
|
|
AstralCourseParse :: (data: string, course: *AstralCourse) -> bool {
|
|
course.* = AstralCourse.{}
|
|
end := ValueEnd(data, 0, 0)
|
|
if data.count > 8192 || end < 0 || Space(data, end) != cast(s32)data.count ||
|
|
AstralInteger(data, FieldAt(data, 0, "schema"), 1) != 1 {
|
|
return false
|
|
}
|
|
duration := AstralInteger(data, FieldAt(data, 0, "duration"), 3600)
|
|
cues := FieldAt(data, 0, "cues")
|
|
count := ArrayCount(data, cues)
|
|
if duration < 60 || count < 2 || count > AstralCueCapacity {
|
|
return false
|
|
}
|
|
parsed: AstralCourse
|
|
parsed.duration = duration
|
|
parsed.count = count
|
|
previous: s32 = -1
|
|
for index: cast(s32)0..count - 1 {
|
|
at := ElementAt(data, cues, index)
|
|
second := AstralInteger(data, FieldAt(data, at, "second"), duration - 1)
|
|
length := ReadStringText(data, FieldAt(data, at, "key"), parsed.cues[index].key[:])
|
|
key := TextFromCString(*parsed.cues[index].key[0])
|
|
if second <= previous || (index == 0 && second != 0) ||
|
|
length <= 12 || length >= 64 || cast(s32)key.count != length || key[:12] != "astral_text_" {
|
|
return false
|
|
}
|
|
for byte: key {
|
|
if !((byte >= cast(u8)97 && byte <= cast(u8)122) ||
|
|
byte == cast(u8)95 || (byte >= cast(u8)48 && byte <= cast(u8)57)) {
|
|
return false
|
|
}
|
|
}
|
|
for earlier: cast(s32)0..index - 1 {
|
|
if key == TextFromCString(*parsed.cues[earlier].key[0]) {
|
|
return false
|
|
}
|
|
}
|
|
parsed.cues[index].second = second
|
|
previous = second
|
|
}
|
|
course.* = parsed
|
|
return true
|
|
}
|
|
|
|
AstralCueAt :: (course: *AstralCourse, elapsed: s32) -> s32 {
|
|
if course.count < 1 {
|
|
return -1
|
|
}
|
|
index: s32 = 0
|
|
while index + 1 < course.count && course.cues[index + 1].second <= elapsed {
|
|
index += 1
|
|
}
|
|
return index
|
|
}
|
|
|
|
AstralAdvance :: (clock: AstralClock, elapsed_ms: s32, paused: bool) -> AstralClock {
|
|
if paused || elapsed_ms <= 0 {
|
|
return clock
|
|
}
|
|
total := cast(s64)clock.remainder_ms + cast(s64)elapsed_ms
|
|
seconds := cast(s64)clock.elapsed_seconds + total / 1000
|
|
if seconds >= 2147483647 {
|
|
return AstralClock.{2147483647, 0}
|
|
}
|
|
return AstralClock.{cast(s32)seconds, cast(s32)(total % 1000)}
|
|
}
|
|
|
|
AstralPractice :: struct {
|
|
id: [64]u8
|
|
duration: s32
|
|
title_key: [64]u8
|
|
description_key: [64]u8
|
|
guide_key: [64]u8
|
|
}
|
|
|
|
AstralCatalogue :: struct {
|
|
count: s32
|
|
practices: [6]AstralPractice
|
|
}
|
|
|
|
AstralIdentifier :: (value: string) -> bool {
|
|
if value.count == 0 || value.count >= 64 {
|
|
return false
|
|
}
|
|
for byte: value {
|
|
if !((byte >= cast(u8)97 && byte <= cast(u8)122) ||
|
|
(byte >= cast(u8)48 && byte <= cast(u8)57) || byte == cast(u8)95) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
AstralCatalogueParse :: (data: string, catalogue: *AstralCatalogue) -> bool {
|
|
catalogue.* = AstralCatalogue.{}
|
|
end := ValueEnd(data, 0, 0)
|
|
if data.count > 8192 || end < 0 || Space(data, end) != cast(s32)data.count ||
|
|
AstralInteger(data, FieldAt(data, 0, "schema"), 1) != 1 {
|
|
return false
|
|
}
|
|
rows := FieldAt(data, 0, "practices")
|
|
count := ArrayCount(data, rows)
|
|
if count != 6 {
|
|
return false
|
|
}
|
|
parsed: AstralCatalogue
|
|
parsed.count = count
|
|
for index: cast(s32)0..count - 1 {
|
|
at := ElementAt(data, rows, index)
|
|
practice: AstralPractice
|
|
id_length := ReadStringText(data, FieldAt(data, at, "id"), practice.id[:])
|
|
title_length := ReadStringText(data, FieldAt(data, at, "title_key"), practice.title_key[:])
|
|
description_length := ReadStringText(data, FieldAt(data, at, "description_key"), practice.description_key[:])
|
|
guide_length := ReadStringText(data, FieldAt(data, at, "guide_key"), practice.guide_key[:])
|
|
if id_length <= 0 || title_length <= 0 || description_length <= 0 || guide_length <= 0 {
|
|
return false
|
|
}
|
|
practice.duration = AstralInteger(data, FieldAt(data, at, "duration"), 3600)
|
|
id := TextFromCString(*practice.id[0])
|
|
if id.count != id_length ||
|
|
TextFromCString(*practice.title_key[0]).count != title_length ||
|
|
TextFromCString(*practice.description_key[0]).count != description_length ||
|
|
TextFromCString(*practice.guide_key[0]).count != guide_length || !AstralIdentifier(id) ||
|
|
!AstralIdentifier(TextFromCString(*practice.title_key[0])) ||
|
|
!AstralIdentifier(TextFromCString(*practice.description_key[0])) ||
|
|
!AstralIdentifier(TextFromCString(*practice.guide_key[0])) || practice.duration < 60 {
|
|
return false
|
|
}
|
|
for earlier: cast(s32)0..index - 1 {
|
|
if id == TextFromCString(*parsed.practices[earlier].id[0]) {
|
|
return false
|
|
}
|
|
}
|
|
parsed.practices[index] = practice
|
|
}
|
|
catalogue.* = parsed
|
|
return true
|
|
}
|
|
|
|
AstralSelectedIndex :: (catalogue: *AstralCatalogue, id: string) -> s32 {
|
|
for index: cast(s32)0..catalogue.count - 1 {
|
|
if TextFromCString(*catalogue.practices[index].id[0]) == id {
|
|
return index
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
AstralAdvanceSession :: (clock: AstralClock, milliseconds: s32, paused: bool,
|
|
duration: s32) -> AstralClock {
|
|
next := AstralAdvance(clock, milliseconds, paused)
|
|
if duration > 0 && next.elapsed_seconds >= duration {
|
|
return AstralClock.{duration, 0}
|
|
}
|
|
return next
|
|
}
|