Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        private void AddMods(string[] files)
        {
            foreach (var file in files)
            {
                var ext = Path.GetExtension(file).ToLower();
                if (ext == ".hmod")
                {
                    var target = Path.Combine(usermodsDirectory, Path.GetFileName(file));
                    if (file != target)
                    {
                        if (Directory.Exists(file))
                        {
                            Shared.DirectoryCopy(file, target, true, false, true, false);
                        }
                        else
                        {
                            File.Copy(file, target, true);
                        }
                    }
                    hmods.Add(new Hmod(Path.GetFileNameWithoutExtension(file)));
                }
                else if (ext == ".7z" || ext == ".zip" || ext == ".rar")
                {
                    using (var extractor = ArchiveFactory.Open(file))
                    {
                        foreach (var f in extractor.Entries)
                        {
                            if (Path.GetExtension(f.Key).ToLower() == ".hmod")
                            {
                                if (f.IsDirectory)
                                {
                                    using (var reader = extractor.ExtractAllEntries())
                                    {
                                        while (reader.MoveToNextEntry())
                                        {
                                            if (!reader.Entry.IsDirectory && reader.Entry.Key.StartsWith(f.Key))
                                            {
                                                reader.WriteEntryToDirectory(usermodsDirectory, new ExtractionOptions()
                                                {
                                                    ExtractFullPath = true, Overwrite = true
                                                });
                                            }
                                        }
                                    }

                                    if (!Directory.Exists(Path.Combine(usermodsDirectory, Path.GetFileName(f.Key))))
                                    {
                                        Directory.Move(Path.Combine(usermodsDirectory, f.Key), Path.Combine(usermodsDirectory, Path.GetFileName(f.Key)));

                                        new DirectoryInfo(usermodsDirectory).Refresh();
                                        int pos = f.Key.IndexOfAny(new char[] { '/', '\\' });
                                        Directory.Delete(Path.Combine(usermodsDirectory, pos > 0 ? f.Key.Substring(0, pos) : f.Key), true);
                                    }

                                    hmods.Add(new Hmod(Path.GetFileNameWithoutExtension(f.Key)));
                                }
                                else
                                {
                                    using (var outFile = new FileStream(Path.Combine(usermodsDirectory, Path.GetFileName(f.Key)), FileMode.Create))
                                    {
                                        f.OpenEntryStream().CopyTo(outFile);
                                        hmods.Add(new Hmod(Path.GetFileNameWithoutExtension(f.Key)));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            populateList();
        }
Esempio n. 3
0
        private void AddMods(string[] files)
        {
            foreach (var file in files)
            {
                var ext = Path.GetExtension(file).ToLower();
                if (ext == ".hmod")
                {
                    var target = Path.Combine(usermodsDirectory, Path.GetFileName(file));
                    if (file != target)
                    {
                        if (Directory.Exists(file))
                        {
                            Shared.DirectoryCopy(file, target, true, false, true, false);
                        }
                        else
                        {
                            File.Copy(file, target, true);
                        }
                    }
                    hmods.Add(new Hmod(Path.GetFileNameWithoutExtension(file)));
                }
                else if (ext == ".7z" || ext == ".zip" || ext == ".rar")
                {
                    using (var szExtractor = new SevenZipExtractor(file))
                    {
                        foreach (var f in szExtractor.ArchiveFileData)
                        {
                            if (Path.GetExtension(f.FileName).ToLower() == ".hmod")
                            {
                                if (f.IsDirectory)
                                {
                                    List <int> indices = new List <int>();
                                    for (int i = 0; i < szExtractor.ArchiveFileData.Count; ++i)
                                    {
                                        if (szExtractor.ArchiveFileData[i].FileName.StartsWith(f.FileName))
                                        {
                                            indices.Add(i);
                                        }
                                    }
                                    szExtractor.ExtractFiles(usermodsDirectory, indices.ToArray());

                                    if (!Directory.Exists(Path.Combine(usermodsDirectory, Path.GetFileName(f.FileName))))
                                    {
                                        Directory.Move(Path.Combine(usermodsDirectory, f.FileName), Path.Combine(usermodsDirectory, Path.GetFileName(f.FileName)));

                                        new DirectoryInfo(usermodsDirectory).Refresh();
                                        int pos = f.FileName.IndexOfAny(new char[] { '/', '\\' });
                                        Directory.Delete(Path.Combine(usermodsDirectory, pos > 0 ? f.FileName.Substring(0, pos) : f.FileName), true);
                                    }

                                    hmods.Add(new Hmod(Path.GetFileNameWithoutExtension(f.FileName)));
                                }
                                else
                                {
                                    using (var outFile = new FileStream(Path.Combine(usermodsDirectory, Path.GetFileName(f.FileName)), FileMode.Create))
                                    {
                                        szExtractor.ExtractFile(f.FileName, outFile);
                                        hmods.Add(new Hmod(Path.GetFileNameWithoutExtension(f.FileName)));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            populateList();
        }