Skip to content

Commit

Permalink
Copy the bin folder to the storage location (for init)
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jan 18, 2025
1 parent 0953237 commit 54fe669
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
10 changes: 7 additions & 3 deletions v2rayN/ServiceLib/Common/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static bool CreateFromDirectory(string sourceDirectoryName, string destin
return true;
}

public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, string? ignoredName)
public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, bool overwrite, string? ignoredName = null)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
Expand All @@ -183,7 +183,11 @@ public static void CopyDirectory(string sourceDir, string destinationDir, bool r
continue;
}
var targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath, true);
if (!overwrite && File.Exists(targetFilePath))
{
continue;
}
file.CopyTo(targetFilePath, overwrite);
}

// If recursive and copying subdirectories, recursively call this method
Expand All @@ -192,7 +196,7 @@ public static void CopyDirectory(string sourceDir, string destinationDir, bool r
foreach (var subDir in dirs)
{
var newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true, ignoredName);
CopyDirectory(subDir.FullName, newDestinationDir, true, overwrite, ignoredName);
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions v2rayN/ServiceLib/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public static int GetFreePort(int defaultPort = 9090)

public static bool UpgradeAppExists(out string upgradeFileName)
{
upgradeFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, GetExeName("AmazTool"));
upgradeFileName = Path.Combine(GetBaseDirectory(), GetExeName("AmazTool"));
return File.Exists(upgradeFileName);
}

Expand Down Expand Up @@ -666,12 +666,12 @@ public static bool HasWritePermission()
try
{
//When this file exists, it is equivalent to having no permission to read and write
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NotStoreConfigHere.txt")))
if (File.Exists(Path.Combine(GetBaseDirectory(), "NotStoreConfigHere.txt")))
{
return false;
}

var tempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "guiTemps");
var tempPath = Path.Combine(GetBaseDirectory(), "guiTemps");
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
Expand Down Expand Up @@ -699,19 +699,24 @@ public static string GetPath(string fileName)
return Path.Combine(startupPath, fileName);
}

public static string GetBaseDirectory(string fileName = "")
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
}

public static string GetExePath()
{
return Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName ?? string.Empty;
}

public static string StartupPath()
{
if (Utils.IsNonWindows() && Environment.GetEnvironmentVariable(Global.LocalAppData) == "1")
if (Environment.GetEnvironmentVariable(Global.LocalAppData) == "1")
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "v2rayN");
}

return AppDomain.CurrentDomain.BaseDirectory;
return GetBaseDirectory();
}

public static string GetTempPath(string filename = "")
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Handler/AppHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public bool IsAdministrator

public bool InitApp()
{
if (Utils.IsNonWindows() && Utils.HasWritePermission() == false)
if (Utils.HasWritePermission() == false)
{
Environment.SetEnvironmentVariable(Global.LocalAppData, "1", EnvironmentVariableTarget.Process);
}
Expand Down
11 changes: 11 additions & 0 deletions v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public async Task Init(Config config, Action<bool, string> updateFunc)
Environment.SetEnvironmentVariable(Global.V2RayLocalAsset, Utils.GetBinPath(""), EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable(Global.XrayLocalAsset, Utils.GetBinPath(""), EnvironmentVariableTarget.Process);

//Copy the bin folder to the storage location (for init)
if (Environment.GetEnvironmentVariable(Global.LocalAppData) == "1")
{
var fromPath = Utils.GetBaseDirectory("bin");
var toPath = Utils.GetBinPath("");
if (fromPath != toPath)
{
FileManager.CopyDirectory(fromPath, toPath, true, false);
}
}

if (Utils.IsNonWindows())
{
var coreInfo = CoreInfoHandler.Instance.GetCoreInfo();
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private async Task<bool> CreateZipFileFromDirectory(string fileName)
var configDirZipTemp = Utils.GetTempPath($"v2rayN_{DateTime.Now:yyyyMMddHHmmss}");
var configDirTemp = Path.Combine(configDirZipTemp, _guiConfigs);

FileManager.CopyDirectory(configDir, configDirTemp, false, "cache.db");
FileManager.CopyDirectory(configDir, configDirTemp, false, true, "cache.db");
var ret = FileManager.CreateFromDirectory(configDirZipTemp, fileName);
Directory.Delete(configDirZipTemp, true);
return await Task.FromResult(ret);
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private async Task UpgradeCore()
{
foreach (var subDir in dir.GetDirectories())
{
FileManager.CopyDirectory(subDir.FullName, toPath, false, null);
FileManager.CopyDirectory(subDir.FullName, toPath, false, true);
subDir.Delete(true);
}
}
Expand Down

0 comments on commit 54fe669

Please sign in to comment.