forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairingRestHandler.cs
More file actions
78 lines (65 loc) · 2.65 KB
/
Copy pathPairingRestHandler.cs
File metadata and controls
78 lines (65 loc) · 2.65 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Diagnostics;
using System.IO;
using UnityEditor.Callbacks;
using UnityEditorInternal;
using UnityEngine;
namespace UnityEditor.RestService
{
internal class PairingRestHandler : JSONHandler
{
protected override JSONValue HandlePost(Request request, JSONValue payload)
{
ScriptEditorSettings.ServerURL = payload["url"].AsString();
ScriptEditorSettings.Name = payload.ContainsKey("name") ? payload["name"].AsString() : null;
ScriptEditorSettings.ProcessId = payload.ContainsKey("processid") ? (int)payload["processid"].AsFloat() : -1;
Logger.Log("[Pair] Name: " + (ScriptEditorSettings.Name ?? "<null>") +
" ServerURL " + ScriptEditorSettings.ServerURL +
" Process id: " + ScriptEditorSettings.ProcessId);
var result = new JSONValue();
result["unityprocessid"] = Process.GetCurrentProcess().Id;
result["unityproject"] = Application.dataPath;
return result;
}
internal static void Register()
{
Router.RegisterHandler("/unity/pair", new PairingRestHandler());
}
[OnOpenAsset]
static bool OnOpenAsset(int instanceID, int line)
{
if (ScriptEditorSettings.ServerURL == null)
return false;
var assetpath = Path.GetFullPath(Application.dataPath + "/../" + AssetDatabase.GetAssetPath(instanceID)).Replace('\\', '/');
var lowerAssetPath = assetpath.ToLower();
if (!lowerAssetPath.EndsWith(".cs") && !lowerAssetPath.EndsWith(".js") && !lowerAssetPath.EndsWith(".boo"))
return false;
if (!IsScriptEditorRunning() || !RestRequest.Send("/openfile", "{ \"file\" : \"" + assetpath + "\", \"line\" : " + line + " }", 5000))
{
ScriptEditorSettings.ServerURL = null;
ScriptEditorSettings.Name = null;
ScriptEditorSettings.ProcessId = -1;
return false;
}
return true;
}
static bool IsScriptEditorRunning()
{
if (ScriptEditorSettings.ProcessId < 0)
return false;
try
{
var process = Process.GetProcessById(ScriptEditorSettings.ProcessId);
return !process.HasExited;
}
catch (Exception e)
{
Logger.Log(e);
return false;
}
}
}
}