Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gm_dotnet_managed/GmodNET.API/GmodNET.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Product>GmodNET API</Product>
<Description>GmodNET API library contains all necessary interfaces to write a GmodNET module.</Description>
Expand Down
28 changes: 14 additions & 14 deletions gm_dotnet_managed/GmodNET.API/ILua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public interface ILua
/// }
/// </code>
/// </example>
/// <seealso cref="ILua.SetField(int, in string)"/>
public void GetField(int iStackPos, in string key);
/// <seealso cref="ILua.SetField(int, string)"/>
public void GetField(int iStackPos, string key);

/// <summary>
/// Does a table key-value assignment <c>t[k] = v</c>,
Expand Down Expand Up @@ -102,8 +102,8 @@ public interface ILua
/// }
/// </code>
/// </example>
/// <seealso cref="ILua.GetField(int, in string)"/>
public void SetField(int iStackPos, in string key);
/// <seealso cref="ILua.GetField(int, string)"/>
public void SetField(int iStackPos, string key);

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

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

/// <summary>
/// Pushes a metatable of the given type onto the stack.
Expand Down Expand Up @@ -668,15 +668,15 @@ public interface ILua
/// and <c>k</c> is an object on top of the stack.
/// </summary>
/// <remarks>
/// 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.
/// 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.
///
/// Pops a key object from the stack.
///
/// See <c>lua_gettable</c> function in the Lua manual: https://www.lua.org/manual/5.1/manual.html
/// </remarks>
/// <param name="iStackPos">A stack position of the table to get a value from.</param>
/// <example>
/// 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)"/>.
/// 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)"/>.
/// <code>
/// public static int GetTableExample(ILua lua)
/// {
Expand All @@ -691,7 +691,7 @@ public interface ILua
/// }
/// </code>
/// </example>
/// <seealso cref="ILua.GetField(int, in string)"/>
/// <seealso cref="ILua.GetField(int, string)"/>
public void GetTable(int iStackPos);

/// <summary>
Expand All @@ -701,15 +701,15 @@ public interface ILua
/// and <c>k</c> is a key at stack index <c>-2</c>.
/// </summary>
/// <remarks>
/// 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.
/// Unlike <see cref="ILua.SetField(int, string)"/>, allows to add a key-value pair to a table with the key not being a string.
///
/// Pops both the key and the value from the stack.
///
/// See <c>lua_settable</c> function in the Lua manual: https://www.lua.org/manual/5.1/manual.html
/// </remarks>
/// <param name="iStackPos">A stack position of the table to add a key-value pair to.</param>
/// <example>
/// The following example shows how <see cref="ILua.SetTable(int)"/> can be used instead of <see cref="ILua.SetField(int, in string)"/>.
/// The following example shows how <see cref="ILua.SetTable(int)"/> can be used instead of <see cref="ILua.SetField(int, string)"/>.
/// <code>
/// public static int SetTableExample(ILua lua)
/// {
Expand All @@ -723,7 +723,7 @@ public interface ILua
/// }
/// </code>
/// </example>
/// <seealso cref="ILua.SetField(int, in string)"/>
/// <seealso cref="ILua.SetField(int, string)"/>
public void SetTable(int iStackPos);

