Пример #1
0
        /// <summary>Generate a filepath for the given pair name</summary>
        public static string DBFilePath(string exchange_name, string pair_name)
        {
            var dbpath = Misc.ResolveUserPath("PriceData", $"{Path_.SanitiseFileName(pair_name)} - {Path_.SanitiseFileName(exchange_name)}.db");

            Path_.CreateDirs(Path_.Directory(dbpath));
            return(dbpath);
        }
Пример #2
0
        public History(string root_directory)
        {
            var filepath = Path.Combine(root_directory, "fronius.db");

            Path_.CreateDirs(Path_.Directory(filepath));

            var connection_string = $"Data Source={filepath};Version=3;journal mode=Memory;synchronous=Off";

            DB = new SQLiteConnection(connection_string);

            InitDBTables();
        }
Пример #3
0
        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);

            // On first time startup, display the settings before creating the data directory
            if (!Path_.DirExists(Settings.Instance.DataPath))
            {
                ShowSettings.Execute();

                // Ensure the data directory exists
                Path_.CreateDirs(Settings.Instance.DataPath);
            }

            // Apply settings
            Settings.Instance.NotifySettingChanged(nameof(Settings.Origin));
            Settings.Instance.NotifySettingChanged(nameof(Settings.Destination));
        }
Пример #4
0
        /// <summary>Load plugin assemblies</summary>
        private void LoadPlugins()
        {
            var plugin_dir = Util.ResolveAppPath("plugins");

            Path_.CreateDirs(plugin_dir);

            // Load line formatters
            m_plugin_formatters = new Plugins <ILineFormatter>()
                                  .Load(plugin_dir);

            // Report failures
            if (m_plugin_formatters.Failures.Count != 0)
            {
                var sb = new StringBuilder();
                sb.AppendLine("Plugin Load Failures:");
                foreach (var fail in m_plugin_formatters.Failures)
                {
                    sb.AppendLine(fail.Name).AppendLine(fail.Error.Message).AppendLine();
                }

                // This will need delaying until the window is visible
                m_report.ErrorPopup(sb.ToString());
            }
        }
Пример #5
0
        /// <summary>Pull 'filename' from EDDB. Returns true if the file was downloaded, and the output filepath</summary>
        public async Task <DownloadFileResult> DownloadFile(string file_url, string output_dir, TimeSpan?maximum_age = null)
        {
            var filename    = Path_.FileName(file_url);
            var output_path = Path_.CombinePath(Path_.CreateDirs(output_dir), filename);

            using (StatusStack.NewStatusMessage($"Downloading '{filename}'..."))
            {
                try
                {
                    HttpRequestMessage  req;
                    HttpResponseMessage resp;

                    // Request the head information about the target file
                    Log.Write(ELogLevel.Info, $"Checking size and timestamp of '{filename}'");
                    req  = new HttpRequestMessage(HttpMethod.Head, file_url);
                    resp = await Client.SendAsync(req, Shutdown);

                    if (!resp.IsSuccessStatusCode)
                    {
                        Log.Write(ELogLevel.Error, $"Downloading information for '{filename}' failed: {resp.StatusCode} {resp.ReasonPhrase}");
                        throw new HttpRequestException($"{resp.ReasonPhrase} ({resp.StatusCode})");
                    }

                    // Only download if the server version is newer.
                    if (maximum_age != null && Path_.FileExists(output_path))
                    {
                        var time_diff = new FileInfo(output_path).LastWriteTime - resp.Content.Headers.LastModified;
                        if (time_diff > -maximum_age.Value)
                        {
                            Log.Write(ELogLevel.Info, $"Local copy of '{filename}' is less than {Settings.Instance.DataAge.ToPrettyString(trailing_zeros:false)} older than the latest version");
                            return(new DownloadFileResult(file_url, output_path, false));
                        }
                    }

                    // Get the download size (remember it might be compressed)
                    var length = resp.Content.Headers.ContentLength;

                    // The server version is newer, download the whole file
                    Log.Write(ELogLevel.Info, $"Downloading '{filename}' ({length} bytes)");
                    using (Scope.Create(() => Downloading = true, () => Downloading = false))
                    {
                        // Make the web request
                        req  = new HttpRequestMessage(HttpMethod.Get, file_url);
                        resp = await Client.SendAsync(req, Shutdown);

                        if (!resp.IsSuccessStatusCode)
                        {
                            Log.Write(ELogLevel.Error, $"Downloading '{filename}' failed: {resp.StatusCode} {resp.ReasonPhrase}");
                            throw new HttpRequestException($"{resp.ReasonPhrase} ({resp.StatusCode})");
                        }

                        // Read the response content into a file
                        using (var file = new FileStream(output_path, FileMode.Create, FileAccess.Write, FileShare.Read))
                        {
                            // Decompress if the content is compressed
                            if (resp.Content.Headers.ContentEncoding.Any(x => x == "gzip"))
                            {
                                using (var content = await resp.Content.ReadAsStreamAsync())
                                    using (var gzip = new GZipStream(content, CompressionMode.Decompress))
                                        await gzip.CopyToAsync(file);
                            }
                            else
                            {
                                await resp.Content.CopyToAsync(file);
                            }

                            Log.Write(ELogLevel.Info, $"Download complete '{filename}'");
                            return(new DownloadFileResult(file_url, output_path, true));
                        }
                    }
                }
                catch
                {
                    Log.Write(ELogLevel.Error, $"Data file '{filename}' was not available from {file_url}.");
                    return(new DownloadFileResult(file_url, output_path, false));
                }
            }
        }
