Skip to content

Commit

Permalink
Merge pull request #17 from lastbattle/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
lastbattle authored Apr 23, 2020
2 parents 2257c89 + d09e4eb commit 22480f0
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 23 deletions.
35 changes: 35 additions & 0 deletions HaRepacker/Converter/IntegerULongCommasConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright (C) 2020 LastBattle
* https://github.com/lastbattle/Harepacker-resurrected
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace HaRepacker.Converter
{
/// <summary>
/// Converts a ulong integer to a string seperated by commas
/// </summary>
public class IntegerULongCommasConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ulong value_ = (ulong)value;

return value_.ToString("N0");
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return 0;
}
}
}

73 changes: 73 additions & 0 deletions HaRepacker/Converter/TicksToRelativeTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Copyright (C) 2020 LastBattle
* https://github.com/lastbattle/Harepacker-resurrected
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace HaRepacker.Converter
{
/// <summary>
/// Convert ticks to human readable relative time
/// </summary>
public class TicksToRelativeTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
long ticks = (long)value;

const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;

var ts = new TimeSpan(ticks);
double delta = Math.Abs(ts.TotalSeconds);

if (delta < 1 * MINUTE)
return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";

if (delta < 2 * MINUTE)
return "a minute ago";

if (delta < 45 * MINUTE)
return ts.Minutes + " minutes ago";

if (delta < 90 * MINUTE)
return "an hour ago";

if (delta < 24 * HOUR)
return ts.Hours + " hours ago";

if (delta < 48 * HOUR)
return "yesterday";

if (delta < 30 * DAY)
return ts.Days + " days ago";

if (delta < 12 * MONTH)
{
int months = (int) (Math.Floor((double)ts.Days / 30));
return months <= 1 ? "one month ago" : months + " months ago";
}
else
{
int years = (int) (Math.Floor((double)ts.Days / 365));
return years <= 1 ? "one year ago" : years + " years ago";
}
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return 0; // faek value
}
}
}
61 changes: 52 additions & 9 deletions HaRepacker/GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Reflection;
using HaRepacker.GUI.Panels.SubPanels;
using MapleLib.PacketLib;
using System.Timers;

namespace HaRepacker.GUI
{
Expand Down Expand Up @@ -587,6 +588,8 @@ private void AddTabsInternal(string defaultName = null)
private DateTime wzKeyBruteforceStartTime = DateTime.Now;
private bool wzKeyBruteforceCompleted = false;

private System.Timers.Timer aTimer_wzKeyBruteforce = null;

/// <summary>
/// Find needles in a haystack o_O
/// </summary>
Expand All @@ -605,7 +608,9 @@ private void StartWzKeyBruteforcing(Dispatcher currentDispatcher)
return;

// Show splash screen
MainPanel.OnSetPanelLoading();
MainPanel.OnSetPanelLoading(currentDispatcher);
MainPanel.loadingPanel.SetWzIvBruteforceStackpanelVisiblity(System.Windows.Visibility.Visible);


// Reset variables
wzKeyBruteforceTries = 0;
Expand All @@ -620,19 +625,56 @@ private void StartWzKeyBruteforcing(Dispatcher currentDispatcher)
cpuIds.Add(cpuId_);
}

var parallelOption = new ParallelOptions
// UI update thread
if (aTimer_wzKeyBruteforce != null)
{
MaxDegreeOfParallelism = processorCount,
};
ParallelLoopResult loop = Parallel.ForEach(cpuIds, parallelOption, cpuId =>
aTimer_wzKeyBruteforce.Stop();
aTimer_wzKeyBruteforce = null;
}
aTimer_wzKeyBruteforce = new System.Timers.Timer();
aTimer_wzKeyBruteforce.Elapsed += new ElapsedEventHandler(OnWzIVKeyUIUpdateEvent);
aTimer_wzKeyBruteforce.Interval = 5000;
aTimer_wzKeyBruteforce.Enabled = true;


// Key finder thread
Task.Run(() =>
{
wzKeyBruteforceComputeTask(cpuId, processorCount, dialog, currentDispatcher);
Thread.Sleep(3000); // delay 3 seconds before starting
var parallelOption = new ParallelOptions
{
MaxDegreeOfParallelism = processorCount,
};
ParallelLoopResult loop = Parallel.ForEach(cpuIds, parallelOption, cpuId =>
{
wzKeyBruteforceComputeTask(cpuId, processorCount, dialog, currentDispatcher);
});
});
}
}

// load complete
/// <summary>
/// UI Updating thread
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void OnWzIVKeyUIUpdateEvent(object source, ElapsedEventArgs e)
{
if (aTimer_wzKeyBruteforce == null)
return;
if (wzKeyBruteforceCompleted)
{
aTimer_wzKeyBruteforce.Stop();
aTimer_wzKeyBruteforce = null;

MainPanel.loadingPanel.SetWzIvBruteforceStackpanelVisiblity(System.Windows.Visibility.Collapsed);
}

MainPanel.loadingPanel.WzIvKeyDuration = DateTime.Now.Ticks - wzKeyBruteforceStartTime.Ticks;
MainPanel.loadingPanel.WzIvKeyTries = wzKeyBruteforceTries;
}

