forked from ppy/osu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_all_headers.py
More file actions
89 lines (73 loc) · 2.96 KB
/
Copy pathfix_all_headers.py
File metadata and controls
89 lines (73 loc) · 2.96 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
import os
files = [
'osu.Game/Rulesets/Objects/SliderPath.cs',
'osu.Game/Rulesets/Objects/Drawables/DrawableHitObject.cs',
'osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSlider.cs',
'osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSliderRepeat.cs',
'osu.Game.Rulesets.Osu/Objects/Drawables/DrawableOsuJudgement.cs',
'osu.Game.Rulesets.Osu/Objects/Drawables/Connections/FollowPointRenderer.cs',
'osu.Game.Rulesets.Osu/Skinning/SnakingSliderBody.cs'
]
HEADER_LINES = [
"// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.",
"// See the LICENCE file in the repository root for full licence text."
]
def fix_file(path):
with open(path, 'rb') as f:
content = f.read()
# Remove BOM if present
if content.startswith(b'\xef\xbb\xbf'):
content = content[3:]
text = content.decode('utf-8')
lines = text.splitlines()
# 1. Strip all copyright header and using lines, and nullable disable
clean_lines = []
usings = []
nullable_line = None
for line in lines:
stripped = line.strip()
if stripped in HEADER_LINES:
continue
if stripped.startswith('using '):
if stripped.endswith(';'):
usings.append(stripped)
continue
if stripped == '#nullable disable':
nullable_line = stripped
continue
clean_lines.append(line)
# 2. Strip leading/trailing empty lines from body
while clean_lines and not clean_lines[0].strip():
clean_lines.pop(0)
while clean_lines and not clean_lines[-1].strip():
clean_lines.pop()
# 3. Deduplicate and sort usings
# Group them: System first, then osu.Framework, then osu.Game, then others
usings = sorted(list(set(usings)))
system_usings = [u for u in usings if u.startswith('using System')]
framework_usings = [u for u in usings if u.startswith('using osu.Framework')]
game_usings = [u for u in usings if u.startswith('using osu.Game')]
other_usings = [u for u in usings if u not in system_usings and u not in framework_usings and u not in game_usings]
sorted_usings = []
if system_usings: sorted_usings.extend(system_usings + [""])
if framework_usings: sorted_usings.extend(framework_usings + [""])
if game_usings: sorted_usings.extend(game_usings + [""])
if other_usings: sorted_usings.extend(other_usings + [""])
# 4. Construct final content
final_lines = HEADER_LINES + [""]
if nullable_line:
final_lines.extend([nullable_line, ""])
final_lines.extend(sorted_usings)
final_lines.extend(clean_lines)
final_lines.append("") # End with newline
final_text = "\r\n".join(final_lines)
# 5. Write back with BOM
with open(path, 'wb') as f:
f.write(b'\xef\xbb\xbf')
f.write(final_text.encode('utf-8'))
for f in files:
if os.path.exists(f):
fix_file(f)
print(f"Fixed {f}")
else:
print(f"Skipped {f} (not found)")