-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZplGmlfmtPluginCommand.cs
275 lines (236 loc) · 10.4 KB
/
ZplGmlfmtPluginCommand.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
using System;
using System.Collections.Generic;
using System.Linq;
using YoYoStudio.Core.Utils;
using YoYoStudio.Core.Utils.Preferences;
using YoYoStudio.FileAPI;
using Core.CoreOS.FileAPI;
using YoYoStudio.GUI.Gadgets;
using YoYoStudio.GUI.Layout;
using YoYoStudio.Plugins.Attributes;
using YoYoStudio.GUI;
namespace YoYoStudio
{
namespace Plugins
{
namespace ZplGmlfmtPlugin
{
[ModuleName("ZplGmlfmtPlugin Command", "Responsible for the plugin's UI and process stuff.")]
public class ZplGmlfmtPluginCommand : IModule, IDisposable
{
public ModulePackage IdeInterface { get; set; }
public ModulePackage Package => IdeInterface;
public ZplGmlfmtPluginPreferences Preferences { get; set; }
public bool LayoutFailed { get; set; }
public bool Running { get; set; }
public string MenuId { get; set; }
public MenuEntry RunButton { get; set; }
public void SetButtonActive()
{
Running = false;
if (!LayoutFailed && RunButton != null)
{
RunButton.Deactivated = Running;
}
}
public void SetButtonDeactive()
{
Running = true;
if (!LayoutFailed)
{
RunButton.Deactivated = Running;
}
}
public void ReloadProject()
{
IDE.OpenProject(MacroExpansion.Expand("${project_full_filename}"), false);
}
public void CloseReloadProject(TimeSpan _delta)
{
WindowManager.OnPreProcess.RemoveThis();
IDE.CloseProject(ReloadProject, false);
}
public void OnCmdCompletion(int _exitcode)
{
SetButtonActive();
// fuck filewatcher
// WindowManager.OnPreProcess += CloseReloadProject;
}
public bool YYFileExists(string _path)
{
return FileSystem.FileExists(_path, null, null).wait() == FileError.OK;
}
public string GetProjectDirectorySafe()
{
return FileSystem.PlatformSafePath(MacroExpansion.Expand("${project_dir}"));
}
public void RunGmlfmt()
{
if (!LayoutFailed && Preferences != null && !Running)
{
string _process = Preferences.GmlfmtPath;
if (string.IsNullOrWhiteSpace(_process) || !YYFileExists(_process))
{
MessageDialog.ShowWarning("ZGFP_Title", "ZGFP_Path");
return;
}
string _wdir = GetProjectDirectorySafe();
if (string.IsNullOrWhiteSpace(_wdir))
{
MessageDialog.ShowWarning("ZGFP_Title", "ZGFP_Project");
return;
}
var gmlfmtproc = new CmdProcess(_process, "", eOutputStream.Output | eOutputStream.AssetCompiler, true);
gmlfmtproc.OnCompletion += OnCmdCompletion;
// deactivate the UI button first, and then run the tool:
SetButtonDeactive();
gmlfmtproc.RunAsync(true, _wdir);
}
}
public void OnButtonClick()
{
RunGmlfmt();
}
public MenuEntry AttachToButton(MenuBar _mb, string _id, ButtonClick _delegate, bool deactiv)
{
MenuEntry me = _mb.RetrieveMenuEntry(_id);
if (me != null)
{
me.OnButtonClick += _delegate;
me.Deactivated = deactiv;
}
else
{
Log.WriteLine(eLog.Default, "[ZplGmlfmt]: Failed to attach to a UI button {0} :(", _id);
}
return me;
}
public void PerformUIAttachement(MenuBar _mb)
{
if (!LayoutFailed && _mb != null)
{
RunButton = AttachToButton(_mb, "menu_run_gmlfmt", OnButtonClick, Running || !IDE.IsProjectLoadingComplete);
}
}
public void AttachToUI()
{
if (!LayoutFailed)
{
if (MenuId != "")
{
IdeInterface.WindowManager.RemoveStaticMenu(MenuId);
//Log.WriteLine(eLog.Default, "[ZplGmlfmt]: Detached from menu.");
}
MenuBar mb = (MenuBar)IdeInterface.WindowManager.CreateLayout("ZplGmlfmtPluginLayout")[0];
MenuBarEntry mbe = (MenuBarEntry)mb.StackedGadgets[0];
PerformUIAttachement(mb);
MenuId = mbe.MenuBarEntryID.Name;
IdeInterface.WindowManager.RegisterStaticMenu(mbe);
}
}
public string GetPluginDirectory()
{
string path = YoYoPath.Combine(AppDomain.CurrentDomain.BaseDirectory, "Custom Plugins");
Log.WriteLine(eLog.Default, "[ZplGmlfmt]: GetPluginDirectory() = '{0}'", path);
return path;
}
public void LoadAssets()
{
try
{
LayoutFailed = false;
string myDir = GetPluginDirectory();
string langret = Language.Load(YoYoPath.Combine(myDir, "ZplGmlfmtPluginStrings.csv"));
LayoutManager.LoadLayout(YoYoPath.Combine(myDir, "ZplGmlfmtPluginLayout.xml"));
Log.WriteLine(eLog.Default, "[ZplGmlfmt]: Assets loaded. {0}", langret);
}
catch (Exception exc)
{
LayoutFailed = true;
MessageDialog.ShowUnlocalisedWarning("gml_fmt for Zeus", "Failed to load the required assets:\n" + exc.ToString());
}
}
public void OnChange(List<string> _changes)
{
if (_changes.Any(_pref => _pref.Contains("ZplGmlfmtPlugin")))
{
Preferences = PreferencesManager.Get<ZplGmlfmtPluginPreferences>();
Log.WriteLine(eLog.Default, "[ZplGmlfmt]: OnChange() Preferences updated.");
}
}
public void OnProjectSaved(bool _success)
{
if (_success)
{
if (Preferences != null && Preferences.RunGmlfmtOnSave)
{
RunGmlfmt();
}
}
}
[Function("OnGmlfmtKeycombo", "zplgmlfmtplugin_run", "Runs gml_fmt if keycombo is pressed.", "function")]
public void OnGmlfmtKeycombo()
{
// only run if the project is loaded.
if (IDE.IsProjectLoadingComplete) RunGmlfmt();
}
public void OnProjectLoaded()
{
SetButtonActive();
}
public void OnInitialised()
{
// initialize properties to default values (not running, no menu attached)
Running = false;
MenuId = "";
// load prefs
PreferencesManager.OnChange += OnChange;
Preferences = PreferencesManager.Get<ZplGmlfmtPluginPreferences>();
// load ui
LoadAssets();
AttachToUI();
IDE.OnProjectLoaded += OnProjectLoaded; // will activate the button on project load.
// 'Run On Every Save' handler.
IDE.OnProjectSaved += OnProjectSaved;
// we're done here
Log.WriteLine(eLog.Default, "[ZplGmlfmt]: Initialised.");
}
public void Initialise(ModulePackage _ide)
{
IdeInterface = _ide;
OnInitialised();
}
#region IDisposable Support
private bool disposed = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposed = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
~ZplGmlfmtPluginCommand()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
GC.SuppressFinalize(this);
}
#endregion
}
}
}
}