Skip to content

Commit

Permalink
SmoothThrust now supports more than one simulteneous controller
Browse files Browse the repository at this point in the history
some code cleanup
  • Loading branch information
TAImatem committed Dec 23, 2019
1 parent 2257f01 commit 593544c
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 148 deletions.
41 changes: 9 additions & 32 deletions ControllerFix/ControllerFix.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
using OWML.Common;
using OWML.Events;
using System;
using UnityEngine;
using Harmony;
using System.Reflection;
using PadEZ;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Collections;

namespace XBoxCOntrollerFix
{

class GamePad_PC_XB1_Fix
{
public static void Postfix(GamePad_PC_XB1 __instance, ref uint ___m_xb1Index, ref XboxControllerFix.XINPUT_State ___m_padState)
public static void SetUp_Postfix(GamePad_PC_XB1 __instance, ref uint ___m_xb1Index, ref XboxControllerFix.XINPUT_State ___m_padState)
{
XboxControllerFix.ModHelper.Console.WriteLine($"controller SetUp normal index: {___m_xb1Index}");
uint chck = ___m_xb1Index;
for (uint num = 0u; num < 4u; num += 1u)
{
if (XboxControllerFix.XInputGetState(num, out ___m_padState) == 0u && !XboxControllerFix.occInds.Contains(num))
Expand All @@ -25,12 +19,12 @@ public static void Postfix(GamePad_PC_XB1 __instance, ref uint ___m_xb1Index, re
}
}
XboxControllerFix.occInds.Add(___m_xb1Index);
XboxControllerFix.ModHelper.Console.WriteLine($"controller SetUp patched index: {___m_xb1Index}");
if (chck != ___m_xb1Index)
XboxControllerFix.ModHelper.Logger.Log($"patch fixed index from {chck} to {___m_xb1Index}");
}

public static void Destr_Prefix(GamePad_PC_XB1 __instance, ref uint ___m_xb1Index)
{
XboxControllerFix.ModHelper.Console.WriteLine("entered patched controller destroy");
XboxControllerFix.occInds.Remove(___m_xb1Index);
}
}
Expand All @@ -51,60 +45,43 @@ public static bool Prefix(ThrusterModel __instance, ref GamePad __result, int pa
{
result = new GamePad_PC_XB1(padIndex, GamePad.PadType.X360);
}

}
if (result != null)
{
XboxControllerFix.ModHelper.Console.WriteLine($"patched controller type: #{padIndex} \"{name}\"; resulting type = {result.GetPadType()}");
XboxControllerFix.ModHelper.Logger.Log($"patched controller type: #{padIndex} \"{name}\"; resulting type = {result.GetPadType()}");
__result = result;
return false;
}
else
{
XboxControllerFix.ModHelper.Console.WriteLine($"normal controller type: #{padIndex} \"{name}\"");
XboxControllerFix.ModHelper.Logger.Log($"normal controller type: #{padIndex} \"{name}\"");
return true;
}
}
}

