Skip to content

Commit

Permalink
adds shortcuts for some API functions, cleans code and applies C#12 s…
Browse files Browse the repository at this point in the history
…yntax upgrades
  • Loading branch information
pardeike committed Feb 19, 2024
1 parent d46b17b commit ca21a76
Show file tree
Hide file tree
Showing 73 changed files with 819 additions and 1,693 deletions.
8 changes: 5 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggesti
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion
csharp_style_expression_bodied_methods = false:suggestion
csharp_style_expression_bodied_constructors = false:suggestion
csharp_style_expression_bodied_operators = false:suggestion
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
csharp_style_expression_bodied_constructors = when_on_single_line:suggestion
csharp_style_expression_bodied_operators = when_on_single_line:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
Expand Down Expand Up @@ -78,6 +78,7 @@ csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_expression_bodied_lambdas = when_on_single_line:silent
csharp_style_expression_bodied_local_functions = when_on_single_line:silent
csharp_style_prefer_primary_constructors = true:suggestion

[*.{cs,vb}]
#### Naming styles ####
Expand Down Expand Up @@ -141,3 +142,4 @@ dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 3
indent_size = 3
dotnet_style_allow_statement_immediately_after_block_experimental = true:silent
1 change: 1 addition & 0 deletions Harmony/Documentation/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

[assembly: SuppressMessage("Performance", "CA1822")]
[assembly: SuppressMessage("Performance", "CA1806")]
[assembly: SuppressMessage("Performance", "CA1859")]

[assembly: SuppressMessage("", "CS0649")]
[assembly: SuppressMessage("", "IDE0251")]
8 changes: 5 additions & 3 deletions Harmony/Documentation/examples/execution_with.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Execution_With
{
using System;

#pragma warning disable CS9113
public class Code
{
// <example>
Expand Down Expand Up @@ -69,13 +70,14 @@ static R ReplacementMethod(T optionalThisArgument /*, ... arguments ... */ )
}

// given the following signatures:
public static R Original() { return new R("original"); }
public static R Original() => new("original");
public static void SimpleFinalizer(ref R result) { }
public static Exception EditFinalizer(Exception ex, ref R result) { return ex; }
public static Exception EditFinalizer(Exception ex, ref R result) => ex;
// </example>

public class R { public R(string s) { } }
public class R(string s) { }
public class T { }
public static bool allVoid;
}
#pragma warning restore CS9113
}
8 changes: 4 additions & 4 deletions Harmony/Documentation/examples/execution_without.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class R { }
static int someArgument;

static void SimplePrefix1(params object[] arguments) { }
static bool Prefix2() { return true; }
static bool Prefix2() => true;
static void SimplePrefix3(params object[] arguments) { }
static void SimplePrefix4(params object[] arguments) { }
static bool Prefix5(ref int i, ref R r) { return true; }
static R ModifiedOriginal(params object[] arguments) { return null; }
static bool Prefix5(ref int i, ref R r) => true;
static R ModifiedOriginal(params object[] arguments) => null;
static void Postfix1(ref R r) { }
static R Postfix2(R r, params object[] arguments) { return r; }
static R Postfix2(R r, params object[] arguments) => r;
static void Postfix3() { }
}
}
5 changes: 1 addition & 4 deletions Harmony/Documentation/examples/intro_annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ static bool Prefix(SomeGameClass __instance, ref int ___counter)
return true;
}

static void Postfix(ref int __result)
{
__result *= 2;
}
static void Postfix(ref int __result) => __result *= 2;
}
// </example>
}
26 changes: 6 additions & 20 deletions Harmony/Documentation/examples/patching-edgecases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Patch
[HarmonyReversePatch]
[HarmonyPatch(typeof(BaseClass), nameof(BaseClass.Method))]
[MethodImpl(MethodImplOptions.NoInlining)]
static string BaseMethodDummy(SubClass instance) { return null; }
static string BaseMethodDummy(SubClass instance) => null;

