Skip to content

Commit

Permalink
⚡ Address nullability warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Nov 21, 2020
1 parent ddf4121 commit 6b0c904
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 40 deletions.
8 changes: 4 additions & 4 deletions Shadowsocks.PAC/PACDaemon.cs
Expand Up @@ -16,11 +16,11 @@ public class PACDaemon : IEnableLogger
public const string USER_RULE_FILE = "user-rule.txt";
public const string USER_ABP_FILE = "abp.txt";

FileSystemWatcher PACFileWatcher;
FileSystemWatcher UserRuleFileWatcher;
FileSystemWatcher? PACFileWatcher;
FileSystemWatcher? UserRuleFileWatcher;

public event EventHandler PACFileChanged;
public event EventHandler UserRuleFileChanged;
public event EventHandler? PACFileChanged;
public event EventHandler? UserRuleFileChanged;

private PACSettings _PACSettings;
private GeositeUpdater _geositeUpdater;
Expand Down
4 changes: 2 additions & 2 deletions Shadowsocks.PAC/PACServer.cs
Expand Up @@ -124,7 +124,7 @@ public override bool Handle(byte[] firstPacket, int length, Socket socket, objec
{
if (kv[0] == "Host")
{
if (kv[1].Trim() == ((IPEndPoint)socket.LocalEndPoint).ToString())
if (kv[1].Trim() == (socket.LocalEndPoint as IPEndPoint)?.ToString())
{
hostMatch = true;
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public void SendResponse(Socket socket, bool useSocks)
{
try
{
IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
IPEndPoint localEndPoint = socket.LocalEndPoint as IPEndPoint ?? throw new ArgumentException("Invalid socket local endpoint.", nameof(socket));

string proxy = GetPACAddress(localEndPoint, useSocks);

Expand Down
2 changes: 1 addition & 1 deletion Shadowsocks.WPF/Localization/LocalizationProvider.cs
Expand Up @@ -5,7 +5,7 @@ namespace Shadowsocks.WPF.Localization
{
public class LocalizationProvider
{
private static readonly string CallingAssemblyName = Assembly.GetCallingAssembly().GetName().Name;
private static readonly string CallingAssemblyName = Assembly.GetCallingAssembly().GetName().Name ?? "Shadowsocks.WPF";

public static T GetLocalizedValue<T>(string key) => LocExtension.GetLocalizedValue<T>($"{CallingAssemblyName}:Strings:{key}");
}
Expand Down
1 change: 1 addition & 0 deletions Shadowsocks.WPF/Services/PortForwarder.cs
Expand Up @@ -6,6 +6,7 @@

namespace Shadowsocks.WPF.Services
{
#nullable disable
public class PortForwarder : StreamService
{
private readonly int _targetPort;
Expand Down
6 changes: 3 additions & 3 deletions Shadowsocks.WPF/Services/Sip003Plugin.cs
Expand Up @@ -12,15 +12,15 @@ namespace Shadowsocks.WPF.Services
// https://github.com/shadowsocks/shadowsocks-org/wiki/Plugin
public sealed class Sip003Plugin : IDisposable
{
public IPEndPoint LocalEndPoint { get; private set; }
public IPEndPoint? LocalEndPoint { get; private set; }
public int ProcessId => _started ? _pluginProcess.Id : 0;

private readonly object _startProcessLock = new object();
private readonly Process _pluginProcess;
private bool _started;
private bool _disposed;

public static Sip003Plugin CreateIfConfigured(Server server, bool showPluginOutput)
public static Sip003Plugin? CreateIfConfigured(Server server, bool showPluginOutput)
{
if (server == null)
{
Expand Down Expand Up @@ -119,7 +119,7 @@ public bool StartIfNeeded()
return true;
}

public string ExpandEnvironmentVariables(string name, StringDictionary environmentVariables = null)
public string ExpandEnvironmentVariables(string name, StringDictionary? environmentVariables = null)
{
// Expand the environment variables from the new process itself
if (environmentVariables != null)
Expand Down
8 changes: 4 additions & 4 deletions Shadowsocks.WPF/Services/SystemProxy/RAS.cs
Expand Up @@ -42,7 +42,7 @@ public class RAS
// pointer to full path and file name of phone-book file
string lpszPhonebook,
// buffer to receive phone-book entries
[In, Out] RasEntryName[] lprasentryname,
[In, Out] RasEntryName[]? lprasentryname,
// size in bytes of buffer
ref int lpcb,
// number of entries written to buffer
Expand All @@ -55,14 +55,14 @@ public static string[] GetAllConnections()
int entryNameSize = 0;
int lpSize = 0;
uint retval = ESuccess;
RasEntryName[] names = null;
RasEntryName[] names = Array.Empty<RasEntryName>();

entryNameSize = Marshal.SizeOf(typeof(RasEntryName));

// Windows Vista or later: To determine the required buffer size, call RasEnumEntries
// with lprasentryname set to NULL. The variable pointed to by lpcb should be set to zero.
// The function will return the required buffer size in lpcb and an error code of ERROR_BUFFER_TOO_SMALL.
retval = RasEnumEntries(null, null, null, ref lpSize, out lpNames);
retval = RasEnumEntries("", "", null, ref lpSize, out lpNames);
if (retval == EBufferTooSmall)
{
names = new RasEntryName[lpNames];
Expand All @@ -71,7 +71,7 @@ public static string[] GetAllConnections()
names[i].dwSize = entryNameSize;
}

retval = RasEnumEntries(null, null, names, ref lpSize, out lpNames);
retval = RasEnumEntries("", "", names, ref lpSize, out lpNames);
}

if (retval == ESuccess)
Expand Down
24 changes: 12 additions & 12 deletions Shadowsocks.WPF/Services/SystemProxy/WinINet.cs
Expand Up @@ -136,7 +136,7 @@ static WinINet()
{
try
{
Record();
initialSetting = Query();
}
catch (DllNotFoundException)
{
Expand All @@ -153,9 +153,14 @@ static WinINet()
}
else
{
throw we;
throw;
}
}
finally
{
if (initialSetting == null)
initialSetting = new();
}
}

public static void ProxyGlobal(string server, string bypass)
Expand Down Expand Up @@ -188,11 +193,6 @@ public static void Direct()
Exec(options);
}

private static void Record()
{
initialSetting ??= Query();
}

public static void Restore()
{
Set(initialSetting);
Expand Down Expand Up @@ -231,7 +231,7 @@ public static WinINetSetting Query()
new InternetPerConnectionOption{dwOption = (int)InternetPerConnectionOptionEnum.AutoConfigUrl},
};

(IntPtr unmanagedList, int listSize) = PrepareOptionList(options, null);
(IntPtr unmanagedList, int listSize) = PrepareOptionList(options, "");
bool ok = InternetQueryOption(IntPtr.Zero, (int)InternetOptions.PerConnectionOption, unmanagedList, ref listSize);
if (!ok)
{
Expand Down Expand Up @@ -260,13 +260,13 @@ public static WinINetSetting Query()
proxy.Flags = (InternetPerConnectionFlags)o.Value.dwValue;
break;
case InternetPerConnectionOptionEnum.AutoConfigUrl:
proxy.AutoConfigUrl = Marshal.PtrToStringAuto(o.Value.pszValue);
proxy.AutoConfigUrl = Marshal.PtrToStringAuto(o.Value.pszValue) ?? "";
break;
case InternetPerConnectionOptionEnum.ProxyBypass:
proxy.ProxyBypass = Marshal.PtrToStringAuto(o.Value.pszValue);
proxy.ProxyBypass = Marshal.PtrToStringAuto(o.Value.pszValue) ?? "";
break;
case InternetPerConnectionOptionEnum.ProxyServer:
proxy.ProxyServer = Marshal.PtrToStringAuto(o.Value.pszValue);
proxy.ProxyServer = Marshal.PtrToStringAuto(o.Value.pszValue) ?? "";
break;
default:
break;
Expand Down Expand Up @@ -343,7 +343,7 @@ private static void ClearOptionList(IntPtr list)

private static void Exec(List<InternetPerConnectionOption> options)
{
Exec(options, null);
Exec(options, "");
foreach (string conn in RAS.GetAllConnections())
{
Exec(options, conn);
Expand Down
8 changes: 4 additions & 4 deletions Shadowsocks.WPF/Utils/AutoStartup.cs
Expand Up @@ -15,7 +15,7 @@ public static class AutoStartup

public static bool Set(bool enabled)
{
RegistryKey runKey = null;
RegistryKey? runKey = null;
try
{
runKey = Registry.CurrentUser.CreateSubKey(registryRunKey, RegistryKeyPermissionCheck.ReadWriteSubTree);
Expand All @@ -26,7 +26,7 @@ public static bool Set(bool enabled)
}
if (enabled)
{
runKey.SetValue(Key, Process.GetCurrentProcess().MainModule?.FileName);
runKey.SetValue(Key, Utilities.ExecutablePath);
}
else
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public static bool Set(bool enabled)

public static bool Check()
{
RegistryKey runKey = null;
RegistryKey? runKey = null;
try
{
runKey = Registry.CurrentUser.CreateSubKey(registryRunKey, RegistryKeyPermissionCheck.ReadWriteSubTree);
Expand All @@ -78,7 +78,7 @@ public static bool Check()
continue;
}
// Remove other startup keys with the same executable path. fixes #3011 and also assures compatibility with older versions
if (Utilities.ExecutablePath.Equals(runKey.GetValue(valueName).ToString(), StringComparison.InvariantCultureIgnoreCase)
if (Utilities.ExecutablePath.Equals(runKey.GetValue(valueName)?.ToString(), StringComparison.InvariantCultureIgnoreCase)
is bool matchedDuplicate && matchedDuplicate)
{
runKey.DeleteValue(valueName);
Expand Down
2 changes: 1 addition & 1 deletion Shadowsocks.WPF/Utils/FileManager.cs
Expand Up @@ -59,7 +59,7 @@ public static string NonExclusiveReadAllText(string path, Encoding encoding)
catch (Exception ex)
{
LogHost.Default.Error(ex, "");
throw ex;
throw;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Shadowsocks.WPF/Utils/IPCService.cs
Expand Up @@ -21,7 +21,7 @@ internal class IPCService
private const int OP_OPEN_URL = 1;
private static readonly string PIPE_PATH = $"Shadowsocks\\{Utilities.ExecutablePath.GetHashCode()}";

public event EventHandler<RequestAddUrlEventArgs> OpenUrlRequested;
public event EventHandler<RequestAddUrlEventArgs>? OpenUrlRequested;

public async void RunServer()
{
Expand Down
8 changes: 4 additions & 4 deletions Shadowsocks.WPF/Utils/ProtocolHandler.cs
Expand Up @@ -10,7 +10,7 @@ static class ProtocolHandler

public static bool Set(bool enabled)
{
RegistryKey ssURLAssociation = null;
RegistryKey? ssURLAssociation = null;

try
{
Expand Down Expand Up @@ -59,7 +59,7 @@ public static bool Set(bool enabled)

public static bool Check()
{
RegistryKey ssURLAssociation = null;
RegistryKey? ssURLAssociation = null;
try
{
ssURLAssociation = Registry.CurrentUser.OpenSubKey(ssURLRegKey, true);
Expand All @@ -69,8 +69,8 @@ public static bool Check()
return false;
}

var shellOpen = ssURLAssociation.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command");
return (string)shellOpen.GetValue("") == $"{Utilities.ExecutablePath} --open-url %1";
var shellOpen = ssURLAssociation.OpenSubKey("shell")?.OpenSubKey("open")?.OpenSubKey("command");
return shellOpen?.GetValue("") as string == $"{Utilities.ExecutablePath} --open-url %1";
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion Shadowsocks.WPF/Views/ServerSharingView.xaml.cs
Expand Up @@ -32,7 +32,7 @@ public ServerSharingView()
view => view.urlTextBox.Text)
.DisposeWith(disposables);
this.BindCommand(ViewModel,
this.BindCommand(ViewModel!,
viewModel => viewModel.CopyLink,
view => view.copyLinkButton)
.DisposeWith(disposables);
Expand Down
6 changes: 3 additions & 3 deletions Shadowsocks.WPF/Views/VersionUpdatePromptView.xaml.cs
Expand Up @@ -21,15 +21,15 @@ public VersionUpdatePromptView()
view => releaseNotesMarkdownScrollViewer.Markdown)
.DisposeWith(disposables);*/
this.BindCommand(ViewModel,
this.BindCommand(ViewModel!,
viewModel => viewModel.Update,
view => view.updateButton)
.DisposeWith(disposables);
this.BindCommand(ViewModel,
this.BindCommand(ViewModel!,
viewModel => viewModel.SkipVersion,
view => view.skipVersionButton)
.DisposeWith(disposables);
this.BindCommand(ViewModel,
this.BindCommand(ViewModel!,
viewModel => viewModel.NotNow,
view => view.notNowButton)
.DisposeWith(disposables);
Expand Down

0 comments on commit 6b0c904

Please sign in to comment.