Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
wasikuss committed Jan 2, 2023
0 parents commit a3a9b00
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: wasikuss
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
bin
obj
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 wasikuss

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions patcher/CastleStory_CustomKeybindingsPatcher.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<AssemblyName>CastleStory_CustomKeybindingsPatcher</AssemblyName>
<Description>CastleStory: Custom Keybindings Patcher</Description>
<Version>0.1.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<CastleStoryManagedPath>PUT_HERE_PATH_TO_CASTLE_STORY_FOLDER/Castle Story/Castle Story_Data/Managed</CastleStoryManagedPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BepInEx.Core" Version="5.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="all" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions patcher/NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="BepInEx" value="https://nuget.bepinex.dev/v3/index.json" />
</packageSources>
</configuration>
60 changes: 60 additions & 0 deletions patcher/src/Patcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections.Generic;
using Mono.Cecil;
using System.Linq;
using Mono.Cecil.Cil;

namespace CastleStory_CustomKeybindingsPatcher
{
public class Patcher
{
public static IEnumerable<string> TargetDLLs { get; } = new[] { "Rewired_Core.dll" };

public static void Patch(AssemblyDefinition assembly)
{
var InputManager_BaseType = assembly.MainModule.GetType("Rewired.InputManager_Base");
var voidType = assembly.MainModule.TypeSystem.Void;
var stringType = assembly.MainModule.TypeSystem.String;
var intType = assembly.MainModule.TypeSystem.Int32;

var ilProcessor = GetMethod(InputManager_BaseType, "Awake").Body.GetILProcessor();
ilProcessor.Body.Instructions.Clear();
ilProcessor.Emit(OpCodes.Ret);

var methodDefinition = new MethodDefinition("Awake2", MethodAttributes.Public, voidType);
ilProcessor = methodDefinition.Body.GetILProcessor();
ilProcessor.Emit(OpCodes.Ldarg_0);
ilProcessor.Emit(OpCodes.Call, GetMethod(InputManager_BaseType, "Initialize"));
ilProcessor.Emit(OpCodes.Ret);
InputManager_BaseType.Methods.Add(methodDefinition);

GenerateReplaceValueMethod(assembly, "Rewired.InputAction", "ReplaceName", "set_name", stringType);
GenerateReplaceValueMethod(assembly, "Rewired.InputAction", "ReplaceDescriptiveName", "set_descriptiveName", stringType);

GenerateReplaceValueMethod(assembly, "Rewired.ActionElementMap", "ReplaceActionId", "set_actionId", intType);
var keyCodeType = assembly.MainModule.GetType("Rewired.KeyboardKeyCode");
GenerateReplaceValueMethod(assembly, "Rewired.ActionElementMap", "ReplaceKeyboardKeyCode", "set_keyboardKeyCode", keyCodeType);
var modifierKeyType = assembly.MainModule.GetType("Rewired.KeyboardKeyCode");
GenerateReplaceValueMethod(assembly, "Rewired.ActionElementMap", "ReplaceModifierKey1", "set_modifierKey1", modifierKeyType);
}

private static void GenerateReplaceValueMethod(AssemblyDefinition assembly, string typeName, string name, string setter, TypeReference paramType)
{
var typeDefinition = assembly.MainModule.GetType(typeName);
var methodDefinition = new MethodDefinition(name, MethodAttributes.Public, assembly.MainModule.TypeSystem.Void);
var ilProcessor = methodDefinition.Body.GetILProcessor();
ilProcessor.Emit(OpCodes.Ldarg_0);
ilProcessor.Emit(OpCodes.Ldarg_1);
ilProcessor.Emit(OpCodes.Call, GetMethod(typeDefinition, setter));
ilProcessor.Emit(OpCodes.Ret);
typeDefinition.Methods.Add(methodDefinition);

var parameterDefinition = new ParameterDefinition(paramType);
methodDefinition.Parameters.Add(parameterDefinition);
}

private static MethodDefinition GetMethod(TypeDefinition typeDefinition, string name)
{
return typeDefinition.Methods.Where(m => m.Name.Equals(name)).First().Resolve();
}
}
}
34 changes: 34 additions & 0 deletions plugin/CastleStory_CustomKeybindingsPlugin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<AssemblyName>CastleStory_CustomKeybindingsPlugin</AssemblyName>
<Description>CastleStory: Custom Keybindings Plugin</Description>
<Version>0.1.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<CastleStoryManagedPath>PUT_HERE_PATH_TO_CASTLE_STORY_FOLDER/Castle Story/Castle Story_Data/Managed</CastleStoryManagedPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BepInEx.Analyzers" Version="1.*" PrivateAssets="all" />
<PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*" />
<PackageReference Include="UnityEngine.Modules" Version="5.6.5" IncludeAssets="compile" />
<Reference Include="Assembly-CSharp">
<HintPath>$(CastleStoryManagedPath)/Assembly-CSharp-firstpass.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="MoonSharp.Interpreter">
<HintPath>$(CastleStoryManagedPath)/MoonSharp.Interpreter.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Rewired">
<HintPath>$(CastleStoryManagedPath)/Rewired_Core.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="all" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions plugin/NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="BepInEx" value="https://nuget.bepinex.dev/v3/index.json" />
</packageSources>
</configuration>
111 changes: 111 additions & 0 deletions plugin/src/Plugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;

