Decompiled source of MoonsEqualised v2.0.3

BepInEx\plugins\MoonsEqualised\MoonsEqualised.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MoonsEqualised")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MoonsEqualised")]
[assembly: AssemblyTitle("MoonsEqualised")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MoonsEqualised
{
	[BepInPlugin("jakov.moonsequalised", "Moons Equalised", "2.0.3")]
	public class Plugin : BaseUnityPlugin
	{
		private sealed class MoonSettings
		{
			public int MinScrap { get; set; }

			public int MaxScrap { get; set; }

			public int MinInsidePower { get; set; }

			public int MaxInsidePower { get; set; }

			public int MinOutsidePower { get; set; }

			public int MaxOutsidePower { get; set; }

			public int FacilityInteriorChance { get; set; }

			public int MineshaftInteriorChance { get; set; }

			public int MansionInteriorChance { get; set; }
		}

		private static readonly Dictionary<string, MoonSettings> VanillaMoonConfig = new Dictionary<string, MoonSettings>(StringComparer.OrdinalIgnoreCase)
		{
			["Experimentation"] = new MoonSettings
			{
				MinScrap = 12,
				MaxScrap = 14,
				MinInsidePower = 6,
				MaxInsidePower = 6,
				MinOutsidePower = 9,
				MaxOutsidePower = 9,
				FacilityInteriorChance = 50,
				MineshaftInteriorChance = 50,
				MansionInteriorChance = 0
			},
			["Assurance"] = new MoonSettings
			{
				MinScrap = 13,
				MaxScrap = 15,
				MinInsidePower = 6,
				MaxInsidePower = 6,
				MinOutsidePower = 8,
				MaxOutsidePower = 8,
				FacilityInteriorChance = 65,
				MineshaftInteriorChance = 35,
				MansionInteriorChance = 0
			},
			["Vow"] = new MoonSettings
			{
				MinScrap = 12,
				MaxScrap = 14,
				MinInsidePower = 7,
				MaxInsidePower = 7,
				MinOutsidePower = 6,
				MaxOutsidePower = 6,
				FacilityInteriorChance = 50,
				MineshaftInteriorChance = 50,
				MansionInteriorChance = 0
			},
			["Offense"] = new MoonSettings
			{
				MinScrap = 17,
				MaxScrap = 19,
				MinInsidePower = 10,
				MaxInsidePower = 10,
				MinOutsidePower = 8,
				MaxOutsidePower = 8,
				FacilityInteriorChance = 0,
				MineshaftInteriorChance = 100,
				MansionInteriorChance = 0
			},
			["March"] = new MoonSettings
			{
				MinScrap = 16,
				MaxScrap = 16,
				MinInsidePower = 14,
				MaxInsidePower = 14,
				MinOutsidePower = 12,
				MaxOutsidePower = 12,
				FacilityInteriorChance = 85,
				MineshaftInteriorChance = 0,
				MansionInteriorChance = 15
			},
			["Adamance"] = new MoonSettings
			{
				MinScrap = 16,
				MaxScrap = 18,
				MinInsidePower = 13,
				MaxInsidePower = 13,
				MinOutsidePower = 11,
				MaxOutsidePower = 11,
				FacilityInteriorChance = 50,
				MineshaftInteriorChance = 50,
				MansionInteriorChance = 0
			},
			["Rend"] = new MoonSettings
			{
				MinScrap = 25,
				MaxScrap = 29,
				MinInsidePower = 12,
				MaxInsidePower = 12,
				MinOutsidePower = 6,
				MaxOutsidePower = 6,
				FacilityInteriorChance = 0,
				MineshaftInteriorChance = 0,
				MansionInteriorChance = 100
			},
			["Dine"] = new MoonSettings
			{
				MinScrap = 30,
				MaxScrap = 30,
				MinInsidePower = 10,
				MaxInsidePower = 10,
				MinOutsidePower = 9,
				MaxOutsidePower = 9,
				FacilityInteriorChance = 0,
				MineshaftInteriorChance = 20,
				MansionInteriorChance = 80
			},
			["Titan"] = new MoonSettings
			{
				MinScrap = 31,
				MaxScrap = 33,
				MinInsidePower = 16,
				MaxInsidePower = 16,
				MinOutsidePower = 7,
				MaxOutsidePower = 7,
				FacilityInteriorChance = 34,
				MineshaftInteriorChance = 33,
				MansionInteriorChance = 33
			},
			["Artifice"] = new MoonSettings
			{
				MinScrap = 27,
				MaxScrap = 30,
				MinInsidePower = 12,
				MaxInsidePower = 12,
				MinOutsidePower = 13,
				MaxOutsidePower = 13,
				FacilityInteriorChance = 0,
				MineshaftInteriorChance = 40,
				MansionInteriorChance = 60
			}
		};

		private static Harmony? _harmony;

		private static ManualLogSource _log = null;

		private void Awake()
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Expected O, but got Unknown
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Expected O, but got Unknown
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Expected O, but got Unknown
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			if (_log == null)
			{
				_log = Logger.CreateLogSource("Moons Equalised");
			}
			if (_harmony == null)
			{
				_harmony = new Harmony("jakov.moonsequalised");
			}
			HarmonyMethod val = new HarmonyMethod(typeof(Plugin).GetMethod("ApplyVanillaMoonOverrides", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
			HarmonyMethod val2 = new HarmonyMethod(typeof(Plugin).GetMethod("ForceExperimentationDungeonFlowTypes", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
			HarmonyMethod val3 = new HarmonyMethod(typeof(Plugin).GetMethod("ApplyGenerationSeed", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
			HarmonyMethod val4 = new HarmonyMethod(typeof(Plugin).GetMethod("ApplyLevelLoadOverrides", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
			HarmonyMethod val5 = new HarmonyMethod(typeof(Plugin).GetMethod("LogSelectedDungeon", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
			HarmonyMethod val6 = new HarmonyMethod(typeof(Plugin).GetMethod("ApplyGenerationRpcSeed", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
			MethodInfo method = typeof(RoundManager).GetMethod("LoadNewLevel", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			_harmony.Patch((MethodBase)method, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			_harmony.Patch((MethodBase)method, val3, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val4.priority = 0;
			_harmony.Patch((MethodBase)method, val4, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			_harmony.Patch((MethodBase)typeof(RoundManager).GetMethod("Start", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic), (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			_harmony.Patch((MethodBase)typeof(TimeOfDay).GetMethod("Start", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic), (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			_harmony.Patch((MethodBase)typeof(StartOfRound).GetMethod("Awake", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic), (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			try
			{
				MethodInfo method2 = typeof(RoundManager).GetMethod("GenerateNewLevelClientRpc", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (method2 != null)
				{
					_harmony.Patch((MethodBase)method2, val6, val5, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
			}
			catch
			{
			}
			ApplyVanillaMoonOverrides();
			ForceExperimentationDungeonFlowTypes();
			_log.LogInfo((object)"[TEST] Moons Equalised: vanilla-only moon values and power caps applied; Experimentation patch installed.");
		}

		private static void ApplyVanillaMoonOverrides()
		{
			try
			{
				StartOfRound instance = StartOfRound.Instance;
				if ((Object)(object)instance == (Object)null)
				{
					return;
				}
				SelectableLevel[] levels = instance.levels;
				if (levels == null)
				{
					return;
				}
				SelectableLevel[] array = levels;
				foreach (SelectableLevel val in array)
				{
					if (!((Object)(object)val == (Object)null))
					{
						string moonName = GetMoonName(val);
						if (!string.IsNullOrWhiteSpace(moonName) && VanillaMoonConfig.TryGetValue(moonName, out MoonSettings value))
						{
							ApplyScrapSettings(val, value);
							ApplyPowerSettings(val, value);
						}
					}
				}
			}
			catch (Exception arg)
			{
				_log.LogError((object)$"Moons Equalised failed to apply vanilla overrides: {arg}");
			}
		}

		private static void ApplyScrapSettings(object level, MoonSettings settings)
		{
			SetIntByCandidates(level, new string[4] { "minScrap", "minScrapAmount", "minimumScrap", "scrapAmountMin" }, settings.MinScrap);
			SetIntByCandidates(level, new string[4] { "maxScrap", "maxScrapAmount", "maximumScrap", "scrapAmountMax" }, settings.MaxScrap);
			string[] array = new string[4] { "minScrapValue", "maxScrapValue", "scrapValueMin", "scrapValueMax" };
			foreach (string text in array)
			{
				SetIntByCandidates(level, new string[1] { text }, settings.MinScrap);
			}
		}

		private static void ApplyPowerSettings(object level, MoonSettings settings)
		{
			SetIntByCandidates(level, new string[3] { "maxEnemyPowerCount", "enemyPowerCount", "maxInsideEnemyPowerCount" }, settings.MaxInsidePower);
			SetIntByCandidates(level, new string[2] { "maxInsideDiversityPowerCount", "insideDiversityPowerCount" }, settings.MaxInsidePower);
			SetIntByCandidates(level, new string[2] { "maxDaytimeEnemyPowerCount", "maxDaytimeEnemyPower" }, settings.MaxInsidePower);
			SetIntByCandidates(level, new string[3] { "maxOutsideEnemyPowerCount", "outsideEnemyPowerCount", "maxOutsideEnemyPower" }, settings.MaxOutsidePower);
			SetIntByCandidates(level, new string[2] { "maxOutsideDiversityPowerCount", "outsideDiversityPowerCount" }, settings.MaxOutsidePower);
		}

		private static void LogSelectedDungeon()
		{
			try
			{
				RoundManager instance = RoundManager.Instance;
				if ((Object)(object)instance == (Object)null)
				{
					return;
				}
				SelectableLevel val = RoundManager.Instance?.currentLevel;
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				string moonName = GetMoonName(val);
				if (!string.Equals(moonName, "Experimentation", StringComparison.OrdinalIgnoreCase))
				{
					return;
				}
				FieldInfo field = typeof(RoundManager).GetField("currentDungeon", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					object value = field.GetValue(instance);
					if (value != null)
					{
						string flowTypeName = GetFlowTypeName(value);
						_log.LogInfo((object)("[TEST] Experimentation dungeon SELECTED: " + flowTypeName));
					}
				}
			}
			catch (Exception arg)
			{
				_log.LogError((object)$"[TEST] Failed to log selected dungeon: {arg}");
			}
		}

		private static void ApplyGenerationSeed(int randomSeed)
		{
			RoundManager instance = RoundManager.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				SetIntByCandidates(instance, new string[2] { "randomMapSeed", "randomSeed" }, randomSeed);
			}
		}

		private static void ApplyLevelLoadOverrides(int randomSeed, SelectableLevel newLevel)
		{
			if ((Object)(object)newLevel == (Object)null)
			{
				return;
			}
			string moonName = GetMoonName(newLevel);
			if (VanillaMoonConfig.TryGetValue(moonName, out MoonSettings value))
			{
				ApplyScrapSettings(newLevel, value);
				ApplyInteriorProbabilities(newLevel, value, randomSeed);
				if (string.Equals(moonName, "Dine", StringComparison.OrdinalIgnoreCase))
				{
					CopyScrapPool("Rend", newLevel);
				}
			}
		}

		private static void CopyScrapPool(string sourceMoonName, SelectableLevel targetLevel)
		{
			StartOfRound instance = StartOfRound.Instance;
			if (instance?.levels == null)
			{
				return;
			}
			FieldInfo field = typeof(SelectableLevel).GetField("spawnableScrap", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field == null)
			{
				return;
			}
			SelectableLevel[] levels = instance.levels;
			foreach (SelectableLevel val in levels)
			{
				if ((Object)(object)val == (Object)null || !string.Equals(GetMoonName(val), sourceMoonName, StringComparison.OrdinalIgnoreCase))
				{
					continue;
				}
				IEnumerable enumerable = field.GetValue(val) as IEnumerable;
				IList list = Activator.CreateInstance(field.FieldType) as IList;
				if (enumerable == null || list == null)
				{
					break;
				}
				foreach (object item in enumerable)
				{
					list.Add(item);
				}
				field.SetValue(targetLevel, list);
				_log.LogInfo((object)"[SCRAP] Dine: copied Rend spawnable scrap pool while keeping Dine scrap count.");
				break;
			}
		}

		private static void ApplyGenerationRpcSeed(RoundManager __instance, int randomSeed, int levelID, int moldIterations, int moldStartPosition, int[] syncDestroyedMold)
		{
			ApplyGenerationSeed(randomSeed);
			SelectableLevel val = ResolveLevel(__instance, levelID);
			if ((Object)(object)val != (Object)null)
			{
				ApplyLevelLoadOverrides(randomSeed, val);
			}
			_log.LogInfo((object)$"[GENERATION] GenerateNewLevelClientRpc seed={randomSeed} levelID={levelID} moldIterations={moldIterations} moldStartPosition={moldStartPosition} destroyedMold={((syncDestroyedMold != null) ? syncDestroyedMold.Length : 0)}");
		}

		private static SelectableLevel? ResolveLevel(RoundManager roundManager, int levelID)
		{
			SelectableLevel[] array = StartOfRound.Instance?.levels;
			if (array != null)
			{
				SelectableLevel[] array2 = array;
				foreach (SelectableLevel val in array2)
				{
					if ((Object)(object)val != (Object)null && GetIntMemberValue(val, "levelID") == levelID)
					{
						return val;
					}
				}
				if (levelID >= 0 && levelID < array.Length)
				{
					return array[levelID];
				}
			}
			return roundManager.currentLevel;
		}

		private static void ForceExperimentationDungeonFlowTypes()
		{
		}

		private static void ApplyInteriorProbabilities(SelectableLevel level, MoonSettings settings, int randomSeed)
		{
			if (!(typeof(SelectableLevel).GetField("dungeonFlowTypes", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(level) is Array { Length: not 0 } array))
			{
				return;
			}
			Array dungeonFlows = typeof(RoundManager).GetField("dungeonFlowTypes", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(RoundManager.Instance) as Array;
			int[] array2 = new int[array.Length];
			int num = 0;
			for (int i = 0; i < array.Length; i++)
			{
				object value = array.GetValue(i);
				if (value != null)
				{
					value = CloneFlowType(value);
					array.SetValue(value, i);
					int intMemberValue = GetIntMemberValue(value, "id");
					string dungeonFlowName = GetDungeonFlowName(dungeonFlows, intMemberValue);
					int interiorRarity = GetInteriorRarity(dungeonFlowName, intMemberValue, settings);
					array2[i] = Math.Max(0, interiorRarity);
					num += array2[i];
					if (SetToFieldOrProperty(value.GetType(), value, "rarity", interiorRarity))
					{
						array.SetValue(value, i);
					}
					_log.LogInfo((object)$"[INTERIOR] {GetMoonName(level)}: id={intMemberValue} flow={dungeonFlowName} rarity={interiorRarity}");
				}
			}
			if (num == 0)
			{
				_log.LogWarning((object)("[INTERIOR] " + GetMoonName(level) + " has no eligible dungeon flows."));
				return;
			}
			int num2 = new Random(randomSeed).Next(num);
			int num3 = -1;
			for (int j = 0; j < array2.Length; j++)
			{
				num2 -= array2[j];
				if (num2 < 0)
				{
					num3 = j;
					break;
				}
			}
			for (int k = 0; k < array.Length; k++)
			{
				object value2 = array.GetValue(k);
				if (value2 != null)
				{
					SetToFieldOrProperty(value2.GetType(), value2, "rarity", (k == num3) ? 100 : 0);
					array.SetValue(value2, k);
				}
			}
			_log.LogInfo((object)$"[INTERIOR] {GetMoonName(level)} deterministic selection index={num3} seed={randomSeed}");
		}

		private static object CloneFlowType(object flowType)
		{
			if (flowType.GetType().IsValueType)
			{
				return flowType;
			}
			return flowType.GetType().GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic)?.Invoke(flowType, null) ?? flowType;
		}

		private static int GetInteriorRarity(string flowName, int id, MoonSettings settings)
		{
			if (flowName.IndexOf("mineshaft", StringComparison.OrdinalIgnoreCase) >= 0 || flowName.IndexOf("level2", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return settings.MineshaftInteriorChance;
			}
			if (flowName.IndexOf("mansion", StringComparison.OrdinalIgnoreCase) >= 0 || flowName.IndexOf("level3", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return settings.MansionInteriorChance;
			}
			if (flowName.IndexOf("facility", StringComparison.OrdinalIgnoreCase) >= 0 || flowName.IndexOf("level1", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return settings.FacilityInteriorChance;
			}
			return id switch
			{
				0 => settings.FacilityInteriorChance, 
				4 => settings.MineshaftInteriorChance, 
				1 => settings.MansionInteriorChance, 
				_ => 0, 
			};
		}

		private static string GetDungeonFlowName(Array? dungeonFlows, int id)
		{
			if (dungeonFlows == null || id < 0 || id >= dungeonFlows.Length)
			{
				return string.Empty;
			}
			object value = dungeonFlows.GetValue(id);
			if (value == null)
			{
				return string.Empty;
			}
			object memberValue = GetMemberValue(value, "dungeonFlow");
			return (memberValue == null) ? string.Empty : (GetStringMemberValue(memberValue, "name") ?? string.Empty);
		}

		private static object? GetMemberValue(object target, string memberName)
		{
			if (target == null)
			{
				return null;
			}
			Type type = target.GetType();
			FieldInfo field = type.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				return field.GetValue(target);
			}
			return type.GetProperty(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(target);
		}

		private static void LogFlowSnapshot(Array flowTypes, string label)
		{
			if (flowTypes == null)
			{
				_log.LogInfo((object)(label + ": <null>"));
				return;
			}
			List<string> list = new List<string>();
			for (int i = 0; i < flowTypes.Length; i++)
			{
				object value = flowTypes.GetValue(i);
				if (value != null)
				{
					Type type = value.GetType();
					int intMemberValue = GetIntMemberValue(value, "id");
					PropertyInfo propertyInfo = type.GetProperty("rarity", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? type.GetProperty("Rarity", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					int num = ((propertyInfo != null) ? Convert.ToInt32(propertyInfo.GetValue(value)) : (-1));
					string flowTypeName = GetFlowTypeName(value);
					list.Add($"index={i} id={intMemberValue} rarity={num} name={flowTypeName}");
				}
			}
			_log.LogInfo((object)((list.Count == 0) ? (label + ": <empty>") : (label + ": " + string.Join("; ", list))));
		}

		private static string GetFlowTypeName(object flow)
		{
			if (flow == null)
			{
				return string.Empty;
			}
			string text = GetStringMemberValue(flow, "name") ?? GetStringMemberValue(flow, "Name");
			if (!string.IsNullOrWhiteSpace(text))
			{
				return text;
			}
			return GetIntMemberValue(flow, "id").ToString();
		}

		private static string GetMoonName(object level)
		{
			if (level == null)
			{
				return string.Empty;
			}
			Type type = level.GetType();
			string[] array = new string[6] { "PlanetName", "planetName", "NumberlessPlanetName", "numberlessPlanetName", "Name", "name" };
			foreach (string memberName in array)
			{
				string stringMemberValue = GetStringMemberValue(level, memberName);
				if (!string.IsNullOrWhiteSpace(stringMemberValue))
				{
					return NormalizeMoonName(stringMemberValue);
				}
			}
			return string.Empty;
		}

		private static string NormalizeMoonName(string moonName)
		{
			if (string.IsNullOrWhiteSpace(moonName))
			{
				return string.Empty;
			}
			string text = moonName.Trim();
			text = text.Replace("_", " ");
			text = text.Replace("-", " ");
			while (text.Contains("  "))
			{
				text = text.Replace("  ", " ");
			}
			string[] array = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
			if (array.Length > 1 && int.TryParse(array[0], out var _))
			{
				text = string.Join(" ", array, 1, array.Length - 1);
			}
			if (text.StartsWith("Moon ", StringComparison.OrdinalIgnoreCase))
			{
				text = text.Substring("Moon ".Length).Trim();
			}
			return text;
		}

		private static void SetIntByCandidates(object target, string[] candidateNames, int value)
		{
			if (target == null)
			{
				return;
			}
			Type type = target.GetType();
			foreach (string fieldOrPropertyName in candidateNames)
			{
				if (SetToFieldOrProperty(type, target, fieldOrPropertyName, value))
				{
					break;
				}
			}
		}

		private static bool SetToFieldOrProperty(Type type, object target, string fieldOrPropertyName, int value)
		{
			FieldInfo field = type.GetField(fieldOrPropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null && (field.FieldType == typeof(int) || field.FieldType == typeof(short) || field.FieldType == typeof(byte)))
			{
				field.SetValue(target, value);
				return true;
			}
			PropertyInfo property = type.GetProperty(fieldOrPropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null && property.CanWrite)
			{
				Type propertyType = property.PropertyType;
				if (propertyType == typeof(int) || propertyType == typeof(short) || propertyType == typeof(byte))
				{
					property.SetValue(target, value);
					return true;
				}
			}
			return false;
		}

		private static string? GetStringMemberValue(object target, string memberName)
		{
			if (target == null)
			{
				return null;
			}
			Type type = target.GetType();
			FieldInfo field = type.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null && field.FieldType == typeof(string))
			{
				return field.GetValue(target) as string;
			}
			PropertyInfo property = type.GetProperty(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null && property.PropertyType == typeof(string))
			{
				return property.GetValue(target) as string;
			}
			return null;
		}

		private static int GetIntMemberValue(object target, string memberName)
		{
			if (target == null)
			{
				return 0;
			}
			Type type = target.GetType();
			FieldInfo field = type.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				object value = field.GetValue(target);
				if (value != null)
				{
					return Convert.ToInt32(value);
				}
			}
			PropertyInfo property = type.GetProperty(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null)
			{
				object value2 = property.GetValue(target);
				if (value2 != null)
				{
					return Convert.ToInt32(value2);
				}
			}
			return 0;
		}
	}
}