Update MainFormHandler.cs

This commit is contained in:
passerby-b 2021-11-30 15:30:19 +08:00 committed by GitHub
parent 53bc6af2f0
commit fd00485e91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using v2rayN.Base; using v2rayN.Base;
using v2rayN.Mode; using v2rayN.Mode;
@ -9,7 +11,7 @@ namespace v2rayN.Handler
class MainFormHandler class MainFormHandler
{ {
private static MainFormHandler instance; private static MainFormHandler instance;
Action<bool, string> updateUI; Action<bool, string> _updateUI;
//private DownloadHandle downloadHandle2; //private DownloadHandle downloadHandle2;
//private Config _config; //private Config _config;
@ -187,103 +189,82 @@ namespace v2rayN.Handler
return counter; return counter;
} }
public void BackupGuiNConfig(Config config, bool auto = false)
public void UpdateSubscriptionProcess(Config config, Action<bool, string> update)
{ {
updateUI = update; string fileName = string.Empty;
if (auto)
updateUI(false, UIRes.I18N("MsgUpdateSubscriptionStart"));
if (config.subItem == null || config.subItem.Count <= 0)
{ {
updateUI(false, UIRes.I18N("MsgNoValidSubscription")); fileName = Utils.GetTempPath($"guiNConfig{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.json");
return;
} }
else
for (int k = 1; k <= config.subItem.Count; k++)
{ {
string id = config.subItem[k - 1].id.TrimEx(); SaveFileDialog fileDialog = new SaveFileDialog
string url = config.subItem[k - 1].url.TrimEx();
string hashCode = $"{k}->";
if (config.subItem[k - 1].enabled == false)
{ {
continue; Filter = "guiNConfig|*.json",
} FilterIndex = 2,
if (Utils.IsNullOrEmpty(id) || Utils.IsNullOrEmpty(url)) RestoreDirectory = true
{
updateUI(false, $"{hashCode}{UIRes.I18N("MsgNoValidSubscription")}");
continue;
}
DownloadHandle downloadHandle3 = new DownloadHandle();
downloadHandle3.UpdateCompleted += (sender2, args) =>
{
if (args.Success)
{
updateUI(false, $"{hashCode}{UIRes.I18N("MsgGetSubscriptionSuccessfully")}");
string result = Utils.Base64Decode(args.Msg);
if (Utils.IsNullOrEmpty(result))
{
updateUI(false, $"{hashCode}{UIRes.I18N("MsgSubscriptionDecodingFailed")}");
return;
}
ConfigHandler.RemoveServerViaSubid(ref config, id);
updateUI(false, $"{hashCode}{UIRes.I18N("MsgClearSubscription")}");
// RefreshServers();
int ret = MainFormHandler.Instance.AddBatchServers(config, result, id);
if (ret > 0)
{
// RefreshServers();
}
else
{
updateUI(false, $"{hashCode}{UIRes.I18N("MsgFailedImportSubscription")}");
}
updateUI(true, $"{hashCode}{UIRes.I18N("MsgUpdateSubscriptionEnd")}");
}
else
{
updateUI(false, args.Msg);
}
}; };
downloadHandle3.Error += (sender2, args) => if (fileDialog.ShowDialog() != DialogResult.OK)
{ {
updateUI(false, args.GetException().Message); return;
}; }
fileName = fileDialog.FileName;
downloadHandle3.WebDownloadString(url);
updateUI(false, $"{hashCode}{UIRes.I18N("MsgStartGettingSubscriptions")}");
} }
}
public void BackupGuiNConfig(Config config)
{
SaveFileDialog fileDialog = new SaveFileDialog
{
Filter = "guiNConfig|*.json",
FilterIndex = 2,
RestoreDirectory = true
};
if (fileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
string fileName = fileDialog.FileName;
if (Utils.IsNullOrEmpty(fileName)) if (Utils.IsNullOrEmpty(fileName))
{ {
return; return;
} }
if (Utils.ToJsonFile(config, fileName) == 0) var ret = Utils.ToJsonFile(config, fileName);
if (!auto)
{ {
UI.Show(UIRes.I18N("OperationSuccess")); if (ret == 0)
} {
else
{ UI.Show(UIRes.I18N("OperationSuccess"));
UI.ShowWarning(UIRes.I18N("OperationFailed")); }
else
{
UI.ShowWarning(UIRes.I18N("OperationFailed"));
}
} }
} }
public void UpdateTask(Config config, Action<bool, string> update)
{
_updateUI = update;
Task.Run(() => UpdateTaskRun(config));
}
private void UpdateTaskRun(Config config)
{
var updateHandle = new UpdateHandle();
while (true)
{
Utils.SaveLog("UpdateTaskRun");
Thread.Sleep(60000);
if (config.autoUpdateInterval <= 0)
{
continue;
}
updateHandle.UpdateGeoFile("geosite", config, (bool success, string msg) =>
{
_updateUI(false, msg);
if (success)
Utils.SaveLog("geosite" + msg);
});
Thread.Sleep(60000);
updateHandle.UpdateGeoFile("geoip", config, (bool success, string msg) =>
{
_updateUI(false, msg);
if (success)
Utils.SaveLog("geoip" + msg);
});
Thread.Sleep(1000 * 3600 * config.autoUpdateInterval);
}
}
} }
} }