[HarmonyPatch(typeof(SubClass), nameof(SubClass.Method))]
static void Prefix(SubClass __instance)
Expand All @@ -23,18 +23,12 @@ static void Prefix(SubClass __instance)

public class BaseClass
{
public virtual string Method()
{
return "base";
}
public virtual string Method() => "base";
}

public class SubClass : BaseClass
{
public override string Method()
{
return "subclass";
}
public override string Method() => "subclass";
}
// </example>

Expand Down Expand Up @@ -67,15 +61,9 @@ class SomeGameObject
{
GameObject gameObject;

void SomeMethod()
{
UnityEngine.Object.DontDestroyOnLoad(gameObject);
}
void SomeMethod() => UnityEngine.Object.DontDestroyOnLoad(gameObject);

void SomeOtherMethod()
{
SomeMethod();
}
void SomeOtherMethod() => SomeMethod();
}
// </early1>
#pragma warning restore CS0649
Expand All @@ -85,11 +73,9 @@ public static class Patcher
{
private static bool patched = false;

public static void Main()
{
public static void Main() =>
//DoPatch(); <-- Do not execute patching on assembly entry point
SceneManager.sceneLoaded += SceneLoaded;
}

private static void DoPatch()
{
Expand Down
10 changes: 2 additions & 8 deletions Harmony/Documentation/examples/patching-finalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ public class MyException : Exception
public MyException() { }
public MyException(string message) : base(message) { }
public MyException(string message, Exception innerException) : base(message, innerException) { }
protected MyException(SerializationInfo serializationInfo, StreamingContext streamingContext)
{
throw new NotImplementedException();
}
protected MyException(SerializationInfo serializationInfo, StreamingContext streamingContext) => throw new NotImplementedException();
}

static Exception Finalizer(Exception __exception)
{
return new MyException("Oops", __exception);
}
static Exception Finalizer(Exception __exception) => new MyException("Oops", __exception);
// </rethrow>
}
}
20 changes: 4 additions & 16 deletions Harmony/Documentation/examples/patching-postfix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public class ResultExample
// <result>
public class OriginalCode
{
public string GetName()
{
return name; // ...
}
public string GetName() => name; // ...
}

[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
Expand All @@ -33,10 +30,7 @@ public class PassThroughExample
// <passthrough>
public class OriginalCode
{
public string GetName()
{
return "David";
}
public string GetName() => "David";

public IEnumerable<int> GetNumbers()
{
Expand All @@ -49,10 +43,7 @@ public IEnumerable<int> GetNumbers()
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
class Patch1
{
static string Postfix(string name)
{
return "Hello " + name;
}
static string Postfix(string name) => "Hello " + name;
}

[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetNumbers))]
Expand Down Expand Up @@ -86,10 +77,7 @@ public void Test(int counter)
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.Test))]
class Patch
{
static void Prefix(int counter)
{
FileLog.Log("counter = " + counter);
}
static void Prefix(int counter) => FileLog.Log("counter = " + counter);
}
// </args>
}
Expand Down
12 changes: 3 additions & 9 deletions Harmony/Documentation/examples/patching-prefix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public class SkipExample
// <skip>
public class OriginalCode
{
public string GetName()
{
return name; // ...
}
public string GetName() => name; // ...
}

[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
Expand All @@ -56,10 +53,7 @@ public class SkipMaybeExample
// <skip_maybe>
public class OriginalCode
{
public bool IsFullAfterTakingIn(int i)
{
return DoSomeExpensiveCalculation() > i;
}
public bool IsFullAfterTakingIn(int i) => DoSomeExpensiveCalculation() > i;
}

[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.IsFullAfterTakingIn))]
Expand All @@ -77,7 +71,7 @@ static bool Prefix(ref bool __result, int i)
}
// </skip_maybe>

static int DoSomeExpensiveCalculation() { return 0; }
static int DoSomeExpensiveCalculation() => 0;
}

public class StateExample
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Documentation/examples/patching-transpiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SomeType { public string someField; }

class Tools
{
public static MethodInfo MyExtraMethod() { return null; }
public static MethodInfo MyExtraMethod() => null;
}

