示例#1
0
        public static ModInfo GetModInfo(NexusMod archive, string apikey)
        {
            if (!Directory.Exists(Consts.NexusCacheDirectory))
            {
                Directory.CreateDirectory(Consts.NexusCacheDirectory);
            }

            string path = Path.Combine(Consts.NexusCacheDirectory, $"mod-info-{archive.GameName}-{archive.ModID}.json");

            if (File.Exists(path))
            {
                return(path.FromJSON <ModInfo>());
            }


            var url    = $"https://api.nexusmods.com/v1/games/{ConvertGameName(archive.GameName)}/mods/{archive.ModID}.json";
            var client = BaseNexusClient(apikey);

            using (var s = client.GetStreamSync(url))
            {
                var result = s.FromJSON <ModInfo>();
                result.ToJSON(path);
                return(result);
            }
        }
示例#2
0
        public static NexusFileInfo GetFileInfo(NexusMod mod, string apikey)
        {
            var url    = $"https://api.nexusmods.com/v1/games/{ConvertGameName(mod.GameName)}/mods/{mod.ModID}/files/{mod.FileID}.json";
            var client = BaseNexusClient(apikey);

            using (var s = client.GetStreamSync(url))
            {
                return(s.FromJSON <NexusFileInfo>());
            }
        }
示例#3
0
        public static string GetNexusDownloadLink(NexusMod archive, string apikey)
        {
            var    client = BaseNexusClient(apikey);
            string url;
            string get_url_link = String.Format("https://api.nexusmods.com/v1/games/{0}/mods/{1}/files/{2}/download_link.json",
                                                ConvertGameName(archive.GameName), archive.ModID, archive.FileID);

            using (var s = client.GetStreamSync(get_url_link))
            {
                url = s.FromJSON <List <DownloadLink> >().First().URI;
                return(url);
            }
        }
示例#4
0
        public static EndorsementResponse EndorseMod(NexusMod mod, string apikey)
        {
            Utils.Status($"Endorsing ${mod.GameName} - ${mod.ModID}");
            var url =
                $"https://api.nexusmods.com/v1/games/{ConvertGameName(mod.GameName)}/mods/{mod.ModID}/endorse.json";
            var client = BaseNexusClient(apikey);

            var content = new FormUrlEncodedContent(new Dictionary <string, string> {
                { "version", mod.Version }
            });

            using (var s = client.PostStreamSync(url, content))
            {
                return(s.FromJSON <EndorsementResponse>());
            }
        }
示例#5
0
        public static ModInfo GetModInfo(NexusMod archive, string apikey)
        {
            if (!Directory.Exists(Consts.NexusCacheDirectory))
            {
                Directory.CreateDirectory(Consts.NexusCacheDirectory);
            }

TOP:
            var path = Path.Combine(Consts.NexusCacheDirectory, $"mod-info-{archive.GameName}-{archive.ModID}.json");

            try
            {
                if (File.Exists(path))
                {
                    var result = path.FromJSON <ModInfo>();
                    if (result._internal_version != CACHED_VERSION_NUMBER)
                    {
                        File.Delete(path);
                        goto TOP;
                    }

                    return(result);
                }
            }
            catch (Exception)
            {
                File.Delete(path);
            }


            var url =
                $"https://api.nexusmods.com/v1/games/{ConvertGameName(archive.GameName)}/mods/{archive.ModID}.json";
            var client = BaseNexusClient(apikey);

            using (var s = client.GetStreamSync(url))
            {
                var result = s.FromJSON <ModInfo>();
                result.game_name         = archive.GameName;
                result.mod_id            = archive.ModID;
                result._internal_version = CACHED_VERSION_NUMBER;
                result.ToJSON(path);
                return(result);
            }
        }
