private static void RetrieveAllModInfoAsync(Action <bool, BasicModInfoDatabase> onCompletion)
        {
            Action <Exception, string> onError = (e, jsonStr) => {
                if (e == null)
                {
                    LogHelpers.Alert((jsonStr.Trunc(64) ?? "") + " - Invalid exception?");
                }
                else if (e is JsonReaderException)
                {
                    LogHelpers.Alert("Bad JSON: " + jsonStr.Trunc(256));
                }
                else if (e is WebException || e is NullReferenceException)
                {
                    LogHelpers.Alert(("'" + jsonStr.Trunc(64) + "'" ?? "...") + " - " + e.Message);
                }
                else
                {
                    LogHelpers.Alert(("'" + jsonStr.Trunc(64) + "'" ?? "...") + " - " + e.ToString());
                }
            };

            Action <bool, string> onWrappedCompletion = (success, jsonStr) => {
                BasicModInfoDatabase modInfoDb;

                if (success)
                {
                    try {
                        success = GetModInfo.HandleModInfoReceipt(jsonStr, out modInfoDb);
                    } catch (Exception e) {
                        modInfoDb = new BasicModInfoDatabase();
                        onError(e, jsonStr);
                    }
                }
                else
                {
                    modInfoDb = new BasicModInfoDatabase();
                }

                onCompletion(success, modInfoDb);
            };

            WebConnectionHelpers.MakeGetRequestAsync(GetModInfo.ModInfoUrl, e => onError(e, ""), onWrappedCompletion);
        }
        public static void SubmitModInfo(string modName, ISet <string> modTags,
                                         Action <Exception, string> onError,
                                         Action <bool, string> onCompletion = null)
        {
            string url  = "http://hamstar.pw/hamstarhelpers/mod_info_submit/";
            var    json = new PostModTagsData {
                modname = modName,
                modtags = string.Join(",", modTags)
            };

            string jsonStr = JsonConvert.SerializeObject(json, Formatting.Indented);

            Action <bool, string> wrappedOnCompletion = (success, output) => {
                string processedOutput = "";

                if (success)
                {
                    JObject respJson = JObject.Parse(output);
                    JToken  msg      = respJson.SelectToken("Msg");

                    success = msg != null;
                    if (success)
                    {
                        processedOutput = msg.ToObject <string>();
                    }
                    else
                    {
                        processedOutput = "Failure.";
                    }
                }

                onCompletion(success, processedOutput);
            };

            Action <Exception, string> wrappedOnError = (Exception e, string str) => {
                LogHelpers.Log("!ModHelpers.PostModInfo.SubmitModInfo - Failed for post: " + jsonStr);
                onError(e, str);
            };

            WebConnectionHelpers.MakePostRequestAsync(url, jsonStr, e => wrappedOnError(e, ""), wrappedOnCompletion);
        }
Example #3
0
        private static void RetrieveGlobalInboxAsync(Action <bool, IDictionary <string, string> > onCompletion)
        {
            Action <Exception, string> onError = (e, output) => {
                if (e is JsonReaderException)
                {
                    LogHelpers.Alert("Bad JSON: " + output.Trunc(64));
                }
                else if (e is WebException || e is NullReferenceException)
                {
                    LogHelpers.Alert((output.Trunc(64) ?? "...") + " - " + e.Message);
                }
                else
                {
                    LogHelpers.Alert((output.Trunc(64) ?? "...") + " - " + e.ToString());
                }
            };

            Action <bool, string> onWrappedCompletion = (success, jsonStr) => {
                IDictionary <string, string> globalInboxSet;

                if (success)
                {
                    try {
                        success = GetGlobalInbox.HandleGlobalInboxReceipt(jsonStr, out globalInboxSet);
                    } catch (Exception e) {
                        globalInboxSet = new Dictionary <string, string>();
                        onError(e, jsonStr);
                    }
                }
                else
                {
                    globalInboxSet = new Dictionary <string, string>();
                }

                onCompletion(success, globalInboxSet);
            };

            WebConnectionHelpers.MakeGetRequestAsync(GetGlobalInbox.GlobalInboxUrl, e => onError(e, ""), onWrappedCompletion);
        }
