-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimation.fs
More file actions
46 lines (40 loc) · 1.59 KB
/
Animation.fs
File metadata and controls
46 lines (40 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Animation
open System
open System.IO
open System.Text
open System.Threading.Tasks
// the laziest zstd parser
let private readAnimationFile zstPath =
let zstdReader (stream:Stream) = seq {
use streamReader = new StreamReader(stream, encoding=Encoding.ASCII)
streamReader.ReadLine() |> ignore // header metadata
while not <| streamReader.EndOfStream do
yield streamReader.ReadLine()
}
let scanLines =
seq {
use zstStream = File.OpenRead(zstPath)
use decompressionStream = new ZstdSharp.DecompressionStream(zstStream)
yield! zstdReader decompressionStream
}
scanLines
|> Seq.scan (fun (_, sb:StringBuilder) line ->
if line = "?"
then (ValueSome (sb.Replace("?","",0,1).ToString()), sb.Clear())
else (ValueNone, sb.AppendLine(line))
) (ValueNone, StringBuilder() )
|> Seq.filter (fst >> ValueOption.isSome)
|> Seq.map (fst >> ValueOption.get)
// the laziest broadcast block with the latest frame of BadApple, plays continuously
let private readBadAppleFrames =
let zstPath = Path.Combine("assets", "badapple.zst")
readAnimationFile zstPath |> Seq.toArray
let private badAppleFrames = readBadAppleFrames
let mutable currentBadAppleFrame = 0
let private totalBadAppleFrames = badAppleFrames |> Array.length
backgroundTask {
while true do
currentBadAppleFrame <- (currentBadAppleFrame + 1) % totalBadAppleFrames
do! Task.Delay(TimeSpan.FromMilliseconds(50L))
} |> ignore
let getCurrentBadAppleFrame () = badAppleFrames[currentBadAppleFrame]