-
-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathupdate_tooltips.py
More file actions
104 lines (81 loc) · 3.36 KB
/
update_tooltips.py
File metadata and controls
104 lines (81 loc) · 3.36 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
import os
import sys
def get_classname(line):
if ' class ' not in line or ':' not in line:
return None, None
classParts = line.strip().split(' ')
delimInd = classParts.index(':')
className = classParts[delimInd - 1]
parentName = classParts[delimInd + 1]
return className, parentName
def find_eligible_classes(file_paths):
child_classes = {}
for file_path in file_paths:
with open(file_path, 'r') as file:
lines = file.readlines()
for line in lines:
className, parentName = get_classname(line)
if className is not None:
child_classes[parentName] = child_classes.get(parentName, []) + [className]
ret_classes = []
check_classes = ['MonoBehaviour']
while len(check_classes) > 0:
check_class = check_classes.pop()
if check_class in ret_classes:
continue
if check_class != 'MonoBehaviour':
ret_classes.append(check_class)
check_classes += child_classes.get(check_class, [])
return ret_classes
def add_tooltips_to_unity_file(file_path, allowed_classes):
# Read the content of the file
with open(file_path, 'r') as file:
lines = file.readlines()
# Initialize variables
updated_lines = []
in_summary = False
allowed_class = False
summary_text = ""
for line in lines:
stripped_line = line.strip()
className, __ = get_classname(line)
if className is not None:
allowed_class = className in allowed_classes
if allowed_class:
if '<summary>' in stripped_line:
in_summary = True
summary_text = ''
if in_summary:
if summary_text != "": summary_text += ' '
summary_text += stripped_line.replace("///", "").replace("<summary>", "").replace("</summary>", "").strip()
if '</summary>' in stripped_line:
in_summary = False
if 'Tooltip' in stripped_line:
if ('Tooltip: ignore' not in stripped_line):
continue
include_terms = ['public', 'private', 'protected']
exclude_terms = ['{', 'static', 'abstract']
if any([x in stripped_line for x in include_terms]) and ';' in stripped_line and not any([x in stripped_line for x in exclude_terms]):
if summary_text != '':
num_spaces = len(line) - len(line.lstrip())
tooltip = ''.join([' '] * num_spaces + [f'[Tooltip("{summary_text}")]', '\n'])
updated_lines.append(tooltip)
summary_text = ''
if not in_summary and ('{' in stripped_line or '}' in stripped_line):
summary_text = ''
# Add the current line to the updated lines
updated_lines.append(line)
# Write the updated content back to the file
with open(file_path, 'w') as file:
file.writelines(updated_lines)
if __name__ == '__main__':
# Find all .cs files
search_directory = 'Runtime'
cs_files = []
for root, _, files in os.walk(search_directory):
for file in files:
if file.endswith(".cs"):
cs_files.append(os.path.join(root, file))
classes = find_eligible_classes(cs_files)
for file in cs_files:
add_tooltips_to_unity_file(file, classes)