Skip to content

Commit

Permalink
use single method to get ExecutablePath
Browse files Browse the repository at this point in the history
  • Loading branch information
Student Main committed Aug 22, 2020
1 parent f5c812e commit ced49e2
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 29 deletions.
4 changes: 2 additions & 2 deletions shadowsocks-csharp/Controller/Service/PACDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal string GetPACContent()
private void WatchPacFile()
{
PACFileWatcher?.Dispose();
PACFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
PACFileWatcher = new FileSystemWatcher(Program.WorkingDirectory);
PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
PACFileWatcher.Filter = PAC_FILE;
PACFileWatcher.Changed += PACFileWatcher_Changed;
Expand All @@ -85,7 +85,7 @@ private void WatchPacFile()
private void WatchUserRuleFile()
{
UserRuleFileWatcher?.Dispose();
UserRuleFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
UserRuleFileWatcher = new FileSystemWatcher(Program.WorkingDirectory);
UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
UserRuleFileWatcher.Filter = USER_RULE_FILE;
UserRuleFileWatcher.Changed += UserRuleFileWatcher_Changed;
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static PrivoxyRunner()
{
try
{
_uid = Application.StartupPath.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance.
_uid = Program.WorkingDirectory.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance.
_uniqueConfigFile = $"privoxy_{_uid}.conf";
_privoxyJob = new Job();

Expand Down
5 changes: 1 addition & 4 deletions shadowsocks-csharp/Controller/Service/Sip003Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using Shadowsocks.Model;
using Shadowsocks.Util.ProcessManagement;

Expand Down Expand Up @@ -55,8 +54,6 @@ private Sip003Plugin(string plugin, string pluginOpts, string pluginArgs, string
throw new ArgumentOutOfRangeException("serverPort");
}

var appPath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath);

_pluginProcess = new Process
{
StartInfo = new ProcessStartInfo
Expand All @@ -67,7 +64,7 @@ private Sip003Plugin(string plugin, string pluginOpts, string pluginArgs, string
CreateNoWindow = !showPluginOutput,
ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = appPath ?? Environment.CurrentDirectory,
WorkingDirectory = Program.WorkingDirectory ?? Environment.CurrentDirectory,
Environment =
{
["SS_REMOTE_HOST"] = serverAddress,
Expand Down
13 changes: 5 additions & 8 deletions shadowsocks-csharp/Controller/System/AutoStartup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
using NLog;
using Shadowsocks.Util;
Expand All @@ -16,9 +14,8 @@ static class AutoStartup

// Don't use Application.ExecutablePath
// see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location;

private static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();

private static string Key = "Shadowsocks_" + Program.ExecutablePath.GetHashCode();

public static bool Set(bool enabled)
{
Expand All @@ -33,7 +30,7 @@ public static bool Set(bool enabled)
}
if (enabled)
{
runKey.SetValue(Key, ExecutablePath);
runKey.SetValue(Key, Program.ExecutablePath);
}
else
{
Expand Down Expand Up @@ -82,10 +79,10 @@ public static bool Check()
else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions
{
string value = Convert.ToString(runKey.GetValue(item));
if (ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase))
if (Program.ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase))
{
runKey.DeleteValue(item);
runKey.SetValue(Key, ExecutablePath);
runKey.SetValue(Key, Program.ExecutablePath);
return true;
}
}
Expand Down
10 changes: 2 additions & 8 deletions shadowsocks-csharp/Controller/System/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -16,11 +15,6 @@ static class ProtocolHandler

private static Logger logger = LogManager.GetCurrentClassLogger();

// Don't use Application.ExecutablePath
// see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location;

// TODO: Elevate when necessary
public static bool Set(bool enabled)
{
RegistryKey ssURLAssociation = null;
Expand All @@ -38,7 +32,7 @@ public static bool Set(bool enabled)
ssURLAssociation.SetValue("", "URL:Shadowsocks");
ssURLAssociation.SetValue("URL Protocol", "");
var shellOpen = ssURLAssociation.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
shellOpen.SetValue("", $"{ExecutablePath} --open-url %1");
shellOpen.SetValue("", $"{Program.ExecutablePath} --open-url %1");
logger.Info(@"Successfully added ss:// association.");
}
else
Expand Down Expand Up @@ -81,7 +75,7 @@ public static bool Check()
}

var shellOpen = ssURLAssociation.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command");
return (string)shellOpen.GetValue("") == $"{ExecutablePath} --open-url %1";
return (string)shellOpen.GetValue("") == $"{Program.ExecutablePath} --open-url %1";
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

using Newtonsoft.Json;
using NLog;
using Shadowsocks.Controller;

namespace Shadowsocks.Model
{
Expand Down
10 changes: 6 additions & 4 deletions shadowsocks-csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ internal static class Program
public static ShadowsocksController MainController { get; private set; }
public static MenuViewController MenuController { get; private set; }
public static string[] Args { get; private set; }

// https://github.com/dotnet/runtime/issues/13051#issuecomment-510267727
public static readonly string ExecutablePath = Process.GetCurrentProcess().MainModule?.FileName;
public static readonly string WorkingDirectory = Path.GetDirectoryName(ExecutablePath);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
/// </summary>
[STAThread]
private static void Main(string[] args)
{
Directory.SetCurrentDirectory(Application.StartupPath);
Directory.SetCurrentDirectory(WorkingDirectory);
// todo: initialize the NLog configuartion
Model.NLogConfig.TouchAndApplyNLogConfig();

Expand Down Expand Up @@ -59,7 +63,7 @@ private static void Main(string[] args)
}
return;
}
string pipename = $"Shadowsocks\\{Application.StartupPath.GetHashCode()}";
string pipename = $"Shadowsocks\\{ExecutablePath.GetHashCode()}";

string addedUrl = null;

Expand Down Expand Up @@ -134,8 +138,6 @@ private static void Main(string[] args)
Application.SetCompatibleTextRenderingDefault(false);
AutoStartup.RegisterForRestart(true);

Directory.SetCurrentDirectory(Application.StartupPath);

#if DEBUG
// truncate privoxy log file while debugging
string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks-csharp/Util/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static string GetTempPath()
}
else
{
_tempPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), @"Shadowsocks\ss_win_temp_" + Application.ExecutablePath.GetHashCode())).FullName;
_tempPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), @"Shadowsocks\ss_win_temp_" + Program.ExecutablePath.GetHashCode())).FullName;
}
}
catch (Exception e)
Expand Down

0 comments on commit ced49e2

Please sign in to comment.