-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpatch_f4se.py
75 lines (64 loc) · 2.31 KB
/
patch_f4se.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
import os, sys, io, re, codecs
#================================================================
# # About
# This script makes changes to F4SE for plugin development under VS2015.
#
# # Arguments
# [1] F4SE_PATH - Path to src/f4se.
#
# # Changes
# ## src/f4se/f4se/f4se.vcxproj
# - Output a static library instead of a dynamic library for linking with plugins.
#
# ## src/f4se/f4se/BSSkin.h
# - Add missing header #include <xmmintrin.h> for use of __m128 type.
#
# ## src/f4se/f4se/PapyrusObjects.h
# - Add missing header #include <algorithm> for use of std::min.
#================================================================
#===================
# Configuration
#===================
# Get F4SE path (src/f4se)
if len(sys.argv) > 1:
F4SE_PATH = sys.argv[1]
else:
print('FATAL: No F4SE directory provided.')
sys.exit(1)
print('F4SE Path: {}'.format(F4SE_PATH))
#===========================
# Update F4SE project file
#===========================
# # Changes:
# ## f4se.vcxproj
# PropertyGroup[@Label="Configuration"].ConfigurationType = StaticLibrary
# Regex Patterns
re_configType = re.compile('(<PropertyGroup.*Label="Configuration">[\S\s]*?<ConfigurationType>)(.*)(<\/ConfigurationType>[\S\s]*?<\/PropertyGroup>)', re.MULTILINE)
# Read vcxproj
with open(os.path.join(F4SE_PATH, 'f4se/f4se.vcxproj'), encoding='utf-8') as f:
fileStr = f.read()
# Set Configuration Type to Static Library (original: Dynamic Library)
fileStr = re_configType.sub(r'\1StaticLibrary\3', fileStr)
# Write new vcxproj
with codecs.open(os.path.join(F4SE_PATH, 'f4se/f4se.vcxproj'), 'w', 'utf-8') as f:
f.write(fileStr)
#===========================
# Update F4SE source code
#===========================
# Adds a line to the specified file
def add_line(filepath, lineToAdd):
with open(os.path.join(F4SE_PATH, filepath), 'r+') as f:
fileStr = f.readlines()
if not (lineToAdd + '\n') in fileStr:
f.seek(0)
for i, line in enumerate(fileStr):
if i == 1:
f.write(lineToAdd + '\n')
f.write(line)
f.truncate()
return True
return False
num_patched = 0
num_patched += add_line('f4se/BSSkin.h', '#include <xmmintrin.h>')
num_patched += add_line('f4se/PapyrusObjects.h', '#include <algorithm>')
print("Patched: {}/2".format(num_patched))