Browse Source

Add check update for sing-box

pull/5853/head
2dust 1 month ago
parent
commit
d004c6860e
  1. 25
      v2rayN/ServiceLib/Common/FileManager.cs
  2. 2
      v2rayN/ServiceLib/Services/UpdateService.cs
  3. 32
      v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs

25
v2rayN/ServiceLib/Common/FileManager.cs

@ -1,4 +1,5 @@
using System.IO.Compression;
using System.Formats.Tar;
using System.IO.Compression;
using System.Text;
namespace ServiceLib.Common
@ -19,7 +20,7 @@ namespace ServiceLib.Common
return false;
}
public static void UncompressedFile(string fileName, byte[] content)
public static void DecompressFile(string fileName, byte[] content)
{
try
{
@ -33,7 +34,7 @@ namespace ServiceLib.Common
}
}
public static void UncompressedFile(string fileName, string toPath, string? toName)
public static void DecompressFile(string fileName, string toPath, string? toName)
{
try
{
@ -49,6 +50,20 @@ namespace ServiceLib.Common
}
}
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);
@ -139,7 +154,7 @@ namespace ServiceLib.Common
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);
@ -166,7 +181,7 @@ namespace ServiceLib.Common
continue;
}
var targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
file.CopyTo(targetFilePath, true);
}
// If recursive and copying subdirectories, recursively call this method

2
v2rayN/ServiceLib/Services/UpdateService.cs

@ -107,7 +107,7 @@ namespace ServiceLib.Services
_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);
}

32
v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs

@ -66,15 +66,12 @@ namespace ServiceLib.ViewModels
CoreType = ECoreType.mihomo.ToString(),
Remarks = ResUI.menuCheckUpdate,
});
if (Utils.IsWindows())
{
_checkUpdateItem.Add(new CheckUpdateItem()
{
IsSelected = true,
CoreType = ECoreType.sing_box.ToString(),
Remarks = ResUI.menuCheckUpdate,
});
}
_checkUpdateItem.Add(new CheckUpdateItem()
{
IsSelected = true,
@ -149,16 +146,6 @@ namespace ServiceLib.ViewModels
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);
@ -262,15 +249,24 @@ namespace ServiceLib.ViewModels
{
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
{
@ -299,12 +295,10 @@ namespace ServiceLib.ViewModels
public void UpdateViewResult(CheckUpdateItem item)
{
var found = _checkUpdateItem.FirstOrDefault(t => t.CoreType == item.CoreType);
if (found != null)
{
if (found == null) return;
var itemCopy = JsonUtils.DeepCopy(found);
itemCopy.Remarks = item.Remarks;
_checkUpdateItem.Replace(found, itemCopy);
}
}
}
}
Loading…
Cancel
Save