/// <summary>
Expand Down Expand Up @@ -882,7 +882,7 @@ public class GmodLuaException : Exception
/// </summary>
/// <param name="lua_error_code">A Lua exception code. Must be one of the values defined by Lua specification.</param>
/// <param name="lua_error_message">A Lua exception message.</param>
public GmodLuaException(int lua_error_code, string lua_error_message) : base(lua_error_message)
public GmodLuaException(int lua_error_code, string? lua_error_message) : base(lua_error_message)
{
this.error_code = lua_error_code;
}
Expand Down
2 changes: 1 addition & 1 deletion gm_dotnet_managed/GmodNET.API/ModuleAssemblyLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class ModuleAssemblyLoadContext : AssemblyLoadContext
/// <summary>
/// Get current custom native library resolver delegate.
/// </summary>
public abstract Func<ModuleAssemblyLoadContext, string, IntPtr> CustomNativeLibraryResolver {get; }
public abstract Func<ModuleAssemblyLoadContext, string, IntPtr>? CustomNativeLibraryResolver { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ModuleAssemblyLoadContext" /> class with a value that indicates whether unloading is enabled.
Expand Down
18 changes: 9 additions & 9 deletions gm_dotnet_managed/GmodNET/GameConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ public GameConsoleWriter(ILua lua)

private delegate void MsgFunc(string str);

private static MsgFunc Msg;
private static MsgFunc? Msg;

public override void Write(string value)
public override void Write(string? value)
{
if (!String.IsNullOrEmpty(value))
if (!String.IsNullOrEmpty(value) && Msg is not null)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is redundant

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but C# null checker is not satisfied otherwise.

{
Msg(value);
}
Expand All @@ -123,7 +123,7 @@ public override void Write(char value)
{
Write(value.ToString());
}
public override void Write(char[] value)
public override void Write(char[]? value)
{
Write(new string(value));
}
Expand All @@ -135,15 +135,15 @@ public override void Write(ReadOnlySpan<char> buffer)
{
Write(new string(buffer));
}
public override void Write(StringBuilder value)
public override void Write(StringBuilder? value)
{
if (value != null)
{
Write(value.ToString());
}
}
// \n begins here
public override void WriteLine(string value)
public override void WriteLine(string? value)
{
if (!String.IsNullOrEmpty(value))
{
Expand All @@ -158,7 +158,7 @@ public override void WriteLine(char value)
{
Write(value.ToString() + NewLine);
}
public override void WriteLine(char[] buffer)
public override void WriteLine(char[]? buffer)
{
Write(new string(buffer) + NewLine);
}
Expand All @@ -170,7 +170,7 @@ public override void WriteLine(ReadOnlySpan<char> buffer)
{
Write(new string(buffer) + NewLine);
}
public override void WriteLine(StringBuilder value)
public override void WriteLine(StringBuilder? value)
{
if (value != null)
{
Expand Down Expand Up @@ -213,7 +213,7 @@ public override void WriteLine(decimal value)
{
Write(value.ToString() + NewLine);
}
public override void WriteLine(object value)
public override void WriteLine(object? value)
{
if (value == null)
{
Expand Down
11 changes: 7 additions & 4 deletions gm_dotnet_managed/GmodNET/GloabalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ int LoadModule(ILua lua)

foreach (Type t in module_types)
{
IModule current_module = (IModule)Activator.CreateInstance(t);
modules.Add(current_module);
gc_handles.Add(GCHandle.Alloc(current_module));
IModule? current_module = Activator.CreateInstance(t) as IModule;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's nullable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add ! somewhere to make it not nullable? 😅

Copy link
Copy Markdown
Member Author

@GlebChili GlebChili Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why? We literally check if it is null in a next line. Let it be this way until Microsoft provide non-nulable guaranties for Activator (so until .NET 8, maybe?).

if (current_module is not null)
{
modules.Add(current_module);
gc_handles.Add(GCHandle.Alloc(current_module));
}
}

if (modules.Count == 0)
Expand Down Expand Up @@ -150,7 +153,7 @@ WeakReference<GmodNetModuleAssemblyLoadContext> UnloadHelper(string module_name)
{
foreach (GCHandle h in module_contexts[module_name].Item2)
{
((IModule)h.Target).Unload(lua);
(h.Target as IModule)?.Unload(lua);
h.Free();
}

Expand Down
2 changes: 2 additions & 0 deletions gm_dotnet_managed/GmodNET/GmodNET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<StartupObject>GmodNET.BuidReq</StartupObject>
<Description>GmodNET managed module loader.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
16 changes: 5 additions & 11 deletions gm_dotnet_managed/GmodNET/GmodNetModuleAssemblyLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class GmodNetModuleAssemblyLoadContext : ModuleAssemblyLoadContext
{
private AssemblyDependencyResolver resolver;
private string module_name;
private Func<ModuleAssemblyLoadContext, string, IntPtr> customNativeLibraryResolver;
private Func<ModuleAssemblyLoadContext, string, IntPtr>? customNativeLibraryResolver;
private List<IntPtr> native_libray_handles;

public override string ModuleName
Expand All @@ -25,13 +25,7 @@ public override string ModuleName
}
}

public override Func<ModuleAssemblyLoadContext, string, IntPtr> CustomNativeLibraryResolver
{
get
{
return customNativeLibraryResolver;
}
}
public override Func<ModuleAssemblyLoadContext, string, IntPtr>? CustomNativeLibraryResolver => customNativeLibraryResolver;

public override void SetCustomNativeLibraryResolver(Func<ModuleAssemblyLoadContext, string, IntPtr> resolver)
{
Expand Down Expand Up @@ -65,14 +59,14 @@ internal GmodNetModuleAssemblyLoadContext(string module_name) : base(module_name
};
}

protected override System.Reflection.Assembly Load(System.Reflection.AssemblyName assemblyName)
protected override System.Reflection.Assembly? Load(System.Reflection.AssemblyName assemblyName)
{
if(assemblyName.Name == "GmodNET.API")
{
return null;
}

string path = resolver.ResolveAssemblyToPath(assemblyName);
string? path = resolver.ResolveAssemblyToPath(assemblyName);
if (string.IsNullOrEmpty(path))
{
return null;
Expand All @@ -98,7 +92,7 @@ protected override IntPtr LoadUnmanagedDll (string unmanagedDllName)
}
else
{
string unmanaged_dep_path = resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
string? unmanaged_dep_path = resolver.ResolveUnmanagedDllToPath(unmanagedDllName);

if(String.IsNullOrEmpty(unmanaged_dep_path))
{
Expand Down
Loading