/// <summary>
/// Internal compute task for figuring out the WzKey automaticagically
/// </summary>
Expand Down Expand Up @@ -679,7 +721,8 @@ private void wzKeyBruteforceComputeTask(int cpuId_, int processorCount, OpenFile
// Hide panel splash sdcreen
Action action = () =>
{
MainPanel.OnSetPanelLoadingCompleted();
MainPanel.OnSetPanelLoadingCompleted(currentDispatcher);
MainPanel.loadingPanel.SetWzIvBruteforceStackpanelVisiblity(System.Windows.Visibility.Collapsed);
};
currentDispatcher.BeginInvoke(action);

Expand Down
16 changes: 12 additions & 4 deletions HaRepacker/GUI/Panels/MainPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,30 +546,38 @@ public void PromptRenameSelectedTreeNode()
#region Panel Loading Events
/// <summary>
/// Set panel loading splash screen from MainForm.cs
/// <paramref name="currentDispatcher"/>
/// </summary>
public void OnSetPanelLoading()
public void OnSetPanelLoading(Dispatcher currentDispatcher = null)
{
Action action = () =>
{
loadingPanel.OnStartAnimate();
grid_LoadingPanel.Visibility = Visibility.Visible;
treeView_WinFormsHost.Visibility = Visibility.Collapsed;
};
grid_LoadingPanel.Dispatcher.BeginInvoke(action);
if (currentDispatcher != null)
currentDispatcher.BeginInvoke(action);
else
grid_LoadingPanel.Dispatcher.BeginInvoke(action);
}

/// <summary>
/// Remove panel loading splash screen from MainForm.cs
/// <paramref name="currentDispatcher"/>
/// </summary>
public void OnSetPanelLoadingCompleted()
public void OnSetPanelLoadingCompleted(Dispatcher currentDispatcher = null)
{
Action action = () =>
{
loadingPanel.OnPauseAnimate();
grid_LoadingPanel.Visibility = Visibility.Collapsed;
treeView_WinFormsHost.Visibility = Visibility.Visible;
};
grid_LoadingPanel.Dispatcher.BeginInvoke(action);
if (currentDispatcher != null)
currentDispatcher.BeginInvoke(action);
else
grid_LoadingPanel.Dispatcher.BeginInvoke(action);
}
#endregion

Expand Down
26 changes: 26 additions & 0 deletions HaRepacker/GUI/Panels/SubPanels/LoadingPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
xmlns:local="clr-namespace:HaRepacker.GUI.Panels.SubPanels"
mc:Ignorable="d"

xmlns:converter="clr-namespace:HaRepacker.Converter"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
gif:ImageBehavior.AnimateInDesignMode="True"

d:DesignHeight="350" d:DesignWidth="640">

<UserControl.Resources>
<converter:TicksToRelativeTimeConverter x:Key="ticksToRelativeTimeConverter" />
<converter:IntegerULongCommasConverter x:Key="integerULongCommasConverter"/>
</UserControl.Resources>

<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
Expand All @@ -26,5 +32,25 @@

<TextBlock Text="{x:Static p:Resources.Subpanel_Loading}" FontSize="25" Foreground="Black"
Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Roboto Mono Medium"/>

<!-- wzIv-->
<StackPanel x:Name="stackPanel_wzIvBruteforceStat"
Visibility="Collapsed"
Orientation="Vertical"
Grid.Row="0"
HorizontalAlignment="Right" VerticalAlignment="Top" Margin="30">
<TextBlock Text="Wz IV Key bruteforcer"/>

<StackPanel Orientation="Horizontal" Margin="10,0,0,0">
<TextBlock Text="Keys tested: " Foreground="Gray"/>
<TextBlock Text="{Binding WzIvKeyTries, Converter={StaticResource integerULongCommasConverter}}" Foreground="Gray"/>
<TextBlock Text="/ " Foreground="Gray"/>
<TextBlock Text="4,294,967,294" Foreground="Gray"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10,0,0,0">
<TextBlock Text="Duration: " Foreground="Gray"/>
<TextBlock Text="{Binding WzIvKeyDuration, Converter={StaticResource ticksToRelativeTimeConverter}}" Foreground="Gray"/>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
57 changes: 53 additions & 4 deletions HaRepacker/GUI/Panels/SubPanels/LoadingPanel.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -19,13 +20,15 @@ namespace HaRepacker.GUI.Panels.SubPanels
/// <summary>
/// Interaction logic for LoadingPanel.xaml
/// </summary>
public partial class LoadingPanel : UserControl
public partial class LoadingPanel : UserControl, INotifyPropertyChanged
{
private ImageAnimationController imageController = null;

public LoadingPanel()
{
InitializeComponent();

this.DataContext = this; // set data binding to self.
}


Expand All @@ -37,23 +40,69 @@ public LoadingPanel()
private void ImageLoadingGif_AnimationLoaded(object sender, RoutedEventArgs e)
{
imageController = ImageBehavior.GetAnimationController(imageLoadingGif);
imageController.Pause(); // pause by default
}

/// <summary>
///
/// </summary>
public void OnStartAnimate()
{
imageController.Play();
//imageController.Play(); // doesnt animate when Visibility is collapsed anyway.
}

/// <summary>
///
/// </summary>
public void OnPauseAnimate()
{
imageController.Pause();
//imageController.Pause();
}

/// <summary>
/// Sets the visibility for the stats on wzIv bruteforcing
/// </summary>
/// <param name="visibility"></param>
public void SetWzIvBruteforceStackpanelVisiblity(Visibility visibility)
{
stackPanel_wzIvBruteforceStat.Visibility = visibility;
}

#region Exported Fields
private ulong _WzIvKeyTries = 0;
public ulong WzIvKeyTries
{
get { return _WzIvKeyTries; }
set
{
_WzIvKeyTries = value;
OnPropertyChanged("WzIvKeyTries");
}
}

private long _WzIvKeyDuration = 0; // number of ticks
public long WzIvKeyDuration
{
get { return _WzIvKeyDuration; }
set
{
_WzIvKeyDuration = value;
OnPropertyChanged("WzIvKeyDuration");
}
}
#endregion


#region PropertyChanged
/// <summary>
/// Property changed event handler to trigger update UI
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
Loading

0 comments on commit 22480f0

Please sign in to comment.