Skip to content

Commit

Permalink
Propagate async of objects generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jetelain committed Aug 24, 2024
1 parent b73b0a6 commit a44b7ed
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
5 changes: 2 additions & 3 deletions GameRealisticMap.Arma3/Arma3DemoMapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using GameRealisticMap.ManMade.Places;
using GameRealisticMap.ManMade.Roads;
using GameRealisticMap.Osm;
using GameRealisticMap.Reporting;
using HugeImages.Storage;
using Pmad.ProgressTracking;

Expand Down Expand Up @@ -129,9 +128,9 @@ private void BuildingAdjust(ElevationGrid grid, List<City> places, List<Editable
}


protected override IEnumerable<EditableWrpObject> GetObjects(IProgressScope progress, IArma3MapConfig config, IContext context, Arma3LayerGeneratorCatalog generators, ElevationGrid grid)
protected override async Task<IEnumerable<EditableWrpObject>> GetObjects(IProgressScope progress, IArma3MapConfig config, IContext context, Arma3LayerGeneratorCatalog generators, ElevationGrid grid)
{
return base.GetObjects(progress, config, context, generators, grid)
return (await base.GetObjects(progress, config, context, generators, grid))
.Concat(context.GetData<List<EditableWrpObject>>());
}

Expand Down
19 changes: 10 additions & 9 deletions GameRealisticMap.Arma3/Arma3MapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ protected virtual async Task<IOsmDataSource> LoadOsmData(IProgressScope progress
// Objects + WRP
var wrpBuilder = new WrpCompiler(progress, projectDrive);

var grid = gridTask.Result.Elevation;
var grid = (await gridTask).Elevation;
var size = area.SizeInMeters;

var objects = GetObjects(progress, config, context, generators, grid)
var objects = (await GetObjects(progress, config, context, generators, grid))
.Where(o => IsStrictlyInside(o, size));

wrpBuilder.Write(config, grid, tiles, objects);
Expand All @@ -162,23 +162,24 @@ protected virtual IImagerySource CreateImagerySource(IProgressScope progress, Ar
return new ImagerySource(assets.Materials, progress, projectDrive, config, context);
}

protected virtual IEnumerable<EditableWrpObject> GetObjects(IProgressScope progress, IArma3MapConfig config, IContext context, Arma3LayerGeneratorCatalog generators, ElevationGrid grid)
protected virtual async Task<IEnumerable<EditableWrpObject>> GetObjects(IProgressScope progress, IArma3MapConfig config, IContext context, Arma3LayerGeneratorCatalog generators, ElevationGrid grid)
{
return GenerateObjects(progress, config, context, generators).Select(o => o.ToWrpObject(grid));
return (await GenerateObjects(progress, config, context, generators))
.SelectMany(o => o)
.Select(o => o.ToWrpObject(grid));
}

private IEnumerable<TerrainBuilderObject> GenerateObjects(IProgressScope progress, IArma3MapConfig config, IContext context, Arma3LayerGeneratorCatalog generators)
private async ValueTask<IEnumerable<IEnumerable<TerrainBuilderObject>>> GenerateObjects(IProgressScope progress, IArma3MapConfig config, IContext context, Arma3LayerGeneratorCatalog generators)
{
var result = new ConcurrentQueue<IEnumerable<TerrainBuilderObject>>();

using (var scope = progress.CreateScope("Objects", generators.Generators.Count))
{
Parallel.ForEachAsync(generators.Generators, new ParallelOptions() { CancellationToken = progress.CancellationToken, MaxDegreeOfParallelism = 4 }, async (tb, _) =>
await Parallel.ForEachAsync(generators.Generators, new ParallelOptions() { CancellationToken = progress.CancellationToken, MaxDegreeOfParallelism = 4 }, async (tb, _) =>
{
result.Enqueue(await tb.Generate(config, context, scope));
}).Wait();
});
}
return result.SelectMany(o => o);
return result;
}

private bool IsStrictlyInside(EditableWrpObject o, float size)
Expand Down
5 changes: 2 additions & 3 deletions GameRealisticMap.Arma3/Arma3TerrainBuilderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using GameRealisticMap.ManMade.Roads;
using GameRealisticMap.Osm;
using GameRealisticMap.Preview;
using GameRealisticMap.Reporting;
using HugeImages;
using HugeImages.Processing;
using HugeImages.Storage;
Expand Down Expand Up @@ -66,7 +65,7 @@ private BuildContext CreateBuildContext(IProgressScope progress, Arma3MapConfig
}

#if DEBUG
PreviewRender.RenderHtml(context, "debug.html").Wait();
await PreviewRender.RenderHtml(context, "debug.html");
#endif

// Convert PAA
Expand Down Expand Up @@ -191,7 +190,7 @@ private async Task<List<ImageryPart>> ExportImagery(IProgressScope progress, Arm
{
await WriteImage(progress, idMap, Path.Combine(targetDirectory, "idmap.png"), parts).ConfigureAwait(false);
}
ImageryCompiler.CreateConfigCppImages(projectDrive, config, source);
await ImageryCompiler.CreateConfigCppImages(projectDrive, config, source);
using (var satMap = await source.CreateSatMap())
{
await WriteImage(progress, satMap, Path.Combine(targetDirectory, "satmap.png"), parts).ConfigureAwait(false);
Expand Down
5 changes: 2 additions & 3 deletions GameRealisticMap.Arma3/Demo/Arma3GdtDemoMapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using GameRealisticMap.ManMade.Roads;
using GameRealisticMap.Nature.Weather;
using GameRealisticMap.Osm;
using GameRealisticMap.Reporting;
using HugeImages.Storage;
using Pmad.ProgressTracking;

Expand Down Expand Up @@ -68,9 +67,9 @@ protected override BuildContext CreateBuildContext(IProgressScope progress, Arma
return context;
}

protected override IEnumerable<EditableWrpObject> GetObjects(IProgressScope progress, IArma3MapConfig config, IContext context, Arma3LayerGeneratorCatalog generators, ElevationGrid grid)
protected override Task<IEnumerable<EditableWrpObject>> GetObjects(IProgressScope progress, IArma3MapConfig config, IContext context, Arma3LayerGeneratorCatalog generators, ElevationGrid grid)
{
return new List<EditableWrpObject>();
return Task.FromResult((IEnumerable<EditableWrpObject>) new List<EditableWrpObject>());
}

protected override IImagerySource CreateImagerySource(IProgressScope progress, Arma3MapConfig config, IContext context)
Expand Down

0 comments on commit a44b7ed

Please sign in to comment.