示例#6
0
        public static string GetNexusDownloadLink(NexusMod archive, string apikey, bool cache = false)
        {
            if (cache && TryGetCachedLink(archive, apikey, out string result))
            {
                return(result);
            }

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var    client = BaseNexusClient(apikey);
            string url;
            string get_url_link = String.Format("https://api.nexusmods.com/v1/games/{0}/mods/{1}/files/{2}/download_link.json",
                                                ConvertGameName(archive.GameName), archive.ModID, archive.FileID);

            using (var s = client.GetStreamSync(get_url_link))
            {
                url = s.FromJSON <List <DownloadLink> >().First().URI;
                return(url);
            }
        }
示例#7
0
        private static bool TryGetCachedLink(NexusMod archive, string apikey, out string result)
        {
            if (!(Directory.Exists(Consts.NexusCacheDirectory)))
            {
                Directory.CreateDirectory(Consts.NexusCacheDirectory);
            }


            string path = Path.Combine(Consts.NexusCacheDirectory, $"link-{archive.GameName}-{archive.ModID}-{archive.FileID}.txt");

            if (!File.Exists(path) || DateTime.Now - new FileInfo(path).LastWriteTime > new TimeSpan(24, 0, 0))
            {
                File.Delete(path);
                result = GetNexusDownloadLink(archive, apikey, false);
                File.WriteAllText(path, result);
                return(true);
            }
            result = File.ReadAllText(path);
            return(true);
        }
示例#8
0
        private Archive ResolveArchive(string sha, Dictionary <string, IndexedArchive> archives)
        {
            if (archives.TryGetValue(sha, out var found))
            {
                if (found.IniData == null)
                {
                    Error("No download metadata found for {0}, please use MO2 to query info or add a .meta file and try again.", found.Name);
                }
                var general = found.IniData.General;
                if (general == null)
                {
                    Error("No General section in mod metadata found for {0}, please use MO2 to query info or add the info and try again.", found.Name);
                }

                Archive result;

                if (general.modID != null && general.fileID != null && general.gameName != null)
                {
                    result = new NexusMod()
                    {
                        GameName = general.gameName,
                        FileID   = general.fileID,
                        ModID    = general.modID
                    };
                }
                else if (general.directURL != null && general.directURL.StartsWith("https://drive.google.com"))
                {
                    var regex = new Regex("((?<=id=)[a-zA-Z0-9_-]*)|(?<=\\/file\\/d\\/)[a-zA-Z0-9_-]*");
                    var match = regex.Match(general.directURL);
                    result = new GoogleDriveMod()
                    {
                        Id = match.ToString()
                    };
                }
                else if (general.directURL != null && general.directURL.StartsWith("https://www.dropbox.com/"))
                {
                    var uri   = new UriBuilder((string)general.directURL);
                    var query = HttpUtility.ParseQueryString(uri.Query);

                    if (query.GetValues("dl").Count() > 0)
                    {
                        query.Remove("dl");
                    }

                    query.Set("dl", "1");

                    uri.Query = query.ToString();

                    result = new DirectURLArchive()
                    {
                        URL = uri.ToString()
                    };
                }
                else if (general.directURL != null && general.directURL.StartsWith("https://www.moddb.com/downloads/start"))
                {
                    result = new MODDBArchive()
                    {
                        URL = general.directURL
                    };
                }
                else if (general.directURL != null && general.directURL.StartsWith("http://www.mediafire.com/file/"))
                {
                    Error("Mediafire links are not currently supported");
                    return(null);

                    /*result = new MediaFireArchive()
                     * {
                     *  URL = general.directURL
                     * };*/
                }
                else if (general.directURL != null)
                {
                    var tmp = new DirectURLArchive()
                    {
                        URL = general.directURL
                    };
                    if (general.directURLHeaders != null)
                    {
                        tmp.Headers = new List <string>();
                        tmp.Headers.AddRange(general.directURLHeaders.Split('|'));
                    }
                    result = tmp;
                }
                else
                {
                    Error("No way to handle archive {0} but it's required by the modpack", found.Name);
                    return(null);
                }

                result.Name = found.Name;
                result.Hash = found.Hash;
                result.Meta = found.Meta;

                return(result);
            }
            Error("No match found for Archive sha: {0} this shouldn't happen", sha);
            return(null);
        }
