/// <summary> /// Uploads the mission to server. /// </summary> /// <param name="mission"></param> /// <param name="password">User account password.</param> /// <param name="missionId">The ID of the mission on server to update; null when publishing a new mission.</param> /// <returns></returns> public static bool SendMission(Mission mission, string password, int?missionId) { try { var info = new ZkData.Mission() { Description = mission.Description, DescriptionStory = mission.DescriptionStory != String.Empty ? mission.DescriptionStory : null, Map = mission.Map.Name, Mod = mission.Mod.Name, Name = mission.Name, ScoringMethod = mission.ScoringMethod, Script = mission.GetScript(), ModRapidTag = mission.RapidTag, }; var slots = mission.GetSlots(); var image = File.ReadAllBytes(mission.ImagePath).ToImage(96, 96); var pngEncoder = new PngBitmapEncoder(); pngEncoder.Frames.Add(BitmapFrame.Create(image)); var imageStream = new MemoryStream(); pngEncoder.Save(imageStream); imageStream.Position = 0; info.Image = imageStream.ToArray(); if (ApplicationDeployment.IsNetworkDeployed) { info.MissionEditorVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(); } using (var unitSync = new UnitSync(Settings.Default.SpringPath)) info.SpringVersion = unitSync.Version; if (missionId.HasValue) { info.MissionID = missionId.Value; } string tempPath = null; var missionFileName = "publish_mission_temp.sdz"; try { string writableDir = Settings.Default.SpringPath; // this is an engine folder, use our actual writable folder bool isEngineFolder = File.Exists(Path.Combine(writableDir, Environment.OSVersion.Platform == PlatformID.Unix ? "spring" : "spring.exe")); if (isEngineFolder) { writableDir = SpringPaths.GetMySpringDocPath(); } var paths = new SpringPaths(writableDir, true, false); string engine = paths.GetEngineList().LastOrDefault(); if (isEngineFolder) { engine = paths.GetEngineList().FirstOrDefault(x => x == new DirectoryInfo(Settings.Default.SpringPath).Name); } using (var unitSync = new ZkData.UnitSyncLib.UnitSync(paths, engine)) { var modPath = Path.Combine(paths.WritableDirectory, "games"); tempPath = Path.Combine(modPath, missionFileName); } if (File.Exists(tempPath)) { File.Delete(tempPath); } mission.CreateArchive(tempPath); Mod mod; using (var unitSync = new ZkData.UnitSyncLib.UnitSync(paths, engine)) { mod = (Mod)unitSync.GetResourceFromFileName(mission.Mod.ArchiveName); if (mod == null) { throw new Exception("Mod metadata not extracted: mod not found"); } } info.Mutator = File.ReadAllBytes(tempPath); var client = MissionServiceClientFactory.MakeClient(); client.SendMission(info, slots, mission.Author, password, mod); MessageBox.Show("Mission successfully uploaded.\n\rIt is now accessible from the lobby.\r\nPlease make sure it works!"); return(true); } catch (Exception e) { if (Debugger.IsAttached) { throw; } MessageBox.Show(e.Message); return(false); } finally { try { if (tempPath != null && File.Exists(tempPath)) { File.Delete(tempPath); } } catch {} } } catch (FaultException <ExceptionDetail> e) { if (Debugger.IsAttached) { throw; } MessageBox.Show(e.Message); return(false); } catch (FaultException e) { if (Debugger.IsAttached) { throw; } MessageBox.Show(e.Message); return(false); } }
public static bool SendMission(Mission mission, string password, int? missionId) { try { var info = new ZkData.Mission() { Description = mission.Description, DescriptionStory = mission.DescriptionStory != String.Empty ? mission.DescriptionStory : null, Map = mission.Map.Name, Mod = mission.Mod.Name, Name = mission.Name, ScoringMethod = mission.ScoringMethod, Script = mission.GetScript(), ModRapidTag = mission.RapidTag, }; var slots = mission.GetSlots(); var image = File.ReadAllBytes(mission.ImagePath).ToImage(96, 96); var pngEncoder = new PngBitmapEncoder(); pngEncoder.Frames.Add(BitmapFrame.Create(image)); var imageStream = new MemoryStream(); pngEncoder.Save(imageStream); imageStream.Position = 0; info.Image = imageStream.ToArray(); if (ApplicationDeployment.IsNetworkDeployed) info.MissionEditorVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(); using (var unitSync = new UnitSync(Settings.Default.SpringPath)) info.SpringVersion = unitSync.Version; if (missionId.HasValue) info.MissionID = missionId.Value; string tempPath = null; var missionFileName = "publish_mission_temp.sdz"; try { var paths = new SpringPaths(Settings.Default.SpringPath); using (var unitSync = new ZkData.UnitSyncLib.UnitSync(paths)) { var modPath = Path.Combine(paths.WritableDirectory, "games"); tempPath = Path.Combine(modPath, missionFileName); } if (File.Exists(tempPath)) File.Delete(tempPath); mission.CreateArchive(tempPath); Mod mod; using (var unitSync = new ZkData.UnitSyncLib.UnitSync(paths)) { mod = (Mod)unitSync.GetResourceFromFileName(mission.Mod.ArchiveName); if (mod == null) throw new Exception("Mod metadata not extracted: mod not found"); } info.Mutator = File.ReadAllBytes(tempPath); var client = MissionServiceClientFactory.MakeClient(); client.SendMission(info, slots, mission.Author, password, mod); MessageBox.Show("Mission successfully uploaded.\n\rIt is now accessible from the lobby.\r\nPlease make sure it works!"); return true; } catch(Exception e) { if (Debugger.IsAttached) throw; MessageBox.Show(e.Message); return false; } finally { try { if (tempPath != null && File.Exists(tempPath)) File.Delete(tempPath); } catch {} } } catch(FaultException<ExceptionDetail> e) { if (Debugger.IsAttached) throw; MessageBox.Show(e.Message); return false; } catch(FaultException e) { if (Debugger.IsAttached) throw; MessageBox.Show(e.Message); return false; } }