@@ -1,5 +1,6 @@
using System.Collections.Generic ;
using System.Reflection.Emit ;
using BepInEx.Configuration ;
using HarmonyLib ;
using UnityEngine ;
using UnityEngine.UI ;
@@ -8,10 +9,11 @@ namespace UniverseGenTweaks;
public class MoreSettings
{
private static float _minDist = 2f ;
private static float _minStep = 2f ;
private static float _max Step = 3.2f ;
private static float _flat ten = 0.18f ;
public static ConfigEntry < int > MaxStarCount ;
private static double _minDist = 2 ;
private static double _min Step = 2 ;
private static double _maxS tep = 3.2 ;
private static double _flatten = 0.18 ;
private static Text _minDistTitle ;
private static Text _minStepTitle ;
@@ -53,9 +55,9 @@ public class MoreSettings
[HarmonyPostfix]
[HarmonyPatch(typeof(UIGalaxySelect), nameof(UIGalaxySelect._OnInit))]
private static void Patch GalaxyUI_OnInit ( UIGalaxySelect __instance )
private static void UI GalaxySelect__OnInit_Postfix ( UIGalaxySelect __instance )
{
__instance . starCountSlider . maxValue = UniverseGenTweaks . MaxStarCount;
__instance . starCountSlider . maxValue = MaxStarCount . Value ;
CreateSliderWithText ( __instance . starCountSlider , out _minDistTitle , out _minDistSlider , out _minDistText ) ;
CreateSliderWithText ( __instance . starCountSlider , out _minStepTitle , out _minStepSlider , out _minStepText ) ;
@@ -65,22 +67,22 @@ public class MoreSettings
_minDistTitle . name = "min-dist" ;
_minDistSlider . minValue = 10f ;
_minDistSlider . maxValue = 50f ;
_minDistSlider . value = _minDist * 10f ;
_minDistSlider . value = ( float ) ( _minDist * 10.0 ) ;
_minStepTitle . name = "min-step" ;
_minStepSlider . minValue = 10f ;
_minStepSlider . maxValue = _maxStep * 10f - 1f ;
_minStepSlider . value = _minStep * 10f ;
_minStepSlider . maxValue = ( float ) ( _maxStep * 10.0 - 1.0 ) ;
_minStepSlider . value = ( float ) ( _minStep * 10.0 ) ;
_maxStepTitle . name = "max-step" ;
_maxStepSlider . minValue = _minStep * 10f + 1f ;
_maxStepSlider . minValue = ( float ) ( _minStep * 10.0 + 1.0 ) ;
_maxStepSlider . maxValue = 100f ;
_maxStepSlider . value = _maxStep * 10f ;
_maxStepSlider . value = ( float ) ( _maxStep * 10.0 ) ;
_flattenTitle . name = "flatten" ;
_flattenSlider . minValue = 1f ;
_flattenSlider . maxValue = 50f ;
_flattenSlider . value = _flatten * 50f ;
_flattenSlider . value = ( float ) ( _flatten * 50.0 ) ;
TransformDeltaY ( _minDistTitle . transform , - 0.3573f ) ;
TransformDeltaY ( _minStepTitle . transform , - 0.3573f * 2 ) ;
@@ -94,7 +96,7 @@ public class MoreSettings
[HarmonyPrefix]
[HarmonyPatch(typeof(UIGalaxySelect), nameof(UIGalaxySelect._OnOpen))]
private static void PatchGalaxyUI_OnOpen ( UIGalaxySelect __instance )
private static void UIGalaxySelect__OnOpen_Prefix ( )
{
_minDistTitle . text = "恒星最小距离" . Translate ( ) ;
_minStepTitle . text = "步进最小距离" . Translate ( ) ;
@@ -108,24 +110,24 @@ public class MoreSettings
[HarmonyPostfix]
[HarmonyPatch(typeof(UIGalaxySelect), nameof(UIGalaxySelect._OnRegEvent))]
private static void Patch GalaxyUI _OnRegEvent( UIGalaxySelect __instance )
private static void UI GalaxySelect_ _OnRegEvent_Postfix ( UIGalaxySelect __instance )
{
_minDistSlider . onValueChanged . RemoveAllListeners ( ) ;
_minDistSlider . onValueChanged . AddListener ( val = >
{
var newVal = Mathf . Round ( val ) / 10f ;
var newVal = Mathf . Round ( val ) / 10.0 ;
if ( newVal . Equals ( _minDist ) ) return ;
_minDist = newVal ;
_minDistText . text = _minDist . ToString ( ) ;
if ( _minStep < _minDist )
{
_minStep = _minDist ;
_minStepSlider . value = _minStep * 10f ;
_minStepSlider . value = ( float ) ( _minStep * 10.0 ) ;
_minStepText . text = _minStep . ToString ( ) ;
if ( _maxStep < _minStep )
{
_maxStep = _minStep ;
_maxStepSlider . value = _maxStep * 10f ;
_maxStepSlider . value = ( float ) ( _maxStep * 10.0 ) ;
_maxStepText . text = _maxStep . ToString ( ) ;
}
}
@@ -134,27 +136,27 @@ public class MoreSettings
_minStepSlider . onValueChanged . RemoveAllListeners ( ) ;
_minStepSlider . onValueChanged . AddListener ( val = >
{
var newVal = Mathf . Round ( val ) / 10f ;
var newVal = Mathf . Round ( val ) / 10.0 ;
if ( newVal . Equals ( _minStep ) ) return ;
_minStep = newVal ;
_maxStepSlider . minValue = newVal * 10f ;
_maxStepSlider . minValue = ( float ) ( newVal * 10.0 ) ;
_minStepText . text = _minStep . ToString ( ) ;
__instance . SetStarmapGalaxy ( ) ;
} ) ;
_maxStepSlider . onValueChanged . RemoveAllListeners ( ) ;
_maxStepSlider . onValueChanged . AddListener ( val = >
{
var newVal = Mathf . Round ( val ) / 10f ;
var newVal = Mathf . Round ( val ) / 10.0 ;
if ( newVal . Equals ( _maxStep ) ) return ;
_maxStep = newVal ;
_minStepSlider . maxValue = newVal * 10f ;
_minStepSlider . maxValue = ( float ) ( newVal * 10.0 ) ;
_maxStepText . text = _maxStep . ToString ( ) ;
__instance . SetStarmapGalaxy ( ) ;
} ) ;
_flattenSlider . onValueChanged . RemoveAllListeners ( ) ;
_flattenSlider . onValueChanged . AddListener ( val = >
{
var newVal = Mathf . Round ( val ) / 50f ;
var newVal = Mathf . Round ( val ) / 50.0 ;
if ( newVal . Equals ( _flatten ) ) return ;
_flatten = newVal ;
_flattenText . text = _flatten . ToString ( ) ;
@@ -164,80 +166,75 @@ public class MoreSettings
[HarmonyPostfix]
[HarmonyPatch(typeof(UIGalaxySelect), nameof(UIGalaxySelect._OnUnregEvent))]
private static void Patch GalaxyUI _OnUnregEvent(UIGalaxySelect __instance )
private static void UI GalaxySelect_ _OnUnregEvent_Postfix ( )
{
_minDistSlider . onValueChanged . RemoveAllListeners ( ) ;
_minStepSlider . onValueChanged . RemoveAllListeners ( ) ;
_maxStepSlider . onValueChanged . RemoveAllListeners ( ) ;
_flattenSlider . onValueChanged . RemoveAllListeners ( ) ;
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(UIGalaxySelect), nameof(UIGalaxySelect.OnStarCountSliderValueChange))]
static IEnumerable < CodeInstruction > Patch StarCountOn ValueChange( IEnumerable < CodeInstruction > instructions )
private static IEnumerable < CodeInstruction > UIGalaxySelect_On StarCountSlider ValueChange_Transpiler ( IEnumerable < CodeInstruction > instructions , ILGenerator generator )
{
foreach ( var instruction in instructions )
{
if ( instruction . opcode = = OpCodes . Ldc_I4_S & & instruction . OperandIs ( 80 ) )
{
yield return new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( UniverseGenTweak s) , nameof ( UniverseGenTweak s. MaxStarCount ) ) ) ;
}
else
{
yield return instruction ;
}
}
// Increase hard-coded maxium star count from 80 to MaxStarCount.Value
var matcher = new CodeMatcher ( instructions , generator ) ;
matcher . MatchForward ( false ,
new CodeMatch ( ci = > ci . opcode = = OpCodes . Ldc_I4_S & & ci . OperandIs ( 80 ) )
) . Set ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSetting s) , nameof ( MoreSetting s. MaxStarCount ) ) ) . Insert (
new CodeInstruction ( OpCodes . Call , AccessTools . PropertyGetter ( typeof ( ConfigEntry < int > ) , nameof ( ConfigEntry < int > . Value ) ) )
) ;
return matcher . InstructionEnumeration ( ) ;
}
[HarmonyPrefix ]
[HarmonyTranspiler ]
[HarmonyPatch(typeof(GalaxyData), MethodType.Constructor)]
static bool PatchGalaxyData ( GalaxyData __ instance )
private static IEnumerable < CodeInstruction > GalaxyData_Constructor_Transpiler ( IEnumerable < CodeInstruction > instructions , ILGenerator generator )
{
__instance . astrosData = new AstroData [ ( UniverseGenTweaks . MaxStarCount + 1 ) * 100 ] ;
return false ;
// 25600 -> (MaxStarCount.Value + 1) * 100
var matcher = new CodeMatcher ( instructions , generator ) ;
matcher . MatchForward ( false ,
new CodeMatch ( ci = > ci . opcode = = OpCodes . Ldc_I4 & & ci . OperandIs ( 25600 ) )
) . Set ( OpCodes . Ldfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( MoreSettings . MaxStarCount ) ) ) . Insert (
new CodeInstruction ( OpCodes . Call , AccessTools . PropertyGetter ( typeof ( ConfigEntry < int > ) , nameof ( ConfigEntry < int > . Value ) ) ) ,
new CodeInstruction ( OpCodes . Ldc_I4_1 ) ,
new CodeInstruction ( OpCodes . Add ) ,
new CodeInstruction ( OpCodes . Ldc_I4_S , 100 ) ,
new CodeInstruction ( OpCodes . Mul )
) ;
return matcher . InstructionEnumeration ( ) ;
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(UniverseGen), nameof(UniverseGen.CreateGalaxy))]
static IEnumerable < CodeInstruction > Patch CreateGalaxy( IEnumerable < CodeInstruction > instructions )
private static IEnumerable < CodeInstruction > UniverseGen_ CreateGalaxy_Transpiler ( IEnumerable < CodeInstruction > instructions , ILGenerator generator )
{
foreach ( var instruction in instructions )
{
if ( instruction . opcode = = OpCodes . Call & &
instruction . OperandIs ( AccessTools . Method ( typeof ( UniverseGen ) , nameof ( UniverseGen . GenerateTempPoses ) ) ) )
{
var pop = new CodeInstruction ( OpCodes . Pop ) ;
yield return pop ;
yield return pop ;
yield return pop ;
yield return pop ;
yield return new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( _minDist ) ) ) ;
yield return new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( _minStep ) ) ) ;
yield return new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( _maxStep ) ) ) ;
yield return new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( _flatten ) ) ) ;
}
yield return instruction ;
}
var matcher = new CodeMatcher ( instructions , generator ) ;
matcher . MatchForward ( false ,
new CodeMatch ( OpCodes . Call , AccessTools . Method ( typeof ( UniverseGen ) , nameof ( UniverseGen . GenerateTempPoses ) ) )
) . Advance ( - 4 ) . RemoveInstructions ( 4 ) . Insert (
new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( _minDist ) ) ) ,
new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( _minStep ) ) ) ,
new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( _maxStep ) ) ) ,
new CodeInstruction ( OpCodes . Ldsfld , AccessTools . Field ( typeof ( MoreSettings ) , nameof ( _flatten ) ) )
) ;
return matcher . InstructionEnumeration ( ) ;
}
/* Patch `rand() * (maxStepLen - minStepLen) + minDist` to `rand() * (maxStepLen - minStepLen) + minStepLen`,
this should be a bugged line in original game code. */
[HarmonyTranspiler]
[HarmonyPatch(typeof(UniverseGen), nameof(UniverseGen.RandomPoses))]
static IEnumerable < CodeInstruction > Patch UniverGenRandomPoses( IEnumerable < CodeInstruction > instructions )
static IEnumerable < CodeInstruction > Universe Gen_ RandomPoses_Transpiler ( IEnumerable < CodeInstruction > instructions , ILGenerator generator )
{
var lastIsMul = false ;
foreach ( var instruction in instructions )
{
if ( lastIsMul & & instruction . opcode = = OpCodes . Ldarg_2 )
{
lastIsMul = false ;
yield return new CodeInstruction ( OpCodes . Ldarg_3 ) ;
continue ;
}
lastIsMul = instruction . opcode = = OpCodes . Mul ;
yield return instruction ;
}
var matcher = new CodeMatcher ( instructions , generator ) ;
matcher . MatchForward ( false ,
new CodeMatch ( OpCodes . Mul ) ,
new CodeMatch ( OpCodes . Ldarg_2 )
) ;
matcher . Repeat ( m = > m . Advance ( 1 ) . SetInstructionAndAdvance ( new CodeInstruction ( OpCodes . Ldarg_3 ) ) ) ;
return matcher . InstructionEnumeration ( ) ;
}
}