Skip to content

Commit ca21a76

Browse files
committed
adds shortcuts for some API functions, cleans code and applies C#12 syntax upgrades
1 parent d46b17b commit ca21a76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+819
-1693
lines changed

.editorconfig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggesti
3737
csharp_style_var_for_built_in_types = true:suggestion
3838
csharp_style_var_when_type_is_apparent = true:suggestion
3939
csharp_style_var_elsewhere = true:suggestion
40-
csharp_style_expression_bodied_methods = false:suggestion
41-
csharp_style_expression_bodied_constructors = false:suggestion
42-
csharp_style_expression_bodied_operators = false:suggestion
40+
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
41+
csharp_style_expression_bodied_constructors = when_on_single_line:suggestion
42+
csharp_style_expression_bodied_operators = when_on_single_line:suggestion
4343
csharp_style_expression_bodied_properties = true:suggestion
4444
csharp_style_expression_bodied_indexers = true:suggestion
4545
csharp_style_expression_bodied_accessors = true:suggestion
@@ -78,6 +78,7 @@ csharp_style_prefer_method_group_conversion = true:silent
7878
csharp_style_prefer_top_level_statements = true:silent
7979
csharp_style_expression_bodied_lambdas = when_on_single_line:silent
8080
csharp_style_expression_bodied_local_functions = when_on_single_line:silent
81+
csharp_style_prefer_primary_constructors = true:suggestion
8182

8283
[*.{cs,vb}]
8384
#### Naming styles ####
@@ -141,3 +142,4 @@ dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
141142
dotnet_style_operator_placement_when_wrapping = beginning_of_line
142143
tab_width = 3
143144
indent_size = 3
145+
dotnet_style_allow_statement_immediately_after_block_experimental = true:silent

Harmony/Documentation/GlobalSuppressions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
[assembly: SuppressMessage("Performance", "CA1822")]
1919
[assembly: SuppressMessage("Performance", "CA1806")]
20+
[assembly: SuppressMessage("Performance", "CA1859")]
2021

2122
[assembly: SuppressMessage("", "CS0649")]
2223
[assembly: SuppressMessage("", "IDE0251")]

Harmony/Documentation/examples/execution_with.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Execution_With
22
{
33
using System;
44

5+
#pragma warning disable CS9113
56
public class Code
67
{
78
// <example>
@@ -69,13 +70,14 @@ static R ReplacementMethod(T optionalThisArgument /*, ... arguments ... */ )
6970
}
7071

7172
// given the following signatures:
72-
public static R Original() { return new R("original"); }
73+
public static R Original() => new("original");
7374
public static void SimpleFinalizer(ref R result) { }
74-
public static Exception EditFinalizer(Exception ex, ref R result) { return ex; }
75+
public static Exception EditFinalizer(Exception ex, ref R result) => ex;
7576
// </example>
7677

77-
public class R { public R(string s) { } }
78+
public class R(string s) { }
7879
public class T { }
7980
public static bool allVoid;
8081
}
82+
#pragma warning restore CS9113
8183
}

Harmony/Documentation/examples/execution_without.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ class R { }
4747
static int someArgument;
4848

4949
static void SimplePrefix1(params object[] arguments) { }
50-
static bool Prefix2() { return true; }
50+
static bool Prefix2() => true;
5151
static void SimplePrefix3(params object[] arguments) { }
5252
static void SimplePrefix4(params object[] arguments) { }
53-
static bool Prefix5(ref int i, ref R r) { return true; }
54-
static R ModifiedOriginal(params object[] arguments) { return null; }
53+
static bool Prefix5(ref int i, ref R r) => true;
54+
static R ModifiedOriginal(params object[] arguments) => null;
5555
static void Postfix1(ref R r) { }
56-
static R Postfix2(R r, params object[] arguments) { return r; }
56+
static R Postfix2(R r, params object[] arguments) => r;
5757
static void Postfix3() { }
5858
}
5959
}

Harmony/Documentation/examples/intro_annotations.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ static bool Prefix(SomeGameClass __instance, ref int ___counter)
3636
return true;
3737
}
3838

39-
static void Postfix(ref int __result)
40-
{
41-
__result *= 2;
42-
}
39+
static void Postfix(ref int __result) => __result *= 2;
4340
}
4441
// </example>
4542
}

Harmony/Documentation/examples/patching-edgecases.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Patch
1111
[HarmonyReversePatch]
1212
[HarmonyPatch(typeof(BaseClass), nameof(BaseClass.Method))]
1313
[MethodImpl(MethodImplOptions.NoInlining)]
14-
static string BaseMethodDummy(SubClass instance) { return null; }
14+
static string BaseMethodDummy(SubClass instance) => null;
1515

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

