Skip to content

Commit

Permalink
Added basic Import Export functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kidfearless committed Aug 31, 2020
1 parent 5d8c2d8 commit 72606ab
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 37 deletions.
12 changes: 7 additions & 5 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
<StackPanel x:Name="StackPanel" Background="#FF232323" Margin="0,0,0,0">
<Grid Margin="0,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="1.5*"/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition Width=".2*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0">Name</Label>
<Label Grid.Column="1">Steam</Label>
<Label Grid.Column="2">Cheat List</Label>
<Label Grid.Column="3">Threat Level</Label>
<Label Grid.Column="1">Name</Label>
<Label Grid.Column="2">Steam</Label>
<Label Grid.Column="3">Cheat List</Label>
<Label Grid.Column="4">Threat Level</Label>
</Grid>
</StackPanel>
</ScrollViewer>
Expand Down
177 changes: 150 additions & 27 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
using DangerZoneHackerTracker.Models;
using Keyboard = DangerZoneHackerTracker.Models.Keyboard;
using Path = System.IO.Path;
using System.Windows.Media.Imaging;
using System.Net;
using System.Text;
using Microsoft.Win32;
using SQLite;


/*
Expand Down Expand Up @@ -61,8 +66,10 @@ public partial class MainWindow
Regex SteamIDRegex;
Regex MapNameRegex;
Regex CommunityURLRegex;
Regex CommunityProfilePictureRegex;
List<uint> AlertedPlayers;
Dictionary<uint, ConnectedUser> Users;
Dictionary<uint, Image> ProfilePictures;
string CurrentMap = "";


Expand All @@ -71,18 +78,29 @@ public MainWindow()
{
InitializeComponent();

var id = new SteamID("STEAM_0:1:29867327");

// Initialize properties
Users = new Dictionary<uint, ConnectedUser>();
ProfilePictures = new Dictionary<uint, Image>();
AlertedPlayers = new List<uint>();
SteamIDRegex = new Regex(string.Format(@"(\\?{0}.*\\?{0}) (STEAM_\d:\d:\d+)", "\""));
MapNameRegex = new Regex(@"map\s+: (\w+)");
CommunityURLRegex = new Regex(@"(\d+)");
CommunityProfilePictureRegex = new Regex(string.Format(@"<link rel={0}image_src{0} href={0}(.*){0}>", '"'));

if(!Directory.Exists(Path.GetDirectoryName(Constants.DatabasePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(Constants.DatabasePath));
}
// Create Table
using var db = new DatabaseConnection();
db.CreateTable<Cheater>();
db.CreateTable<Settings>();
var setting = db.Table<Settings>().SingleOrDefault();
if (setting != null && setting.StatusKey != null)
{
BtnAutoStatus.Content = $"<{setting.StatusKey}>";
StatusKey = (Key)setting.StatusKey;
}

// Create a repeating timer to check the console file
var timer = new System.Timers.Timer(TimeSpan.FromSeconds(10).TotalMilliseconds);
Expand Down Expand Up @@ -209,27 +227,40 @@ private void ReadConsole(object nill)
// update the list of users in the app
foreach (var user in Users.Values)
{
StackPanel.Children.Add(this.CreateUserRow(user.Name, user.SteamID.Render()));
StackPanel.Children.Add(this.CreateUserRow(user.Name, user.SteamID));
}
});
}
}

private void ExportClicked(object sender, RoutedEventArgs e)
{
// TODO: Implement
ShowToastAsync(new Cheater()
var dialog = new SaveFileDialog();
dialog.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "exported_cheaters.sq3");
dialog.DefaultExt = ".sq3";
if((bool)dialog.ShowDialog())
{
LastKnownName = "KiD Fearless",
ThreatLevel = 10,
CheatList = "Aimbot, Bhop scripts, Spinbot, God"
});
File.Copy(Constants.DatabasePath, dialog.FileName);
}

PlayHax();
//dialog.Filter = "sqlite files (*.sq3)|*.sq3|All Files (*.*)|*.*";
}

private void ImportClicked(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Filter = "sqlite files (*.sq3)|*.sq3|All Files (*.*)|*.*";
dialog.Multiselect = false;
if((bool)dialog.ShowDialog())
{
using var otherDB = new SQLiteConnection(dialog.FileName, Constants.Flags);
var cheaterList = otherDB.Table<Cheater>().ToList();
using var myDB = new DatabaseConnection();
foreach (var cheater in cheaterList)
{
myDB.InsertOrReplace(cheater, typeof(Cheater));
}
}

// TODO: Implement
}
Expand Down Expand Up @@ -298,6 +329,8 @@ private void OnKeyDown(object sender, KeyEventArgs e)
{
BtnAutoStatus.Content = $"<{e.Key}>";
StatusKey = e.Key;
using var db = new DatabaseConnection();
db.InsertOrReplace(new Settings() { StatusKey = e.Key }, typeof(Settings));
}

IsTrackingStatusKey = false;
Expand Down Expand Up @@ -361,20 +394,25 @@ private void PlayHax()
/// <param name="name"></param>
/// <param name="steamid"></param>
/// <returns></returns>
private Grid CreateUserRow(string name, string steamid)
private Grid CreateUserRow(string name, SteamID steamid)
{
/*
* <Label Grid.Column="0">Name</Label>
<Label Grid.Column="1">Steam</Label>
<Label Grid.Column="2">Cheat List</Label>
<Label Grid.Column="3">Threat Level</Label>
<Label Grid.Column="4">Add</Label>
* <Label Grid.Column="0">pic</Label>
* <Label Grid.Column="1">Name</Label>
<Label Grid.Column="2">Steam</Label>
<Label Grid.Column="3">Cheat List</Label>
<Label Grid.Column="4">Threat Level</Label>
<Label Grid.Column="5">Add</Label>
*/
Grid grid = new Grid();
//column definitions have to be unique... so this exists
grid.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(2, GridUnitType.Star)
Width = new GridLength(0.5, GridUnitType.Star)
});
grid.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(1.5, GridUnitType.Star)
});
grid.ColumnDefinitions.Add(new ColumnDefinition()
{
Expand All @@ -396,19 +434,38 @@ private Grid CreateUserRow(string name, string steamid)
// create our controls
Label lName = new Label()
{
Content = name
Content = name.Replace("_", "__"),
Margin = new Thickness(0.0, 48/4, 48/4, 5.0)
};
// single underscores have special meanings and have to be escaped
Label steamID = new Label()
{
Content = steamid.Replace("_", "__")
Content = steamid.Render(false).Replace("_", "__"),
Margin = new Thickness(0.0, 48 / 4, 48 / 4, 5.0)

};
var cheatList = new TextBox()
{
Margin = new Thickness(5.0, 0.0, 5.0, 0.0)
};
var threatLevel = new TextBox()
{
Margin = new Thickness(5.0, 0.0, 5.0, 0.0)
};
var profilePicture = GetProfilePicture(steamid);
profilePicture.MouseDown += (object sender, MouseButtonEventArgs e) =>
{
try
{
Process.Start(new ProcessStartInfo($"http://steamcommunity.com/profiles/{steamid.ConvertToUInt64()}") { UseShellExecute = true });
}
catch { }
};
var cheatList = new TextBox();
var threatLevel = new TextBox();

var addButton = new Button()
{
Content = "Add"
Content = "Add",
Margin = new Thickness(0, 0, 10, 0)
};

// since we aren't tracking any of our objects outside of this function we create an anonymous function so we can reference the intended objects.
Expand All @@ -429,20 +486,86 @@ private Grid CreateUserRow(string name, string steamid)
};

// add our controls to the grid
grid.Children.Add(profilePicture);
grid.Children.Add(lName);
grid.Children.Add(steamID);
grid.Children.Add(cheatList);
grid.Children.Add(threatLevel);
grid.Children.Add(addButton);

// tell our controls which grid column they should use.
Grid.SetColumn(lName, 0);
Grid.SetColumn(steamID, 1);
Grid.SetColumn(cheatList, 2);
Grid.SetColumn(threatLevel, 3);
Grid.SetColumn(addButton, 4);
Grid.SetColumn(profilePicture, 0);
Grid.SetColumn(lName, 1);
Grid.SetColumn(steamID, 2);
Grid.SetColumn(cheatList, 3);
Grid.SetColumn(threatLevel, 4);
Grid.SetColumn(addButton, 5);

return grid;
}

