-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta.py
143 lines (119 loc) · 6.19 KB
/
meta.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
import os, sys, json
from datetime import datetime
config = {'manual': False,'info_level': 0, 'restricted_extensions': [], 'ignore_files': ["meta.xml", "meta-generated.xml", "resource.json"], 'author': "Default", 'cache': False, 'override': True, 'force_override': False,'generate_exported': True}
type_cases = {'client':["client"], 'server':["server"]}
prefix_cases = {'client':["c_"], 'server':["s_"]}
def get_type(file_path, name):
if config['manual']:
input_type = input("USER-INPUT: Choose type for " + file_path + os.sep + name)
for type in type_cases:
if input_type.find(type) >= 0:
return type
else:
name = name.lower()
for type in type_cases:
for base_case in type_cases[type]: #check is file name contains cases listed in dictionary above
if name.find(base_case) >= 0:
return type
for base_case in prefix_cases[type]:
if name.startswith(base_case):
return type
folders = [folder.lower() for folder in file_path.split("\\")]
for folder in folders:
for type in type_cases:
if folder.find(type) >= 0: #same check, but for folders
return type
for base_case in prefix_cases[type]:
if name.startswith(base_case):
return type
if config['info_level'] < 3:
print("WARNING: Can't determine file type!")
return "shared"
exported_functions = list()
def obtain_exported_functions(file, type):
with open(file, encoding="utf8") as f:
lines = f.read().splitlines()
for line in lines:
if "function " in line and "--exported" in line:
function_name = line.replace("function ", "").split("(")[0]
exported_functions.append("\t<export function=\"" + function_name + "\" type=\"" + type + "\" />\n")
if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]):
working_folder = sys.argv[1]
if working_folder == ".":
working_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(working_folder)
with open("meta-generated.xml", "w+") as meta_file:
meta_file.truncate(0)
meta_file.write("<meta>" + "\n")
name = os.path.basename(working_folder)
description = "Generated by MTA-Resource-Tool"
author = config['author']
# Handle resource config
resource = dict()
if os.path.isfile("resource.json"):
with open('resource.json', 'r') as f:
resource = json.load(f)
if 'author' in resource:
author = resource['author']
if 'description' in resource:
description = resource['description']
meta_file.write("\t" + "<info name=\"" + name +"\" description=\"" + description + "\" author=\"" + author + "\"/>" + "\n")
if resource:
if 'oop' in resource and resource['oop']:
meta_file.write("\t" + "<oop>true</oop>" + "\n")
if 'rights' in resource and len(resource['rights']) > 0:
meta_file.write("\t" + "<aclrequest>" + "\n")
for right in resource['rights']:
meta_file.write("\t\t" + '<right name="' + right + '" access="' + str(resource['rights'][right]).lower() + '"></right>' + "\n")
meta_file.write("\t" + "</aclrequest>" + "\n")
for root, dirs, files in os.walk(working_folder):
files[:] = [f for f in files if (not f in config['ignore_files']) and not f.startswith(".")]
folders = root.split("\\")
if not folders[-1].startswith('.'): # Ignoring .git and etc. basically
meta_file.write("\n")
for file in files:
file_path = os.path.join(root, file)
file_path = file_path.replace(working_folder + os.sep, "")
name, ext = os.path.splitext(file)
if ext == ".lua" or ext == ".luac":
type = get_type(file_path, name)
if config['info_level'] == 0:
print("INFO:", file_path, "will be written as", type, "file")
if type == "client" and config['cache'] == False:
post_addition = 'cache = "false"'
else:
post_addition = ""
if config['generate_exported']:
obtain_exported_functions(file_path, type)
meta_row = "\t" + "<script src=\"" + file_path + "\" type=\"" + type + "\" " + post_addition +"/>" + "\n"
meta_file.write(meta_row)
elif not ext in config['restricted_extensions']:
if config['info_level'] == 0:
print("INFO:", file_path, "will be written")
meta_row = "\t" + "<file src=\"" + file_path + "\"/>" + "\n"
meta_file.write(meta_row)
else:
if config['info_level'] == 0:
print("INFO:", file_path, "has restricted extensions")
if config['generate_exported']:
for line in exported_functions:
meta_file.write(line)
meta_file.write("</meta>")
if os.path.isfile("meta.xml"):
if config['force_override']:
os.remove("meta.xml")
os.rename("meta-generated.xml", "meta.xml")
elif config['override']:
toOverwite = input("USER-INPUT: Should the meta.xml be overwritten by newly generated?")
if "y" in toOverwite:
if config['info_level'] < 3:
print("WARNING: meta.xml will be overwritten!")
os.rename("meta.xml", ".meta-old xml " + str(datetime.now().time()).replace(":", ".", -1))
os.rename("meta-generated.xml", "meta.xml")
else:
if config['info_level'] < 3:
print("WARNING: meta.xml won't be overwritten!")
else:
os.rename("meta-generated.xml", "meta.xml")
else:
sys.exit("ERROR: No file or directory specified!")