Example #4
0
        private static void RetrieveAllModTagsAsync(Action <bool, ModTagsDatabase> onCompletion)
        {
            Action <Exception, string> onError = (e, output) => {
                if (e is JsonReaderException)
                {
                    LogHelpers.Alert("Bad JSON: " + output.Trunc(256));
                }
                else if (e is WebException || e is NullReferenceException)
                {
                    LogHelpers.Alert(("'" + output.Trunc(64) + "'" ?? "...") + " - " + e.Message);
                }
                else
                {
                    LogHelpers.Alert(("'" + output.Trunc(64) + "'" ?? "...") + " - " + e.ToString());
                }
            };

            Action <bool, string> onWrappedCompletion = (success, jsonStr) => {
                ModTagsDatabase modTagSet;

                if (success)
                {
                    try {
                        success = GetModTags.HandleModTagsReceipt(jsonStr, out modTagSet);
                    } catch (Exception e) {
                        modTagSet = new ModTagsDatabase();
                        onError(e, jsonStr);
                    }
                }
                else
                {
                    modTagSet = new ModTagsDatabase();
                }

                onCompletion(success, modTagSet);
            };

            WebConnectionHelpers.MakeGetRequestAsync(GetModTags.ModTagsUrl, e => onError(e, ""), onWrappedCompletion);
        }
        public static void ReportIssue(Mod mod, string issueTitle, string issueBody,
                                       Action <Exception, string> onError,
                                       Action <bool, string> onCompletion)
        {
            if (!ModFeaturesHelpers.HasGithub(mod))
            {
                throw new ModHelpersException("Mod is not eligable for submitting issues.");
            }

            int maxLines = ModHelpersMod.Config.ModIssueReportErrorLogMaxLines;

            IEnumerable <Mod> mods       = ModListHelpers.GetAllLoadedModsPreferredOrder();
            string            bodyInfo   = string.Join("\n \n", FormattedGameInfoHelpers.GetFormattedGameInfo(mods).ToArray());
            string            bodyErrors = string.Join("\n", GameInfoHelpers.GetErrorLog(maxLines).ToArray());

            string url   = "http://hamstar.pw/hamstarhelpers/issue_submit/";
            string title = "Reported from in-game: " + issueTitle;
            string body  = bodyInfo;

            body += "\n \n \n \n" + "Recent error logs:\n```\n" + bodyErrors + "\n```";
            body += "\n \n" + issueBody;

            var json = new GithubModIssueReportData {
                githubuser    = ModFeaturesHelpers.GetGithubUserName(mod),
                githubproject = ModFeaturesHelpers.GetGithubProjectName(mod),
                title         = title,
                body          = body
            };
            string jsonStr = JsonConvert.SerializeObject(json, Formatting.Indented);

            Action <bool, string> wrappedOnCompletion = (success, output) => {
                string processedOutput = "";

                if (success)
                {
                    JObject respJson = JObject.Parse(output);
                    JToken  data     = respJson.SelectToken("Data.html_url");
                    JToken  msg      = respJson.SelectToken("Msg");

                    if (data != null)
                    {
                        string issueUrl = data.ToObject <string>();
                        if (!string.IsNullOrEmpty(issueUrl))
                        {
                            SystemHelpers.OpenUrl(issueUrl);
                        }
                    }

                    success = msg != null;
                    if (success)
                    {
                        processedOutput = msg.ToObject <string>();
                    }
                    else
                    {
                        processedOutput = "Failure.";
                    }
                }

                onCompletion(success, processedOutput);
            };

            Action <Exception, string> wrappedOnError = (Exception e, string str) => {
                LogHelpers.Log("!ModHelpers.PostGithubModIssueReports.ReportIssue - Failed for POST to " + url + " : " + jsonStr);
                onError(e, str);
            };

            WebConnectionHelpers.MakePostRequestAsync(url, jsonStr, e => wrappedOnError(e, ""), wrappedOnCompletion);
        }