File size: 5,649 Bytes
f4bef29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ZerionLauncher.Services
{
    /// <summary>
    /// Giá trị mặc định khi không tải được config từ host (offline / host chết).
    /// Giữ nguyên giá trị production hiện tại để launcher vẫn chạy được.
    /// </summary>
    public class RemoteConfigData
    {
        [JsonProperty("apiBaseUrl")]
        public string ApiBaseUrl { get; set; } = "https://api-production-20fe.up.railway.app/api/v1";

        [JsonProperty("newsUrl")]
        public string NewsUrl { get; set; } = "https://huggingface.co/datasets/nttl050611/LauncherPC/raw/main/news.json";

        [JsonProperty("manifestUrl")]
        public string ManifestUrl { get; set; } = "https://huggingface.co/datasets/nttl050611/LauncherPC/resolve/main/manifest.json";

        [JsonProperty("playersUrl")]
        public string PlayersUrl { get; set; } = "http://15.235.149.123/players.json";

        [JsonProperty("autoUpdaterXmlUrl")]
        public string AutoUpdaterXmlUrl { get; set; } = "https://raw.githubusercontent.com/ntl050611/gta/refs/heads/main/autoupdater.xml";

        [JsonProperty("launcherReleaseApiUrl")]
        public string LauncherReleaseApiUrl { get; set; } = "https://api.github.com/repos/ntl050611/gta/releases/latest";

        [JsonProperty("payloadEndpoint")]
        public string PayloadEndpoint { get; set; } = "http://127.0.0.1:3000/api/v1/security/payload";

        [JsonProperty("payloadEncryptionKeyHex")]
        public string PayloadEncryptionKeyHex { get; set; } = "0000000000000000000000000000000000000000000000000000000000000000";

        [JsonProperty("discordInviteUrl")]
        public string DiscordInviteUrl { get; set; } = "https://discord.gg/X63Zbz99j8";

        [JsonProperty("websiteUrl")]
        public string WebsiteUrl { get; set; } = "https://dinhcaotdm.net";

        [JsonProperty("fanpageUrl")]
        public string FanpageUrl { get; set; } = "https://fb.dinhcaotdm.net";

        [JsonProperty("registerUrl")]
        public string RegisterUrl { get; set; } = "https://zerion.vn/register";

        [JsonProperty("accountUrl")]
        public string AccountUrl { get; set; } = "https://zerion.vn/account";
    }

    /// <summary>
    /// Tải cấu hình từ host ngay khi launcher khởi động. Mọi URL / key / IP / port
    /// được lấy từ đây — muốn đổi server chỉ cần sửa file launcher-config.json trên
    /// host, không cần build lại launcher. Nếu fetch thất bại thì dùng giá trị
    /// mặc định trong RemoteConfigData.
    /// </summary>
    public static class RemoteConfig
    {
        // ⚠️ Đây là URL duy nhất còn hardcode — bỏ file launcher-config.json cạnh nó.
        private const string ConfigUrl =
            "https://huggingface.co/datasets/nttl050611/LauncherPC/raw/main/launcher-config.json";

        private static readonly HttpClient _http = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };

        private static RemoteConfigData _current = new RemoteConfigData();
        private static bool _loaded = false;

        public static RemoteConfigData Current => _current;

        public static async Task LoadAsync()
        {
            if (_loaded) return;
            _loaded = true;

            try
            {
                string json = await _http.GetStringAsync(ConfigUrl).ConfigureAwait(false);
                var parsed = JsonConvert.DeserializeObject<RemoteConfigData>(json);
                if (parsed != null)
                {
                    _current = MergeWithDefaults(parsed);
                }
            }
            catch
            {
                // Mạng lỗi / host lỗi — giữ nguyên mặc định, launcher vẫn chạy.
            }
        }

        /// <summary>Trường nào host để trống thì giữ giá trị mặc định.</summary>
        private static RemoteConfigData MergeWithDefaults(RemoteConfigData fetched)
        {
            var fallback = new RemoteConfigData();

            if (string.IsNullOrWhiteSpace(fetched.ApiBaseUrl)) fetched.ApiBaseUrl = fallback.ApiBaseUrl;
            if (string.IsNullOrWhiteSpace(fetched.NewsUrl)) fetched.NewsUrl = fallback.NewsUrl;
            if (string.IsNullOrWhiteSpace(fetched.ManifestUrl)) fetched.ManifestUrl = fallback.ManifestUrl;
            if (string.IsNullOrWhiteSpace(fetched.PlayersUrl)) fetched.PlayersUrl = fallback.PlayersUrl;
            if (string.IsNullOrWhiteSpace(fetched.AutoUpdaterXmlUrl)) fetched.AutoUpdaterXmlUrl = fallback.AutoUpdaterXmlUrl;
            if (string.IsNullOrWhiteSpace(fetched.LauncherReleaseApiUrl)) fetched.LauncherReleaseApiUrl = fallback.LauncherReleaseApiUrl;
            if (string.IsNullOrWhiteSpace(fetched.PayloadEndpoint)) fetched.PayloadEndpoint = fallback.PayloadEndpoint;
            if (string.IsNullOrWhiteSpace(fetched.PayloadEncryptionKeyHex)) fetched.PayloadEncryptionKeyHex = fallback.PayloadEncryptionKeyHex;
            if (string.IsNullOrWhiteSpace(fetched.DiscordInviteUrl)) fetched.DiscordInviteUrl = fallback.DiscordInviteUrl;
            if (string.IsNullOrWhiteSpace(fetched.WebsiteUrl)) fetched.WebsiteUrl = fallback.WebsiteUrl;
            if (string.IsNullOrWhiteSpace(fetched.FanpageUrl)) fetched.FanpageUrl = fallback.FanpageUrl;
            if (string.IsNullOrWhiteSpace(fetched.RegisterUrl)) fetched.RegisterUrl = fallback.RegisterUrl;
            if (string.IsNullOrWhiteSpace(fetched.AccountUrl)) fetched.AccountUrl = fallback.AccountUrl;

            return fetched;
        }
    }
}