forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginMain.cs
More file actions
297 lines (264 loc) · 9.33 KB
/
PluginMain.cs
File metadata and controls
297 lines (264 loc) · 9.33 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.IO;
using System.ComponentModel;
using WeifenLuo.WinFormsUI;
using PluginCore.Localization;
using PluginCore.Managers;
using PluginCore.Utilities;
using PluginCore.Helpers;
using PluginCore;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace UnityContext
{
public class PluginMain : IPlugin, InstalledSDKOwner
{
private String pluginName = "UnityContext";
private String pluginGuid = "1f387fab-421b-42ac-a985-72a03534f732";
private String pluginHelp = "www.flashdevelop.org/community/";
private String pluginDesc = "UnityScript context for the ASCompletion engine.";
private String pluginAuth = "GameJam / FlashDevelop Team";
private UnitySettings settingObject;
private Context contextInstance;
private String settingFilename;
#region Required Properties
/// <summary>
/// Api level of the plugin
/// </summary>
public Int32 Api
{
get { return 1; }
}
/// <summary>
/// Name of the plugin
/// </summary>
public String Name
{
get { return this.pluginName; }
}
/// <summary>
/// GUID of the plugin
/// </summary>
public String Guid
{
get { return this.pluginGuid; }
}
/// <summary>
/// Author of the plugin
/// </summary>
public String Author
{
get { return this.pluginAuth; }
}
/// <summary>
/// Description of the plugin
/// </summary>
public String Description
{
get { return this.pluginDesc; }
}
/// <summary>
/// Web address for help
/// </summary>
public String Help
{
get { return this.pluginHelp; }
}
/// <summary>
/// Object that contains the settings
/// </summary>
[Browsable(false)]
public Object Settings
{
get { return this.settingObject; }
}
#endregion
#region Required Methods
/// <summary>
/// Initializes the plugin
/// </summary>
public void Initialize()
{
this.InitBasics();
this.LoadSettings();
this.AddEventHandlers();
// Register Unity3D project type
ProjectManager.Helpers.ProjectCreator.AppendProjectType("project.u3dproj", typeof(UnityProject));
}
/// <summary>
/// Disposes the plugin
/// </summary>
public void Dispose()
{
this.SaveSettings();
}
/// <summary>
/// Handles the incoming events
/// </summary>
public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority prority)
{
switch (e.Type)
{
case EventType.SyntaxDetect:
// detect Actionscript language version
ITabbedDocument doc = PluginBase.MainForm.CurrentDocument;
if (doc == null || !doc.IsEditable) return;
if (doc.FileName.ToLower().EndsWith(".js"))
{
if (IsUnityProject(doc.FileName))
{
(e as TextEvent).Value = "unityscript";
e.Handled = true;
}
}
break;
case EventType.Completion:
ITabbedDocument doc2 = PluginBase.MainForm.CurrentDocument;
if (doc2 == null || !doc2.IsEditable) return;
if (doc2.FileName.ToLower().EndsWith(".js"))
{
if (IsUnityProject(doc2.FileName))
{
e.Handled = true;
}
}
break;
case EventType.UIStarted:
contextInstance = new Context(settingObject);
ValidateSettings();
// Associate this context with UnityScript language
ASCompletion.Context.ASContext.RegisterLanguage(contextInstance, "UnityScript");
break;
}
}
/// <summary>
///
/// </summary>
private bool IsUnityProject(string fileName)
{
if (contextInstance != null && contextInstance.Classpath != null)
{
foreach (ASCompletion.Model.PathModel path in contextInstance.Classpath)
{
if (fileName.StartsWith(path.Path, StringComparison.OrdinalIgnoreCase)) return true;
}
}
string dir = Path.GetDirectoryName(fileName);
while (Directory.GetFiles(dir, "*.u3dproj").Length == 0)
{
dir = Path.GetDirectoryName(dir);
if (dir.Length <= 3) return false;
}
return true;
}
#endregion
#region Custom Methods
/// <summary>
/// Initializes important variables
/// </summary>
public void InitBasics()
{
String dataPath = Path.Combine(PathHelper.DataDir, "UnityContext");
if (!Directory.Exists(dataPath)) Directory.CreateDirectory(dataPath);
this.settingFilename = Path.Combine(dataPath, "Settings.fdb");
this.pluginDesc = TextHelper.GetString("Info.Description");
}
/// <summary>
/// Adds the required event handlers
/// </summary>
public void AddEventHandlers()
{
EventManager.AddEventHandler(this, EventType.UIStarted | EventType.SyntaxDetect | EventType.Completion);
}
/// <summary>
/// Loads the plugin settings
/// </summary>
public void LoadSettings()
{
this.settingObject = new UnitySettings();
if (!File.Exists(this.settingFilename)) this.SaveSettings();
else
{
Object obj = ObjectSerializer.Deserialize(this.settingFilename, this.settingObject);
this.settingObject = (UnitySettings)obj;
if (settingObject.InstalledSDKs != null)
{
foreach (InstalledSDK sdk in settingObject.InstalledSDKs)
{
sdk.Owner = this;
}
}
}
if (this.settingObject.UserClasspath == null)
{
this.settingObject.UserClasspath = new String[] {};
}
}
/// <summary>
/// Fix some settings values when the context has been created
/// </summary>
private void ValidateSettings()
{
if (settingObject.InstalledSDKs == null || settingObject.InstalledSDKs.Length == 0)
{
/*string includedSDK = "Tools\\mtasc";
if (Directory.Exists(PathHelper.ResolvePath(includedSDK)))
{
InstalledSDK sdk = new InstalledSDK(this);
sdk.Path = includedSDK;
settingObject.InstalledSDKs = new InstalledSDK[] { sdk };
}*/
}
else foreach (InstalledSDK sdk in settingObject.InstalledSDKs) ValidateSDK(sdk);
settingObject.OnClasspathChanged += SettingObjectOnClasspathChanged;
}
/// <summary>
/// Update the classpath if an important setting has changed
/// </summary>
private void SettingObjectOnClasspathChanged()
{
if (contextInstance != null) contextInstance.BuildClassPath();
}
/// <summary>
/// Saves the plugin settings
/// </summary>
public void SaveSettings()
{
ObjectSerializer.Serialize(this.settingFilename, this.settingObject);
}
#endregion
#region InstalledSDKOwner Membres
public bool ValidateSDK(InstalledSDK sdk)
{
sdk.Owner = this;
IProject project = PluginBase.CurrentProject;
string path = sdk.Path;
if (project != null) path = PathHelper.ResolvePath(path, Path.GetDirectoryName(project.ProjectPath));
else path = PathHelper.ResolvePath(path);
try
{
if (path == null || (!Directory.Exists(path) && !File.Exists(path)))
{
ErrorManager.ShowInfo("Path not found:\n" + sdk.Path);
return false;
}
}
catch (Exception ex)
{
ErrorManager.ShowInfo("Invalid path (" + ex.Message + "):\n" + sdk.Path);
return false;
}
if (!Directory.Exists(path)) path = Path.GetDirectoryName(path);
string descriptor = Path.Combine(path, "Unity.exe");
if (File.Exists(descriptor))
{
FileVersionInfo info = FileVersionInfo.GetVersionInfo(descriptor);
sdk.Version = info.ProductVersion;
sdk.Name = info.ProductName;
}
else ErrorManager.ShowInfo("Unity.exe found:\n" + descriptor);
return false;
}
#endregion
}
}