<commit_before>﻿/*
 * Copyright (c) 2013-2015, SteamDB. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using SteamKit2;
using SteamKit2.Internal;

namespace SteamDatabaseBackend
{
    static class LocalConfig
    {
        [JsonObject(MemberSerialization.OptIn)]
        public class CDNAuthToken
        {
            [JsonProperty]
            public string Server { get; set; }

            [JsonProperty]
            public string Token { get; set; }

            [JsonProperty]
            public DateTime Expiration { get; set; }
        }

        [JsonObject(MemberSerialization.OptIn)]
        class LocalConfigJson
        {
            [JsonProperty]
            public uint CellID { get; set; } 

            [JsonProperty]
            public string SentryFileName { get; set; } 

            [JsonProperty]
            public byte[] Sentry { get; set; } 

            [JsonProperty]
            public Dictionary<uint, CDNAuthToken> CDNAuthTokens { get; set; } 

            public LocalConfigJson()
            {
                CDNAuthTokens = new Dictionary<uint, CDNAuthToken>();
            }
        }

        public static uint CellID { get; set; }

        public static string SentryFileName { get; set; }

        public static byte[] Sentry { get; set; }

        public static Dictionary<uint, CDNAuthToken> CDNAuthTokens { get; private set; }

        public static void Load()
        {
            LoadServers();

            var path = GetPath();

            LocalConfigJson current;

            if (File.Exists(path))
            {
                current = JsonConvert.DeserializeObject<LocalConfigJson>(File.ReadAllText(path), GetSettings());
            }
            else
            {
                current = new LocalConfigJson();
            }

            CellID = current.CellID;
            Sentry = current.Sentry;
            SentryFileName = current.SentryFileName;

            // Disabled for now, need new logic to deal with terrible CDN servers. Can't rely on cached ones! SAD!
            //CDNAuthTokens = current.CDNAuthTokens; 
            CDNAuthTokens = new Dictionary<uint, CDNAuthToken>();

            if (!File.Exists(path))
            {
                Save();
            }
        }

        public static void Save()
        {
            Log.WriteDebug("Local Config", "Saving...");

            var current = new LocalConfigJson
            {
                CellID = CellID,
                Sentry = Sentry,
                SentryFileName = SentryFileName,
                CDNAuthTokens = CDNAuthTokens,
            };

            string json;

            lock (CDNAuthTokens)
            {
                json = JsonConvert.SerializeObject(current, GetSettings());
            }

            File.WriteAllText(GetPath(), json);
        }

        public static void LoadServers()
        {
            Log.WriteInfo("Steam", "Loading Steam servers...");

            var loadServersTask = SteamDirectory.Initialize(CellID);
            loadServersTask.Wait();

            TaskManager.RegisterErrorHandler(loadServersTask);

            if (loadServersTask.IsFaulted)
            {
                Environment.Exit(1);
            }
        }

        private static JsonSerializerSettings GetSettings()
        {
            return new JsonSerializerSettings
            {
                Formatting = Formatting.Indented
            };
        }

        private static string GetPath()
        {
            return Path.Combine(Application.Path, "files", ".support", "localconfig.json");
        }
    }
}<commit_msg>Register error handler before waiting, load servers for correct cellid<commit_after>﻿/*
 * Copyright (c) 2013-2015, SteamDB. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using SteamKit2;
using SteamKit2.Internal;

namespace SteamDatabaseBackend
{
    static class LocalConfig
    {
        [JsonObject(MemberSerialization.OptIn)]
        public class CDNAuthToken
        {
            [JsonProperty]
            public string Server { get; set; }

            [JsonProperty]
            public string Token { get; set; }

            [JsonProperty]
            public DateTime Expiration { get; set; }
        }

        [JsonObject(MemberSerialization.OptIn)]
        class LocalConfigJson
        {
            [JsonProperty]
            public uint CellID { get; set; } 

            [JsonProperty]
            public string SentryFileName { get; set; } 

            [JsonProperty]
            public byte[] Sentry { get; set; } 

            [JsonProperty]
            public Dictionary<uint, CDNAuthToken> CDNAuthTokens { get; set; } 

            public LocalConfigJson()
            {
                CDNAuthTokens = new Dictionary<uint, CDNAuthToken>();
            }
        }

        public static uint CellID { get; set; }

        public static string SentryFileName { get; set; }

        public static byte[] Sentry { get; set; }

        public static Dictionary<uint, CDNAuthToken> CDNAuthTokens { get; private set; }

        public static void Load()
        {
            var path = GetPath();

            LocalConfigJson current;

            if (File.Exists(path))
            {
                current = JsonConvert.DeserializeObject<LocalConfigJson>(File.ReadAllText(path), GetSettings());
            }
            else
            {
                current = new LocalConfigJson();
            }

            CellID = current.CellID;
            Sentry = current.Sentry;
            SentryFileName = current.SentryFileName;

            // Disabled for now, need new logic to deal with terrible CDN servers. Can't rely on cached ones! SAD!
            //CDNAuthTokens = current.CDNAuthTokens; 
            CDNAuthTokens = new Dictionary<uint, CDNAuthToken>();

            if (!File.Exists(path))
            {
                Save();
            }

            LoadServers();
        }

        public static void Save()
        {
            Log.WriteDebug("Local Config", "Saving...");

            var current = new LocalConfigJson
            {
                CellID = CellID,
                Sentry = Sentry,
                SentryFileName = SentryFileName,
                CDNAuthTokens = CDNAuthTokens,
            };

            string json;

            lock (CDNAuthTokens)
            {
                json = JsonConvert.SerializeObject(current, GetSettings());
            }

            File.WriteAllText(GetPath(), json);
        }

        public static void LoadServers()
        {
            Log.WriteInfo("Steam", "Loading Steam servers...");

            var loadServersTask = SteamDirectory.Initialize(CellID);

            TaskManager.RegisterErrorHandler(loadServersTask);

            loadServersTask.Wait();

            if (loadServersTask.IsFaulted)
            {
                Environment.Exit(1);
            }
        }

        private static JsonSerializerSettings GetSettings()
        {
            return new JsonSerializerSettings
            {
                Formatting = Formatting.Indented
            };
        }

        private static string GetPath()
        {
            return Path.Combine(Application.Path, "files", ".support", "localconfig.json");
        }
    }
}
{'fraction_non_alphanumeric': 0.0843389296956978, 'fraction_numerical': 0.0028856243441762854, 'mean_word_length': 1.5821198780900778, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2927453', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1868, 'n_words': 493}