SteamDialog.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using MobiusEditor.Controls;
  15. using MobiusEditor.Interface;
  16. using MobiusEditor.Utility;
  17. using Steamworks;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.ComponentModel;
  21. using System.Diagnostics;
  22. using System.Drawing;
  23. using System.Drawing.Imaging;
  24. using System.IO;
  25. using System.Windows.Forms;
  26. namespace MobiusEditor.Dialogs
  27. {
  28. public partial class SteamDialog : Form
  29. {
  30. private static readonly string PreviewDirectory = Path.Combine(Path.GetTempPath(), "CnCRCMapEditor");
  31. private readonly IGamePlugin plugin;
  32. private readonly Timer statusUpdateTimer = new Timer();
  33. public SteamDialog(IGamePlugin plugin)
  34. {
  35. this.plugin = plugin;
  36. InitializeComponent();
  37. visibilityComboBox.DataSource = new []
  38. {
  39. new { Name = "Public", Value = ERemoteStoragePublishedFileVisibility.k_ERemoteStoragePublishedFileVisibilityPublic },
  40. new { Name = "Friends Only", Value = ERemoteStoragePublishedFileVisibility.k_ERemoteStoragePublishedFileVisibilityFriendsOnly },
  41. new { Name = "Private", Value = ERemoteStoragePublishedFileVisibility.k_ERemoteStoragePublishedFileVisibilityPrivate }
  42. };
  43. statusUpdateTimer.Interval = 500;
  44. statusUpdateTimer.Tick += StatusUpdateTimer_Tick;
  45. Disposed += (o, e) => { (previewTxt.Tag as Image)?.Dispose(); };
  46. }
  47. protected override void OnLoad(EventArgs e)
  48. {
  49. base.OnLoad(e);
  50. titleTxt.Text = plugin.Map.SteamSection.Title;
  51. descriptionTxt.Text = plugin.Map.SteamSection.Description;
  52. previewTxt.Text = plugin.Map.SteamSection.PreviewFile;
  53. visibilityComboBox.SelectedValue = plugin.Map.SteamSection.Visibility;
  54. btnPublishMap.SplitWidth = (plugin.Map.SteamSection.PublishedFileId != PublishedFileId_t.Invalid.m_PublishedFileId) ? MenuButton.DefaultSplitWidth : 0;
  55. Directory.CreateDirectory(PreviewDirectory);
  56. var previewPath = Path.Combine(PreviewDirectory, "Minimap.png");
  57. plugin.Map.GenerateWorkshopPreview().ToBitmap().Save(previewPath, ImageFormat.Png);
  58. if (plugin.Map.BasicSection.SoloMission)
  59. {
  60. var soloBannerPath = Path.Combine(PreviewDirectory, "SoloBanner.png");
  61. Properties.Resources.UI_CustomMissionPreviewDefault.Save(soloBannerPath, ImageFormat.Png);
  62. previewTxt.Text = soloBannerPath;
  63. }
  64. else
  65. {
  66. previewTxt.Text = previewPath;
  67. }
  68. imageTooltip.SetToolTip(previewTxt, "Preview.png");
  69. statusUpdateTimer.Start();
  70. UpdateControls();
  71. }
  72. private void StatusUpdateTimer_Tick(object sender, EventArgs e)
  73. {
  74. var status = SteamworksUGC.CurrentOperation?.Status;
  75. if (!string.IsNullOrEmpty(status))
  76. {
  77. statusLbl.Text = status;
  78. }
  79. }
  80. protected override void OnClosed(EventArgs e)
  81. {
  82. base.OnClosed(e);
  83. statusUpdateTimer.Stop();
  84. statusUpdateTimer.Dispose();
  85. }
  86. protected virtual void OnPublishSuccess()
  87. {
  88. statusLbl.Text = "Done.";
  89. EnableControls(true);
  90. }
  91. protected virtual void OnOperationFailed(string status)
  92. {
  93. statusLbl.Text = status;
  94. EnableControls(true);
  95. }
  96. private void EnableControls(bool enable)
  97. {
  98. titleTxt.Enabled = enable;
  99. visibilityComboBox.Enabled = enable;
  100. previewTxt.Enabled = enable;
  101. previewBtn.Enabled = enable;
  102. descriptionTxt.Enabled = enable;
  103. btnPublishMap.Enabled = enable;
  104. btnClose.Enabled = enable;
  105. }
  106. private void btnGoToSteam_Click(object sender, EventArgs e)
  107. {
  108. var workshopUrl = SteamworksUGC.WorkshopURL;
  109. if (!string.IsNullOrEmpty(workshopUrl))
  110. {
  111. Process.Start(workshopUrl);
  112. }
  113. }
  114. private void btnPublishMap_Click(object sender, EventArgs e)
  115. {
  116. if (string.IsNullOrEmpty(plugin.Map.BasicSection.Name))
  117. {
  118. plugin.Map.BasicSection.Name = titleTxt.Text;
  119. }
  120. if (string.IsNullOrEmpty(plugin.Map.BasicSection.Author))
  121. {
  122. plugin.Map.BasicSection.Author = SteamFriends.GetPersonaName();
  123. }
  124. plugin.Map.SteamSection.PreviewFile = previewTxt.Text;
  125. plugin.Map.SteamSection.Title = titleTxt.Text;
  126. plugin.Map.SteamSection.Description = descriptionTxt.Text;
  127. plugin.Map.SteamSection.Visibility = (ERemoteStoragePublishedFileVisibility)visibilityComboBox.SelectedValue;
  128. var tempPath = Path.Combine(Path.GetTempPath(), "CnCRCMapEditorPublishUGC");
  129. Directory.CreateDirectory(tempPath);
  130. foreach (var file in new DirectoryInfo(tempPath).EnumerateFiles()) file.Delete();
  131. var pgmPath = Path.Combine(tempPath, "MAPDATA.PGM");
  132. plugin.Save(pgmPath, FileType.PGM);
  133. var tags = new List<string>();
  134. switch (plugin.GameType)
  135. {
  136. case GameType.TiberianDawn:
  137. tags.Add("TD");
  138. break;
  139. case GameType.RedAlert:
  140. tags.Add("RA");
  141. break;
  142. }
  143. if (plugin.Map.BasicSection.SoloMission)
  144. {
  145. tags.Add("SinglePlayer");
  146. }
  147. else
  148. {
  149. tags.Add("MultiPlayer");
  150. }
  151. if (SteamworksUGC.PublishUGC(tempPath, plugin.Map.SteamSection, tags, OnPublishSuccess, OnOperationFailed))
  152. {
  153. statusLbl.Text = SteamworksUGC.CurrentOperation.Status;
  154. EnableControls(false);
  155. }
  156. }
  157. private void previewBtn_Click(object sender, EventArgs e)
  158. {
  159. var ofd = new OpenFileDialog
  160. {
  161. AutoUpgradeEnabled = false,
  162. RestoreDirectory = true,
  163. Filter = "Preview Files (*.png)|*.png",
  164. CheckFileExists = true,
  165. InitialDirectory = Path.GetDirectoryName(previewTxt.Text),
  166. FileName = Path.GetFileName(previewTxt.Text)
  167. };
  168. if (!string.IsNullOrEmpty(previewTxt.Text))
  169. {
  170. ofd.FileName = previewTxt.Text;
  171. }
  172. if (ofd.ShowDialog() == DialogResult.OK)
  173. {
  174. previewTxt.Text = ofd.FileName;
  175. }
  176. }
  177. private void publishAsNewToolStripMenuItem_Click(object sender, EventArgs e)
  178. {
  179. plugin.Map.SteamSection.PublishedFileId = PublishedFileId_t.Invalid.m_PublishedFileId;
  180. btnPublishMap.PerformClick();
  181. }
  182. private void previewTxt_TextChanged(object sender, EventArgs e)
  183. {
  184. try
  185. {
  186. (previewTxt.Tag as Image)?.Dispose();
  187. Bitmap preview = null;
  188. using (Bitmap b = new Bitmap(previewTxt.Text))
  189. {
  190. preview = new Bitmap(b.Width, b.Height, b.PixelFormat);
  191. using (Graphics g = Graphics.FromImage(preview))
  192. {
  193. g.DrawImage(b, Point.Empty);
  194. g.Flush();
  195. }
  196. }
  197. previewTxt.Tag = preview;
  198. }
  199. catch (Exception)
  200. {
  201. previewTxt.Tag = null;
  202. }
  203. UpdateControls();
  204. }
  205. private void descriptionTxt_TextChanged(object sender, EventArgs e)
  206. {
  207. UpdateControls();
  208. }
  209. private void UpdateControls()
  210. {
  211. btnPublishMap.Enabled = (previewTxt.Tag != null) && !string.IsNullOrEmpty(descriptionTxt.Text);
  212. }
  213. }
  214. }