336 lines
11 KiB
C#
336 lines
11 KiB
C#
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* 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 GameCult.Mesh;
|
|
using UnityEngine;
|
|
|
|
public static class AetheriaUnityRuntimeClientProvider
|
|
{
|
|
private static readonly Dictionary<string, AetheriaClient> RuntimeClients =
|
|
new Dictionary<string, AetheriaClient>(StringComparer.Ordinal);
|
|
private static RuntimePlayerSettings _runtimePlayerSettings;
|
|
private static bool _playerSettingsLoaded;
|
|
|
|
public static RuntimePlayerSettings PlayerSettings
|
|
{
|
|
get
|
|
{
|
|
_runtimePlayerSettings ??= CreateDefaultPlayerSettings();
|
|
RefreshPlayerSettings(_runtimePlayerSettings);
|
|
return _runtimePlayerSettings;
|
|
}
|
|
}
|
|
|
|
private static AetheriaClient ResolveClient(string stateFilePath, string runtimeId = "")
|
|
{
|
|
var effectiveRuntimeId = string.IsNullOrWhiteSpace(runtimeId) ? "raven-unity" : runtimeId;
|
|
var cacheKey = CacheKey(stateFilePath, effectiveRuntimeId);
|
|
if (RuntimeClients.TryGetValue(cacheKey, out var runtimeClient))
|
|
return runtimeClient;
|
|
|
|
runtimeClient = AetheriaClient
|
|
.OpenAsync(
|
|
stateFilePath,
|
|
effectiveRuntimeId,
|
|
"local",
|
|
startServer: false,
|
|
pullOnOpen: true)
|
|
.GetAwaiter()
|
|
.GetResult();
|
|
RuntimeClients[cacheKey] = runtimeClient;
|
|
return runtimeClient;
|
|
}
|
|
|
|
private static AetheriaClient ResolveClient(AetheriaRuntimeStateBootReport stateBoot, string runtimeId = "")
|
|
{
|
|
return ResolveClient(
|
|
stateBoot.StateFilePath,
|
|
string.IsNullOrWhiteSpace(runtimeId) ? stateBoot.RuntimeId : runtimeId);
|
|
}
|
|
|
|
private static AetheriaClient RuntimeClient(string runtimeId = "")
|
|
{
|
|
return ResolveClient(AetheriaUnityRuntimePaths.RuntimeStateFilePath, runtimeId);
|
|
}
|
|
|
|
private static AetheriaClient RuntimeClient(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return ResolveClient(stateBoot, runtimeId);
|
|
}
|
|
|
|
public static AetheriaClientState RuntimeState(string runtimeId = "")
|
|
{
|
|
return RuntimeClient(runtimeId).State;
|
|
}
|
|
|
|
public static AetheriaClientState RuntimeState(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeClient(stateBoot, runtimeId).State;
|
|
}
|
|
|
|
public static AetheriaRuntimeCurrentDockingDocument CurrentDockingState(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentDockingState();
|
|
}
|
|
|
|
public static AetheriaRuntimeCurrentDockingDocument CurrentDockingState(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(stateBoot, runtimeId).CurrentDockingState();
|
|
}
|
|
|
|
public static AetheriaRuntimeCatalogSnapshot CurrentCatalog(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentCatalog();
|
|
}
|
|
|
|
public static AetheriaRuntimeCatalogSnapshot CurrentCatalog(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(stateBoot, runtimeId).CurrentCatalog();
|
|
}
|
|
|
|
public static AetheriaRuntimePlayerSettingsDocument CurrentPlayerSettings(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentPlayerSettings();
|
|
}
|
|
|
|
public static AetheriaRuntimePlayerSettingsDocument CurrentPlayerSettings(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(stateBoot, runtimeId).CurrentPlayerSettings();
|
|
}
|
|
|
|
public static AetheriaRuntimeCurrentEntityDocument CurrentEntityState(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentEntityState();
|
|
}
|
|
|
|
public static AetheriaRuntimeCurrentZoneDocument CurrentZoneState(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentZoneState();
|
|
}
|
|
|
|
public static AetheriaRuntimeZoneContactsDocument CurrentZoneContacts(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentZoneContacts();
|
|
}
|
|
|
|
public static CultMeshReactiveDocument<AetheriaRuntimeZoneContactsDocument> ReactiveZoneContacts(
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).ReactiveZoneContacts();
|
|
}
|
|
|
|
public static AetheriaRuntimeStationRefitDocument CurrentStationRefit(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentStationRefit();
|
|
}
|
|
|
|
public static AetheriaRuntimeSectorMapDocument CurrentSectorMap(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentSectorMap();
|
|
}
|
|
|
|
public static AetheriaRuntimeSectorMapDocument CurrentSectorMap(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(stateBoot, runtimeId).CurrentSectorMap();
|
|
}
|
|
|
|
public static AetheriaRuntimeZoneDetailsDocument CurrentZoneDetails(int zoneIndex, string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentZoneDetails(zoneIndex);
|
|
}
|
|
|
|
public static AetheriaRuntimeInventoryDocument CurrentInventory(int entityIndex, string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentInventory(entityIndex);
|
|
}
|
|
|
|
public static CultMeshReactiveDocument<AetheriaRuntimeObjectsViewportDocument> ReactiveObjectsViewport(
|
|
AetheriaRuntimeRtsViewportBounds viewport,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).ReactiveObjectsViewport(viewport);
|
|
}
|
|
|
|
public static CultMeshReactiveDocument<AetheriaRuntimeRenderSplatsViewportDocument> ReactiveRenderSplatsViewport(
|
|
AetheriaRuntimeRtsViewportBounds viewport,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).ReactiveRenderSplatsViewport(viewport);
|
|
}
|
|
|
|
public static AetheriaRuntimeDaemonFrameDocument CurrentDaemonFrame(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CurrentDaemonFrame();
|
|
}
|
|
|
|
public static AetheriaRuntimeDaemonFrameDocument CurrentDaemonFrame(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(stateBoot, runtimeId).CurrentDaemonFrame();
|
|
}
|
|
|
|
public static AetheriaUnityDaemonRenderDocuments DaemonRenderDocuments(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return AetheriaUnityDaemonRenderDocuments.Open(RuntimeState(stateBoot, runtimeId));
|
|
}
|
|
|
|
public static AetheriaRuntimeVerseHostSettingsDocument CurrentVerseHostSettings(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(stateBoot, runtimeId).CurrentVerseHostSettings();
|
|
}
|
|
|
|
public static AetheriaControl Control(string runtimeId = "")
|
|
{
|
|
return RuntimeClient(runtimeId).Control;
|
|
}
|
|
|
|
public static AetheriaControl Control(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeClient(stateBoot, runtimeId).Control;
|
|
}
|
|
|
|
public static AetheriaUi Ui(string runtimeId = "")
|
|
{
|
|
return RuntimeClient(runtimeId).Ui;
|
|
}
|
|
|
|
public static AetheriaUi Ui(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeClient(stateBoot, runtimeId).Ui;
|
|
}
|
|
|
|
public static CultMeshStateRefResolver EveSurfaceStateRefResolver(string runtimeId = "")
|
|
{
|
|
return RuntimeState(runtimeId).CreateEveSurfaceCultMeshStateRefResolver();
|
|
}
|
|
|
|
public static CultMeshStateRefResolver EveSurfaceStateRefResolver(
|
|
AetheriaRuntimeStateBootReport stateBoot,
|
|
string runtimeId = "")
|
|
{
|
|
return RuntimeState(stateBoot, runtimeId).CreateEveSurfaceCultMeshStateRefResolver();
|
|
}
|
|
|
|
public static void Dispose()
|
|
{
|
|
_runtimePlayerSettings = null;
|
|
_playerSettingsLoaded = false;
|
|
|
|
foreach (var client in RuntimeClients.Values)
|
|
client.Dispose();
|
|
RuntimeClients.Clear();
|
|
}
|
|
|
|
private static string CacheKey(string stateFilePath, string runtimeId)
|
|
{
|
|
return (stateFilePath ?? "") + "\n" + runtimeId;
|
|
}
|
|
|
|
private static void RefreshPlayerSettings(RuntimePlayerSettings settings)
|
|
{
|
|
try
|
|
{
|
|
var stored = CurrentPlayerSettings();
|
|
if (stored == null)
|
|
return;
|
|
|
|
ApplyPlayerSettings(settings, stored);
|
|
if (!_playerSettingsLoaded)
|
|
{
|
|
Debug.Log("Loaded Aetheria Verse player settings from typed state.");
|
|
_playerSettingsLoaded = true;
|
|
}
|
|
}
|
|
catch (FileNotFoundException)
|
|
{
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogWarning($"Failed to load Aetheria Verse player settings from typed state; using defaults: {ex}");
|
|
}
|
|
}
|
|
|
|
private static RuntimePlayerSettings CreateDefaultPlayerSettings()
|
|
{
|
|
var settings = new RuntimePlayerSettings();
|
|
settings.Name = Environment.UserName;
|
|
settings.InputSettings.SetActionBarInputEnabled("<Keyboard>/leftShift", true);
|
|
settings.InputSettings.SetActionBarInputEnabled("<Mouse>/leftButton", true);
|
|
settings.InputSettings.SetActionBarInputEnabled("<Mouse>/rightButton", true);
|
|
settings.InputSettings.SetActionBarInputEnabled("<Mouse>/middleButton", true);
|
|
for (var i = 1; i < 6; i++)
|
|
settings.InputSettings.SetActionBarInputEnabled($"<Keyboard>/{i}", true);
|
|
return settings;
|
|
}
|
|
|
|
private static void ApplyPlayerSettings(
|
|
RuntimePlayerSettings settings,
|
|
AetheriaRuntimePlayerSettingsDocument stored)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(stored.PlayerName))
|
|
settings.Name = stored.PlayerName;
|
|
|
|
settings.TutorialPassed = stored.TutorialPassed;
|
|
|
|
settings.HashedStoryFiles.Clear();
|
|
foreach (var storyFileHash in stored.StoryFileHashes)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(storyFileHash.StoryPath))
|
|
settings.HashedStoryFiles[storyFileHash.StoryPath] = storyFileHash.Hash;
|
|
}
|
|
|
|
if (Enum.TryParse(stored.TemperatureUnit, out TemperatureUnit temperatureUnit))
|
|
settings.GameplaySettings.TemperatureUnit = temperatureUnit;
|
|
settings.GameplaySettings.SignificantDigits = Math.Max(0, stored.SignificantDigits);
|
|
|
|
if (Enum.TryParse(stored.NebulaQuality, out Quality nebulaQuality))
|
|
settings.GraphicsSettings.NebulaQuality = nebulaQuality;
|
|
settings.GraphicsSettings.ShowAsteroidsInMinimap = stored.ShowAsteroidsInMinimap;
|
|
|
|
settings.InputSettings.InputActionMap.Clear();
|
|
foreach (var binding in stored.BindingOverrides)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(binding.ActionName) ||
|
|
string.IsNullOrWhiteSpace(binding.BindingPath) ||
|
|
binding.BindingIndex < 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
settings.InputSettings.SetBindingOverride(binding.ActionName, binding.BindingIndex, binding.BindingPath);
|
|
}
|
|
|
|
settings.InputSettings.ActionBarInputs.Clear();
|
|
foreach (var input in stored.ActionBarInputs)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(input))
|
|
settings.InputSettings.SetActionBarInputEnabled(input, true);
|
|
}
|
|
}
|
|
}
|