257 lines
6.6 KiB
C#
257 lines
6.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace IdleSpectator;
|
|
|
|
/// <summary>Live asset/task lookup and species-token hygiene. No voice classification.</summary>
|
|
public static class ActivityAssetCatalog
|
|
{
|
|
private static HashSet<string> _liveSpeciesPlaceTokens;
|
|
private static int _liveSpeciesPlaceTokensCount = -1;
|
|
|
|
public static ActorAsset TryGetActorAsset(string speciesId)
|
|
{
|
|
if (string.IsNullOrEmpty(speciesId))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
return AssetManager.actor_library?.get(speciesId.Trim());
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static bool IsLiveActorAssetId(string speciesId)
|
|
{
|
|
if (string.IsNullOrEmpty(speciesId))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
return AssetManager.actor_library != null
|
|
&& AssetManager.actor_library.has(speciesId.Trim());
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static string SpeciesDisplayLabel(ActorAsset asset)
|
|
{
|
|
if (asset == null)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
try
|
|
{
|
|
string translated = asset.getTranslatedName();
|
|
if (!string.IsNullOrEmpty(translated))
|
|
{
|
|
string localeId = asset.getLocaleID() ?? "";
|
|
if (!translated.Equals(localeId, StringComparison.Ordinal)
|
|
&& translated.IndexOf('_') < 0)
|
|
{
|
|
return translated.Trim().ToLowerInvariant();
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Localization may not be ready during early harness setup.
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(asset.name_locale))
|
|
{
|
|
return asset.name_locale.Trim().ToLowerInvariant();
|
|
}
|
|
|
|
return string.IsNullOrEmpty(asset.id) ? "" : asset.id.Replace('_', ' ').Trim();
|
|
}
|
|
|
|
public static string SpeciesDisplayLabel(string speciesId)
|
|
{
|
|
ActorAsset asset = TryGetActorAsset(speciesId);
|
|
return asset != null
|
|
? SpeciesDisplayLabel(asset)
|
|
: (string.IsNullOrEmpty(speciesId) ? "" : speciesId.Replace('_', ' ').Trim());
|
|
}
|
|
|
|
public static List<string> EnumerateLiveActorAssetIds()
|
|
{
|
|
var ids = new List<string>();
|
|
try
|
|
{
|
|
ActorAssetLibrary lib = AssetManager.actor_library;
|
|
if (lib?.list == null)
|
|
{
|
|
return ids;
|
|
}
|
|
|
|
for (int i = 0; i < lib.list.Count; i++)
|
|
{
|
|
ActorAsset asset = lib.list[i];
|
|
if (asset != null && !string.IsNullOrEmpty(asset.id))
|
|
{
|
|
ids.Add(asset.id);
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Library can be unavailable during early boot.
|
|
}
|
|
|
|
return ids;
|
|
}
|
|
|
|
public static List<string> EnumerateLiveStatusIds()
|
|
{
|
|
var ids = new List<string>();
|
|
try
|
|
{
|
|
StatusLibrary lib = AssetManager.status;
|
|
if (lib?.list == null)
|
|
{
|
|
return ids;
|
|
}
|
|
|
|
for (int i = 0; i < lib.list.Count; i++)
|
|
{
|
|
StatusAsset asset = lib.list[i];
|
|
if (asset != null && !string.IsNullOrEmpty(asset.id))
|
|
{
|
|
ids.Add(asset.id);
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Library can be unavailable during early boot.
|
|
}
|
|
|
|
return ids;
|
|
}
|
|
|
|
public static StatusAsset TryGetStatusAsset(string statusId)
|
|
{
|
|
if (string.IsNullOrEmpty(statusId))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
return AssetManager.status?.get(statusId.Trim());
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static List<string> EnumerateLiveTaskIds()
|
|
{
|
|
var ids = new List<string>();
|
|
try
|
|
{
|
|
object lib = typeof(AssetManager).GetField("tasks_actor")?.GetValue(null);
|
|
object listObj = lib?.GetType().GetField("list")?.GetValue(lib)
|
|
?? lib?.GetType().GetProperty("list")?.GetValue(lib, null);
|
|
System.Collections.IList list = listObj as System.Collections.IList;
|
|
if (list == null)
|
|
{
|
|
return ids;
|
|
}
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
object task = list[i];
|
|
object idObj = task?.GetType().GetField("id")?.GetValue(task)
|
|
?? task?.GetType().GetProperty("id")?.GetValue(task, null);
|
|
string id = idObj as string;
|
|
if (!string.IsNullOrEmpty(id))
|
|
{
|
|
ids.Add(id);
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Library can be unavailable during early boot.
|
|
}
|
|
|
|
return ids;
|
|
}
|
|
|
|
public static bool IsLiveSpeciesPlaceToken(string label)
|
|
{
|
|
if (string.IsNullOrEmpty(label))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
EnsureLiveSpeciesPlaceTokens();
|
|
return _liveSpeciesPlaceTokens != null
|
|
&& _liveSpeciesPlaceTokens.Contains(label.Trim());
|
|
}
|
|
|
|
private static void EnsureLiveSpeciesPlaceTokens()
|
|
{
|
|
try
|
|
{
|
|
ActorAssetLibrary lib = AssetManager.actor_library;
|
|
if (lib?.list == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
int count = lib.list.Count;
|
|
if (_liveSpeciesPlaceTokens != null && _liveSpeciesPlaceTokensCount == count)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var tokens = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
ActorAsset asset = lib.list[i];
|
|
if (asset == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Add(tokens, asset.id);
|
|
Add(tokens, asset.kingdom_id_wild);
|
|
Add(tokens, asset.name_locale);
|
|
if (!string.IsNullOrEmpty(asset.id))
|
|
{
|
|
Add(tokens, asset.id.Replace('_', ' '));
|
|
}
|
|
}
|
|
|
|
_liveSpeciesPlaceTokens = tokens;
|
|
_liveSpeciesPlaceTokensCount = count;
|
|
}
|
|
catch
|
|
{
|
|
// Keep prior cache if any.
|
|
}
|
|
}
|
|
|
|
private static void Add(HashSet<string> tokens, string value)
|
|
{
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
tokens.Add(value.Trim());
|
|
}
|
|
}
|
|
}
|