2424
public class BaseClass
2525
{
26-
public virtual string Method()
27-
{
28-
return "base";
29-
}
26+
public virtual string Method() => "base";
3027
}
3128

3229
public class SubClass : BaseClass
3330
{
34-
public override string Method()
35-
{
36-
return "subclass";
37-
}
31+
public override string Method() => "subclass";
3832
}
3933
// </example>
4034

@@ -67,15 +61,9 @@ class SomeGameObject
6761
{
6862
GameObject gameObject;
6963

70-
void SomeMethod()
71-
{
72-
UnityEngine.Object.DontDestroyOnLoad(gameObject);
73-
}
64+
void SomeMethod() => UnityEngine.Object.DontDestroyOnLoad(gameObject);
7465

75-
void SomeOtherMethod()
76-
{
77-
SomeMethod();
78-
}
66+
void SomeOtherMethod() => SomeMethod();
7967
}
8068
// </early1>
8169
#pragma warning restore CS0649
@@ -85,11 +73,9 @@ public static class Patcher
8573
{
8674
private static bool patched = false;
8775

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

9480
private static void DoPatch()
9581
{

Harmony/Documentation/examples/patching-finalizer.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,10 @@ public class MyException : Exception
1515
public MyException() { }
1616
public MyException(string message) : base(message) { }
1717
public MyException(string message, Exception innerException) : base(message, innerException) { }
18-
protected MyException(SerializationInfo serializationInfo, StreamingContext streamingContext)
19-
{
20-
throw new NotImplementedException();
21-
}
18+
protected MyException(SerializationInfo serializationInfo, StreamingContext streamingContext) => throw new NotImplementedException();
2219
}
2320

24-
static Exception Finalizer(Exception __exception)
25-
{
26-
return new MyException("Oops", __exception);
27-
}
21+
static Exception Finalizer(Exception __exception) => new MyException("Oops", __exception);
2822
// </rethrow>
2923
}
3024
}

Harmony/Documentation/examples/patching-postfix.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ public class ResultExample
88
// <result>
99
public class OriginalCode
1010
{
11-
public string GetName()
12-
{
13-
return name; // ...
14-
}
11+
public string GetName() => name; // ...
1512
}
1613

1714
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
@@ -33,10 +30,7 @@ public class PassThroughExample
3330
// <passthrough>
3431
public class OriginalCode
3532
{
36-
public string GetName()
37-
{
38-
return "David";
39-
}
33+
public string GetName() => "David";
4034

4135
public IEnumerable<int> GetNumbers()
4236
{
@@ -49,10 +43,7 @@ public IEnumerable<int> GetNumbers()
4943
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
5044
class Patch1
5145
{
52-
static string Postfix(string name)
53-
{
54-
return "Hello " + name;
55-
}
46+
static string Postfix(string name) => "Hello " + name;
5647
}
5748

5849
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetNumbers))]
@@ -86,10 +77,7 @@ public void Test(int counter)
8677
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.Test))]
8778
class Patch
8879
{
89-
static void Prefix(int counter)
90-
{
91-
FileLog.Log("counter = " + counter);
92-
}
80+
static void Prefix(int counter) => FileLog.Log("counter = " + counter);
9381
}
9482
// </args>
9583
}

Harmony/Documentation/examples/patching-prefix.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public class SkipExample
3131
// <skip>
3232
public class OriginalCode
3333
{
34-
public string GetName()
35-
{
36-
return name; // ...
37-
}
34+
public string GetName() => name; // ...
3835
}
3936

4037
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
@@ -56,10 +53,7 @@ public class SkipMaybeExample
5653
// <skip_maybe>
5754
public class OriginalCode
5855
{
59-
public bool IsFullAfterTakingIn(int i)
60-
{
61-
return DoSomeExpensiveCalculation() > i;
62-
}
56+
public bool IsFullAfterTakingIn(int i) => DoSomeExpensiveCalculation() > i;
6357
}
6458

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

80-
static int DoSomeExpensiveCalculation() { return 0; }
74+
static int DoSomeExpensiveCalculation() => 0;
8175
}
8276

8377
public class StateExample

Harmony/Documentation/examples/patching-transpiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SomeType { public string someField; }
3636

3737
class Tools
3838
{
39-
public static MethodInfo MyExtraMethod() { return null; }
39+
public static MethodInfo MyExtraMethod() => null;
4040
}
4141

