forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPragmaFixing30.cs
More file actions
148 lines (124 loc) · 4.67 KB
/
Copy pathPragmaFixing30.cs
File metadata and controls
148 lines (124 loc) · 4.67 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using RequiredByNativeCodeAttribute = UnityEngine.Scripting.RequiredByNativeCodeAttribute;
namespace UnityEditor.Scripting
{
internal class PragmaFixing30
{
[RequiredByNativeCode]
static void FixJavaScriptPragmas()
{
string[] filesToFix = CollectBadFiles();
if (filesToFix.Length == 0)
return;
if (!InternalEditorUtility.inBatchMode)
PragmaFixingWindow.ShowWindow(filesToFix);
else
FixFiles(filesToFix);
}
public static void FixFiles(string[] filesToFix)
{
foreach (string f in filesToFix)
{
try
{
FixPragmasInFile(f);
}
catch (Exception ex)
{
Debug.LogError("Failed to fix pragmas in file '" + f + "'.\n" + ex.Message);
}
}
}
static bool FileNeedsPragmaFixing(string fileName)
{
return CheckOrFixPragmas(fileName, true);
}
static void FixPragmasInFile(string fileName)
{
CheckOrFixPragmas(fileName, false);
}
static bool CheckOrFixPragmas(string fileName, bool onlyCheck)
{
string oldText = File.ReadAllText(fileName);
StringBuilder text = new StringBuilder(oldText);
LooseComments(text);
Match strictMatch = PragmaMatch(text, "strict");
if (!strictMatch.Success)
return false;
bool hasDowncast = PragmaMatch(text, "downcast").Success;
bool hasImplicit = PragmaMatch(text, "implicit").Success;
if (hasDowncast && hasImplicit)
return false;
if (!onlyCheck)
DoFixPragmasInFile(fileName, oldText, strictMatch.Index + strictMatch.Length, hasDowncast, hasImplicit);
return true;
}
static void DoFixPragmasInFile(string fileName, string oldText, int fixPos, bool hasDowncast, bool hasImplicit)
{
string textToAdd = string.Empty;
string lineEndings = HasWinLineEndings(oldText) ? "\r\n" : "\n";
if (!hasImplicit)
textToAdd += lineEndings + "#pragma implicit";
if (!hasDowncast)
textToAdd += lineEndings + "#pragma downcast";
File.WriteAllText(fileName, oldText.Insert(fixPos, textToAdd));
}
static bool HasWinLineEndings(string text)
{
return text.IndexOf("\r\n") != -1;
}
static IEnumerable<string> SearchRecursive(string dir, string mask)
{
foreach (string d in Directory.GetDirectories(dir))
foreach (string f in SearchRecursive(d, mask))
yield return f;
foreach (string f in Directory.GetFiles(dir, mask))
yield return f;
}
static void LooseComments(StringBuilder sb)
{
// TODO: better comment ignoring? this one sort of does the job, it handles //
// and if it's in multiline comments, our added lines will end up commented as well
Regex r = new Regex("//");
foreach (Match m in r.Matches(sb.ToString()))
{
int pos = m.Index;
while (pos < sb.Length && sb[pos] != '\n' && sb[pos] != '\r')
sb[pos++] = ' ';
}
}
static Match PragmaMatch(StringBuilder sb, string pragma)
{
// unity java script, like regex, treats new line as space character as well
return new Regex(@"#\s*pragma\s*" + pragma).Match(sb.ToString());
}
static string[] CollectBadFiles()
{
List<string> filesToFix = new List<string>();
foreach (string f in SearchRecursive(Path.Combine(Directory.GetCurrentDirectory(), "Assets"), "*.js"))
{
try
{
if (FileNeedsPragmaFixing(f))
filesToFix.Add(f);
}
catch (Exception ex)
{
Debug.LogError("Failed to fix pragmas in file '" + f + "'.\n" + ex.Message);
}
}
return filesToFix.ToArray();
}
}
}