Skip to content

Commit

Permalink
(#13) More code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed May 28, 2023
1 parent fb7651e commit 8cf6f76
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
22 changes: 18 additions & 4 deletions O21.Game/Loading/LoadingLoop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ open O21.Game.U95
type private CustomSynchronizationContext(queue: ConcurrentQueue<unit -> unit>) =
inherit SynchronizationContext()

override this.CreateCopy() = CustomSynchronizationContext(queue)
override this.Post(callback, state) = queue.Enqueue(fun () -> callback.Invoke state)
override this.Send(_, _) = failwith "Cannot use synchronous send on custom context."
let mutable locker = obj()
let mutable isDisposed = false

override this.CreateCopy() = this
override this.Post(callback, state) =
lock locker (fun () ->
if isDisposed then failwith $"{this} has been disposed and cannot accept any more tasks."
queue.Enqueue(fun () -> callback.Invoke state)
)
override this.Send(_, _) = failwith "It's forbidden to use synchronous send on custom context."

interface IDisposable with
member this.Dispose() =
lock locker (fun () -> isDisposed <- true)
let count = queue.Count
if count > 0 then
failwith $"{this}'s queue contains {count} tasks after dispose. These tasks will never be processed."

type private Result<'a> =
| Success of 'a
Expand All @@ -35,7 +49,7 @@ with

let private processWithPumping(scene: ILoadingScene<_, _>, input) =
let queue = ConcurrentQueue()
let context = CustomSynchronizationContext(queue)
use context = new CustomSynchronizationContext(queue)
let prevContext = SynchronizationContext.Current
try
SynchronizationContext.SetSynchronizationContext context
Expand Down
3 changes: 1 addition & 2 deletions O21.Game/Loading/LoadingSceneBase.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ type LoadingSceneBase<'Output>(config: Config) =
let font = content.UiFontRegular
let fontSize = 24f

let mutable progressString = (loadingProgress * 100.0).ToString("##", CultureInfo.InvariantCulture)
if progressString = "" then progressString <- "00"
let progressString = (loadingProgress * 100.0).ToString("00", CultureInfo.InvariantCulture)

let text = $"{loadingStatus} {progressString}%%"
let textRect = MeasureTextEx(font, text, fontSize, 0f)
Expand Down
5 changes: 2 additions & 3 deletions O21.Game/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ let main(args: string[]): int =
}

RaylibEnvironment.Run(config, fun () ->
match LoadingLoop.Run config with
| Some(content, data) -> GameLoop.Run(content, data)
| None -> ()
LoadingLoop.Run config
|> Option.iter GameLoop.Run
)

| _ -> printfn "Usage:\nexport <inputFile> <outDir>: export resources\n<dataDir>: start the game"
Expand Down
4 changes: 2 additions & 2 deletions O21.Game/U95/U95Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ type U95Data private (sprites: Sprites, sounds: Map<SoundType, Sound>, help: Lan
MarkdownHelp.Load hlpFilePath markdownFilePath

loadController.ReportProgress(translation.LoadingData, 0.6)
let! sounds = Async.AwaitTask <| Sound.Load directory
let! sounds = Sound.Load directory

loadController.ReportProgress(translation.LoadingData, 0.8)
let! level = Async.AwaitTask <| Level.Load directory 1 2
let! level = Level.Load directory 1 2

loadController.ReportProgress(translation.CatchingUp, 1.0)
return new U95Data(sprites, sounds, help, [| level |])
Expand Down

0 comments on commit 8cf6f76

Please sign in to comment.