using BepInEx;
using Brix.Lua;
using MoonSharp.Interpreter;
using Rewired;
using Rewired.Data.Mapping;


namespace CastleStory_CustomKeybindingsPlugin
{
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]

public class Plugin : BaseUnityPlugin
{
private void Awake()
{
var im = UnityEngine.GameObject.Find("Rewired Input Manager").GetComponent<InputManager>();

Directory.CreateDirectory("Info/Lua/modconf");

Script script;
DynValue result = LuaLoader.Load(out script, "modconf/keybindings.lua", null, null);
ParseLuaKeybindings(im, result.Table);
Script.Kill(ref script);

InvokeMethod(im, "Awake2", new object[0]);
}

private void ParseLuaKeybindings(InputManager im, Table table)
{
for (int i = 0; i != table.Length; i++)
{
Table bind = (Table)table[i + 1];
string category = (string)bind[1];
string action = (string)bind[2];
string descriptiveName = (string)bind[3];

Table primaryKeys = (Table)bind[4];
List<KeyboardKeyCode> primaryKeyList = new List<KeyboardKeyCode>();
for (int j = 0; j != primaryKeys.Length; j++)
{
string entryString = (string)primaryKeys[j + 1];
primaryKeyList.Add((KeyboardKeyCode)System.Enum.Parse(typeof(KeyboardKeyCode), entryString));
}
AddInput(im, category, action, descriptiveName, primaryKeyList);
}
}

private void AddInput(InputManager im, string category, string name, string descriptiveName, List<KeyboardKeyCode> keyCodes)
{
var categoryId = im.userData.IndexOfActionCategory(category);
var actionIdx = im.userData.DuplicateAction_FromButton(categoryId, 0);
var action = im.userData.GetAction(actionIdx);

InvokeMethod(action, "ReplaceName", new object[] { name });
InvokeMethod(action, "ReplaceDescriptiveName", new object[] { descriptiveName });

var layoutId = im.userData.GetKeyboardLayoutId("QWERTY");
var keyboardMap = im.userData.GetKeyboardMap(categoryId, layoutId);

BindKeyCode(keyboardMap, action.id, keyCodes);
BindKeyCode(keyboardMap, action.id, keyCodes);
}

private void BindKeyCode(ControllerMap_Editor keyboardMap, int actionId, List<KeyboardKeyCode> keyCodes)
{
keyboardMap.InsertActionElementMap(0);
var actionElementMap = keyboardMap.GetActionElementMap(0);

InvokeMethod(actionElementMap, "ReplaceActionId", new object[] { actionId });
if (keyCodes.Count == 1)
{
InvokeMethod(actionElementMap, "ReplaceKeyboardKeyCode", new object[] { keyCodes[0] });
}
else
{
InvokeMethod(actionElementMap, "ReplaceKeyboardKeyCode", new object[] { keyCodes[1] });
InvokeMethod(actionElementMap, "ReplaceModifierKey1", new object[] { ToModifier(keyCodes[0]) });
}
}

private void InvokeMethod<T>(T obj, string method, object[] _params)
{
typeof(T).GetMethod(method, BindingFlags.Instance | BindingFlags.Public).Invoke(obj, _params);
}

private ModifierKey ToModifier(KeyboardKeyCode keyCode)
{
switch (keyCode)
{
case KeyboardKeyCode.LeftAlt:
case KeyboardKeyCode.RightAlt:
return ModifierKey.Alt;
case KeyboardKeyCode.LeftControl:
case KeyboardKeyCode.RightControl:
return ModifierKey.Control;
case KeyboardKeyCode.LeftShift:
case KeyboardKeyCode.RightShift:
return ModifierKey.Shift;
case KeyboardKeyCode.LeftCommand:
case KeyboardKeyCode.RightCommand:
return ModifierKey.Command;
default:
return ModifierKey.None;
}
}
}
}

0 comments on commit a3a9b00

Please sign in to comment.