Пример #6
0
        // Notes:
        //  - Parsed command line options

        public StartupOptions(string[] args)
        {
            FilesToLoad  = new List <string>();
            SettingsPath = null !;

            var exe_dir = Util.ResolveAppPath();

            if (!Path_.DirExists(exe_dir))
            {
                throw new ArgumentException("Cannot determine the current executable directory");
            }

            // Check the command line options
            for (int i = 0, iend = args.Length; i != iend; ++i)
            {
                var arg = args[i].ToLowerInvariant();

                // No option character implies the file to load
                if (arg[0] != '-' && arg[0] != '/')
                {
                    FilesToLoad.Add(arg);
                    continue;
                }

                // Helper for comparing option strings
                bool IsOption(string opt) => string.CompareOrdinal(arg, 0, opt, 0, opt.Length) == 0;

                // (order these by longest option first)
                if (IsOption(CmdLine.SettingsPath))
                {
                    SettingsPath = arg.Substring(CmdLine.SettingsPath.Length);
                }
                else if (IsOption(CmdLine.Portable))
                {
                    PortableMode = true;
                }
                else if (IsOption(CmdLine.ShowHelp))
                {
                    ShowHelp = true;
                }
                else if (IsOption(CmdLine.ShowHelp2))
                {
                    ShowHelp = true;
                }
                else
                {
                    throw new ArgumentException($"Unknown command line option '{arg}'.");
                }
            }

            // Determine whether to run the app in portable mode
            PortableMode |= Path_.FileExists(Path_.CombinePath(exe_dir, "portable"));

            // Set the UserDataDir based on whether we're running in portable mode or not
            UserDataDir = Path.GetFullPath(PortableMode ? Path_.CombinePath(exe_dir, "UserData") : Util.ResolveUserDocumentsPath("Rylogic", "LDraw"));
            Path_.CreateDirs(UserDataDir);

            // Check that we have write access to the user data directory
            if (!Path_.DirExists(UserDataDir) || (new DirectoryInfo(UserDataDir).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                throw new IOException($"The user data directory ('{UserDataDir}') is readonly.");
            }

            // If a settings path has not been given, use the defaults
            SettingsPath ??= Path_.CombinePath(UserDataDir, "settings2.xml");
        }
Пример #7
0
 public History()
 {
     Path_.CreateDirs(Path_.Directory(Filepath));
     DB = new SQLiteConnection(DBConnectionString);
     InitDBTables();
 }