-
Notifications
You must be signed in to change notification settings - Fork 12
/
Main.Migrator.cs
276 lines (262 loc) · 12.6 KB
/
Main.Migrator.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using Newtonsoft.Json.Linq;
using SuchByte.MacroDeck.ActionButton;
using SuchByte.MacroDeck.DataTypes.Core;
using SuchByte.MacroDeck.Plugins;
using SuchByte.MacroDeck.Profiles;
using SuchByte.MacroDeck.Variables;
using SuchByte.OBSWebSocketPlugin.Actions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SuchByte.OBSWebSocketPlugin
{
public partial class Main
{
private void MigrateVersion()
{
var configVersion = GetConfigVersion();
var currentVersion = GetCurrentVersion();
if (configVersion != currentVersion)
{
var choice = PromptAutoUpdate();
if (choice == DialogResult.Yes)
{
MigrateConfigs();
MigrateActions();
SetCurrentVersion();
} else if (choice == DialogResult.No)
{
var skip = PromptSkipUpdate();
if (skip == DialogResult.Yes)
{
SetCurrentVersion();
}
}
}
}
private DialogResult PromptAutoUpdate()
{
var message = new MacroDeck.GUI.CustomControls.MessageBox();
message.Width = 500;
message.Height = 300;
return message.ShowDialog(
string.Format("{0}: Auto-Update Buttons?", Name),
"Actions and/or Condition configurations can be updated from the previous version for you automatically.\n\n>> WARNING <<\nYou should perform a backup before attempting this! While it should work, there's a chance it could fail.\n\nProceed to automatically update existing configurations to newest version?",
MessageBoxButtons.YesNo
);
}
private DialogResult PromptSkipUpdate()
{
var message = new MacroDeck.GUI.CustomControls.MessageBox();
return message.ShowDialog(
string.Format("{0}: Skip Auto-Update Buttons?", Name),
"Select \"Yes\" to never see the Auto-Update Buttons dialog for this version again. Select \"No\" to show it next time Macro Deck is started.",
MessageBoxButtons.YesNo
);
}
private System.Version GetConfigVersion()
{
var versionString = PluginConfiguration.GetValue(this, "Version");
versionString = versionString == "" ? "0.0.0" : versionString;
return new System.Version(versionString);
}
private System.Version GetCurrentVersion()
{
return System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
}
private void SetCurrentVersion()
{
var current_version = GetCurrentVersion();
PluginConfiguration.SetValue(this, "Version", current_version.ToString());
}
private void MigrateConfigs()
{
var version = GetConfigVersion();
if (version.ToString() == "0.0.0")
{
V1_5_0();
}
}
private void V1_5_0()
{
var variables = VariableManager.GetVariables(this);
foreach (var variable in variables)
{
if (!variable.Name.StartsWith("obs_default_"))
{
VariableManager.SetValue(String.Format("obs_default_{0}", variable.Name.Replace("obs_", "")), variable.Value, variable.VariableType, PluginInstance.Main, null);
}
}
foreach (var profile in ProfileManager.Profiles)
{
foreach (var folder in profile.Folders)
{
foreach (var button in folder.ActionButtons)
{
if (button.StateBindingVariable.StartsWith("obs_"))
{
button.StateBindingVariable = String.Format("obs_default_{0}", button.StateBindingVariable.Replace("obs_", ""));
}
foreach (var action in button.Actions)
{
if (action.GetType() == typeof(ConditionAction))
{
var condition = (ConditionAction)action;
if (condition.ConditionType == ConditionType.Variable && condition.ConditionValue1Source.StartsWith("obs_"))
{
condition.ConditionValue1Source = String.Format("obs_default_{0}", condition.ConditionValue1Source.Replace("obs_", ""));
};
}
}
foreach (var action in button.ActionsRelease)
{
if (action.GetType() == typeof(ConditionAction))
{
var condition = (ConditionAction)action;
if (condition.ConditionType == ConditionType.Variable && condition.ConditionValue1Source.StartsWith("obs_"))
{
condition.ConditionValue1Source = String.Format("obs_default_{0}", condition.ConditionValue1Source.Replace("obs_", ""));
};
}
}
foreach (var action in button.ActionsLongPress)
{
if (action.GetType() == typeof(ConditionAction))
{
var condition = (ConditionAction)action;
if (condition.ConditionType == ConditionType.Variable && condition.ConditionValue1Source.StartsWith("obs_"))
{
condition.ConditionValue1Source = String.Format("obs_default_{0}", condition.ConditionValue1Source.Replace("obs_", ""));
};
}
}
foreach (var action in button.ActionsLongPressRelease)
{
if (action.GetType() == typeof(ConditionAction))
{
var condition = (ConditionAction)action;
if (condition.ConditionType == ConditionType.Variable && condition.ConditionValue1Source.StartsWith("obs_"))
{
condition.ConditionValue1Source = String.Format("obs_default_{0}", condition.ConditionValue1Source.Replace("obs_", ""));
};
}
}
foreach (var listener in button.EventListeners)
{
if (listener.Parameter.StartsWith("obs_"))
{
listener.Parameter = String.Format("obs_default_{0}", listener.Parameter.Replace("obs_", ""));
}
foreach (var action in listener.Actions)
{
if (action.GetType() == typeof(ConditionAction))
{
var condition = (ConditionAction)action;
if (condition.ConditionType == ConditionType.Variable && condition.ConditionValue1Source.StartsWith("obs_"))
{
condition.ConditionValue1Source = String.Format("obs_default_{0}", condition.ConditionValue1Source.Replace("obs_", ""));
};
}
}
}
}
}
}
foreach (var variable in variables)
{
if (!variable.Name.StartsWith("obs_default_"))
{
VariableManager.DeleteVariable(variable.Name);
}
}
GC.Collect();
}
private List<PluginAction> FindNestedActions(List<PluginAction> actions)
{
var allActions = new List<PluginAction>();
foreach (var action in actions)
{
if (action.GetType() == typeof(ConditionAction))
{
var condition = action as ConditionAction;
allActions.AddRange(FindNestedActions(condition.Actions));
allActions.AddRange(FindNestedActions(condition.ActionsElse));
}
else if ((action as ActionBase) != null)
{
allActions.Add(action);
}
}
return allActions;
}
private void MigrateActions()
{
foreach (var profile in ProfileManager.Profiles)
{
foreach (var folder in profile.Folders)
{
foreach (var button in folder.ActionButtons)
{
foreach (var action in FindNestedActions(button.Actions))
{
var actionBase = action as ActionBase;
var actionConfig = actionBase?.GetConfig();
if (actionBase != null && actionConfig.Version != actionConfig.TargetVersion)
{
var newConfig = actionBase.GetConfig().UpgradeConfig();
action.Configuration = JObject.FromObject(newConfig).ToString();
}
}
foreach (var action in FindNestedActions(button.ActionsRelease))
{
var actionBase = action as ActionBase;
var actionConfig = actionBase?.GetConfig();
if (actionBase != null && actionConfig.Version != actionConfig.TargetVersion)
{
var newConfig = actionBase.GetConfig().UpgradeConfig();
action.Configuration = JObject.FromObject(newConfig).ToString();
}
}
foreach (var action in FindNestedActions(button.ActionsLongPress))
{
var actionBase = action as ActionBase;
var actionConfig = actionBase?.GetConfig();
if (actionBase != null && actionConfig.Version != actionConfig.TargetVersion)
{
var newConfig = actionBase.GetConfig().UpgradeConfig();
action.Configuration = JObject.FromObject(newConfig).ToString();
}
}
foreach (var action in FindNestedActions(button.ActionsLongPressRelease))
{
var actionBase = action as ActionBase;
var actionConfig = actionBase?.GetConfig();
if (actionBase != null && actionConfig.Version != actionConfig.TargetVersion)
{
var newConfig = actionBase.GetConfig().UpgradeConfig();
action.Configuration = JObject.FromObject(newConfig).ToString();
}
}
foreach (var listener in button.EventListeners)
{
foreach (var action in FindNestedActions(listener.Actions))
{
var actionBase = action as ActionBase;
var actionConfig = actionBase?.GetConfig();
if (actionBase != null && actionConfig.Version != actionConfig.TargetVersion)
{
var newConfig = actionBase.GetConfig().UpgradeConfig();
action.Configuration = JObject.FromObject(newConfig).ToString();
}
}
}
}
}
}
GC.Collect();
}
}
}