Cache managed runtime clients by runtime id
This commit is contained in:
parent
82596467ac
commit
5d03a64ddb
4 changed files with 64 additions and 79 deletions
|
|
@ -3,15 +3,15 @@
|
|||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using GameCult.Aetheria.State.Verse;
|
||||
using UnityEngine;
|
||||
|
||||
public static class AetheriaUnityRuntimeClientProvider
|
||||
{
|
||||
private static AetheriaClient _runtimeClient;
|
||||
private static string _runtimeClientStatePath;
|
||||
private static string _runtimeClientRuntimeId;
|
||||
private static readonly Dictionary<string, AetheriaClient> RuntimeClients =
|
||||
new Dictionary<string, AetheriaClient>(StringComparer.Ordinal);
|
||||
private static RuntimePlayerSettings _runtimePlayerSettings;
|
||||
|
||||
public static RuntimePlayerSettings PlayerSettings =>
|
||||
|
|
@ -20,15 +20,11 @@ public static class AetheriaUnityRuntimeClientProvider
|
|||
public static AetheriaClient ResolveClient(string stateFilePath, string runtimeId = "")
|
||||
{
|
||||
var effectiveRuntimeId = string.IsNullOrWhiteSpace(runtimeId) ? "raven-unity" : runtimeId;
|
||||
if (_runtimeClient != null &&
|
||||
string.Equals(_runtimeClientStatePath, stateFilePath, StringComparison.Ordinal) &&
|
||||
string.Equals(_runtimeClientRuntimeId, effectiveRuntimeId, StringComparison.Ordinal))
|
||||
{
|
||||
return _runtimeClient;
|
||||
}
|
||||
var cacheKey = CacheKey(stateFilePath, effectiveRuntimeId);
|
||||
if (RuntimeClients.TryGetValue(cacheKey, out var runtimeClient))
|
||||
return runtimeClient;
|
||||
|
||||
Dispose();
|
||||
_runtimeClient = AetheriaClient
|
||||
runtimeClient = AetheriaClient
|
||||
.OpenAsync(
|
||||
stateFilePath,
|
||||
effectiveRuntimeId,
|
||||
|
|
@ -37,9 +33,8 @@ public static class AetheriaUnityRuntimeClientProvider
|
|||
pullOnOpen: true)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
_runtimeClientStatePath = stateFilePath;
|
||||
_runtimeClientRuntimeId = effectiveRuntimeId;
|
||||
return _runtimeClient;
|
||||
RuntimeClients[cacheKey] = runtimeClient;
|
||||
return runtimeClient;
|
||||
}
|
||||
|
||||
public static AetheriaClient ResolveClient(AetheriaRuntimeStateBootReport stateBoot, string runtimeId = "")
|
||||
|
|
@ -54,18 +49,30 @@ public static class AetheriaUnityRuntimeClientProvider
|
|||
|
||||
public static AetheriaClient CurrentClientForStateFile(string stateFilePath)
|
||||
{
|
||||
return _runtimeClient != null &&
|
||||
string.Equals(_runtimeClientStatePath, stateFilePath, StringComparison.Ordinal)
|
||||
? _runtimeClient
|
||||
: null;
|
||||
foreach (var pair in RuntimeClients)
|
||||
{
|
||||
if (pair.Key.StartsWith(StatePathCachePrefix(stateFilePath), StringComparison.Ordinal))
|
||||
return pair.Value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void Dispose()
|
||||
{
|
||||
_runtimeClient?.Dispose();
|
||||
_runtimeClient = null;
|
||||
_runtimeClientStatePath = null;
|
||||
_runtimeClientRuntimeId = null;
|
||||
foreach (var client in RuntimeClients.Values)
|
||||
client.Dispose();
|
||||
RuntimeClients.Clear();
|
||||
}
|
||||
|
||||
private static string CacheKey(string stateFilePath, string runtimeId)
|
||||
{
|
||||
return StatePathCachePrefix(stateFilePath) + runtimeId;
|
||||
}
|
||||
|
||||
private static string StatePathCachePrefix(string stateFilePath)
|
||||
{
|
||||
return (stateFilePath ?? "") + "\n";
|
||||
}
|
||||
|
||||
private static RuntimePlayerSettings LoadPlayerSettings()
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ using System;
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using GameCult.Aetheria.State.Verse;
|
||||
using TMPro;
|
||||
|
|
@ -69,7 +68,6 @@ public class SchematicDisplay : MonoBehaviour
|
|||
private EquippedCargoBay[] _cargoBays;
|
||||
private SchematicDisplayItem[] _schematicItems;
|
||||
private AetherDrive _aetherDrive;
|
||||
private AetheriaClient _client;
|
||||
private string _clientStatePath = "";
|
||||
private AetheriaRuntimeCatalogSnapshot _catalog;
|
||||
private AetheriaRuntimePlayerSettingsDocument _playerSettings;
|
||||
|
|
@ -388,22 +386,14 @@ public class SchematicDisplay : MonoBehaviour
|
|||
|
||||
private AetheriaClient ResolveClient()
|
||||
{
|
||||
var gameDataDirectory = new DirectoryInfo(Path.Combine(Application.dataPath, "..", "GameData"));
|
||||
var stateBoot = AetheriaRuntimeStateBoot.Inspect(gameDataDirectory);
|
||||
if (_client != null && string.Equals(_clientStatePath, stateBoot.StateFilePath, StringComparison.Ordinal))
|
||||
return _client;
|
||||
var stateBoot = AetheriaRuntimeStateBoot.Inspect(AetheriaUnityRuntimePaths.GameDataDirectory);
|
||||
if (!string.Equals(_clientStatePath, stateBoot.StateFilePath, StringComparison.Ordinal))
|
||||
{
|
||||
_clientStatePath = stateBoot.StateFilePath;
|
||||
ClearClientCaches();
|
||||
}
|
||||
|
||||
DisposeClient();
|
||||
_client = AetheriaClient
|
||||
.OpenLocalAsync(
|
||||
gameDataDirectory,
|
||||
"unity-schematic-display",
|
||||
"local",
|
||||
pullOnOpen: true)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
_clientStatePath = stateBoot.StateFilePath;
|
||||
return _client;
|
||||
return AetheriaUnityRuntimeClientProvider.ResolveClient(stateBoot, "unity-schematic-display");
|
||||
}
|
||||
|
||||
private string FormatValue(float value)
|
||||
|
|
@ -434,19 +424,11 @@ public class SchematicDisplay : MonoBehaviour
|
|||
return $"{FormatValue(value - 273.15f)} C";
|
||||
}
|
||||
|
||||
private void DisposeClient()
|
||||
private void ClearClientCaches()
|
||||
{
|
||||
_client?.Dispose();
|
||||
_client = null;
|
||||
_clientStatePath = "";
|
||||
_catalog = null;
|
||||
_playerSettings = null;
|
||||
_currentEntityDocument = null;
|
||||
_currentEntityDocumentReadTime = float.NegativeInfinity;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
DisposeClient();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ using System;
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using GameCult.Aetheria.EveRuntime;
|
||||
using GameCult.Aetheria.State.Verse;
|
||||
|
|
@ -46,7 +45,6 @@ public class SectorRenderer : MonoBehaviour, IBeginDragHandler, IDragHandler, IS
|
|||
private float _sectorBackgroundDepth;
|
||||
private float _sectorCameraDepth;
|
||||
private UIDocument _zoneDetailsSurfaceDocument;
|
||||
private AetheriaClient _client;
|
||||
private string _clientStatePath = "";
|
||||
private AetheriaRuntimeCatalogSnapshot _catalog;
|
||||
private AetheriaRuntimePlayerSettingsDocument _playerSettings;
|
||||
|
|
@ -275,31 +273,18 @@ public class SectorRenderer : MonoBehaviour, IBeginDragHandler, IDragHandler, IS
|
|||
|
||||
private AetheriaClient ResolveClient()
|
||||
{
|
||||
var gameDataDirectory = new DirectoryInfo(Path.Combine(Application.dataPath, "..", "GameData"));
|
||||
var stateBoot = AetheriaRuntimeStateBoot.Inspect(gameDataDirectory);
|
||||
if (_client != null && string.Equals(_clientStatePath, stateBoot.StateFilePath, StringComparison.Ordinal))
|
||||
return _client;
|
||||
var stateBoot = AetheriaRuntimeStateBoot.Inspect(AetheriaUnityRuntimePaths.GameDataDirectory);
|
||||
if (!string.Equals(_clientStatePath, stateBoot.StateFilePath, StringComparison.Ordinal))
|
||||
{
|
||||
_clientStatePath = stateBoot.StateFilePath;
|
||||
ClearClientCaches();
|
||||
}
|
||||
|
||||
DisposeClient();
|
||||
_client = AetheriaClient
|
||||
.OpenLocalAsync(
|
||||
gameDataDirectory,
|
||||
"unity-sector-renderer",
|
||||
"local",
|
||||
pullOnOpen: true)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
_clientStatePath = stateBoot.StateFilePath;
|
||||
_catalog = null;
|
||||
_playerSettings = null;
|
||||
return _client;
|
||||
return AetheriaUnityRuntimeClientProvider.ResolveClient(stateBoot, "unity-sector-renderer");
|
||||
}
|
||||
|
||||
private void DisposeClient()
|
||||
private void ClearClientCaches()
|
||||
{
|
||||
_client?.Dispose();
|
||||
_client = null;
|
||||
_clientStatePath = "";
|
||||
_catalog = null;
|
||||
_playerSettings = null;
|
||||
}
|
||||
|
|
@ -350,8 +335,6 @@ public class SectorRenderer : MonoBehaviour, IBeginDragHandler, IDragHandler, IS
|
|||
|
||||
private void OnDestroy()
|
||||
{
|
||||
DisposeClient();
|
||||
|
||||
if (_zoneDetailsSurfaceDocument != null)
|
||||
{
|
||||
AetheriaEveUnitySurfaceHost.DestroyDocument(_zoneDetailsSurfaceDocument);
|
||||
|
|
|
|||
Loading…
Reference in a new issue