using System; using System.Collections.Generic; using System.IO; using System.Windows; using System.Windows.Controls; using Sandbox.Engine.Networking; using Sandbox.Game.World; using Torch.Server.Managers; using Torch.Server.ViewModels; using Torch.Utils; using VRage; using VRage.Dedicated; using VRage.FileSystem; using VRage.Game; using VRage.Utils; namespace Torch.Server { /// /// Interaction logic for WorldGeneratorDialog.xaml /// public partial class WorldGeneratorDialog : Window { private InstanceManager _instanceManager; private List _checkpoints = new List(); private PremadeCheckpointItem _currentItem; [ReflectedStaticMethod(Type = typeof(ConfigForm), Name = "LoadLocalization")] private static Action _loadLocalization = null!; public WorldGeneratorDialog(InstanceManager instanceManager) { _instanceManager = instanceManager; InitializeComponent(); _loadLocalization(); var scenarios = MyLocalCache.GetAvailableWorldInfos(new List {Path.Combine(MyFileSystem.ContentPath, "CustomWorlds")}); foreach (var tup in scenarios) { if (tup.Item2 == null) continue; string directory = tup.Item1; MyWorldInfo info = tup.Item2; var sessionNameId = MyStringId.GetOrCompute(info.SessionName); string localizedName = MyTexts.GetString(sessionNameId); var checkpoint = MyLocalCache.LoadCheckpoint(directory, out _); checkpoint.OnlineMode = MyOnlineModeEnum.PUBLIC; // Keen, why do random checkpoints point to the SBC and not the folder! directory = directory.Replace("Sandbox.sbc", ""); _checkpoints.Add(new PremadeCheckpointItem { Name = localizedName, Icon = Path.Combine(directory, "thumb.jpg"), Path = directory, Checkpoint = checkpoint}); } /* var premadeCheckpoints = Directory.EnumerateDirectories(Path.Combine("Content", "CustomWorlds")); foreach (var path in premadeCheckpoints) { var thumbPath = Path.GetFullPath(Directory.EnumerateFiles(path).First(x => x.Contains("thumb"))); _checkpoints.Add(new PremadeCheckpointItem { Path = path, Icon = thumbPath, Name = Path.GetFileName(path) }); }*/ PremadeCheckpoints.ItemsSource = _checkpoints; } private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { string worldName = string.IsNullOrEmpty(WorldName.Text) ? _currentItem.Name : WorldName.Text; #pragma warning disable CS0618 var worldPath = Path.Combine(TorchBase.Instance.Config.InstancePath, "Saves", worldName); #pragma warning restore CS0618 var checkpoint = _currentItem.Checkpoint; if (Directory.Exists(worldPath)) { MessageBox.Show("World already exists with that name."); return; } Directory.CreateDirectory(worldPath); foreach (var file in Directory.EnumerateFiles(_currentItem.Path, "*", SearchOption.AllDirectories)) { // Trash code to work around inconsistent path formats. var fileRelPath = file.Replace($"{_currentItem.Path.TrimEnd('\\')}\\", ""); var destPath = Path.Combine(worldPath, fileRelPath); File.Copy(file, destPath); } checkpoint.SessionName = worldName; MyLocalCache.SaveCheckpoint(checkpoint, worldPath); _instanceManager.SelectCreatedWorld(worldPath); Close(); } private void PremadeCheckpoints_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selected = (PremadeCheckpointItem)PremadeCheckpoints.SelectedItem; _currentItem = selected; SettingsView.DataContext = new SessionSettingsViewModel(_currentItem.Checkpoint.Settings); } } public class PremadeCheckpointItem { public string Path { get; set; } public string Name { get; set; } public string Icon { get; set; } public MyObjectBuilder_Checkpoint Checkpoint { get; set; } } }