forked from CombatExtended-Continued/CombatExtended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reference.py
179 lines (149 loc) · 4.76 KB
/
Reference.py
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
#!/usr/bin/python3
from twisted.python.filepath import FilePath
import sys
import re
class Counter(object):
val = 0
def incr(self):
self.val += 1
return self.val
def decr(self):
self.val -= 1
return self.val
def __int__(self):
return self.val
indent = Counter()
TAB=8
METHOD = re.compile(r'(private )?(public )?(static )?(override )? ?([a-zA-Z0-9_<>]+) ([a-zA-Z0-9_]+)(\(.*?\))$')
def findClose(source, out=None):
count = 0
dquote = False
squote = False
if out is None:
out = []
for idx, c in enumerate(source):
out.append(c)
if c == '"':
if squote:
continue
dquote = not dquote
continue
if c == "'":
if dquote:
continue
squote = not squote
continue
if dquote or squote:
continue
if c == '{':
count += 1
continue
if c == '}':
count -= 1
if count == 0:
return idx
def findClasses(source, out):
lines = iter(source.split('\n'))
for line in lines:
if ' class ' in line:
c = Class(line, lines)
out.append(c)
yield c
else:
out.append(line)
def wrappedIter(lines):
for line in lines:
yield from line
yield '\n'
def findMethods(source, out):
lines = iter(source)
for line in lines:
if isinstance(line, Class):
out.append(line)
continue
if match := METHOD.match(line.strip()):
m = Method(match, line, lines)
out.append(m)
yield m
else:
out.append(line)
class Method(object):
def __init__(self, match, first, lines):
self.level = indent.incr()
self.name = match.group(6)
self.header = first
o = []
findClose(wrappedIter(lines), o)
self.body = str.join('', o).strip()[1:-1]
indent.decr()
def __repr__(self):
return f"Method(name={self.name})"
def __str__(self):
body = self.body
return f"{self.header}\n{' '*(TAB*(self.level-1))}{{\n{body}}}"
def makeReference(self):
self.body = f"{' '*TAB*(self.level)}throw null;\n{' '*TAB*(self.level-1)}"
class Class(object):
def __init__(self, first, lines):
self.level = indent.incr()
self.name = first.split('class ', 1)[1].split(' ', 1)[0].split('\n', 1)[0]
self.header = first
o = []
findClose(wrappedIter(lines), o)
body = str.join('', o).strip()[1:-1]
self.body = []
self.inner = list(findClasses(body, self.body))
body = self.body
self.body = []
self.methods = list(findMethods(body, self.body))
indent.decr()
def __repr__(self):
return f"Class(name={self.name}, methods={self.methods}, internal={self.inner})"
def __str__(self):
body = str.join('\n', (str(i) for i in self.body))
return f"{self.header}\n{' '*(TAB*(self.level-1))}{{{body}}}"
def makeReference(self):
for c in self.inner:
c.makeReference()
for m in self.methods:
m.makeReference()
b = iter(self.body)
self.body = []
for line in b:
if not isinstance(line, str):
self.body.append(line)
continue
if line.strip().startswith("private"):
if not line.strip().endswith(';'):
c = []
findClose(wrappedIter(b), c)
continue
self.body.append(line)
class Namespace(object):
def __init__(self, source):
self.level = indent.incr()
self.header, source = source.split('namespace', 1)
self.namespace, source = source.strip().split('\n', 1)
idx = findClose(source)
body = source[1:idx]
self.body = []
self.classes = list(findClasses(body, self.body))
indent.decr()
def __repr__(self):
return f"Namespace(name={self.namespace}, classes={self.classes})"
def __str__(self):
body = str.join("\n", (str(i) for i in self.body))
return f"{self.header}\nnamespace {self.namespace}\n{{{body}\n}}"
for f in FilePath(sys.argv[1]).walk():
if f.isdir():
continue
if not f.path.endswith('.cs'):
continue
source = f.getContent().decode('utf-8')
if 'namespace' in source:
namespace = Namespace(source)
print(namespace)
for c in namespace.classes:
c.makeReference()
print(str(namespace))
f.setContent(str(namespace).encode('utf-8'))