private Image CreateImage(string source, double width = 48.0, double height = 48.0)
{
var image = new Image();

BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(source, UriKind.Absolute);
bitmap.EndInit();

image.Source = bitmap;
image.Width = width;
image.Height = height;

return image;
}

private string GetProfilePictureURL(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream;

if (string.IsNullOrWhiteSpace(response.CharacterSet))
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}

string line;
while((line = readStream.ReadLine()) != null)
{
var match = CommunityProfilePictureRegex.Match(line);
if(match.Success)
{
response.Close();
readStream.Close();

return match.Groups[1].Value;
}
}

response.Close();
readStream.Close();
}

return string.Empty;
}

private Image GetProfilePicture(SteamID steam)
{

var url = $"http://steamcommunity.com/profiles/{steam.ConvertToUInt64()}";
var profilePicURL = GetProfilePictureURL(url);
var image = CreateImage(profilePicURL);
return image;
}
}
}
3 changes: 1 addition & 2 deletions Models/Cheater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ namespace DangerZoneHackerTracker
[Table("Cheaters")]
public class Cheater
{
[PrimaryKey, AutoIncrement, Column(nameof(Id))]
public int Id { get; set; }
[Column(nameof(AccountID))]
[Unique]
public uint AccountID { get; set; }
[Column(nameof(ThreatLevel))]
public int ThreatLevel { get; set; }
Expand Down
7 changes: 4 additions & 3 deletions Models/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace DangerZoneHackerTracker
{
class Constants
{
public const string DatabaseFilename = "Cheaters.db3";
public const string DatabaseFilename = "Cheaters.sq3";

public const SQLite.SQLiteOpenFlags Flags =
// open the database in read/write mode
Expand All @@ -21,8 +21,9 @@ public static string DatabasePath
{
get
{
var basePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return Path.Combine(basePath, DatabaseFilename);
var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var folder = Path.Combine(basePath, "HackerTracker");
return Path.Combine(folder, DatabaseFilename);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions Models/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace DangerZoneHackerTracker.Models
{
class Settings
{
public Key? StatusKey { get; set; }
}
}

0 comments on commit 72606ab

Please sign in to comment.