static void ReportError(string s) { }
Expand Down
20 changes: 4 additions & 16 deletions Harmony/Documentation/examples/priorities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ namespace Priorities
// <foo>
class Foo
{
static string Bar()
{
return "secret";
}
static string Bar() => "secret";
}
// </foo>

Expand All @@ -26,10 +23,7 @@ void Main_Plugin1()
[HarmonyPatch("Bar")]
class MyPatch
{
static void Postfix(ref string result)
{
result = "new secret 1";
}
static void Postfix(ref string result) => result = "new secret 1";
}
// </plugin1>
}
Expand All @@ -48,10 +42,7 @@ void Main_Plugin1b()
class MyPatch
{
[HarmonyAfter(["net.example.plugin2"])]
static void Postfix(ref string result)
{
result = "new secret 1";
}
static void Postfix(ref string result) => result = "new secret 1";
}
// </plugin1b>
}
Expand All @@ -69,10 +60,7 @@ void Main_Plugin2()
[HarmonyPatch("Bar")]
class MyPatch
{
static void Postfix(ref string result)
{
result = "new secret 2";
}
static void Postfix(ref string result) => result = "new secret 2";
}
// </plugin2>
}
Expand Down
8 changes: 2 additions & 6 deletions Harmony/Documentation/examples/reverse-patching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@ public class Patch
{
[HarmonyReversePatch]
[HarmonyPatch(typeof(OriginalCode), "Test")]
public static void MyTest(object instance, int counter, string name)
{
public static void MyTest(object instance, int counter, string name) =>
// its a stub so it has no initial content
throw new NotImplementedException("It's a stub");
}
}

class Main
{
void Test()
{
void Test() =>
// here we call OriginalCode.Test()
Patch.MyTest(originalInstance, 100, "hello");
}
}
// </example>

Expand Down
15 changes: 3 additions & 12 deletions Harmony/Documentation/examples/utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ struct Bar
{
static string secret = "hello";

public string ModifiedSecret()
{
return secret.ToUpper();
}
public string ModifiedSecret() => secret.ToUpper();
}

Bar MyBar
Expand All @@ -26,19 +23,13 @@ Bar MyBar
}
}

public string GetSecret()
{
return MyBar.ModifiedSecret();
}
public string GetSecret() => MyBar.ModifiedSecret();

Foo()
{
}

static Foo MakeFoo()
{
return new Foo();
}
static Foo MakeFoo() => new();
}

void Test()
Expand Down
12 changes: 3 additions & 9 deletions Harmony/Extras/FastAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static InstantiationHandler<T> CreateInstantiationHandler<T>()
{
var constructorInfo =
typeof(T).GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null,
new Type[0], null);
[], null);
if (constructorInfo is null)
{
throw new ApplicationException(string.Format(
Expand Down Expand Up @@ -162,14 +162,8 @@ public static SetterHandler<T, S> CreateSetterHandler<T, S>(FieldInfo fieldInfo)
return (SetterHandler<T, S>)dynamicSet.Generate().CreateDelegate(typeof(SetterHandler<T, S>));
}

static DynamicMethodDefinition CreateGetDynamicMethod<T, S>(Type type)
{
return new DynamicMethodDefinition($"DynamicGet_{type.Name}", typeof(S), [typeof(T)]);
}
static DynamicMethodDefinition CreateGetDynamicMethod<T, S>(Type type) => new($"DynamicGet_{type.Name}", typeof(S), [typeof(T)]);

static DynamicMethodDefinition CreateSetDynamicMethod<T, S>(Type type)
{
return new DynamicMethodDefinition($"DynamicSet_{type.Name}", typeof(void), [typeof(T), typeof(S)]);
}
static DynamicMethodDefinition CreateSetDynamicMethod<T, S>(Type type) => new($"DynamicSet_{type.Name}", typeof(void), [typeof(T), typeof(S)]);
}
}
Loading

0 comments on commit ca21a76

Please sign in to comment.