Skip to content

Commit

Permalink
Added KeybindManager
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrianoTurner committed Sep 2, 2022
1 parent 56d6a8b commit 15a55fa
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
128 changes: 128 additions & 0 deletions PulsarModLoader/KeybindManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
namespace PulsarModLoader
{

public class PMLKeybind
{
public string Name;
public string ID;
public string Category;
public string Key;


public PMLKeybind(string inName, string inID, string inCategory, string inKey)
{

this.Name = inName;
this.ID = inID;
this.Category = inCategory;
this.Key = inKey;
}
}

public interface IKeybind
{
void RegisterBinds(KeybindManager manager);
}
public class KeybindManager
{
private static KeybindManager _instance;
public List<PMLKeybind> keybindings = new List<PMLKeybind>();
private KeybindManager()
{
ModManager.Instance.OnModSuccessfullyLoaded += OnModLoaded;
}
public static KeybindManager Instance
{
get
{
if (_instance == null)
{
_instance = new KeybindManager();

}

return _instance;
}
}


void OnModLoaded(string modName, PulsarMod mod)
{
if (mod is IKeybind k)
{
k.RegisterBinds(KeybindManager.Instance);
}
}
public PMLKeybind GetPMLKeybind(string inID)
{
return keybindings.FirstOrDefault(k => k.ID == inID);
}

public bool GetButtonDown(string inID)
{
return PLInput.Instance.GetButtonDown(inID);
}

public void NewBind(string inName, string inID, string inCategory, string inKey)
{
KeybindManager.Instance.keybindings.Add(new PMLKeybind(inName, inID, inCategory, inKey));
}


}

[HarmonyPatch(typeof(PLInput), "LoadFromXmlDoc")]
internal class LoadFromXmlPatch
{
private static void Postfix(PLInput __instance, string xmlFileName)
{
foreach (PMLKeybind keybind in KeybindManager.Instance.keybindings)
{
List<PLInputAction> list = __instance.FindActionsByID(keybind.ID);
int num = 0;
PLInputCategory plinputCategory = null;
foreach (PLInputCategory plinputCategory2 in __instance.AllInputCategories)
{
if (plinputCategory2 != null && plinputCategory2.m_Name == keybind.Category)
{
plinputCategory = plinputCategory2;
break;
}
}
using (List<PLInputAction>.Enumerator enumerator4 = list.GetEnumerator())
{
while (enumerator4.MoveNext())
{
if (enumerator4.Current.m_Category == plinputCategory)
{
num++;
}
}
}

if (num == 0 && plinputCategory != null)
{
var plinputAction = new PLInputAction(keybind.Name, keybind.ID, plinputCategory);
var plinputKey = new PLInputKey();
plinputKey.Type = "standard";
plinputKey.ID = keybind.Key.ToLower();
plinputKey.ID_Upper = keybind.Key.ToUpper();
plinputAction.AddKey(plinputKey);
__instance.AllInputActions.Add(plinputAction);
plinputAction.m_Category = plinputCategory;
__instance.SaveToXmlFile(xmlFileName, false);


}
}
}
}

}
3 changes: 2 additions & 1 deletion PulsarModLoader/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void LoadModsDirectory(string modsDir)
Directory.CreateDirectory(modsDir);
}
modDirectories.Add(modsDir);

// Load mods
foreach (string assemblyPath in Directory.GetFiles(modsDir, "*.dll"))
{
Expand Down Expand Up @@ -145,6 +145,7 @@ public void LoadModsDirectory(string modsDir)
_ = PulsarModLoader.Content.Components.Virus.VirusModManager.Instance;
_ = PulsarModLoader.Content.Components.WarpDrive.WarpDriveModManager.Instance;
_ = PulsarModLoader.Content.Components.WarpDriveProgram.WarpDriveProgramModManager.Instance;
_ = PulsarModLoader.KeybindManager.Instance;

OnAllModsLoaded?.Invoke();
}
Expand Down
2 changes: 1 addition & 1 deletion PulsarModLoader/PulsarMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,6 @@ public virtual void Disable()
public virtual void Enable()
{
enabled = true;
}
}
}
}
4 changes: 4 additions & 0 deletions PulsarModLoader/PulsarModLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<HintPath>..\lib\Photon3Unity3D.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="PLInputBase">
<HintPath>..\..\..\SteamLibrary\steamapps\common\PULSARLostColony\PULSAR_LostColony_Data\Managed\PLInputBase.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
Expand Down Expand Up @@ -190,6 +193,7 @@
<Compile Include="CustomGUI\GUIMain.cs" />
<Compile Include="CustomGUI\ModSettingsMenu.cs" />
<Compile Include="CustomGUI\PMLSettings.cs" />
<Compile Include="KeybindManager.cs" />
<Compile Include="ModMessage\ModMessage.cs" />
<Compile Include="ModMessage\ModMessageHelper.cs" />
<Compile Include="MPModChecks.cs" />
Expand Down

0 comments on commit 15a55fa

Please sign in to comment.