public class XboxControllerFix:ModBehaviour
public class XboxControllerFix : ModBehaviour
{
private void Start()
{
ModHelper.HarmonyHelper.AddPostfix<PadEZ.GamePad_PC_XB1>("SetUp", typeof(GamePad_PC_XB1_Fix), "Postfix");
ModHelper.HarmonyHelper.AddPostfix<PadEZ.GamePad_PC_XB1>("SetUp", typeof(GamePad_PC_XB1_Fix), "SetUp_Postfix");
ModHelper.HarmonyHelper.AddPrefix<PadEZ.GamePad_PC_XB1>("Destroy", typeof(GamePad_PC_XB1_Fix), "Destr_Prefix");
ModHelper.HarmonyHelper.AddPrefix<PadEZ.PadManager>("CreateController", typeof(PadManager_Fix), "Prefix");
ModHelper.Console.WriteLine("Controller Fix ready!");
}
public static HashSet<uint> occInds = new HashSet<uint>();
public static bool looppreventer;

[DllImport("XINPUT9_1_0.DLL")]
public static extern uint XInputGetState(uint userIndex, out XboxControllerFix.XINPUT_State state);
public struct XINPUT_State
{
// Token: 0x040000C3 RID: 195
public uint PacketNumber;

// Token: 0x040000C4 RID: 196
public ushort Buttons;

// Token: 0x040000C5 RID: 197
public byte LeftTrigger;

// Token: 0x040000C6 RID: 198
public byte RightTrigger;

// Token: 0x040000C7 RID: 199
public short ThumbLX;

// Token: 0x040000C8 RID: 200
public short ThumbLY;

// Token: 0x040000C9 RID: 201
public short ThumbRX;

// Token: 0x040000CA RID: 202
public short ThumbRY;
}
}
Expand Down
20 changes: 12 additions & 8 deletions ControllerFix/ControllerFix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>bin\debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand All @@ -39,23 +39,26 @@
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="OWML.Common, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Reference Include="OWML.Common">
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\OWML.Common.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="OWML.Events, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\OWML.Events.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Outer Wilds\OuterWilds_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Reference Include="UnityEngine.CoreModule">
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -69,7 +72,8 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
<PostBuildEvent>xcopy /Y "$(TargetPath)" "$(SolutionDir)releases\$(ProjectName)\"
xcopy /Y "$(TargetDir)manifest.json" "$(SolutionDir)releases\$(ProjectName)\"
7z a -tzip -mx5 -r0 $(SolutionDir)releases\$(ProjectName).zip $(SolutionDir)releases\$(ProjectName)\*.*</PostBuildEvent>
</PropertyGroup>
</Project>
13 changes: 7 additions & 6 deletions ControllerFix/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"filename": "OWML.TestMod.dll",
"author": "Alek",
"name": "TestMod",
"uniqueName": "Alek.TestMod",
"version": "0.1",
"enabled": false
"filename": "ControllerFix.dll",
"author": "TAImatem",
"name": "XBoxControllerFix",
"uniqueName": "TruAI.XBoxControllerFix",
"version": "1.0",
"owmlVersion": "0.2.1",
"enabled": true
}
53 changes: 34 additions & 19 deletions SmoothThrust/SmoothThrust.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
using OWML.Common;
using OWML.Events;
using System;
using System.Collections.Generic;
using UnityEngine;
using Harmony;
using System.Reflection;

