199 lines
5.4 KiB
C#
199 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace FloodSWE.Networking
|
|
{
|
|
[Serializable]
|
|
public sealed class SweBoundaryManifest
|
|
{
|
|
public int schema_version;
|
|
public SweBoundarySource[] sources;
|
|
public SweBoundarySink[] sinks;
|
|
public SweBoundaryTile[] tiles;
|
|
|
|
[NonSerialized] private Dictionary<string, SweBoundaryTile> tileLookup;
|
|
[NonSerialized] private Dictionary<int, SweBoundarySource> sourceLookup;
|
|
[NonSerialized] private Dictionary<int, SweBoundarySink> sinkLookup;
|
|
|
|
public static bool TryLoad(string json, out SweBoundaryManifest manifest)
|
|
{
|
|
manifest = null;
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
manifest = JsonUtility.FromJson<SweBoundaryManifest>(json);
|
|
if (manifest == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
manifest.BuildLookup();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogWarning($"SweBoundaryManifest: failed to parse manifest. {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool TryGetTile(string lod, int tileX, int tileY, out SweBoundaryTile tile)
|
|
{
|
|
tile = null;
|
|
if (tileLookup == null)
|
|
{
|
|
BuildLookup();
|
|
}
|
|
|
|
return tileLookup != null && tileLookup.TryGetValue(MakeTileKey(lod, tileX, tileY), out tile);
|
|
}
|
|
|
|
public bool TryGetSource(int id, out SweBoundarySource source)
|
|
{
|
|
source = null;
|
|
if (sourceLookup == null)
|
|
{
|
|
BuildLookup();
|
|
}
|
|
|
|
return sourceLookup != null && sourceLookup.TryGetValue(id, out source);
|
|
}
|
|
|
|
public bool TryGetSink(int id, out SweBoundarySink sink)
|
|
{
|
|
sink = null;
|
|
if (sinkLookup == null)
|
|
{
|
|
BuildLookup();
|
|
}
|
|
|
|
return sinkLookup != null && sinkLookup.TryGetValue(id, out sink);
|
|
}
|
|
|
|
public static int ParseLod(string lod)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(lod))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
string value = lod.Trim().ToLowerInvariant();
|
|
if (value.StartsWith("lod", StringComparison.Ordinal))
|
|
{
|
|
value = value.Substring(3);
|
|
}
|
|
|
|
return int.TryParse(value, out int parsed) ? parsed : 0;
|
|
}
|
|
|
|
private void BuildLookup()
|
|
{
|
|
tileLookup = new Dictionary<string, SweBoundaryTile>(StringComparer.OrdinalIgnoreCase);
|
|
sourceLookup = new Dictionary<int, SweBoundarySource>();
|
|
sinkLookup = new Dictionary<int, SweBoundarySink>();
|
|
if (tiles == null)
|
|
{
|
|
tileLookup = tileLookup ?? new Dictionary<string, SweBoundaryTile>(StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
|
|
if (tiles != null)
|
|
{
|
|
for (int i = 0; i < tiles.Length; i++)
|
|
{
|
|
SweBoundaryTile tile = tiles[i];
|
|
if (tile == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
tileLookup[MakeTileKey(tile.lod, tile.tile_x, tile.tile_y)] = tile;
|
|
}
|
|
}
|
|
|
|
if (sources != null)
|
|
{
|
|
for (int i = 0; i < sources.Length; i++)
|
|
{
|
|
SweBoundarySource source = sources[i];
|
|
if (source != null)
|
|
{
|
|
sourceLookup[source.id] = source;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (sinks != null)
|
|
{
|
|
for (int i = 0; i < sinks.Length; i++)
|
|
{
|
|
SweBoundarySink sink = sinks[i];
|
|
if (sink != null)
|
|
{
|
|
sinkLookup[sink.id] = sink;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static string MakeTileKey(string lod, int tileX, int tileY)
|
|
{
|
|
return $"{lod}|{tileX}|{tileY}";
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class SweBoundarySource
|
|
{
|
|
public int id;
|
|
public int tile_count;
|
|
public int total_pixels;
|
|
public SweBoundaryParams @params;
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class SweBoundarySink
|
|
{
|
|
public int id;
|
|
public int tile_count;
|
|
public int total_pixels;
|
|
public SweBoundaryParams @params;
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class SweBoundaryTile
|
|
{
|
|
public string lod;
|
|
public int tile_x;
|
|
public int tile_y;
|
|
public SweBoundaryTileIdRef[] source_ids;
|
|
public SweBoundaryTileIdRef[] sink_ids;
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class SweBoundaryParams
|
|
{
|
|
public string name;
|
|
public string mode;
|
|
public float trigger_level_m;
|
|
public float max_outflow_m3s;
|
|
|
|
public bool IsMode(string value)
|
|
{
|
|
return !string.IsNullOrWhiteSpace(mode) &&
|
|
string.Equals(mode.Trim(), value, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public sealed class SweBoundaryTileIdRef
|
|
{
|
|
public int id;
|
|
public int pixels;
|
|
}
|
|
}
|