-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathWorldGeneratorDialog.xaml.cs
More file actions
190 lines (166 loc) · 7.19 KB
/
WorldGeneratorDialog.xaml.cs
File metadata and controls
190 lines (166 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using NLog;
using Sandbox.Definitions;
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.Game.Localization;
using VRage.Utils;
namespace Torch.Server
{
/// <summary>
/// Interaction logic for WorldGeneratorDialog.xaml
/// </summary>
public partial class WorldGeneratorDialog : Window
{
private InstanceManager _instanceManager;
private List<PremadeCheckpointItem> _checkpoints = new List<PremadeCheckpointItem>();
private List<string> _worldNames = new List<string>();
private PremadeCheckpointItem _currentItem;
[ReflectedStaticMethod(Type = typeof(ConfigForm), Name = "LoadLocalization")]
private static Action _loadLocalization;
public WorldGeneratorDialog(InstanceManager instanceManager)
{
_instanceManager = instanceManager;
InitializeComponent();
_loadLocalization();
string worldsDir = Path.Combine(MyFileSystem.ContentPath, "CustomWorlds");
var result = new List<Tuple<string,MyWorldInfo>>();
GetWorldInfo(worldsDir, result);
foreach (var tup in result)
{
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
});
}
foreach (var checkpoint in _checkpoints)
{
_worldNames.Add(checkpoint.Name);
}
PremadeCheckpoints.ItemsSource = _worldNames;
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
string worldName = string.IsNullOrEmpty(WorldName.Text) ? _currentItem.Name : WorldName.Text;
var worldPath = Path.Combine(TorchBase.Instance.Config.InstancePath, "Saves", worldName);
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.SelectWorld(worldPath, false);
_instanceManager.ImportSelectedWorldConfig();
Close();
}
private void PremadeCheckpoints_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = _checkpoints.FirstOrDefault(x => x.Name == PremadeCheckpoints.SelectedItem.ToString());
_currentItem = selected;
if (_currentItem == null) return;
SettingsView.DataContext = new SessionSettingsViewModel(_currentItem.Checkpoint.Settings);
CheckpointImage.Source = new BitmapImage(new Uri(_currentItem.Icon));
}
private void GetWorldInfo(string savesPath, List<Tuple<string, MyWorldInfo>> result)
{
foreach (var saveDir in Directory.GetDirectories(savesPath, "*", SearchOption.TopDirectoryOnly))
{
bool isCompatible;
string platformSessionPath = null;
platformSessionPath = MyLocalCache.GetSessionPathFromScenario(saveDir, false, out isCompatible);
if (platformSessionPath != null && isCompatible)
{
AddWorldInfo(result, platformSessionPath, saveDir, " [PC]");
}
string xboxPlatformSessionPath = MyLocalCache.GetSessionPathFromScenario(saveDir, true, out isCompatible);
if (xboxPlatformSessionPath != null && isCompatible)
{
AddWorldInfo(result, xboxPlatformSessionPath, saveDir, " [XBOX]");
}
if (platformSessionPath == null && xboxPlatformSessionPath == null && isCompatible)
{
AddWorldInfo(result, saveDir, saveDir, "");
}
}
}
private static void AddWorldInfo(List<Tuple<string, MyWorldInfo>> result, string sessionDir, string saveDir, string namePostfix)
{
MyWorldInfo worldInfo = null;
var worldConfiguration = MyLocalCache.LoadWorldConfiguration(sessionDir);
if (worldConfiguration == null)
{
worldInfo = MyLocalCache.LoadWorldInfoFromFile(sessionDir);
}
else
{
if (string.IsNullOrEmpty(worldConfiguration.SessionName) || !worldConfiguration.LastSaveTime.HasValue)
{
worldInfo = MyLocalCache.LoadWorldInfoFromFile(sessionDir);
}
else
{
worldInfo = new MyWorldInfo
{
SessionName = worldConfiguration.SessionName,
LastSaveTime = worldConfiguration.LastSaveTime.Value
};
}
if (worldInfo != null && string.IsNullOrEmpty(worldInfo.SessionName))
{
worldInfo.SessionName = Path.GetFileName(sessionDir);
}
}
if (worldInfo != null)
{
worldInfo.SessionName += namePostfix;
}
worldInfo.SaveDirectory = saveDir;
result.Add(Tuple.Create(sessionDir, worldInfo));
}
}
public class PremadeCheckpointItem
{
public string Path { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public MyObjectBuilder_Checkpoint Checkpoint { get; set; }
}
}