-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup.cs
240 lines (194 loc) · 7.79 KB
/
Backup.cs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Sandbox;
using Sandbox.Graphics;
using VRage.FileSystem;
using VRage.Game;
using VRage.Render.Particles;
using VRage.Utils;
using VRageMath;
using VRageRender;
namespace Digi.ParticleEditor
{
public class Backup : EditorComponentBase
{
public bool Enable = true;
bool _shouldBackup;
public bool ShouldBackup
{
get => _shouldBackup;
set
{
_shouldBackup = value;
//if(value && ShouldBackupAt <= 0)
// ShouldBackupAt = Time + 3;
}
}
DateTime? ShouldBackupAt;
bool ShowMessage = false;
Task BackupTask;
DateTime? MessageHideAt;
DateTime Time => DateTime.Now;
public static string BackupPath = Path.Combine(MyFileSystem.UserDataPath, @"ParticleEditor\Backups");
public const string CrashedTokenFile = "LastWasCrash.token";
ParticleHandler SelectedParticle => EditorUI.SelectedParticle;
bool CheckedAskFile = false;
public Backup(Editor editor) : base(editor)
{
AlwaysUpdate = true;
Editor.EditorUI.SelectedParticle.EditsMade += SelectedParticle_ChangesMade;
Editor.EditorVisibleChanged += EditorVisibleChanged;
InsertUnhandledExceptionHandler();
}
public override void Dispose()
{
AppDomain.CurrentDomain.UnhandledException -= UnhandledException;
}
public override void Update()
{
if(Enable && ShouldBackupAt != null && Time >= ShouldBackupAt)
{
ShouldBackupAt = null;
BackupCurrentParticle();
}
if(ShowMessage)
{
if(Editor.ShowEditor)
{
bool inProgress = MessageHideAt == null;
string text = inProgress ? "Backing up..." : "Backed up!";
Color color = inProgress ? Color.Gray : Color.White; // inProgress ? Color.Yellow : Color.Lime;
MyRenderProxy.DebugDrawText2D(MyGuiManager.GetHudPixelCoordFromNormalizedCoord(new Vector2(0.5f, 0.01f)), text, color, 0.5f, MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER);
}
if(MessageHideAt != null && Time >= MessageHideAt.Value)
{
MessageHideAt = null;
ShowMessage = false;
}
}
}
void EditorVisibleChanged(bool visible)
{
if(visible)
{
CheckCrashRestoreBackup();
}
else
{
if(ShouldBackup)
BackupCurrentParticle();
}
}
void CheckCrashRestoreBackup()
{
if(CheckedAskFile)
return;
CheckedAskFile = true;
string askFile = Path.Combine(BackupPath, CrashedTokenFile);
if(File.Exists(askFile))
{
File.Delete(askFile);
EditorUI.PopupConfirmation("Game crashed last time, want to load a backup?", () =>
{
Editor.EditorUI.LoadParticleDialog(BackupPath);
});
}
}
void SelectedParticle_ChangesMade()
{
ShouldBackup = true;
// back up a bit later in case the change causes a crash
if(ShouldBackupAt == null)
ShouldBackupAt = Time + TimeSpan.FromSeconds(5);
}
public void BackupCurrentParticle()
{
try
{
ShouldBackup = false;
if(SelectedParticle.Name == null || !SelectedParticle.HasChanges)
return;
if(BackupTask == null || BackupTask.IsCompleted)
{
ShowMessage = true;
string name = SelectedParticle.Name;
MyObjectBuilder_ParticleEffect particleOB = MyParticleEffectDataSerializer.SerializeToObjectBuilder(SelectedParticle.Data);
BackupTask = Task.Run(() => BackupParticle(name, particleOB));
}
}
catch(Exception e)
{
Log.Error(e);
}
}
void BackupParticle(string fileNameNoExtension, MyObjectBuilder_ParticleEffect particleOB)
{
try
{
string fileName = fileNameNoExtension;
foreach(char c in Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(c, '_');
}
fileName += ".sbc";
Directory.CreateDirectory(BackupPath);
string filePath = Path.Combine(BackupPath, fileName);
string oldFile = Path.Combine(BackupPath, fileName + " (previous).sbc");
if(File.Exists(filePath))
{
if(File.Exists(oldFile))
{
File.Delete(oldFile);
}
File.Move(filePath, oldFile);
}
MyObjectBuilder_Definitions definitionsOB = new MyObjectBuilder_Definitions();
definitionsOB.ParticleEffects = new MyObjectBuilder_ParticleEffect[] { particleOB };
if(!EditorUI.SerializeToXML(filePath, definitionsOB))
{
Notifications.Show($"Failed to backup particle '{particleOB.Id.SubtypeName}'! Check SE log.", 5, Color.Red);
}
}
catch(Exception e)
{
Log.Error(e);
}
MessageHideAt = Time + TimeSpan.FromSeconds(1);
}
void InsertUnhandledExceptionHandler()
{
MethodInfo gameExceptionHandlerMethod = typeof(MyInitializer).GetMethod("UnhandledExceptionHandler", BindingFlags.NonPublic | BindingFlags.Static);
if(gameExceptionHandlerMethod == null)
{
Log.Error("Couldn't find 'MyInitializer.UnhandledExceptionHandler()', backup-on-crash will not work!");
}
else
{
UnhandledExceptionEventHandler handlerDelegate = gameExceptionHandlerMethod.CreateDelegate<UnhandledExceptionEventHandler>(null);
// butt-in to be first to get unhandled exceptions because the game's handler kills the process and doesn't allow anyone else to get the event, rood!
AppDomain.CurrentDomain.UnhandledException -= handlerDelegate;
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
AppDomain.CurrentDomain.UnhandledException += handlerDelegate;
}
}
void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string stack = e.ExceptionObject.ToString();
MyParticleEffectData data = EditorUI?.SelectedParticle?.Data;
if(data == null)
return;
// token to ask to restore backup on next load
File.WriteAllText(Path.Combine(BackupPath, CrashedTokenFile), string.Empty);
if(stack.Contains("Exception in render!")
|| stack.Contains("MyParticleGPUGenerationData"))
{
Log.Error("Crashed in render, if you remember what you changed on the particle right before this, report it on ParticleEditor's page!");
return; // don't backup if particle caused crash =)
}
MyObjectBuilder_ParticleEffect particleOB = MyParticleEffectDataSerializer.SerializeToObjectBuilder(data);
BackupParticle(data.Name + " (on crash)", particleOB);
}
}
}