示例#1
0
        private void Publish(UIMouseEvent evt, UIElement listeningElement)
        {
            if (ModLoader.modBrowserPassphrase == "")
            {
                Main.menuMode = Interface.enterPassphraseMenuID;
                Interface.enterPassphraseMenu.SetGotoMenu(Interface.modSourcesID);
                return;
            }
            Main.PlaySound(10);
            try {
                var modFile = builtMod.modFile;
                var bp      = builtMod.properties;

                var files = new List <UploadFile>();
                files.Add(new UploadFile {
                    Name     = "file",
                    Filename = Path.GetFileName(modFile.path),
                    //    ContentType = "text/plain",
                    Content = File.ReadAllBytes(modFile.path)
                });
                if (modFile.HasFile("icon.png"))
                {
                    files.Add(new UploadFile {
                        Name     = "iconfile",
                        Filename = "icon.png",
                        Content  = modFile.GetBytes("icon.png")
                    });
                }
                if (bp.beta)
                {
                    throw new WebException(Language.GetTextValue("tModLoader.BetaModCantPublishError"));
                }
                if (bp.buildVersion != modFile.tModLoaderVersion)
                {
                    throw new WebException(Language.GetTextValue("OutdatedModCantPublishError.BetaModCantPublishError"));
                }

                var values = new NameValueCollection
                {
                    { "displayname", bp.displayName },
                    { "name", modFile.name },
                    { "version", "v" + bp.version },
                    { "author", bp.author },
                    { "homepage", bp.homepage },
                    { "description", bp.description },
                    { "steamid64", ModLoader.SteamID64 },
                    { "modloaderversion", "tModLoader v" + modFile.tModLoaderVersion },
                    { "passphrase", ModLoader.modBrowserPassphrase },
                    { "modreferences", String.Join(", ", bp.modReferences.Select(x => x.mod)) },
                    { "modside", bp.side.ToFriendlyString() },
                };
                if (values["steamid64"].Length != 17)
                {
                    throw new WebException($"The steamid64 '{values["steamid64"]}' is invalid, verify that you are logged into Steam and don't have a pirated copy of Terraria.");
                }
                if (string.IsNullOrEmpty(values["author"]))
                {
                    throw new WebException($"You need to specify an author in build.txt");
                }
                ServicePointManager.Expect100Continue = false;
                string url = "http://javid.ddns.net/tModLoader/publishmod.php";
                using (PatientWebClient client = new PatientWebClient()) {
                    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, policyErrors) => true;
                    Interface.uploadMod.SetDownloading(modFile.name);
                    Interface.uploadMod.SetCancel(() => {
                        Main.menuMode = Interface.modSourcesID;
                        client.CancelAsync();
                    });
                    client.UploadProgressChanged += (s, e) => Interface.uploadMod.SetProgress(e);
                    client.UploadDataCompleted   += (s, e) => PublishUploadDataComplete(s, e, modFile);

                    var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", System.Globalization.NumberFormatInfo.InvariantInfo);
                    client.Headers["Content-Type"] = "multipart/form-data; boundary=" + boundary;
                    //boundary = "--" + boundary;
                    byte[] data = UploadFile.GetUploadFilesRequestData(files, values);
                    client.UploadDataAsync(new Uri(url), data);
                }
                Main.menuMode = Interface.uploadModID;
            }
            catch (WebException e) {
                UIModBrowser.LogModBrowserException(e);
            }
        }
        private void Publish(UIMouseEvent evt, UIElement listeningElement)
        {
            if (ModLoader.modBrowserPassphrase == "")
            {
                Main.menuMode = Interface.enterPassphraseMenuID;
                Interface.enterPassphraseMenu.SetGotoMenu(Interface.modSourcesID);
                return;
            }
            Main.PlaySound(10, -1, -1, 1);
            try
            {
                TmodFile[] modFiles    = ModLoader.FindMods();
                bool       ok          = false;
                TmodFile   theTModFile = null;
                foreach (TmodFile tModFile in modFiles)
                {
                    if (Path.GetFileName(tModFile.path).Equals(@Path.GetFileName(mod) + @".tmod"))
                    {
                        ok          = true;
                        theTModFile = tModFile;
                    }
                }
                if (!ok)
                {
                    throw new Exception();
                }
                System.Net.ServicePointManager.Expect100Continue = false;
                string filename = @ModLoader.ModPath + @Path.DirectorySeparatorChar + @Path.GetFileName(mod) + @".tmod";
                string url      = "http://javid.ddns.net/tModLoader/publishmod.php";
                using (var iconStream = theTModFile.HasFile("icon.png") ? new MemoryStream(theTModFile.GetFile("icon.png")) : null)
                    using (var stream = File.Open(filename, FileMode.Open))
                    {
                        var files = new List <UploadFile>();
                        files.Add(new IO.UploadFile
                        {
                            Name     = "file",
                            Filename = Path.GetFileName(filename),
                            //    ContentType = "text/plain",
                            Stream = stream
                        }
                                  );
                        if (iconStream != null)
                        {
                            files.Add(new IO.UploadFile
                            {
                                Name     = "iconfile",
                                Filename = "icon.png",
                                Stream   = iconStream
                            }
                                      );
                        }
                        BuildProperties bp     = BuildProperties.ReadModFile(theTModFile);
                        var             values = new NameValueCollection
                        {
                            { "displayname", bp.displayName },
                            { "name", Path.GetFileNameWithoutExtension(filename) },
                            { "version", "v" + bp.version },
                            { "author", bp.author },
                            { "homepage", bp.homepage },
                            { "description", bp.description },
                            { "steamid64", ModLoader.SteamID64 },
                            { "modloaderversion", "tModLoader v" + theTModFile.tModLoaderVersion },
                            { "passphrase", ModLoader.modBrowserPassphrase },
                            { "modreferences", String.Join(", ", bp.modReferences.Select(x => x.mod)) },
                            { "modside", bp.side.ToFriendlyString() },
                        };
                        using (PatientWebClient client = new PatientWebClient())
                        {
                            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return(true); });
                            Interface.uploadMod.SetDownloading(Path.GetFileNameWithoutExtension(filename));
                            Interface.uploadMod.SetCancel(() =>
                            {
                                Main.menuMode = Interface.modSourcesID;
                                client.CancelAsync();
                            });
                            client.UploadProgressChanged += (s, e) => Interface.uploadMod.SetProgress(e);
                            client.UploadDataCompleted   += (s, e) => PublishUploadDataComplete(s, e, theTModFile);

                            var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", System.Globalization.NumberFormatInfo.InvariantInfo);
                            client.Headers["Content-Type"] = "multipart/form-data; boundary=" + boundary;
                            boundary = "--" + boundary;
                            byte[] data = IO.UploadFile.GetUploadFilesRequestData(files, values);
                            client.UploadDataAsync(new Uri(url), data);
                        }
                        Main.menuMode = Interface.uploadModID;
                    }
            }
            catch (WebException e)
            {
                ErrorLogger.LogModBrowserException(e);
            }
        }