namespace SmoothThrustContainer
namespace TAI_SmoothThrust
{

//[HarmonyPatch(typeof(ThrusterModel), "AddTranslationalInput")]
class ThrusterModel_AddTranslationalInput
class ThrusterModel_SmoothPatch
{
public static void Prefix(ThrusterModel __instance, ref Vector3 input)
{
if (!SmoothThrust.container.ContainsKey(__instance))
{
SmoothThrust.container.Add(__instance, new SmoothThrust.SmoothCont());
}
SmoothThrust.SmoothCont iter = SmoothThrust.container[__instance];
if (OWInput.IsNewlyPressed(InputLibrary.interact, InputMode.All))
{
SmoothThrustContainer._prevTranslation = input;
SmoothThrustContainer._buildupVelocity = Vector3.zero;
iter._prevTranslation = input;
iter._buildupVelocity = Vector3.zero;
}
if (input.magnitude > 0.0001f && OWInput.IsHeld(InputLibrary.interact, 0.05f, InputMode.All))
{
SmoothThrustContainer._prevTranslation.x = Mathf.SmoothDamp(SmoothThrustContainer._prevTranslation.x, input.x, ref SmoothThrustContainer._buildupVelocity.x, SmoothThrustContainer._analogSmoothTime);
SmoothThrustContainer._prevTranslation.z = Mathf.SmoothDamp(SmoothThrustContainer._prevTranslation.z, input.z, ref SmoothThrustContainer._buildupVelocity.z, SmoothThrustContainer._analogSmoothTime);
SmoothThrustContainer._prevTranslation.y = Mathf.SmoothDamp(SmoothThrustContainer._prevTranslation.y, input.y, ref SmoothThrustContainer._buildupVelocity.y, SmoothThrustContainer._analogSmoothTime);
input = SmoothThrustContainer._prevTranslation;
iter._prevTranslation.x = Mathf.SmoothDamp(iter._prevTranslation.x, input.x, ref iter._buildupVelocity.x, SmoothThrust._analogSmoothTime);
iter._prevTranslation.z = Mathf.SmoothDamp(iter._prevTranslation.z, input.z, ref iter._buildupVelocity.z, SmoothThrust._analogSmoothTime);
iter._prevTranslation.y = Mathf.SmoothDamp(iter._prevTranslation.y, input.y, ref iter._buildupVelocity.y, SmoothThrust._analogSmoothTime);
input = iter._prevTranslation;
}
iter._prevTranslation = input;
SmoothThrust.container[__instance] = iter;
}

public static void Postfix(ThrusterModel __instance)
{
if (SmoothThrust.container.ContainsKey(__instance))
{
SmoothThrust.container.Remove(__instance);
}
SmoothThrustContainer._prevTranslation = input;
}
}
public class SmoothThrustContainer:ModBehaviour
public class SmoothThrust : ModBehaviour
{
private void Start()
{
ModHelper.HarmonyHelper.AddPrefix<ThrusterModel>("AddTranslationalInput", typeof(ThrusterModel_AddTranslationalInput), "Prefix");
ModHelper.HarmonyHelper.AddPrefix<ThrusterModel>("AddTranslationalInput", typeof(ThrusterModel_SmoothPatch), "Prefix");
ModHelper.HarmonyHelper.AddPostfix<ThrusterModel>("OnDestroy", typeof(ThrusterModel_SmoothPatch), "Postfix");
ModHelper.Console.WriteLine("Smooth Thrust Ready!");
}
public static Vector3 _prevTranslation;
public static Vector3 _buildupVelocity;

public static Dictionary<MonoBehaviour, SmoothCont> container = new Dictionary<MonoBehaviour, SmoothCont>();
public static float _analogSmoothTime = 0.3f;
public struct SmoothCont
{
public Vector3 _prevTranslation;
public Vector3 _buildupVelocity;
}
}
}
22 changes: 13 additions & 9 deletions SmoothThrust/SmoothThrust.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>bin\debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand All @@ -39,23 +39,26 @@
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Outer Wilds\OuterWilds_Data\Managed\Assembly-CSharp.dll</HintPath>
<Reference Include="Assembly-CSharp">
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="OWML.Common, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\OWML.Common.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="OWML.Events, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\OWML.Events.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Outer Wilds\OuterWilds_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Reference Include="UnityEngine.CoreModule">
<HintPath>D:\Games\Outer Wilds\OuterWilds_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -69,7 +72,8 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
<PostBuildEvent>xcopy /Y "$(TargetPath)" "$(SolutionDir)releases\$(ProjectName)\"
xcopy /Y "$(TargetDir)manifest.json" "$(SolutionDir)releases\$(ProjectName)\"
7z a -tzip -mx5 -r0 $(SolutionDir)releases\$(ProjectName).zip $(SolutionDir)releases\$(ProjectName)\*.*</PostBuildEvent>
</PropertyGroup>
</Project>
13 changes: 7 additions & 6 deletions SmoothThrust/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"filename": "OWML.TestMod.dll",
"author": "Alek",
"name": "TestMod",
"uniqueName": "Alek.TestMod",
"version": "0.1",
"enabled": false
"filename": "KeyboardSmoothThrust.dll",
"author": "TAImatem & Schinken",
"name": "SmoothThrust",
"uniqueName": "TruAI.SmoothThrust",
"version": "1.0",
"owmlVersion": "0.2.1",
"enabled": true
}
Loading

0 comments on commit 593544c

Please sign in to comment.