Ejemplo n.º 1
0
 public MainForm()
 {
     InitializeComponent();
     ConfigIni.Load();
     BaseDir  = Path.GetDirectoryName(Application.ExecutablePath);
     GamesDir = Path.Combine(BaseDir, "games");
     //UBootDump = Path.Combine(Path.Combine(BaseDir, "dump"), "uboot.bin");
     KernelDump = Path.Combine(Path.Combine(BaseDir, "dump"), "kernel.img");
     LoadGames();
 }
Ejemplo n.º 2
0
        static void Main()
        {
#if DEBUG
            try
            {
                AllocConsole();
                IntPtr         stdHandle      = CreateFile("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
                SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
                FileStream     fileStream     = new FileStream(safeFileHandle, FileAccess.Write);
                Encoding       encoding       = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
                StreamWriter   standardOutput = new StreamWriter(fileStream, encoding);
                standardOutput.AutoFlush = true;
                Console.SetOut(standardOutput);
                Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));
            }
            catch { }
            try
            {
                Stream logFile = File.Create("debuglog.txt");
                Debug.Listeners.Add(new TextWriterTraceListener(logFile));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + ex.StackTrace);
            }
            Debug.AutoFlush = true;
#endif

            try
            {
                bool createdNew = true;
                using (Mutex mutex = new Mutex(true, "hakchi2", out createdNew))
                {
                    if (createdNew)
                    {
                        BaseDirectoryInternal = Path.GetDirectoryName(Application.ExecutablePath);
                        if (ApplicationDeployment.IsNetworkDeployed)
                        {
                            // This is not correct way for Windows 7+...
                            BaseDirectoryExternal = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "hakchi2");
                            // So if it's not exists, lets try to get documents library path (Win7+)
                            try
                            {
                                if (!Directory.Exists(BaseDirectoryExternal))
                                {
                                    BaseDirectoryExternal = Path.Combine(GetDocumentsLibraryPath(), "hakchi2");
                                }
                            }
                            catch (Exception ex)
                            {
                                // TODO: Test it on Windows XP
                                Debug.WriteLine(ex.Message);
                            }
                        }
                        else
                        {
                            BaseDirectoryExternal = BaseDirectoryInternal;
                        }
                        Debug.WriteLine("Base directory: " + BaseDirectoryExternal);
                        ConfigIni.Load();
                        try
                        {
                            if (!string.IsNullOrEmpty(ConfigIni.Language))
                            {
                                Thread.CurrentThread.CurrentUICulture = new CultureInfo(ConfigIni.Language);
                            }
                        }
                        catch { }

                        // There are some folders which should be accessed by user
                        // Moving them to "My documents"
                        if (ApplicationDeployment.IsNetworkDeployed && ApplicationDeployment.CurrentDeployment.IsFirstRun)
                        {
                            var externalDirs = new string[]
                            {
                                "art", "folder_images", "patches", "user_mods"
                            };
                            foreach (var dir in externalDirs)
                            {
                                DirectoryCopy(Path.Combine(BaseDirectoryInternal, dir), Path.Combine(BaseDirectoryExternal, dir), true);
                            }
                        }

                        string       languagesDirectory = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "languages");
                        const string langFileNames      = "hakchi.resources.dll";
                        AppDomain.CurrentDomain.AppendPrivatePath(languagesDirectory);
                        // For updates
                        var oldFiles = Directory.GetFiles(Path.GetDirectoryName(Application.ExecutablePath), langFileNames, SearchOption.AllDirectories);
                        foreach (var d in oldFiles)
                        {
                            if (!d.Contains(@"\languages\"))
                            {
                                var dir = Path.GetDirectoryName(d);
                                Debug.WriteLine("Removing old directory: " + dir);
                                if (ApplicationDeployment.IsNetworkDeployed)
                                {
                                    var targetDir = Path.Combine(languagesDirectory, Path.GetFileName(dir));
                                    Directory.CreateDirectory(targetDir);
                                    var targetFile = Path.Combine(targetDir, langFileNames);
                                    if (File.Exists(targetFile))
                                    {
                                        File.Delete(targetFile);
                                    }
                                    File.Move(Path.Combine(dir, langFileNames), targetFile);
                                }
                                else
                                {
                                    Directory.Delete(dir, true);
                                }
                            }
                        }

                        Debug.WriteLine("Starting, version: " + Assembly.GetExecutingAssembly().GetName().Version);
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new MainForm());
                        Debug.WriteLine("Done.");
                    }
                    else
                    {
                        Process current = Process.GetCurrentProcess();
                        foreach (Process process in Process.GetProcessesByName("hakchi"))
                        {
                            if (process.Id != current.Id)
                            {
                                ShowWindow(process.MainWindowHandle, 9);       // Restore
                                SetForegroundWindow(process.MainWindowHandle); // Foreground
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + ex.StackTrace);
                MessageBox.Show(ex.Message + ex.StackTrace, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            SevenZip.SevenZipExtractor.SetLibraryPath(Path.Combine(Program.BaseDirectoryInternal, IntPtr.Size == 8 ? @"tools\7z64.dll" : @"tools\7z.dll"));

#if DEBUG
            try
            {
                AllocConsole();
                IntPtr         stdHandle         = CreateFile("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
                SafeFileHandle safeFileHandle    = new SafeFileHandle(stdHandle, true);
                FileStream     consoleFileStream = new FileStream(safeFileHandle, FileAccess.Write);
                Encoding       encoding          = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
                StreamWriter   standardOutput    = new StreamWriter(consoleFileStream, encoding);
                standardOutput.AutoFlush = true;
                Console.SetOut(standardOutput);
                debugStreams.Add(consoleFileStream);
                Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));
            }
            catch { }
            try
            {
                Stream logFile = File.Create("debuglog.txt");
                debugStreams.Add(logFile);
                Debug.Listeners.Add(new TextWriterTraceListener(logFile));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + ex.StackTrace);
            }
            Debug.AutoFlush = true;
#else
            Trace.Listeners.Clear();
#endif
#if TRACE
            try
            {
                MemoryStream inMemoryLog = new MemoryStream();
                debugStreams.Add(inMemoryLog);
                Trace.Listeners.Add(new TextWriterTraceListener(new StreamWriter(inMemoryLog, System.Text.Encoding.GetEncoding(MY_CODE_PAGE))));
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message + ex.StackTrace);
            }
            Trace.AutoFlush = true;
#endif
            isPortable = !args.Contains("/nonportable") || args.Contains("/portable");

            if (File.Exists(Path.Combine(BaseDirectoryInternal, "nonportable.flag")))
            {
                isPortable = false;
            }

            bool isFirstRun = false;

            if (!isPortable)
            {
                isFirstRun = Shared.isFirstRun();
            }

            try
            {
                bool createdNew = true;
                using (Mutex mutex = new Mutex(true, "hakchi2", out createdNew))
                {
                    if (createdNew)
                    {
                        if (!isPortable)
                        {
                            // This is not correct way for Windows 7+...
                            BaseDirectoryExternal = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "hakchi2");
                            // So if it's not exists, lets try to get documents library path (Win7+)
                            try
                            {
                                if (!Directory.Exists(BaseDirectoryExternal))
                                {
                                    BaseDirectoryExternal = Path.Combine(GetDocumentsLibraryPath(), "hakchi2");
                                }

                                // There are some folders which should be accessed by user
                                // Moving them to "My documents"
                                if (isFirstRun)
                                {
                                    var externalDirs = new string[]
                                    { "art", "folder_images", "patches", "user_mods", "sfrom_tool" };
                                    foreach (var dir in externalDirs)
                                    {
                                        Shared.DirectoryCopy(Path.Combine(BaseDirectoryInternal, dir), Path.Combine(BaseDirectoryExternal, dir), true, false, true, false);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                // TODO: Test it on Windows XP
                                Trace.WriteLine(ex.Message);
                            }
                        }
                        else
                        {
                            BaseDirectoryExternal = BaseDirectoryInternal;
                        }

                        Trace.WriteLine("Base directory: " + BaseDirectoryExternal + " (" + (isPortable ? "portable" : "non-portable") + " mode)");
                        ConfigIni.Load();
                        try
                        {
                            if (!string.IsNullOrEmpty(ConfigIni.Instance.Language))
                            {
                                Thread.CurrentThread.CurrentUICulture = new CultureInfo(ConfigIni.Instance.Language);
                            }
                        }
                        catch { }

                        string       languagesDirectory = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "languages");
                        const string langFileNames      = "hakchi.resources.dll";
                        AppDomain.CurrentDomain.AppendPrivatePath(languagesDirectory);
                        // For updates
                        var oldFiles = Directory.GetFiles(Path.GetDirectoryName(Application.ExecutablePath), langFileNames, SearchOption.AllDirectories);
                        foreach (var d in oldFiles)
                        {
                            if (!d.Contains(Path.DirectorySeparatorChar + "languages" + Path.DirectorySeparatorChar))
                            {
                                var dir = Path.GetDirectoryName(d);
                                Trace.WriteLine("Removing old directory: " + dir);
                                if (!isPortable)
                                {
                                    var targetDir = Path.Combine(languagesDirectory, Path.GetFileName(dir));
                                    Directory.CreateDirectory(targetDir);
                                    var targetFile = Path.Combine(targetDir, langFileNames);
                                    if (File.Exists(targetFile))
                                    {
                                        File.Delete(targetFile);
                                    }
                                    File.Copy(Path.Combine(dir, langFileNames), targetFile);
                                }
                                else
                                {
                                    Directory.Delete(dir, true);
                                }
                            }
                        }

                        Trace.WriteLine("Starting, version: " + Shared.AppDisplayVersion);

                        System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType) 4080; // set default security protocol
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);

                        FormContext.AllFormsClosed += Process.GetCurrentProcess().Kill; // Suicide! Just easy and dirty way to kill all threads.

                        FormContext.AddForm(new MainForm());
                        Application.Run(FormContext);
                        Trace.WriteLine("Done.");
                    }
                    else
                    {
                        Process current = Process.GetCurrentProcess();
                        foreach (Process process in Process.GetProcessesByName("hakchi"))
                        {
                            if (process.Id != current.Id)
                            {
                                ShowWindow(process.MainWindowHandle, 9);       // Restore
                                SetForegroundWindow(process.MainWindowHandle); // Foreground
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message + ex.StackTrace);
                MessageBox.Show(ex.Message + ex.StackTrace, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }