Files
valheim-mod-sync-desktop/Valheim Mod Sync Desktop/Views/SyncControl.xaml.cs
2025-08-01 21:10:51 +02:00

1159 lines
44 KiB
C#

using System;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Text.Json;
using System.IO;
using Application = System.Windows.Application;
using System.IO.Compression;
using System.Diagnostics;
using System.Threading;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Linq.Expressions;
using System.Collections.Specialized;
using System.Web;
using System.Text;
namespace Valheim_Mod_Sync_Desktop.Views
{
public partial class SyncControl : System.Windows.Controls.UserControl
{
public SyncControl()
{
InitializeComponent();
DeleteOld();
if (IsConnectionAvailable())
{
if (Properties.Settings.Default.Server.Contains(":"))
{
try
{
if (IsConnectionAvailableModServer())
{
CheckForUpdate(false, true, true);
}
else
{
output.Text += (string)Application.Current.FindResource("ServerModServerError");
Sync_Update.IsEnabled = false;
}
}
catch
{
Debug.WriteLine(IsConnectionAvailableTBS());
output.Text += (string)Application.Current.FindResource("ServerModServerError");
Sync_Update.IsEnabled = false;
}
}else
{
output.Text += (string)Application.Current.FindResource("ServerModKonfigurieren");
Sync_Update.IsEnabled = false;
}
if (IsConnectionAvailableTBS())
{
CheckForUpdate(true, false, false);
CheckUnsendMessage();
}else
{
output.Text += (string)Application.Current.FindResource("ServerTBSError");
Changelog.IsEnabled = false;
}
} else
{
output.Text = (string)Application.Current.FindResource("SyncInternet");
Sync_Update.IsEnabled = false;
Changelog.IsEnabled = false;
}
Changelog.Content = Application.Current.FindResource("ChangelogIO") + Assembly.GetExecutingAssembly().GetName().Version.ToString();
Pfad.Text = Properties.Settings.Default.Pfad;
}
public static bool IsConnectionAvailable()
{
bool connect = NetworkInterface.GetIsNetworkAvailable();
return connect;
}
public static bool IsConnectionAvailableTBS()
{
try
{
using (WebClient client = new WebClient())
{
string url = "https://vms.tbs-server.de/check.php";
NameValueCollection data = new NameValueCollection();
data["Lizenz"] = Valheim_Mod_Sync_Desktop.Properties.Settings.Default.Lizenz;
byte[] responseBytes = client.UploadValues(url, "POST", data);
NameValueCollection response = HttpUtility.ParseQueryString(Encoding.UTF8.GetString(responseBytes));
return true;
}
}
catch(WebException ex)
{
Debug.WriteLine("Fehler beim Zugriff IsConnectionAvailableTBS:");
Debug.WriteLine("Nachricht: " + ex.Message);
Debug.WriteLine("Statuscode: " + ex.Status);
return false;
}
}
static bool IsConnectionAvailableModServer_bool = false;
public static bool IsConnectionAvailableModServer()
{
TcpClient TcpScan = new TcpClient();
var ip = Properties.Settings.Default.Server.Substring(0, Properties.Settings.Default.Server.LastIndexOf(":") + 1);
ip = ip.Replace(":", "");
var port = Properties.Settings.Default.Server.Substring(Properties.Settings.Default.Server.LastIndexOf(":"));
port = port.Replace(":", "");
var portInt = Int32.Parse(port);
using (TcpClient tcp = new TcpClient())
{
var ar = tcp.BeginConnect(ip, portInt, null, null);
using (ar.AsyncWaitHandle)
{
//Wait 2 seconds for connection.
if (ar.AsyncWaitHandle.WaitOne(2000, false))
{
try
{
//Connect was successful.
tcp.EndConnect(ar);
IsConnectionAvailableModServer_bool = true;
return true;
}
catch(WebException ex)
{
//EndConnect threw an exception.
//Most likely means the server refused the connection.
//IsConnectionAvailableModServer_bool = false;
Debug.WriteLine("Fehler beim Zugriff auf IsConnectionAvailableModServer:");
Debug.WriteLine("Nachricht: " + ex.Message);
Debug.WriteLine("Statuscode: " + ex.Status);
IsConnectionAvailableModServer_bool = false;
return false;
}
}
else
{
//Connection timed out.
IsConnectionAvailableModServer_bool = false;
return false;
}
}
}
}
public async void CheckUnsendMessage()
{
if (File.Exists("Message.json"))
{
await Kontakt.MessageSend();
}
}
//Pfad Select
private void Auswählen_Pfad_Click(object sender, RoutedEventArgs e)
{
using (FolderBrowserDialog ValheimPath = new FolderBrowserDialog())
{
ValheimPath.SelectedPath = @"C:\\";
ValheimPath.ShowDialog();
if (!ValheimPath.SelectedPath.Equals(@"C:\\"))
{
Pfad.Text = ValheimPath.SelectedPath;
Properties.Settings.Default.Pfad = ValheimPath.SelectedPath;
Properties.Settings.Default.Save();
}
}
}
//Start Valheim
private static void Valheim(bool autostart)
{
if (!autostart)
{
var uri = "steam://rungameid/892970";
var psi = new ProcessStartInfo();
psi.UseShellExecute = true;
psi.FileName = uri;
Process.Start(psi);
Application.Current.MainWindow.Close();
}
if (autostart)
{
Benachrichtigung modalWindow = new Benachrichtigung(false, false, false, false, false, false, false, true);
modalWindow.ShowDialog();
}
}
private void Valheim_Event(object sender, RoutedEventArgs e)
{
Valheim(false);
}
//Synchronisieren
private async void Sync(object sender, RoutedEventArgs e)
{
if (File.Exists("Valheim.zip"))
{
File.Delete("Valheim.zip");
}
if (File.Exists(Properties.Settings.Default.Pfad + "\\BepInEx\\backup\\config.zip"))
{
File.Delete(Properties.Settings.Default.Pfad + "\\BepInEx\\backup\\config.zip");
}
if (File.Exists(Properties.Settings.Default.Pfad + "\\BepInEx\\backup\\plugins.zip"))
{
File.Delete(Properties.Settings.Default.Pfad + "\\BepInEx\\backup\\plugins.zip");
}
if (Pfad.Text == "...")
{
output.Text += Application.Current.FindResource("SyncPfadError");
return;
}
if (VersionModRemote - VersionModLocal > 1 && VersionModLocal != 0)
{
output.Text += Application.Current.FindResource("SyncVerfügbarModUpdateSicherung");
output.Text += Properties.Settings.Default.Pfad + "\\BepInEx\\backup";
if (!Directory.Exists(Properties.Settings.Default.Pfad + "\\BepInEx\\backup\\"))
{
Directory.CreateDirectory(Properties.Settings.Default.Pfad + "\\BepInEx\\backup\\");
}
ZipFile.CreateFromDirectory(Properties.Settings.Default.Pfad + "\\BepInEx\\config", Properties.Settings.Default.Pfad + "\\BepInEx\\backup\\config.zip");
ZipFile.CreateFromDirectory(Properties.Settings.Default.Pfad + "\\BepInEx\\plugins", Properties.Settings.Default.Pfad + "\\BepInEx\\backup\\plugins.zip");
}
//prüfe auf entfernte mods und configs
string PluginstoDelete = IsConnectionAvailableModServer_bool ? ReadJson("PluginFilestoDelete", false, true) : "";
string ClientSidePluginstoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ClientSidePluginFilestoDelete", false, true) : "";
string PluginsFolderFilestoDelete = IsConnectionAvailableModServer_bool ? ReadJson("PluginFolderFilestoDelete", false, true) : "";
string ClientSidePluginsFolderFilestoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ClientSidePluginFolderFilestoDelete", false, true) : "";
string ConfigstoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ConfigFilestoDelete", false, true) : "";
string ClientSideConfigstoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ClientSideConfigFilestoDelete", false, true) : "";
string ConfigsFolderFilestoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ConfigFolderFilestoDelete", false, true) : "";
string ClientSideConfigsFolderFilestoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ClientSideConfigFolderFilestoDelete", false, true) : "";
if(PluginstoDelete != null | ClientSidePluginstoDelete != null | ConfigstoDelete != null | ClientSideConfigstoDelete != null | PluginsFolderFilestoDelete != null | ClientSidePluginsFolderFilestoDelete != null | ConfigsFolderFilestoDelete != null | ClientSideConfigsFolderFilestoDelete != null && VersionModLocal != 0)
{
output.Text += Application.Current.FindResource("SyncDeleteOldFiles");
}
if (PluginstoDelete != null && VersionModLocal != 0)
{
string[] PluginstoDeleteArray = PluginstoDelete.Split('|');
foreach (string PluginName in PluginstoDeleteArray)
{
File.Delete(Properties.Settings.Default.Pfad+"/"+PluginName);
}
}
if (ClientSidePluginstoDelete != null && VersionModLocal != 0)
{
string[] ClientSidePluginstoDeleteArray = ClientSidePluginstoDelete.Split('|');
foreach (string ClientSidePluginName in ClientSidePluginstoDeleteArray)
{
File.Delete(Properties.Settings.Default.Pfad + "/" + ClientSidePluginName.Replace("ClientSide", "BepInEx"));
}
}
if (ConfigstoDelete != null && VersionModLocal != 0)
{
string[] ConfigstoDeleteArray = ConfigstoDelete.Split('|');
foreach (string ConfigName in ConfigstoDeleteArray)
{
File.Delete(Properties.Settings.Default.Pfad+"/"+ConfigName);
}
}
if (ClientSideConfigstoDelete != null && VersionModLocal != 0)
{
string[] ClientSideConfigstoDeleteArray = ClientSideConfigstoDelete.Split('|');
foreach (string ClientSideConfigName in ClientSideConfigstoDeleteArray)
{
File.Delete(Properties.Settings.Default.Pfad + "/" + ClientSideConfigName.Replace("ClientSide", "BepInEx"));
}
}
if (ConfigsFolderFilestoDelete != null && VersionModLocal != 0)
{
string[] ConfigsFolderFilestoDeleteArray = ConfigsFolderFilestoDelete.Split('|');
foreach (string ConfigFolderFileName in ConfigsFolderFilestoDeleteArray)
{
File.Delete(Properties.Settings.Default.Pfad + "/" + ConfigFolderFileName);
}
}
if (ClientSideConfigsFolderFilestoDelete != null && VersionModLocal != 0)
{
string[] ClientSideConfigsFolderFilestoDeleteArray = ClientSideConfigsFolderFilestoDelete.Split('|');
foreach (string ClientSideConfigFolderFileName in ClientSideConfigsFolderFilestoDeleteArray)
{
File.Delete(Properties.Settings.Default.Pfad + "/" + ClientSideConfigFolderFileName.Replace("ClientSide", "BepInEx"));
}
}
if (PluginsFolderFilestoDelete != null && VersionModLocal != 0)
{
string[] PluginsFolderFilestoDeleteArray = PluginsFolderFilestoDelete.Split('|');
foreach (string PluginFolderFileName in PluginsFolderFilestoDeleteArray)
{
File.Delete(Properties.Settings.Default.Pfad + "/" + PluginFolderFileName);
}
}
if (ClientSidePluginsFolderFilestoDelete != null && VersionModLocal != 0)
{
string[] ClientSidePluginsFolderFilestoDeleteArray = ClientSidePluginsFolderFilestoDelete.Split('|');
foreach (string ClientSidePluginFolderFileName in ClientSidePluginsFolderFilestoDeleteArray)
{
File.Delete(Properties.Settings.Default.Pfad + "/" + ClientSidePluginFolderFileName.Replace("ClientSide", "BepInEx"));
}
}
//prüfe auf entfernte mods und configs ordner
string PluginsFoldertoDelete = IsConnectionAvailableModServer_bool ? ReadJson("PluginFoldertoDelete", false, true) : "";
string ClientSidePluginsFoldertoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ClientSidePluginFoldertoDelete", false, true) : "";
string ConfigsFoldertoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ConfigFoldertoDelete", false, true) : "";
string ClientSideConfigsFoldertoDelete = IsConnectionAvailableModServer_bool ? ReadJson("ClientSideConfigFoldertoDelete", false, true) : "";
if (PluginsFoldertoDelete != null | ClientSidePluginsFoldertoDelete != null | ConfigsFoldertoDelete != null | ClientSideConfigsFoldertoDelete != null && VersionModLocal != 0)
{
output.Text += Application.Current.FindResource("SyncDeleteOldFolder");
}
if (PluginsFoldertoDelete != null && VersionModLocal != 0)
{
string[] PluginsFoldertoDeleteArray = PluginsFoldertoDelete.Split('|');
foreach (string PluginFolderName in PluginsFoldertoDeleteArray)
{
Directory.Delete(Properties.Settings.Default.Pfad + "/" + PluginFolderName);
}
}
if (ClientSidePluginsFoldertoDelete != null && VersionModLocal != 0)
{
string[] ClientSidePluginsFoldertoDeleteArray = ClientSidePluginsFoldertoDelete.Split('|');
foreach (string ClientSidePluginFolderName in ClientSidePluginsFoldertoDeleteArray)
{
Directory.Delete(Properties.Settings.Default.Pfad + "/" + ClientSidePluginFolderName.Replace("ClientSide", "BepInEx"));
}
}
if (ConfigsFoldertoDelete != null && VersionModLocal != 0)
{
string[] ConfigsFoldertoDeleteArray = ConfigsFoldertoDelete.Split('|');
foreach (string ConfigFolderName in ConfigsFoldertoDeleteArray)
{
Directory.Delete(Properties.Settings.Default.Pfad + "/" + ConfigFolderName);
}
}
if (ClientSideConfigsFoldertoDelete != null && VersionModLocal != 0)
{
string[] ClientSideConfigsFoldertoDeleteArray = ClientSideConfigsFoldertoDelete.Split('|');
foreach (string ClientSideConfigFolderName in ClientSideConfigsFoldertoDeleteArray)
{
Directory.Delete(Properties.Settings.Default.Pfad + "/" + ClientSideConfigFolderName.Replace("ClientSide", "BepInEx"));
}
}
output.Text += Application.Current.FindResource("SyncStartDownload");
await Download("Valheim.zip", Properties.Settings.Default.Server);
output.Text += Application.Current.FindResource("SyncFertigDownload");
ZipFile.ExtractToDirectory("Valheim.zip", Properties.Settings.Default.Pfad, true);
output.Text += Application.Current.FindResource("SyncFertigEntpacken");
await Task.Run(() => Thread.Sleep(2000));
File.Delete("Valheim.zip");
Properties.Settings.Default.ModVersion = Int32.Parse(ReadJson("ModVersion", false, true));
Properties.Settings.Default.Save();
if (Properties.Settings.Default.Autostart)
{
Valheim(true);
}
else
{//####################################################################### handlung umändern auf benachrichtigung ?
Sync_Update.Content = Application.Current.FindResource("StartValheim");
Sync_Update.Click -= new RoutedEventHandler(Sync);
Sync_Update.Click += new RoutedEventHandler(Valheim_Event);
}
}
//Methode on Changelog Click.
private void Changelog_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
GetChangelog(false);
}
//Get Changelog
static string before;
bool changelog;
private void GetChangelog(bool neu)
{
bool test = true;
int InterVersion1 = InterVersion + 1;
var InterVersionString = InterVersion1.ToString();
InterVersionString = InterVersionString.Insert(1, ".");
InterVersionString = InterVersionString.Insert(3, ".");
InterVersionString = InterVersionString.Insert(5, ".");
WebClient wc = new WebClient();
string changelog_text = wc.DownloadString("https://vms.tbs-server.de/Changelog_" + Properties.Settings.Default.Sprache + ".txt");
if (!neu)
{
if (!changelog)
{
before = output.Text;
output.Text = "Changelog:";
foreach (string line in changelog_text.Split("\r\n"))
{
if (line.Equals("v" + InterVersionString))
{
test = false;
}
if (test)
{
output.Text += "\n" + line;
}
}
changelog = true;
}
else
{
output.Text = before;
changelog = false;
}
}
if (neu)
{
int VersionSyncInt = Int32.Parse(ReadJson("VersionSync", false, false)) + 1;
string VersionSync = VersionSyncInt.ToString();
VersionSync = VersionSync.Insert(1, ".");
VersionSync = VersionSync.Insert(3, ".");
VersionSync = VersionSync.Insert(5, ".");
output.Text += "\nChangelog:";
foreach (string line in changelog_text.Split("\r\n"))
{
if (line.Equals("v" + InterVersionString))
{
test = false;
}
if (line.Equals("v" + VersionSync))
{
test = true;
}
if (!test)
{
output.Text += "\n" + line;
}
}
}
}
//Json
public class Version_Json
{
public string VersionSync { get; set; }
public string PluginVersion { get; set; }
}
public class Version_JsonRemote
{
public string ModVersion { get; set; }
public string PluginVersion { get; set; }
public string ConfigFilestoDelete { get; set; }
public string ClientSideConfigFilestoDelete { get; set; }
public string ConfigFoldertoDelete { get; set; }
public string ClientSideConfigFoldertoDelete { get; set; }
public string ConfigFolderFilestoDelete { get; set; }
public string ClientSideConfigFolderFilestoDelete { get; set; }
public string PluginFilestoDelete { get; set; }
public string ClientSidePluginFilestoDelete { get; set; }
public string PluginFoldertoDelete { get; set; }
public string ClientSidePluginFoldertoDelete { get; set; }
public string PluginFolderFilestoDelete { get; set; }
public string ClientSidePluginFolderFilestoDelete { get; set; }
}
private static string ReadJson(string value, bool show, bool remote)
{
if (!remote)
{
Version_Json version_read = JsonSerializer.Deserialize<Version_Json>(new WebClient().DownloadString("https://vms.tbs-server.de/version.json"));
if (value.Equals("VersionSync"))
{
var json = version_read.VersionSync;
if (show)
{
json = json.Insert(1, ".");
json = json.Insert(3, ".");
json = json.Insert(5, ".");
}
return json;
}
if (value.Equals("PluginVersion"))
{
var json = version_read.PluginVersion;
if (show)
{
json = json.Insert(1, ".");
json = json.Insert(3, ".");
json = json.Insert(5, ".");
}
return json;
}
} else if (remote)
{
//if (IsConnectionAvailableModServer_bool) {
try
{
if (Properties.Settings.Default.Server.Contains("https://") | Properties.Settings.Default.Server.Contains("http://"))
{
Version_JsonRemote version_readRemote = JsonSerializer.Deserialize<Version_JsonRemote>(new WebClient().DownloadString(Properties.Settings.Default.Server + "/version.json"));
if (value.Equals("ModVersion"))
{
var json = version_readRemote.ModVersion;
if (show)
{
json = json.Insert(1, ".");
json = json.Insert(3, ".");
json = json.Insert(5, ".");
}
return json;
}
if (value.Equals("PluginVersion"))
{
var json = version_readRemote.PluginVersion;
if (show)
{
json = json.Insert(1, ".");
json = json.Insert(3, ".");
json = json.Insert(5, ".");
}
return json;
}
if (value.Equals("PluginFilestoDelete"))
{
var json = version_readRemote.PluginFilestoDelete;
return json;
}
if (value.Equals("ClientSidePluginFilestoDelete"))
{
var json = version_readRemote.ClientSidePluginFilestoDelete;
return json;
}
if (value.Equals("PluginFoldertoDelete"))
{
var json = version_readRemote.PluginFoldertoDelete;
return json;
}
if (value.Equals("ClientSidePluginFoldertoDelete"))
{
var json = version_readRemote.ClientSidePluginFoldertoDelete;
return json;
}
if (value.Equals("PluginFolderFilestoDelete"))
{
var json = version_readRemote.PluginFolderFilestoDelete;
return json;
}
if (value.Equals("ClientSidePluginFolderFilestoDelete"))
{
var json = version_readRemote.ClientSidePluginFolderFilestoDelete;
return json;
}
if (value.Equals("ConfigFilestoDelete"))
{
var json = version_readRemote.ConfigFilestoDelete;
return json;
}
if (value.Equals("ClientSideConfigFilestoDelete"))
{
var json = version_readRemote.ClientSideConfigFilestoDelete;
return json;
}
if (value.Equals("ConfigFoldertoDelete"))
{
var json = version_readRemote.ConfigFoldertoDelete;
return json;
}
if (value.Equals("ClientSideConfigFoldertoDelete"))
{
var json = version_readRemote.ClientSideConfigFoldertoDelete;
return json;
}
if (value.Equals("ConfigFolderFilestoDelete"))
{
var json = version_readRemote.ConfigFolderFilestoDelete;
return json;
}
if (value.Equals("ClientSideConfigFolderFilestoDelete"))
{
var json = version_readRemote.ClientSideConfigFolderFilestoDelete;
return json;
}
}
else if (!Properties.Settings.Default.Server.Contains("https://") | Properties.Settings.Default.Server.Contains("http://"))
{
Version_JsonRemote version_readRemote = JsonSerializer.Deserialize<Version_JsonRemote>(new WebClient().DownloadString("http://" + Properties.Settings.Default.Server + "/version.json"));
if (value.Equals("ModVersion"))
{
var json = version_readRemote.ModVersion;
if (show)
{
json = json.Insert(1, ".");
json = json.Insert(3, ".");
json = json.Insert(5, ".");
}
return json;
}
if (value.Equals("PluginVersion"))
{
var json = version_readRemote.PluginVersion;
if (show)
{
json = json.Insert(1, ".");
json = json.Insert(3, ".");
json = json.Insert(5, ".");
}
return json;
}
if (value.Equals("PluginFilestoDelete"))
{
var json = version_readRemote.PluginFilestoDelete;
return json;
}
if (value.Equals("ClientSidePluginFilestoDelete"))
{
var json = version_readRemote.ClientSidePluginFilestoDelete;
return json;
}
if (value.Equals("PluginFoldertoDelete"))
{
var json = version_readRemote.PluginFoldertoDelete;
return json;
}
if (value.Equals("ClientSidePluginFoldertoDelete"))
{
var json = version_readRemote.ClientSidePluginFoldertoDelete;
return json;
}
if (value.Equals("PluginFolderFilestoDelete"))
{
var json = version_readRemote.PluginFolderFilestoDelete;
return json;
}
if (value.Equals("ClientSidePluginFolderFilestoDelete"))
{
var json = version_readRemote.ClientSidePluginFolderFilestoDelete;
return json;
}
if (value.Equals("ConfigFilestoDelete"))
{
var json = version_readRemote.ConfigFilestoDelete;
return json;
}
if (value.Equals("ClientSideConfigFilestoDelete"))
{
var json = version_readRemote.ClientSideConfigFilestoDelete;
return json;
}
if (value.Equals("ConfigFoldertoDelete"))
{
var json = version_readRemote.ConfigFoldertoDelete;
return json;
}
if (value.Equals("ClientSideConfigFoldertoDelete"))
{
var json = version_readRemote.ClientSideConfigFoldertoDelete;
return json;
}
if (value.Equals("ConfigFolderFilestoDelete"))
{
var json = version_readRemote.ConfigFolderFilestoDelete;
return json;
}
if (value.Equals("ClientSideConfigFolderFilestoDelete"))
{
var json = version_readRemote.ClientSideConfigFolderFilestoDelete;
return json;
}
}
else if (Properties.Settings.Default.Server.Contains("https://") | !Properties.Settings.Default.Server.Contains("https://"))
{
Version_JsonRemote version_readRemote = JsonSerializer.Deserialize<Version_JsonRemote>(new WebClient().DownloadString("https://" + Properties.Settings.Default.Server + "/version.json"));
if (value.Equals("ModVersion"))
{
var json = version_readRemote.ModVersion;
if (show)
{
json = json.Insert(1, ".");
json = json.Insert(3, ".");
json = json.Insert(5, ".");
}
return json;
}
if (value.Equals("PluginVersion"))
{
var json = version_readRemote.PluginVersion;
if (show)
{
json = json.Insert(1, ".");
json = json.Insert(3, ".");
json = json.Insert(5, ".");
}
return json;
}
if (value.Equals("PluginFilestoDelete"))
{
var json = version_readRemote.PluginFilestoDelete;
return json;
}
if (value.Equals("ClientSidePluginFilestoDelete"))
{
var json = version_readRemote.ClientSidePluginFilestoDelete;
return json;
}
if (value.Equals("PluginFoldertoDelete"))
{
var json = version_readRemote.PluginFoldertoDelete;
return json;
}
if (value.Equals("ClientSidePluginFoldertoDelete"))
{
var json = version_readRemote.ClientSidePluginFoldertoDelete;
return json;
}
if (value.Equals("PluginFolderFilestoDelete"))
{
var json = version_readRemote.PluginFolderFilestoDelete;
return json;
}
if (value.Equals("ClientSidePluginFolderFilestoDelete"))
{
var json = version_readRemote.ClientSidePluginFolderFilestoDelete;
return json;
}
if (value.Equals("ConfigFilestoDelete"))
{
var json = version_readRemote.ConfigFilestoDelete;
return json;
}
if (value.Equals("ClientSideConfigFilestoDelete"))
{
var json = version_readRemote.ClientSideConfigFilestoDelete;
return json;
}
if (value.Equals("ConfigFoldertoDelete"))
{
var json = version_readRemote.ConfigFoldertoDelete;
return json;
}
if (value.Equals("ClientSideConfigFoldertoDelete"))
{
var json = version_readRemote.ClientSideConfigFoldertoDelete;
return json;
}
if (value.Equals("ConfigFolderFilestoDelete"))
{
var json = version_readRemote.ConfigFolderFilestoDelete;
return json;
}
if (value.Equals("ClientSideConfigFolderFilestoDelete"))
{
var json = version_readRemote.ClientSideConfigFolderFilestoDelete;
return json;
}
}
}catch(WebException ex)
{
Debug.WriteLine("Fehler beim Zugriff auf den JSON REMOTE:");
Debug.WriteLine("Nachricht: " + ex.Message);
Debug.WriteLine("Statuscode: " + ex.Status);
return "0000";
}
//}
//else
//{
// return "0001";
//}
}
return "0000";
}
//Version check
//static int VersionSyncJson = Int32.Parse("1002");
static int VersionSyncJson = Int32.Parse(ReadJson("VersionSync", false, false));
static int VersionPluginJson = Int32.Parse(ReadJson("PluginVersion", false, false));
static int InterVersion = Int32.Parse(Assembly.GetExecutingAssembly().GetName().Version.ToString().Replace(".", ""));
static int VersionModRemote = IsConnectionAvailableModServer_bool ? Int32.Parse(ReadJson("ModVersion", false, true)) : 0;
static int PluginVersionRemote = IsConnectionAvailableModServer_bool ? Int32.Parse(ReadJson("PluginVersion", false, true)) : 0;
static int VersionModLocal = Properties.Settings.Default.ModVersion;
private void CheckForUpdate(bool client, bool modserver, bool plugin)
{
//Debug.WriteLine(VersionModLocal + " / " + VersionModRemote);
if (client)
{
if (VersionSyncJson <= InterVersion)
{
output.Text += Application.Current.FindResource("SyncKeinClientUpdate");
}
else
{
output.Text += Application.Current.FindResource("SyncVerfügbarClientUpdate") + ReadJson("VersionSync", true, false);
Sync_Update.Content = Application.Current.FindResource("SyncUpdate");
Sync_Update.Click -= new RoutedEventHandler(Sync);
Sync_Update.Click += new RoutedEventHandler(Update);
Sync_Update.IsEnabled = true;
GetChangelog(true);
}
}
if (modserver)
{
if (VersionModRemote == VersionModLocal)
{
output.Text += Application.Current.FindResource("SyncKeinModUpdate");
Sync_Update.Content = Application.Current.FindResource("StartValheim");
Sync_Update.Click -= new RoutedEventHandler(Sync);
Sync_Update.Click += new RoutedEventHandler(Valheim_Event);
}
else
{
output.Text += Application.Current.FindResource("SyncVerfügbarModUpdate");
//unterschied festellen
if (VersionModRemote - VersionModLocal > 1 && VersionModLocal != 0)
{
output.Text += Application.Current.FindResource("SyncVerfügbarModUpdateZuWeit");
}
}
}
if (plugin)
{
if (VersionPluginJson <= PluginVersionRemote)
{
output.Text += Application.Current.FindResource("SyncKeinPluginUpdate");
}
else
{
output.Text += Application.Current.FindResource("SyncVerfügbarPluginUpdate");
Sync_Update.IsEnabled = false;
}
}
}
public static void CheckUpdateAutostart()
{
if (Properties.Settings.Default.Server != "..." && Properties.Settings.Default.Server.Contains(":"))
{
if (IsConnectionAvailableModServer())
{
int VersionModRemote = Int32.Parse(ReadJson("ModVersion", false, true));
int VersionModLocal = Properties.Settings.Default.ModVersion;
if (VersionModRemote <= VersionModLocal && VersionSyncJson <= InterVersion)
{
if (Properties.Settings.Default.Autostart == true)
{
Valheim(true);
}
}
}
}
}
//Start Update
private async void Update(object sender, RoutedEventArgs e)
{
output.Text += Application.Current.FindResource("SyncClientStartUpdate");
await Rename();
try
{
await Download("Valheim Mod Sync Desktop.exe", "https://vms.tbs-server.de/");
await Task.Run(() => File.Move("Valheim Mod Sync Desktop.exe", System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe"));
await Task.Run(() => File.SetAttributes(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe", FileAttributes.Normal));
}
catch (WebException)
{
output.Text = (string)Application.Current.FindResource("SyncClientUpdateError");
}
output.Text += Application.Current.FindResource("SyncClientUpdateFertig");
await Start();
}
private async Task Rename()
{
await Task.Run(() => File.Move(System.Diagnostics.Process.GetCurrentProcess().ProcessName+".exe", "vms.old"));
await Task.Run(() => File.SetAttributes("vms.old", FileAttributes.Normal));
}
private async Task Download(string Datei, string URL)
{
if (URL.Contains("https://") | URL.Contains("http://"))
{
await Task.Run(() => new WebClient().DownloadFile(URL + "/" + Datei, Datei));
}else if (!URL.Contains("https://") | !URL.Contains("http://"))
{
try
{
await Task.Run(() => new WebClient().DownloadFile("http://"+URL + "/" + Datei, Datei));
}catch
{
if (File.Exists(Datei))
{
File.Delete(Datei);
}
try
{
await Task.Run(() => new WebClient().DownloadFile("https://"+URL + "/" + Datei, Datei));
}catch
{
if (File.Exists(Datei))
{
File.Delete(Datei);
}
output.Text += Application.Current.FindResource("DownloadError");
}
}
}
}
private async Task Start()
{
await Task.Run(() => Thread.Sleep(5000));
Process.Start(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe");
Application.Current.MainWindow.Close();
}
private void DeleteOld()
{
if (File.Exists("vms.old"))
{
if (File.GetAttributes("vms.old") == FileAttributes.Normal)
{
File.Delete("vms.old");
}
else
{
File.SetAttributes("vms.old", FileAttributes.Normal);
DeleteOld();
}
}
}
}
}