示例#9
0
        private Archive ResolveArchive(string sha, IDictionary <string, IndexedArchive> archives)
        {
            if (archives.TryGetValue(sha, out var found))
            {
                if (found.IniData == null)
                {
                    Error("No download metadata found for {0}, please use MO2 to query info or add a .meta file and try again.", found.Name);
                }
                var general = found.IniData.General;
                if (general == null)
                {
                    Error("No General section in mod metadata found for {0}, please use MO2 to query info or add the info and try again.", found.Name);
                }

                Archive result;

                if (general.directURL != null && general.directURL.StartsWith("https://drive.google.com"))
                {
                    var regex = new Regex("((?<=id=)[a-zA-Z0-9_-]*)|(?<=\\/file\\/d\\/)[a-zA-Z0-9_-]*");
                    var match = regex.Match(general.directURL);
                    result = new GoogleDriveMod()
                    {
                        Id = match.ToString()
                    };
                }
                else if (general.directURL != null && general.directURL.StartsWith(Consts.MegaPrefix))
                {
                    result = new MEGAArchive()
                    {
                        URL = general.directURL
                    };
                }
                else if (general.directURL != null && general.directURL.StartsWith("https://www.dropbox.com/"))
                {
                    var uri   = new UriBuilder((string)general.directURL);
                    var query = HttpUtility.ParseQueryString(uri.Query);

                    if (query.GetValues("dl").Count() > 0)
                    {
                        query.Remove("dl");
                    }

                    query.Set("dl", "1");

                    uri.Query = query.ToString();

                    result = new DirectURLArchive()
                    {
                        URL = uri.ToString()
                    };
                }
                else if (general.directURL != null && general.directURL.StartsWith("https://www.moddb.com/downloads/start"))
                {
                    result = new MODDBArchive()
                    {
                        URL = general.directURL
                    };
                }
                else if (general.directURL != null && general.directURL.StartsWith("http://www.mediafire.com/file/"))
                {
                    Error("Mediafire links are not currently supported");
                    return(null);

                    /*result = new MediaFireArchive()
                     * {
                     *  URL = general.directURL
                     * };*/
                }
                else if (general.directURL != null)
                {
                    var tmp = new DirectURLArchive()
                    {
                        URL = general.directURL
                    };
                    if (general.directURLHeaders != null)
                    {
                        tmp.Headers = new List <string>();
                        tmp.Headers.AddRange(general.directURLHeaders.Split('|'));
                    }
                    result = tmp;
                }
                else if (general.manualURL != null)
                {
                    result = new ManualURLArchive()
                    {
                        URL = general.manualURL.ToString()
                    };
                }
                else if (general.modID != null && general.fileID != null && general.gameName != null)
                {
                    var nm = new NexusMod()
                    {
                        GameName = general.gameName,
                        FileID   = general.fileID,
                        ModID    = general.modID,
                        Version  = general.version ?? "0.0.0.0"
                    };
                    var info = NexusAPI.GetModInfo(nm, NexusKey);
                    nm.Author          = info.author;
                    nm.UploadedBy      = info.uploaded_by;
                    nm.UploaderProfile = info.uploaded_users_profile_url;
                    result             = nm;
                }
                else
                {
                    Error("No way to handle archive {0} but it's required by the modpack", found.Name);
                    return(null);
                }

                result.Name = found.Name;
                result.Hash = found.File.Hash;
                result.Meta = found.Meta;

                Info($"Checking link for {found.Name}");

                var installer = new Installer(null, "", s => Utils.Log(s));
                installer.NexusAPIKey = NexusKey;
                if (!installer.DownloadArchive(result, false))
                {
                    Error($"Unable to resolve link for {found.Name}. If this is hosted on the nexus the file may have been removed.");
                }

                return(result);
            }
            Error("No match found for Archive sha: {0} this shouldn't happen", sha);
            return(null);
        }