Skip to content

Commit

Permalink
Add check update for sing-box
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 17, 2024
1 parent fc3ba6c commit d004c68
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
25 changes: 20 additions & 5 deletions v2rayN/ServiceLib/Common/FileManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO.Compression;
using System.Formats.Tar;
using System.IO.Compression;
using System.Text;

namespace ServiceLib.Common
Expand All @@ -19,7 +20,7 @@ public static bool ByteArrayToFile(string fileName, byte[] content)
return false;
}

public static void UncompressedFile(string fileName, byte[] content)
public static void DecompressFile(string fileName, byte[] content)
{
try
{
Expand All @@ -33,7 +34,7 @@ public static void UncompressedFile(string fileName, byte[] content)
}
}

public static void UncompressedFile(string fileName, string toPath, string? toName)
public static void DecompressFile(string fileName, string toPath, string? toName)
{
try
{
Expand All @@ -49,6 +50,20 @@ public static void UncompressedFile(string fileName, string toPath, string? toNa
}
}

public static void DecompressTarFile(string fileName, string toPath)
{
try
{
using var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
using var gz = new GZipStream(fs, CompressionMode.Decompress, leaveOpen: true);
TarFile.ExtractToDirectory(gz, toPath, overwriteFiles: true);
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
}
}

public static string NonExclusiveReadAllText(string path)
{
return NonExclusiveReadAllText(path, Encoding.Default);
Expand Down Expand Up @@ -139,7 +154,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, string? ignoredName)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
Expand All @@ -166,7 +181,7 @@ public static void CopyDirectory(string sourceDir, string destinationDir, bool r
continue;
}
var targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
file.CopyTo(targetFilePath, true);
}

// If recursive and copying subdirectories, recursively call this method
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Services/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async Task CheckUpdateCore(ECoreType type, Config config, Action<bool, st
_updateFunc?.Invoke(false, args.Msg);

url = args.Url;
var ext = Path.GetExtension(url);
var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url);
fileName = Utils.GetTempPath(Utils.GetGuid() + ext);
await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout);
}
Expand Down
48 changes: 21 additions & 27 deletions v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ private void RefreshSubItems()
CoreType = ECoreType.mihomo.ToString(),
Remarks = ResUI.menuCheckUpdate,
});
if (Utils.IsWindows())
_checkUpdateItem.Add(new CheckUpdateItem()
{
_checkUpdateItem.Add(new CheckUpdateItem()
{
IsSelected = true,
CoreType = ECoreType.sing_box.ToString(),
Remarks = ResUI.menuCheckUpdate,
});
}
IsSelected = true,
CoreType = ECoreType.sing_box.ToString(),
Remarks = ResUI.menuCheckUpdate,
});
_checkUpdateItem.Add(new CheckUpdateItem()
{
IsSelected = true,
Expand Down Expand Up @@ -149,16 +146,6 @@ void _updateUI(bool success, string msg)

private async Task CheckUpdateN(bool preRelease)
{
////Check for standalone windows .Net version
//if (Utils.IsWindows()
// && File.Exists(Path.Combine(Utils.StartupPath(), "wpfgfx_cor3.dll"))
// && File.Exists(Path.Combine(Utils.StartupPath(), "D3DCompiler_47_cor3.dll"))
// )
//{
// UpdateView(_v2rayN, ResUI.UpdateStandalonePackageTip);
// return;
//}

void _updateUI(bool success, string msg)
{
UpdateView(_v2rayN, msg);
Expand Down Expand Up @@ -262,15 +249,24 @@ private void UpgradeCore()
{
continue;
}
string toPath = Utils.GetBinPath("", item.CoreType);
var toPath = Utils.GetBinPath("", item.CoreType);

if (fileName.Contains(".tar.gz"))
{
//It's too complicated to unzip. TODO
FileManager.DecompressTarFile(fileName, toPath);
var dir = new DirectoryInfo(toPath);
if (dir.Exists)
{
foreach (var subDir in dir.GetDirectories())
{
FileManager.CopyDirectory(subDir.FullName, toPath, false, null);
subDir.Delete(true);
}
}
}
else if (fileName.Contains(".gz"))
{
FileManager.UncompressedFile(fileName, toPath, item.CoreType);
FileManager.DecompressFile(fileName, toPath, item.CoreType);
}
else
{
Expand Down Expand Up @@ -299,12 +295,10 @@ private void UpdateView(string coreType, string msg)
public void UpdateViewResult(CheckUpdateItem item)
{
var found = _checkUpdateItem.FirstOrDefault(t => t.CoreType == item.CoreType);
if (found != null)
{
var itemCopy = JsonUtils.DeepCopy(found);
itemCopy.Remarks = item.Remarks;
_checkUpdateItem.Replace(found, itemCopy);
}
if (found == null) return;
var itemCopy = JsonUtils.DeepCopy(found);
itemCopy.Remarks = item.Remarks;
_checkUpdateItem.Replace(found, itemCopy);
}
}
}

0 comments on commit d004c68

Please sign in to comment.