Skip to content

Commit a3708b6

Browse files
authored
Null annotations and in string args (#142)
* Deleted `in` arg modifiers from GmodNET.API * Removed `in` arg modifiers form the core GmodNET runtime * Enabled nullable annotations for GmodNET.API * Enabled nullable annotations in GmodNET core * Update Startup.cs
1 parent 7f118d6 commit a3708b6

File tree

12 files changed

+405
-105
lines changed

12 files changed

+405
-105
lines changed

gm_dotnet_managed/GmodNET.API/GmodNET.API.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<WarningsAsErrors>Nullable</WarningsAsErrors>
57
<PackageLicenseExpression>MIT</PackageLicenseExpression>
68
<Product>GmodNET API</Product>
79
<Description>GmodNET API library contains all necessary interfaces to write a GmodNET module.</Description>

gm_dotnet_managed/GmodNET.API/ILua.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public interface ILua
6161
/// }
6262
/// </code>
6363
/// </example>
64-
/// <seealso cref="ILua.SetField(int, in string)"/>
65-
public void GetField(int iStackPos, in string key);
64+
/// <seealso cref="ILua.SetField(int, string)"/>
65+
public void GetField(int iStackPos, string key);
6666

6767
/// <summary>
6868
/// Does a table key-value assignment <c>t[k] = v</c>,
@@ -102,8 +102,8 @@ public interface ILua
102102
/// }
103103
/// </code>
104104
/// </example>
105-
/// <seealso cref="ILua.GetField(int, in string)"/>
106-
public void SetField(int iStackPos, in string key);
105+
/// <seealso cref="ILua.GetField(int, string)"/>
106+
public void SetField(int iStackPos, string key);
107107

108108
/// <summary>
109109
/// Creates a new table and pushes it to the top of the stack.
@@ -375,7 +375,7 @@ public interface ILua
375375
/// See <c>lua_pushstring</c> function in the Lua manual: https://www.lua.org/manual/5.1/manual.html
376376
/// </remarks>
377377
/// <param name="str">A string to push.</param>
378-
public void PushString(in string str);
378+
public void PushString(string str);
379379

380380
/// <summary>
381381
/// Pushes a given double-precision number to the Lua stack.
@@ -593,15 +593,15 @@ public interface ILua
593593
/// Creates a new Lua type, pushes its metatable onto the stack, and returns new type’s id.
594594
/// </summary>
595595
/// <remarks>
596-
/// <see cref="ILua.CreateMetaTable(in string)"/> allows you to extend Lua and Garry’s Mod type system with custom types.
596+
/// <see cref="ILua.CreateMetaTable(string)"/> allows you to extend Lua and Garry’s Mod type system with custom types.
597597
/// Returned type id can be used with <see cref="ILua.PushUserType(IntPtr, int)"/>.
598598
///
599599
/// See section "Metatables" in the Lua manual for more information about types and metatables: https://www.lua.org/manual/5.1/manual.html
600600
/// </remarks>
601601
/// <param name="name">A name for the new type.</param>
602602
/// <returns>A type id for newly created type.</returns>
603603
/// <seealso cref="ILua.PushUserType(IntPtr, int)"/>
604-
public int CreateMetaTable(in string name);
604+
public int CreateMetaTable(string name);
605605

606606
/// <summary>
607607
/// Pushes a metatable of the given type onto the stack.
@@ -668,15 +668,15 @@ public interface ILua
668668
/// and <c>k</c> is an object on top of the stack.
669669
/// </summary>
670670
/// <remarks>
671-
/// Unlike <see cref="ILua.GetField(int, in string)"/>, allows to get a value from the table when the key in the key-value pair is not a string.
671+
/// Unlike <see cref="ILua.GetField(int, string)"/>, allows to get a value from the table when the key in the key-value pair is not a string.
672672
///
673673
/// Pops a key object from the stack.
674674
///
675675
/// See <c>lua_gettable</c> function in the Lua manual: https://www.lua.org/manual/5.1/manual.html
676676
/// </remarks>
677677
/// <param name="iStackPos">A stack position of the table to get a value from.</param>
678678
/// <example>
679-
/// The following example shows how <see cref="ILua.GetTable(int)"/> can be used to get a value from the table instead of <see cref="ILua.GetField(int, in string)"/>.
679+
/// The following example shows how <see cref="ILua.GetTable(int)"/> can be used to get a value from the table instead of <see cref="ILua.GetField(int, string)"/>.
680680
/// <code>
681681
/// public static int GetTableExample(ILua lua)
682682
/// {
@@ -691,7 +691,7 @@ public interface ILua
691691
/// }
692692
/// </code>
693693
/// </example>
694-
/// <seealso cref="ILua.GetField(int, in string)"/>
694+
/// <seealso cref="ILua.GetField(int, string)"/>
695695
public void GetTable(int iStackPos);
696696

697697
/// <summary>
@@ -701,15 +701,15 @@ public interface ILua
701701
/// and <c>k</c> is a key at stack index <c>-2</c>.
702702
/// </summary>
703703
/// <remarks>
704-
/// Unlike <see cref="ILua.SetField(int, in string)"/>, allows to add a key-value pair to a table with the key not being a string.
704+
/// Unlike <see cref="ILua.SetField(int, string)"/>, allows to add a key-value pair to a table with the key not being a string.
705705
///
706706
/// Pops both the key and the value from the stack.
707707
///
708708
/// See <c>lua_settable</c> function in the Lua manual: https://www.lua.org/manual/5.1/manual.html
709709
/// </remarks>
710710
/// <param name="iStackPos">A stack position of the table to add a key-value pair to.</param>
711711
/// <example>
712-
/// The following example shows how <see cref="ILua.SetTable(int)"/> can be used instead of <see cref="ILua.SetField(int, in string)"/>.
712+
/// The following example shows how <see cref="ILua.SetTable(int)"/> can be used instead of <see cref="ILua.SetField(int, string)"/>.
713713
/// <code>
714714
/// public static int SetTableExample(ILua lua)
715715
/// {
@@ -723,7 +723,7 @@ public interface ILua
723723
/// }
724724
/// </code>
725725
/// </example>
726-
/// <seealso cref="ILua.SetField(int, in string)"/>
726+
/// <seealso cref="ILua.SetField(int, string)"/>
727727
public void SetTable(int iStackPos);
728728

729729
/// <summary>
@@ -882,7 +882,7 @@ public class GmodLuaException : Exception
882882
/// </summary>
883883
/// <param name="lua_error_code">A Lua exception code. Must be one of the values defined by Lua specification.</param>
884884
/// <param name="lua_error_message">A Lua exception message.</param>
885-
public GmodLuaException(int lua_error_code, string lua_error_message) : base(lua_error_message)
885+
public GmodLuaException(int lua_error_code, string? lua_error_message) : base(lua_error_message)
886886
{
887887
this.error_code = lua_error_code;
888888
}

gm_dotnet_managed/GmodNET.API/ModuleAssemblyLoadContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract class ModuleAssemblyLoadContext : AssemblyLoadContext
2121
/// <summary>
2222
/// Get current custom native library resolver delegate.
2323
/// </summary>
24-
public abstract Func<ModuleAssemblyLoadContext, string, IntPtr> CustomNativeLibraryResolver {get; }
24+
public abstract Func<ModuleAssemblyLoadContext, string, IntPtr>? CustomNativeLibraryResolver { get; }
2525

2626
/// <summary>
2727
/// Initializes a new instance of the <see cref="ModuleAssemblyLoadContext" /> class with a value that indicates whether unloading is enabled.

gm_dotnet_managed/GmodNET/GameConsoleWriter.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ public GameConsoleWriter(ILua lua)
110110

111111
private delegate void MsgFunc(string str);
112112

113-
private static MsgFunc Msg;
113+
private static MsgFunc? Msg;
114114

115-
public override void Write(string value)
115+
public override void Write(string? value)
116116
{
117-
if (!String.IsNullOrEmpty(value))
117+
if (!String.IsNullOrEmpty(value) && Msg is not null)
118118
{
119119
Msg(value);
120120
}
@@ -123,7 +123,7 @@ public override void Write(char value)
123123
{
124124
Write(value.ToString());
125125
}
126-
public override void Write(char[] value)
126+
public override void Write(char[]? value)
127127
{
128128
Write(new string(value));
129129
}
@@ -135,15 +135,15 @@ public override void Write(ReadOnlySpan<char> buffer)
135135
{
136136
Write(new string(buffer));
137137
}
138-
public override void Write(StringBuilder value)
138+
public override void Write(StringBuilder? value)
139139
{
140140
if (value != null)
141141
{
142142
Write(value.ToString());
143143
}
144144
}
145145
// \n begins here
146-
public override void WriteLine(string value)
146+
public override void WriteLine(string? value)
147147
{
148148
if (!String.IsNullOrEmpty(value))
149149
{
@@ -158,7 +158,7 @@ public override void WriteLine(char value)
158158
{
159159
Write(value.ToString() + NewLine);
160160
}
161-
public override void WriteLine(char[] buffer)
161+
public override void WriteLine(char[]? buffer)
162162
{
163163
Write(new string(buffer) + NewLine);
164164
}
@@ -170,7 +170,7 @@ public override void WriteLine(ReadOnlySpan<char> buffer)
170170
{
171171
Write(new string(buffer) + NewLine);
172172
}
173-
public override void WriteLine(StringBuilder value)
173+
public override void WriteLine(StringBuilder? value)
174174
{
175175
if (value != null)
176176
{
@@ -213,7 +213,7 @@ public override void WriteLine(decimal value)
213213
{
214214
Write(value.ToString() + NewLine);
215215
}
216-
public override void WriteLine(object value)
216+
public override void WriteLine(object? value)
217217
{
218218
if (value == null)
219219
{

gm_dotnet_managed/GmodNET/GloabalContext.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,12 @@ int LoadModule(ILua lua)
103103

104104
foreach (Type t in module_types)
105105
{
106-
IModule current_module = (IModule)Activator.CreateInstance(t);
107-
modules.Add(current_module);
108-
gc_handles.Add(GCHandle.Alloc(current_module));
106+
IModule? current_module = Activator.CreateInstance(t) as IModule;
107+
if (current_module is not null)
108+
{
109+
modules.Add(current_module);
110+
gc_handles.Add(GCHandle.Alloc(current_module));
111+
}
109112
}
110113

111114
if (modules.Count == 0)
@@ -150,7 +153,7 @@ WeakReference<GmodNetModuleAssemblyLoadContext> UnloadHelper(string module_name)
150153
{
151154
foreach (GCHandle h in module_contexts[module_name].Item2)
152155
{
153-
((IModule)h.Target).Unload(lua);
156+
(h.Target as IModule)?.Unload(lua);
154157
h.Free();
155158
}
156159

gm_dotnet_managed/GmodNET/GmodNET.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net7.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<WarningsAsErrors>Nullable</WarningsAsErrors>
68
<StartupObject>GmodNET.BuidReq</StartupObject>
79
<Description>GmodNET managed module loader.</Description>
810
<PackageLicenseExpression>MIT</PackageLicenseExpression>

gm_dotnet_managed/GmodNET/GmodNetModuleAssemblyLoadContext.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class GmodNetModuleAssemblyLoadContext : ModuleAssemblyLoadContext
1414
{
1515
private AssemblyDependencyResolver resolver;
1616
private string module_name;
17-
private Func<ModuleAssemblyLoadContext, string, IntPtr> customNativeLibraryResolver;
17+
private Func<ModuleAssemblyLoadContext, string, IntPtr>? customNativeLibraryResolver;
1818
private List<IntPtr> native_libray_handles;
1919

2020
public override string ModuleName
@@ -25,13 +25,7 @@ public override string ModuleName
2525
}
2626
}
2727

28-
public override Func<ModuleAssemblyLoadContext, string, IntPtr> CustomNativeLibraryResolver
29-
{
30-
get
31-
{
32-
return customNativeLibraryResolver;
33-
}
34-
}
28+
public override Func<ModuleAssemblyLoadContext, string, IntPtr>? CustomNativeLibraryResolver => customNativeLibraryResolver;
3529

3630
public override void SetCustomNativeLibraryResolver(Func<ModuleAssemblyLoadContext, string, IntPtr> resolver)
3731
{
@@ -65,14 +59,14 @@ internal GmodNetModuleAssemblyLoadContext(string module_name) : base(module_name
6559
};
6660
}
6761

68-
protected override System.Reflection.Assembly Load(System.Reflection.AssemblyName assemblyName)
62+
protected override System.Reflection.Assembly? Load(System.Reflection.AssemblyName assemblyName)
6963
{
7064
if(assemblyName.Name == "GmodNET.API")
7165
{
7266
return null;
7367
}
7468

75-
string path = resolver.ResolveAssemblyToPath(assemblyName);
69+
string? path = resolver.ResolveAssemblyToPath(assemblyName);
7670
if (string.IsNullOrEmpty(path))
7771
{
7872
return null;
@@ -98,7 +92,7 @@ protected override IntPtr LoadUnmanagedDll (string unmanagedDllName)
9892
}
9993
else
10094
{
101-
string unmanaged_dep_path = resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
95+
string? unmanaged_dep_path = resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
10296

10397
if(String.IsNullOrEmpty(unmanaged_dep_path))
10498
{

0 commit comments

Comments
 (0)