forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPragmaFixing30.cs
More file actions
140 lines (140 loc) · 3.79 KB
/
Copy pathPragmaFixing30.cs
File metadata and controls
140 lines (140 loc) · 3.79 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditorInternal;
using UnityEngine;
namespace UnityEditor.Scripting
{
internal class PragmaFixing30
{
private static void FixJavaScriptPragmas()
{
string[] array = PragmaFixing30.CollectBadFiles();
if (array.Length == 0)
{
return;
}
if (!InternalEditorUtility.inBatchMode)
{
PragmaFixingWindow.ShowWindow(array);
}
else
{
PragmaFixing30.FixFiles(array);
}
}
public static void FixFiles(string[] filesToFix)
{
for (int i = 0; i < filesToFix.Length; i++)
{
string text = filesToFix[i];
try
{
PragmaFixing30.FixPragmasInFile(text);
}
catch (Exception ex)
{
UnityEngine.Debug.LogError("Failed to fix pragmas in file '" + text + "'.\n" + ex.Message);
}
}
}
private static bool FileNeedsPragmaFixing(string fileName)
{
return PragmaFixing30.CheckOrFixPragmas(fileName, true);
}
private static void FixPragmasInFile(string fileName)
{
PragmaFixing30.CheckOrFixPragmas(fileName, false);
}
private static bool CheckOrFixPragmas(string fileName, bool onlyCheck)
{
string text = File.ReadAllText(fileName);
StringBuilder sb = new StringBuilder(text);
PragmaFixing30.LooseComments(sb);
Match match = PragmaFixing30.PragmaMatch(sb, "strict");
if (!match.Success)
{
return false;
}
bool success = PragmaFixing30.PragmaMatch(sb, "downcast").Success;
bool success2 = PragmaFixing30.PragmaMatch(sb, "implicit").Success;
if (success && success2)
{
return false;
}
if (!onlyCheck)
{
PragmaFixing30.DoFixPragmasInFile(fileName, text, match.Index + match.Length, success, success2);
}
return true;
}
private static void DoFixPragmasInFile(string fileName, string oldText, int fixPos, bool hasDowncast, bool hasImplicit)
{
string text = string.Empty;
string str = (!PragmaFixing30.HasWinLineEndings(oldText)) ? "\n" : "\r\n";
if (!hasImplicit)
{
text = text + str + "#pragma implicit";
}
if (!hasDowncast)
{
text = text + str + "#pragma downcast";
}
File.WriteAllText(fileName, oldText.Insert(fixPos, text));
}
private static bool HasWinLineEndings(string text)
{
return text.IndexOf("\r\n") != -1;
}
[DebuggerHidden]
private static IEnumerable<string> SearchRecursive(string dir, string mask)
{
PragmaFixing30.<SearchRecursive>c__Iterator6 <SearchRecursive>c__Iterator = new PragmaFixing30.<SearchRecursive>c__Iterator6();
<SearchRecursive>c__Iterator.dir = dir;
<SearchRecursive>c__Iterator.mask = mask;
<SearchRecursive>c__Iterator.<$>dir = dir;
<SearchRecursive>c__Iterator.<$>mask = mask;
PragmaFixing30.<SearchRecursive>c__Iterator6 expr_23 = <SearchRecursive>c__Iterator;
expr_23.$PC = -2;
return expr_23;
}
private static void LooseComments(StringBuilder sb)
{
Regex regex = new Regex("//");
foreach (Match match in regex.Matches(sb.ToString()))
{
int index = match.Index;
while (index < sb.Length && sb[index] != '\n' && sb[index] != '\r')
{
sb[index++] = ' ';
}
}
}
private static Match PragmaMatch(StringBuilder sb, string pragma)
{
return new Regex("#\\s*pragma\\s*" + pragma).Match(sb.ToString());
}
private static string[] CollectBadFiles()
{
List<string> list = new List<string>();
foreach (string current in PragmaFixing30.SearchRecursive(Path.Combine(Directory.GetCurrentDirectory(), "Assets"), "*.js"))
{
try
{
if (PragmaFixing30.FileNeedsPragmaFixing(current))
{
list.Add(current);
}
}
catch (Exception ex)
{
UnityEngine.Debug.LogError("Failed to fix pragmas in file '" + current + "'.\n" + ex.Message);
}
}
return list.ToArray();
}
}
}