public BungieClient(Action <BungieClientSettings> configure)
        {
            BungieClientSettings settings = new BungieClientSettings();

            configure(settings);

            Configuration = StaticUnityContainer.GetConfiguration();

            if (settings.UseExistingConfig)
            {
                Configuration.ApplySettingsFromConfig(settings.ExistingConfigPath);
            }
            else
            {
                Configuration.ApplySettings(settings);
            }

            _logger = StaticUnityContainer.GetLogger();

            if (Configuration.Settings.IsLoggingEnabled)
            {
                LogListener = new LogListener();
                _logger.Register(LogListener);
            }

            CDN      = new BungieCDNClient();
            Platform = new BungiePlatfromClient(Configuration.Settings.ApiKey, Configuration);

            _versionControl = StaticUnityContainer.GetManifestUpdateHandler();
        }
 /// <summary>
 /// Struct .ctor
 /// </summary>
 /// <param name="hash">Definition hash</param>
 /// <param name="type">Definition type</param>
 public DefinitionHashPointer(uint?hash, DefinitionsEnum type)
 {
     m_value            = default;
     Exists             = false;
     Hash               = hash;
     DefinitionEnumType = type;
     _repository        = StaticUnityContainer.GetDestinyDefinitionRepositories();
     Locale             = _repository.CurrentLocaleLoadContext;
 }
Пример #3
0
 internal BungiePlatfromClient(string apiKey, IConfigurationService configuration)
 {
     _apiKey      = apiKey;
     _httpClient  = StaticUnityContainer.GetHTTPClient();
     _logger      = StaticUnityContainer.GetLogger();
     _authHandler = StaticUnityContainer.GetAuthHandler();
     _httpClient.AddAcceptHeader("application/json");
     _httpClient.AddHeader("X-API-Key", apiKey);
     _config = configuration;
 }
        internal void AfterConfigurated()
        {
            var assemblyData     = StaticUnityContainer.GetAssemblyData();
            var excludedFromLoad = DefinitionLoadingSettings.ForbiddenDefinitions;

            DefinitionLoadingSettings.AllowedDefinitions = assemblyData
                                                           .DefinitionsToTypeMapping
                                                           .Keys
                                                           .Where(x => !excludedFromLoad.Contains(x))
                                                           .ToArray();
            if (DefinitionLoadingSettings.UsedProvider is null)
            {
                throw new Exception("Add UseDefaultProvider to config if you want to use default provider.");
            }
        }
Пример #5
0
        public DefinitionHashPointer <DestinyActivityDefinition> GetPointer()
        {
            var repo     = StaticUnityContainer.GetDestinyDefinitionRepositories();
            var settings = StaticUnityContainer.GetConfiguration().Settings;

            foreach (var locale in settings.Locales)
            {
                if (repo.TryGetDestinyDefinition <DestinyActivityDefinition>(DefinitionsEnum.DestinyActivityDefinition, Hash, locale, out var definition))
                {
                    if (Equals(definition))
                    {
                        return(new DefinitionHashPointer <DestinyActivityDefinition>(Hash, DefinitionsEnum.DestinyActivityDefinition, locale, definition, repo));
                    }
                }
            }
            return(null);
        }
 internal DefinitionHashPointer(uint?hash, DefinitionsEnum type, DestinyLocales locale, T def = default, ILocalisedDestinyDefinitionRepositories repo = null)
 {
     if (def != null)
     {
         m_value = def;
         Exists  = true;
     }
     Hash = hash;
     DefinitionEnumType = type;
     if (repo != null)
     {
         _repository = repo;
     }
     else
     {
         _repository = StaticUnityContainer.GetDestinyDefinitionRepositories();
     }
     Locale = locale;
 }
