2014-07-12 08:53:00 +00:00
//#define LOCAL_SERVER
2015-07-06 14:29:34 +00:00
using System ; //Copyright (c) NorbiPeti 2015 - See LICENSE file
2014-04-25 21:55:43 +00:00
using System.Collections.Generic ;
2014-07-12 08:53:00 +00:00
using System.ComponentModel ;
2014-10-24 17:02:38 +00:00
using System.Diagnostics ;
2015-07-06 11:04:54 +00:00
using System.Drawing ;
using System.Drawing.Imaging ;
2014-04-25 21:55:43 +00:00
using System.Globalization ;
using System.IO ;
using System.Linq ;
using System.Net ;
2015-07-06 11:04:54 +00:00
using System.Net.NetworkInformation ;
2014-10-24 17:02:38 +00:00
using System.Net.Sockets ;
2015-07-06 11:04:54 +00:00
using System.Reflection ;
2014-10-24 17:02:38 +00:00
using System.Runtime.Serialization.Formatters.Binary ;
2014-04-25 21:55:43 +00:00
using System.Text ;
2014-07-12 08:53:00 +00:00
using System.Threading ;
2014-04-25 21:55:43 +00:00
using System.Threading.Tasks ;
using System.Windows.Forms ;
2014-07-12 08:53:00 +00:00
using SzNPProjects ;
2014-04-25 21:55:43 +00:00
namespace MSGer.tk
{
static class Program
{
2014-10-24 17:02:38 +00:00
public static MainForm MainF ;
2015-03-26 19:36:18 +00:00
public static SettingsForm SettingsF ;
2014-10-24 17:02:38 +00:00
public static string ProcessName = Process . GetCurrentProcess ( ) . ProcessName ;
2014-04-25 21:55:43 +00:00
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
2014-10-24 17:02:38 +00:00
static void Main ( string [ ] args )
2014-04-25 21:55:43 +00:00
{
2014-10-24 17:02:38 +00:00
Process [ ] pname = Process . GetProcessesByName ( ProcessName ) ;
if ( pname . Length > 1 & & ! ( args . Length > 0 & & args [ 0 ] = = "multi" ) )
Environment . Exit ( 0 ) ; //2014.09.26. - Felesleges bármi más műveletet végrehajtani, még nem is töltött be semmit
2014-04-25 21:55:43 +00:00
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
2014-10-24 17:02:38 +00:00
BeforeLogin . Create ( ) ;
2014-04-25 21:55:43 +00:00
Console . WriteLine ( "Starting MSGer.tk..." ) ;
2014-10-24 17:02:38 +00:00
AppDomain . CurrentDomain . FirstChanceException + = CurrentDomain_FirstChanceException ;
AppDomain . CurrentDomain . UnhandledException + = CurrentDomain_UnhandledException ;
2015-07-06 11:04:54 +00:00
Load ( ) ; //2015.05.23.
Application . Run ( MainF ) ; //2015.05.23.
2014-10-24 17:02:38 +00:00
}
public static readonly bool ShowFirstChangeExceptions = false ;
static void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs e )
{
2015-07-06 11:04:54 +00:00
new ErrorHandler ( ErrorType . Unknown , e . ExceptionObject as Exception ) ; //2015.05.04.
2014-10-24 17:02:38 +00:00
}
static void CurrentDomain_FirstChanceException ( object sender , System . Runtime . ExceptionServices . FirstChanceExceptionEventArgs e )
{
if ( ShowFirstChangeExceptions )
MessageBox . Show ( "An error occured (a first change exception):\n" + e . Exception . Message + "\nHere: " + e . Exception . TargetSite ) ;
2014-04-25 21:55:43 +00:00
}
private static readonly DateTime Epoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 ,
DateTimeKind . Utc ) ;
2015-07-06 11:04:54 +00:00
public static DateTime UnixTimeToDateTime ( double seconds )
2014-04-25 21:55:43 +00:00
{
//2014.04.25.
DateTime time = Epoch . AddSeconds ( seconds ) ;
time = DateTime . SpecifyKind ( time , DateTimeKind . Utc ) ;
return time . ToLocalTime ( ) ;
}
2015-07-06 11:04:54 +00:00
public static double DateTimeToUnixTime ( DateTime time )
2014-10-24 17:02:38 +00:00
{
2015-07-06 11:04:54 +00:00
time = time . ToUniversalTime ( ) ; //2015.07.04.
return ( ( time . Ticks - 621355968000000000 ) / 10000000 ) ;
2014-10-24 17:02:38 +00:00
}
2015-07-06 11:04:54 +00:00
private static void BeforeExit ( )
2014-10-24 17:02:38 +00:00
{ //2014.09.01.
2014-04-25 21:55:43 +00:00
if ( MainF ! = null )
{
MainF . Hide ( ) ;
MainF . toolStripMenuItem4 . Enabled = false ; //2014.04.12.
MainF . toolStripMenuItem8 . Enabled = false ; //2014.04.12.
2014-10-24 17:02:38 +00:00
MainF . notifyIcon1 . Dispose ( ) ; //2014.08.28.
2014-04-25 21:55:43 +00:00
if ( CurrentUser . UserID ! = 0 ) //2014.04.18.
{
2014-10-24 17:02:38 +00:00
CurrentUser . SendChanges = false ; //2014.08.30.
2014-04-25 21:55:43 +00:00
MainF . SetOnlineState ( null , null ) ; //2014.04.12.)
2014-10-24 17:02:38 +00:00
if ( MainF . WindowState = = FormWindowState . Maximized ) //2014.04.18. - 2014.08.08.
2015-07-06 11:04:54 +00:00
Storage . Settings [ SettingType . WindowState ] = "1" ;
2014-04-25 21:55:43 +00:00
else if ( MainF . WindowState = = FormWindowState . Minimized )
2015-07-06 11:04:54 +00:00
Storage . Settings [ SettingType . WindowState ] = "2" ;
2014-04-25 21:55:43 +00:00
else if ( MainF . WindowState = = FormWindowState . Normal )
2015-07-06 11:04:54 +00:00
Storage . Settings [ SettingType . WindowState ] = "3" ;
2014-10-24 17:02:38 +00:00
Storage . Save ( true ) ; //2014.08.07.
2014-04-25 21:55:43 +00:00
}
}
2015-07-06 11:04:54 +00:00
if ( Storage . IsLoaded )
Storage . Save ( false ) ;
2014-10-24 17:02:38 +00:00
if ( Networking . ReceiverConnection ! = null )
Networking . ReceiverConnection . Close ( ) ;
if ( Networking . SenderConnection ! = null )
Networking . SenderConnection . Close ( ) ;
}
2015-07-06 11:04:54 +00:00
public static void Exit ( ) //2015.06.04.
2014-10-24 17:02:38 +00:00
{ //2014.04.12.
2015-07-06 11:04:54 +00:00
BeforeExit ( ) ;
2014-07-27 08:28:59 +00:00
Environment . Exit ( 0 ) ; //Hatásosabb
2014-04-25 21:55:43 +00:00
MessageBox . Show ( "Hiba: Nem sikerült leállítani a programot." ) ;
}
2015-07-06 11:04:54 +00:00
public static void Restart ( )
2014-10-24 17:02:38 +00:00
{ //2014.09.01.
2015-07-06 11:04:54 +00:00
BeforeExit ( ) ;
2014-10-24 17:02:38 +00:00
Process . Start ( ( ( ProcessName . Contains ( "vshost" ) ) ? ProcessName . Replace ( ".vshost" , "" ) : ProcessName ) + ".exe" ) ;
Environment . Exit ( 0 ) ;
MessageBox . Show ( "Hiba: Nem sikerült leállítani a programot." ) ;
}
2015-07-06 11:04:54 +00:00
public static Thread MainThread = null ;
public static void Load ( )
{
BeforeLogin . SetText ( "Starting..." ) ;
Thread . CurrentThread . Name = "Main Thread" ;
BeforeLogin . SetText ( "Loading program settings..." ) ;
Storage . Load ( false ) ; //Töltse be a nyelvet, legutóbb használt E-mail-t...
BeforeLogin . SetText ( "Checking available ports..." ) ;
SetPort ( ) ;
BeforeLogin . SetText ( "Loading packs..." ) ;
PackManager . LoadAll ( ) ; //2015.05.16.
MainThread = Thread . CurrentThread ; //2015.05.23.
MainF = new MainForm ( ) ; //2015.05.23.
BeforeLogin . SetText ( Language . Translate ( Language . StringID . BeforeLogin_CheckForUpdates ) ) ;
//2014.04.25.
string response = Networking . SendRequest ( Networking . RequestType . CheckForUpdates ,
Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) . Replace ( "." , "" ) ,
0 , false ) ;
if ( response = = "outofdate" )
{
var res = MessageBox . Show ( Language . Translate ( Language . StringID . OutOfDate ) , Language . Translate ( Language . StringID . OutOfDate_Caption ) , MessageBoxButtons . YesNo ) ;
if ( res = = DialogResult . Yes )
{ //2014.12.13.
Process . Start ( "Updater.exe" , "\"" + Language . Translate ( Language . StringID . Updater ) + "\"" ) ; //2015.06.14.
Program . Exit ( ) ;
}
}
else if ( response ! = "fine" )
new ErrorHandler ( ErrorType . ServerError , new Exception ( response ) ) ; //2015.06.04.
BeforeLogin . SetText ( Language . Translate ( Language . StringID . BeforeLogin_LoginForm ) ) ;
var LoginDialog = new LoginForm ( ) ; //2015.05.23.
BeforeLogin . Destroy ( ) ;
LoginDialog . ShowDialog ( ) ;
//Nézzük, sikerült-e
if ( CurrentUser . UserID = = 0 )
return ; //2014.09.06.
// Start the thread
MainForm . PartnerListUpdateThread . Start ( ) ; //Áthelyezve: 2015.06.30.
Storage . Load ( true ) ; //2014.08.07.
}
/// <summary>
/// Beállítja a portot a Storage.Settings-ben található értéktől kezdve a legelső elérhető portra
/// </summary>
public static void SetPort ( )
{
if ( Storage . Settings [ SettingType . Port ] = = "" )
Storage . Settings [ SettingType . Port ] = "4510" ; //2015.05.21.
CurrentUser . Port = int . Parse ( Storage . Settings [ SettingType . Port ] ) ; //2015.05.24.
while ( true )
{
if ( IPGlobalProperties . GetIPGlobalProperties ( ) . GetActiveUdpListeners ( ) . Select ( entry = > entry . Port ) . Contains ( CurrentUser . Port ) ) //2015.05.24.
CurrentUser . Port + + ; //2015.05.24.
else
break ;
}
Networking . ReceiverConnection = new UdpClient ( CurrentUser . Port , AddressFamily . InterNetworkV6 ) ; //2015.05.24.
Networking . SenderConnection . AllowNatTraversal ( true ) ; //2014.09.04.
}
public static Image LoadImageFromFile ( string path )
{ //2015.06.06.
//TODO: Ellenőrizni és használni mindenhol, ahol kell - http://stackoverflow.com/questions/12680618/exception-parameter-is-not-valid-on-passing-new-image-to-picturebox
using ( Image sourceImg = Image . FromFile ( path ) )
{
Image clonedImg = new Bitmap ( sourceImg . Width , sourceImg . Height , PixelFormat . Format32bppArgb ) ;
using ( var copy = Graphics . FromImage ( clonedImg ) )
{
copy . DrawImage ( sourceImg , 0 , 0 ) ;
}
return clonedImg ;
}
}
2014-04-25 21:55:43 +00:00
}
2014-10-24 17:02:38 +00:00
2015-07-06 11:04:54 +00:00
static class Extensions
2014-04-25 21:55:43 +00:00
{
2014-10-24 17:02:38 +00:00
public static bool HasSameElementsAs < T > (
this IEnumerable < T > first ,
IEnumerable < T > second
)
2014-04-25 21:55:43 +00:00
{
2014-10-24 17:02:38 +00:00
var firstMap = first
. GroupBy ( x = > x )
. ToDictionary ( x = > x . Key , x = > x . Count ( ) ) ;
var secondMap = second
. GroupBy ( x = > x )
. ToDictionary ( x = > x . Key , x = > x . Count ( ) ) ;
return
firstMap . Keys . All ( x = >
secondMap . Keys . Contains ( x ) & & firstMap [ x ] = = secondMap [ x ]
) & &
secondMap . Keys . All ( x = >
firstMap . Keys . Contains ( x ) & & secondMap [ x ] = = firstMap [ x ]
) ;
}
2014-04-25 21:55:43 +00:00
2014-10-24 17:02:38 +00:00
public static bool Test ( this object A , object B )
{
return object . ReferenceEquals ( A , B ) ;
}
2014-04-25 21:55:43 +00:00
2014-10-24 17:02:38 +00:00
///
/// Clones an object by using the .
///
/// The object to clone.
///
/// The object to be cloned must be serializable.
///
2015-03-26 19:36:18 +00:00
public static object Clone ( this object obj )
2014-10-24 17:02:38 +00:00
{
using ( MemoryStream buffer = new MemoryStream ( ) )
2014-04-25 21:55:43 +00:00
{
2014-10-24 17:02:38 +00:00
BinaryFormatter formatter = new BinaryFormatter ( ) ;
formatter . Serialize ( buffer , obj ) ;
buffer . Position = 0 ;
object temp = formatter . Deserialize ( buffer ) ;
return temp ;
2014-04-25 21:55:43 +00:00
}
}
2015-03-26 19:36:18 +00:00
public static IEnumerable < Control > GetAll ( this Control control , Type type = null )
{ //2015.02.26.
var controls = control . Controls . Cast < Control > ( ) ;
var ret = controls . SelectMany ( ctrl = > GetAll ( ctrl , type ) )
. Concat ( controls ) ;
return ( type = = null ) ? ret : ret . Where ( c = > c . GetType ( ) = = type ) ;
}
2015-07-06 11:04:54 +00:00
public static void AppendText ( this RichTextBox box , string text , Color color )
{
box . SelectionStart = box . TextLength ;
box . SelectionLength = 0 ;
box . SelectionColor = color ;
box . AppendText ( text ) ;
box . SelectionColor = box . ForeColor ;
}
2014-04-25 21:55:43 +00:00
}
}