4242
static void ReportError(string s) { }

Harmony/Documentation/examples/priorities.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ namespace Priorities
66
// <foo>
77
class Foo
88
{
9-
static string Bar()
10-
{
11-
return "secret";
12-
}
9+
static string Bar() => "secret";
1310
}
1411
// </foo>
1512

@@ -26,10 +23,7 @@ void Main_Plugin1()
2623
[HarmonyPatch("Bar")]
2724
class MyPatch
2825
{
29-
static void Postfix(ref string result)
30-
{
31-
result = "new secret 1";
32-
}
26+
static void Postfix(ref string result) => result = "new secret 1";
3327
}
3428
// </plugin1>
3529
}
@@ -48,10 +42,7 @@ void Main_Plugin1b()
4842
class MyPatch
4943
{
5044
[HarmonyAfter(["net.example.plugin2"])]
51-
static void Postfix(ref string result)
52-
{
53-
result = "new secret 1";
54-
}
45+
static void Postfix(ref string result) => result = "new secret 1";
5546
}
5647
// </plugin1b>
5748
}
@@ -69,10 +60,7 @@ void Main_Plugin2()
6960
[HarmonyPatch("Bar")]
7061
class MyPatch
7162
{
72-
static void Postfix(ref string result)
73-
{
74-
result = "new secret 2";
75-
}
63+
static void Postfix(ref string result) => result = "new secret 2";
7664
}
7765
// </plugin2>
7866
}

Harmony/Documentation/examples/reverse-patching.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,16 @@ public class Patch
2323
{
2424
[HarmonyReversePatch]
2525
[HarmonyPatch(typeof(OriginalCode), "Test")]
26-
public static void MyTest(object instance, int counter, string name)
27-
{
26+
public static void MyTest(object instance, int counter, string name) =>
2827
// its a stub so it has no initial content
2928
throw new NotImplementedException("It's a stub");
30-
}
3129
}
3230

3331
class Main
3432
{
35-
void Test()
36-
{
33+
void Test() =>
3734
// here we call OriginalCode.Test()
3835
Patch.MyTest(originalInstance, 100, "hello");
39-
}
4036
}
4137
// </example>
4238

Harmony/Documentation/examples/utilities.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ struct Bar
1212
{
1313
static string secret = "hello";
1414

15-
public string ModifiedSecret()
16-
{
17-
return secret.ToUpper();
18-
}
15+
public string ModifiedSecret() => secret.ToUpper();
1916
}
2017

2118
Bar MyBar
@@ -26,19 +23,13 @@ Bar MyBar
2623
}
2724
}
2825

29-
public string GetSecret()
30-
{
31-
return MyBar.ModifiedSecret();
32-
}
26+
public string GetSecret() => MyBar.ModifiedSecret();
3327

3428
Foo()
3529
{
3630
}
3731

38-
static Foo MakeFoo()
39-
{
40-
return new Foo();
41-
}
32+
static Foo MakeFoo() => new();
4233
}
4334

4435
void Test()

Harmony/Extras/FastAccess.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static InstantiationHandler<T> CreateInstantiationHandler<T>()
4343
{
4444
var constructorInfo =
4545
typeof(T).GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null,
46-
new Type[0], null);
46+
[], null);
4747
if (constructorInfo is null)
4848
{
4949
throw new ApplicationException(string.Format(
@@ -162,14 +162,8 @@ public static SetterHandler<T, S> CreateSetterHandler<T, S>(FieldInfo fieldInfo)
162162
return (SetterHandler<T, S>)dynamicSet.Generate().CreateDelegate(typeof(SetterHandler<T, S>));
163163
}
164164

165-
static DynamicMethodDefinition CreateGetDynamicMethod<T, S>(Type type)
166-
{
167-
return new DynamicMethodDefinition($"DynamicGet_{type.Name}", typeof(S), [typeof(T)]);
168-
}
165+
static DynamicMethodDefinition CreateGetDynamicMethod<T, S>(Type type) => new($"DynamicGet_{type.Name}", typeof(S), [typeof(T)]);
169166

170-
static DynamicMethodDefinition CreateSetDynamicMethod<T, S>(Type type)
171-
{
172-
return new DynamicMethodDefinition($"DynamicSet_{type.Name}", typeof(void), [typeof(T), typeof(S)]);
173-
}
167+
static DynamicMethodDefinition CreateSetDynamicMethod<T, S>(Type type) => new($"DynamicSet_{type.Name}", typeof(void), [typeof(T), typeof(S)]);
174168
}
175169
}

0 commit comments

Comments
 (0)