Пример #7
0
        public string FetchJSONFromDB(DestinyLocales locale, DefinitionsEnum definitionType, uint hash)
        {
            if (!_assemblyData.DefinitionsToTypeMapping[definitionType].AttributeData.Sources.HasFlag(Attributes.DefinitionSources.SQLite))
            {
                throw new Exception("This definition type isn't present in SQLite database.");
            }

            var    manifest = StaticUnityContainer.GetManifestUpdateHandler().CurrentManifest;
            var    mobileWorldContentPathsLocalePath = Path.GetFileName(manifest.MobileWorldContentPaths[locale.LocaleToString()]);
            var    connectionString = @$ "Data Source={_configs.Settings.VersionsRepositoryPath}\\{manifest.Version}\\MobileWorldContent\\{locale.LocaleToString()}\\{mobileWorldContentPathsLocalePath}; Version=3;";
            string result           = string.Empty;

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                string        query   = $"SELECT * FROM {definitionType.ToString()} WHERE id={hash.ToInt32()}";
                SQLiteCommand command = new SQLiteCommand
                {
                    Connection  = connection,
                    CommandText = query
                };
                try
                {
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        var byteArray = (byte[])reader["json"];
                        result = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
                    }
                }
                catch (Exception e)
                {
                    _logger.Log(e.Message, LogType.Error);
                }
                connection.Close();
            }
            return(result);
        }
        public async Task Run()
        {
            _logger.Log("Starting client...", LogType.Info);

            await _versionControl.InitiateManifestHandler();

            if (Configuration.Settings.CheckUpdates)
            {
                await _versionControl.UpdateManifestData();
            }

            if (Configuration.Settings.CacheDefinitionsInMemory)
            {
                Repository = StaticUnityContainer.GetDestinyDefinitionRepositories();
                Repository.Initialize(Configuration.Settings.Locales);

                if (Configuration.Settings.IsUsingPreloadedData)
                {
                    _logger.Log($"Using preloaded cache for set locales: {string.Join(", ", Configuration.Settings.Locales)}", LogType.Info);
                    await _versionControl.LoadData();
                }
            }
        }
        public async Task DownloadAndSaveToLocalFiles(bool unpackSqlite)
        {
            var    logger = StaticUnityContainer.GetLogger();
            string path   = StaticUnityContainer.GetConfiguration().Settings.VersionsRepositoryPath;

            if (string.IsNullOrWhiteSpace(path))
            {
                throw new Exception("Specify files path in configs.json or use DownloadAndSaveToLocalFiles(string) method");
            }
            path = $"{path}\\{Version}";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            var       _httpClient = StaticUnityContainer.GetHTTPClient();
            Stopwatch stopwatch   = new Stopwatch();

            stopwatch.Start();

            Uri _cdnUri = BungieClient.BungieCDNUri;

            logger.Log($"Loading data from: {_cdnUri.AbsoluteUri}", LogType.Info);

            HttpResponseMessage message;

            #region Downloading MobileAssetContent (zip SQLite)
            if (!Directory.Exists($"{path}/MobileAssetContent"))
            {
                Directory.CreateDirectory($"{path}/MobileAssetContent");
            }

            logger.Log($"Getting data from: {_cdnUri + MobileAssetContentPath}", LogType.Info);
            if (!File.Exists($"{path}/MobileAssetContent/{Path.GetFileName(MobileAssetContentPath)}"))
            {
                message = await _httpClient.Get(_cdnUri + MobileAssetContentPath);

                using (var stream = await message.Content.ReadAsStreamAsync())
                {
                    var fileInfo = new FileInfo($"{path}/MobileAssetContent/{Path.GetFileName(MobileAssetContentPath)}");
                    using (var fileStream = fileInfo.OpenWrite())
                    {
                        await stream.CopyToAsync(fileStream);
                    }
                }
            }
            else
            {
                logger.Log("File already exists, skipping.", LogType.Info);
            }
            if (unpackSqlite)
            {
                logger.Log("Unpacking zip...", LogType.Info);
                var packedFileName = $"{path}/MobileAssetContent/{Path.GetFileName(MobileAssetContentPath)}.zip";
                if (IsZIP($"{path}/MobileAssetContent/{Path.GetFileName(MobileAssetContentPath)}"))
                {
                    File.Move($"{path}/MobileAssetContent/{Path.GetFileName(MobileAssetContentPath)}", packedFileName);
                    ZipFile.ExtractToDirectory(packedFileName, $"{path}/MobileAssetContent/");
                    logger.Log("Clearing leftovers.", LogType.Info);
                    File.Delete(packedFileName);
                }
                else
                {
                    logger.Log("File is already unpacked", LogType.Info);
                }
            }
            #endregion

            #region Downloading MobileGearAssetDataBases (zip SQLite)
            if (!Directory.Exists($"{path}/MobileGearAssetDataBases"))
            {
                Directory.CreateDirectory($"{path}/MobileGearAssetDataBases");
            }
            foreach (var entry in MobileGearAssetDataBases)
            {
                if (!Directory.Exists($"{path}/MobileGearAssetDataBases/{entry.Version}"))
                {
                    Directory.CreateDirectory($"{path}/MobileGearAssetDataBases/{entry.Version}");
                }
                logger.Log($"Getting data from: {_cdnUri + entry.Path}", LogType.Info);
                if (!File.Exists($"{path}/MobileGearAssetDataBases/{entry.Version}/{Path.GetFileName(entry.Path)}"))
                {
                    message = await _httpClient.Get(_cdnUri + entry.Path);

                    using (var stream = await message.Content.ReadAsStreamAsync())
                    {
                        var fileInfo = new FileInfo($"{path}/MobileGearAssetDataBases/{entry.Version}/{Path.GetFileName(entry.Path)}");
                        using (var fileStream = fileInfo.OpenWrite())
                        {
                            await stream.CopyToAsync(fileStream);
                        }
                    }
                }
                else
                {
                    logger.Log("File already exists, skipping.", LogType.Info);
                }
                if (unpackSqlite)
                {
                    logger.Log("Unpacking zip...", LogType.Info);
                    var packedFileName = $"{path}/MobileGearAssetDataBases/{entry.Version}/{Path.GetFileName(entry.Path)}.zip";
                    if (IsZIP($"{path}/MobileGearAssetDataBases/{entry.Version}/{Path.GetFileName(entry.Path)}"))
                    {
                        File.Move($"{path}/MobileGearAssetDataBases/{entry.Version}/{Path.GetFileName(entry.Path)}", packedFileName);
                        ZipFile.ExtractToDirectory(packedFileName, $"{path}/MobileGearAssetDataBases/{entry.Version}");
                        logger.Log("Clearing leftovers.", LogType.Info);
                        File.Delete(packedFileName);
                    }
                    else
                    {
                        logger.Log("File is already unpacked", LogType.Info);
                    }
                }
            }
            #endregion

            #region Downloading MobileWorldContent (zip SQLite)
            if (!Directory.Exists($"{path}/MobileWorldContent"))
            {
                Directory.CreateDirectory($"{path}/MobileWorldContent");
            }
            foreach (var key in MobileWorldContentPaths.Keys)
            {
                if (!Directory.Exists($"{path}/MobileWorldContent/{key}"))
                {
                    Directory.CreateDirectory($"{path}/MobileWorldContent/{key}");
                }
                logger.Log($"Getting data from: {_cdnUri + MobileWorldContentPaths[key]}", LogType.Info);
                if (!File.Exists($"{path}/MobileWorldContent/{key}/{Path.GetFileName(MobileWorldContentPaths[key])}"))
                {
                    message = await _httpClient.Get(_cdnUri + MobileWorldContentPaths[key]);

                    using (var stream = await message.Content.ReadAsStreamAsync())
                    {
                        var fileInfo = new FileInfo($"{path}/MobileWorldContent/{key}/{Path.GetFileName(MobileWorldContentPaths[key])}");
                        using (var fileStream = fileInfo.OpenWrite())
                        {
                            await stream.CopyToAsync(fileStream);
                        }
                    }
                }
                else
                {
                    logger.Log("File already exists, skipping.", LogType.Info);
                }
                if (unpackSqlite)
                {
                    logger.Log("Unpacking zip...", LogType.Info);
                    var packedFileName = $"{path}/MobileWorldContent/{key}/{Path.GetFileName(MobileWorldContentPaths[key])}.zip";
                    if (IsZIP($"{path}/MobileWorldContent/{key}/{Path.GetFileName(MobileWorldContentPaths[key])}"))
                    {
                        File.Move($"{path}/MobileWorldContent/{key}/{Path.GetFileName(MobileWorldContentPaths[key])}", packedFileName);
                        ZipFile.ExtractToDirectory(packedFileName, $"{path}/MobileWorldContent/{key}/");
                        logger.Log("Clearing leftovers.", LogType.Info);
                        File.Delete(packedFileName);
                    }
                    else
                    {
                        logger.Log("File is already unpacked", LogType.Info);
                    }
                }
            }
            #endregion

            #region Downloading JsonWorldContent (json text file)
            if (!Directory.Exists($"{path}/JsonWorldContent"))
            {
                Directory.CreateDirectory($"{path}/JsonWorldContent");
            }
            foreach (var key in JsonWorldContentPaths.Keys)
            {
                if (!Directory.Exists($"{path}/JsonWorldContent/{key}"))
                {
                    Directory.CreateDirectory($"{path}/JsonWorldContent/{key}");
                }
                logger.Log($"Getting data from: {_cdnUri + JsonWorldContentPaths[key]}", LogType.Info);
                if (!File.Exists($"{path}/JsonWorldContent/{key}/{Path.GetFileName(JsonWorldContentPaths[key])}"))
                {
                    message = await _httpClient.Get(_cdnUri + JsonWorldContentPaths[key]);

                    using (var stream = await message.Content.ReadAsStreamAsync())
                    {
                        var fileInfo = new FileInfo($"{path}/JsonWorldContent/{key}/{Path.GetFileName(JsonWorldContentPaths[key])}");
                        using (var fileStream = fileInfo.OpenWrite())
                        {
                            await stream.CopyToAsync(fileStream);
                        }
                    }
                }
            }
            #endregion

            #region Downloading JsonWorldComponentContent (json text files)
            if (!Directory.Exists($"{path}/JsonWorldComponentContent"))
            {
                Directory.CreateDirectory($"{path}/JsonWorldComponentContent");
            }
            foreach (var key in JsonWorldComponentContentPaths.Keys)
            {
                if (!Directory.Exists($"{path}/JsonWorldComponentContent/{key}"))
                {
                    Directory.CreateDirectory($"{path}/JsonWorldComponentContent/{key}");
                }
                foreach (var innerKey in JsonWorldComponentContentPaths[key].Keys)
                {
                    if (!Directory.Exists($"{path}/JsonWorldComponentContent/{key}/{innerKey}"))
                    {
                        Directory.CreateDirectory($"{path}/JsonWorldComponentContent/{key}/{innerKey}");
                    }
                    logger.Log($"Getting data from: {_cdnUri + JsonWorldComponentContentPaths[key][innerKey]}", LogType.Info);
                    if (!File.Exists($"{path}/JsonWorldComponentContent/{key}/{innerKey}/{Path.GetFileName(JsonWorldComponentContentPaths[key][innerKey])}"))
                    {
                        message = await _httpClient.Get(_cdnUri + JsonWorldComponentContentPaths[key][innerKey]);

                        using (var stream = await message.Content.ReadAsStreamAsync())
                        {
                            var fileInfo = new FileInfo($"{path}/JsonWorldComponentContent/{key}/{innerKey}/{Path.GetFileName(JsonWorldComponentContentPaths[key][innerKey])}");
                            using (var fileStream = fileInfo.OpenWrite())
                            {
                                await stream.CopyToAsync(fileStream);
                            }
                        }
                    }
                }
            }
            #endregion

            #region Downloading MobileClanBannerDatabase (zip SQLite)
            if (!Directory.Exists($"{path}/MobileClanBannerDatabase"))
            {
                Directory.CreateDirectory($"{path}/MobileClanBannerDatabase");
            }
            logger.Log($"Getting data from: {_cdnUri + MobileClanBannerDatabasePath}", LogType.Info);
            if (!File.Exists($"{path}/MobileClanBannerDatabase/{Path.GetFileName(MobileClanBannerDatabasePath)}"))
            {
                message = await _httpClient.Get(_cdnUri + MobileClanBannerDatabasePath);

                using (var stream = await message.Content.ReadAsStreamAsync())
                {
                    var fileInfo = new FileInfo($"{path}/MobileClanBannerDatabase/{Path.GetFileName(MobileClanBannerDatabasePath)}");
                    using (var fileStream = fileInfo.OpenWrite())
                    {
                        await stream.CopyToAsync(fileStream);
                    }
                }
            }
            else
            {
                logger.Log("File already exists, skipping.", LogType.Info);
            }
            if (unpackSqlite)
            {
                logger.Log("Unpacking zip...", LogType.Info);
                var packedFileName = $"{path}/MobileClanBannerDatabase/{Path.GetFileName(MobileClanBannerDatabasePath)}.zip";
                if (IsZIP($"{path}/MobileClanBannerDatabase/{Path.GetFileName(MobileClanBannerDatabasePath)}"))
                {
                    File.Move($"{path}/MobileClanBannerDatabase/{Path.GetFileName(MobileClanBannerDatabasePath)}", packedFileName);
                    ZipFile.ExtractToDirectory(packedFileName, $"{path}/MobileClanBannerDatabase/");
                    logger.Log("Clearing leftovers.", LogType.Info);
                    File.Delete(packedFileName);
                }
                else
                {
                    logger.Log("File is already unpacked", LogType.Info);
                }
            }
            #endregion

            if (!File.Exists($"{path}/Manifest.json"))
            {
                var manifestJson = JsonConvert.SerializeObject(this, Formatting.Indented);
                File.WriteAllText($"{path}/Manifest.json", manifestJson);
            }

            stopwatch.Stop();
            logger.Log($"Finished getting data! {stopwatch.ElapsedMilliseconds} ms.", LogType.Info);
        }
 internal BungieCDNClient()
 {
     _httpClient = StaticUnityContainer.GetHTTPClient();
 }