2016-08-02 06:13:58 +02:00
using System ;
2016-08-02 08:50:31 +02:00
using System.Diagnostics ;
using System.IO ;
using System.Linq ;
using System.Reflection ;
using System.Threading ;
using System.Threading.Tasks ;
2016-08-02 06:13:58 +02:00
using System.Windows.Forms ;
2016-11-24 07:32:16 +01:00
using SteamKit2 ;
2016-08-02 06:13:58 +02:00
namespace ArchiSteamFarm {
internal static class Program {
2016-12-23 18:49:52 +01:00
internal static readonly ArchiLogger ArchiLogger = new ArchiLogger ( SharedInfo . ASF ) ;
2016-08-02 06:13:58 +02:00
internal static GlobalConfig GlobalConfig { get ; private set ; }
internal static GlobalDatabase GlobalDatabase { get ; private set ; }
internal static WebBrowser WebBrowser { get ; private set ; }
2016-08-02 08:50:31 +02:00
internal static void Exit ( int exitCode = 0 ) {
InitShutdownSequence ( ) ;
Environment . Exit ( exitCode ) ;
2016-08-02 06:13:58 +02:00
}
2016-11-24 07:32:16 +01:00
internal static string GetUserInput ( ASF . EUserInputType userInputType , string botName = SharedInfo . ASF , string extraInformation = null ) {
return null ; // TODO
2016-08-02 06:13:58 +02:00
}
2016-08-02 08:50:31 +02:00
internal static void InitShutdownSequence ( ) {
foreach ( Bot bot in Bot . Bots . Values . Where ( bot = > bot . KeepRunning ) ) {
bot . Stop ( ) ;
}
}
2016-11-24 07:32:16 +01:00
internal static void Restart ( ) {
InitShutdownSequence ( ) ;
2016-08-02 08:50:31 +02:00
2016-11-24 07:32:16 +01:00
try {
Process . Start ( Assembly . GetEntryAssembly ( ) . Location , string . Join ( " " , Environment . GetCommandLineArgs ( ) . Skip ( 1 ) ) ) ;
} catch ( Exception e ) {
2016-12-23 18:49:52 +01:00
ArchiLogger . LogGenericException ( e ) ;
2016-08-02 08:50:31 +02:00
}
2016-11-24 07:32:16 +01:00
Environment . Exit ( 0 ) ;
2016-08-02 08:50:31 +02:00
}
private static void Init ( ) {
AppDomain . CurrentDomain . UnhandledException + = UnhandledExceptionHandler ;
TaskScheduler . UnobservedTaskException + = UnobservedTaskExceptionHandler ;
Logging . InitCoreLoggers ( ) ;
2016-08-02 22:51:09 +02:00
if ( ! Runtime . IsRuntimeSupported ) {
2016-12-23 18:49:52 +01:00
ArchiLogger . LogGenericError ( "ASF detected unsupported runtime version, program might NOT run correctly in current environment. You're running it at your own risk!" ) ;
2016-08-02 08:50:31 +02:00
}
string homeDirectory = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) ;
if ( ! string . IsNullOrEmpty ( homeDirectory ) ) {
Directory . SetCurrentDirectory ( homeDirectory ) ;
// Allow loading configs from source tree if it's a debug build
if ( Debugging . IsDebugBuild ) {
// Common structure is bin/(x64/)Debug/ArchiSteamFarm.exe, so we allow up to 4 directories up
for ( byte i = 0 ; i < 4 ; i + + ) {
Directory . SetCurrentDirectory ( ".." ) ;
if ( ! Directory . Exists ( SharedInfo . ASFDirectory ) ) {
continue ;
}
Directory . SetCurrentDirectory ( SharedInfo . ASFDirectory ) ;
break ;
}
// If config directory doesn't exist after our adjustment, abort all of that
if ( ! Directory . Exists ( SharedInfo . ConfigDirectory ) ) {
Directory . SetCurrentDirectory ( homeDirectory ) ;
}
}
}
InitServices ( ) ;
// If debugging is on, we prepare debug directory prior to running
if ( GlobalConfig . Debug ) {
if ( Directory . Exists ( SharedInfo . DebugDirectory ) ) {
Directory . Delete ( SharedInfo . DebugDirectory , true ) ;
Thread . Sleep ( 1000 ) ; // Dirty workaround giving Windows some time to sync
}
2016-08-31 13:56:09 +02:00
2016-08-02 08:50:31 +02:00
Directory . CreateDirectory ( SharedInfo . DebugDirectory ) ;
2016-11-24 07:32:16 +01:00
DebugLog . AddListener ( new Debugging . DebugListener ( ) ) ;
DebugLog . Enabled = true ;
2016-08-02 08:50:31 +02:00
}
Logging . InitEnhancedLoggers ( ) ;
}
2016-11-24 07:32:16 +01:00
private static void InitServices ( ) {
string globalConfigFile = Path . Combine ( SharedInfo . ConfigDirectory , SharedInfo . GlobalConfigFileName ) ;
GlobalConfig = GlobalConfig . Load ( globalConfigFile ) ;
if ( GlobalConfig = = null ) {
2016-12-23 18:49:52 +01:00
ArchiLogger . LogGenericError ( "Global config could not be loaded, please make sure that " + globalConfigFile + " exists and is valid!" ) ;
2016-11-24 07:32:16 +01:00
Exit ( 1 ) ;
}
string globalDatabaseFile = Path . Combine ( SharedInfo . ConfigDirectory , SharedInfo . GlobalDatabaseFileName ) ;
GlobalDatabase = GlobalDatabase . Load ( globalDatabaseFile ) ;
if ( GlobalDatabase = = null ) {
2016-12-23 18:49:52 +01:00
ArchiLogger . LogGenericError ( "Global database could not be loaded, if issue persists, please remove " + globalDatabaseFile + " in order to recreate database!" ) ;
2016-11-24 07:32:16 +01:00
Exit ( 1 ) ;
}
ArchiWebHandler . Init ( ) ;
WebBrowser . Init ( ) ;
2016-12-23 18:49:52 +01:00
WebBrowser = new WebBrowser ( ArchiLogger ) ;
2016-11-24 07:32:16 +01:00
}
2016-08-02 06:13:58 +02:00
/// <summary>
2016-11-24 07:32:16 +01:00
/// The main entry point for the application.
2016-08-02 06:13:58 +02:00
/// </summary>
[STAThread]
private static void Main ( ) {
2016-08-02 08:50:31 +02:00
Init ( ) ;
2016-08-02 06:13:58 +02:00
Application . EnableVisualStyles ( ) ;
Application . SetCompatibleTextRenderingDefault ( false ) ;
2016-08-02 08:50:31 +02:00
Application . Run ( new MainForm ( ) ) ;
2016-08-02 06:13:58 +02:00
}
2016-11-24 07:32:16 +01:00
private static void UnhandledExceptionHandler ( object sender , UnhandledExceptionEventArgs args ) {
if ( args ? . ExceptionObject = = null ) {
2016-12-23 18:49:52 +01:00
ArchiLogger . LogNullError ( nameof ( args ) + " || " + nameof ( args . ExceptionObject ) ) ;
2016-11-24 07:32:16 +01:00
return ;
}
2016-12-23 18:49:52 +01:00
ArchiLogger . LogFatalException ( ( Exception ) args . ExceptionObject ) ;
2016-11-24 07:32:16 +01:00
}
private static void UnobservedTaskExceptionHandler ( object sender , UnobservedTaskExceptionEventArgs args ) {
if ( args ? . Exception = = null ) {
2016-12-23 18:49:52 +01:00
ArchiLogger . LogNullError ( nameof ( args ) + " || " + nameof ( args . Exception ) ) ;
2016-11-24 07:32:16 +01:00
return ;
}
2016-12-23 18:49:52 +01:00
ArchiLogger . LogFatalException ( args . Exception ) ;
2016-11-24 07:32:16 +01:00
}
2016-08-02 06:13:58 +02:00
}
2016-11-24 07:32:16 +01:00
}