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

174 lines
6.7 KiB
C#

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Valheim_Mod_Sync_Desktop
{
public partial class Lizenz : Window
{
public class LizenzReturn
{
public bool success { get; set; }
public LizenzDetail data { get; set; }
}
public class LizenzDetail
{
public int timesActivatedMax { get; set; }
public int remainingActivations { get; set; }
public int timesActivated { get; set; }
public string hwid { get; set; }
}
public Lizenz()
{
InitializeComponent();
Sprache.Text = Properties.Settings.Default.Sprache;
}
//Close Button
private void exitBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.Close();
MainWindow.LizenzReturn = "false";
}
//Minimize Button
private void miniBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
//Header Move
private void header_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
//Save Sprache
private void Sprache_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem sprachItem = (ComboBoxItem)Sprache.SelectedItem;
string sprachvalue = sprachItem.Content.ToString();
Properties.Settings.Default.Sprache = sprachvalue;
Properties.Settings.Default.Save();
var app1 = (App)Application.Current;
app1.ChangeLanguage(new Uri("/Language/" + sprachvalue + ".xaml", UriKind.RelativeOrAbsolute));
}
private async void bestätigen_Click(object sender, RoutedEventArgs e)
{
var LizenzKeyClean = LizenzKey.Text.Replace(" ", "");
try
{
string html = string.Empty;
string url = @"https://tbs-server.de//wp-json/lmfwc/v2/licenses/validate/" + LizenzKeyClean;
string encoded = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1")
.GetBytes(Properties.Settings.Default.benutzername + ":" + Properties.Settings.Default.passwort));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.Headers.Add("Authorization", "Basic " + encoded);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
LizenzReturn lizenz_return = JsonSerializer.Deserialize<LizenzReturn>(html);
//Debug.WriteLine(lizenz_return.success);
//Debug.WriteLine(lizenz_return.data.timesActivated);
//Debug.WriteLine(lizenz_return.data.timesActivatedMax);
//Debug.WriteLine(lizenz_return.data.remainingActivations);
//Debug.WriteLine(lizenz_return.data.hwid);
//Debug.WriteLine(html);
if (lizenz_return.data.timesActivated != lizenz_return.data.timesActivatedMax && lizenz_return.data.remainingActivations != 0)
{
string html1 = string.Empty;
string url1 = @"https://tbs-server.de//wp-json/lmfwc/v2/licenses/activate/" + LizenzKeyClean + "?hwid=" + HardwareID();
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);
request1.AutomaticDecompression = DecompressionMethods.GZip;
request1.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(Properties.Settings.Default.benutzername + ":" + Properties.Settings.Default.passwort));
using (HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse())
using (Stream stream1 = response1.GetResponseStream())
using (StreamReader reader1 = new StreamReader(stream1))
{
html1 = reader1.ReadToEnd();
}
outputlizenz.Text = (string)Application.Current.FindResource("LizenzKeyAktiviert");
MainWindow.LizenzReturn = "true";
Properties.Settings.Default.Lizenz = LizenzKeyClean;
Properties.Settings.Default.Save();
await Task.Delay(2000);
this.Close();
}
else if (lizenz_return.data.hwid != HardwareID())
{
outputlizenz.Text = (string)Application.Current.FindResource("LizenzKeyBereitsAktiviert");
}
else if (lizenz_return.data.hwid == HardwareID())
{
Properties.Settings.Default.Lizenz = LizenzKeyClean;
Properties.Settings.Default.Save();
outputlizenz.Text = (string)Application.Current.FindResource("LizenzKeyAktiviert");
MainWindow.LizenzReturn = "true";
Properties.Settings.Default.Lizenz = LizenzKeyClean;
Properties.Settings.Default.Save();
await Task.Delay(2000);
this.Close();
}
}
catch
{
outputlizenz.Text = (string)Application.Current.FindResource("LizenzKeyFehler");
}
}
public static string HardwareID()
{
string location = @"SOFTWARE\Microsoft\Cryptography";
string name = "MachineGuid";
using (RegistryKey localMachineX64View =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (RegistryKey rk = localMachineX64View.OpenSubKey(location))
{
if (rk == null)
throw new KeyNotFoundException(
string.Format("Key Not Found: {0}", location));
object machineGuid = rk.GetValue(name);
if (machineGuid == null)
throw new IndexOutOfRangeException(
string.Format("Index Not Found: {0}", name));
return machineGuid.ToString();
}
}
}
}
}