-
Notifications
You must be signed in to change notification settings - Fork 2
/
praCompiler.pas
224 lines (185 loc) · 6.88 KB
/
praCompiler.pas
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
Helper functions to compile scripts
}
unit praCompiler;
var
PapyrusCompiler: string;
DefaultFlagsFile: string;
ScriptIncludes: TStringList;
procedure initCompiler();
begin
// this is a config of sorts.
ScriptIncludes := TStringList.create;
// Put in here the include directories for scripts.
// They will be passed to the compiler with the -i flag in the order specified here.
// This configuration works for me, but it might not work for you.
ScriptIncludes.add('..\Script-Includes\SCRIPTS\Source\user');//I:\Steam\steamapps\common\Fallout 4\Script-Includes\SCRIPTS\Source\user
ScriptIncludes.add('..\Script-Includes\SCRIPTS');
ScriptIncludes.add('SCRIPTS\Source\Base');
ScriptIncludes.add('SCRIPTS\Source\user');
ScriptIncludes.add('SCRIPTS');
// these are default values. Probably don't change them
PapyrusCompiler := DataPath + '..\Papyrus Compiler\PapyrusCompiler.exe';
DefaultFlagsFile := DataPath + 'SCRIPTS\source\Base\Institute_Papyrus_Flags.flg';
end;
function assembleCompilerParamsForVanilla(inputFile: string; outputDir: string): string;
var
i: integer;
includes: String;
begin
Result := '"'+inputFile + '" ';
//
includes := '';
for i:=0 to ScriptIncludes.count-1 do begin
if(i>0) then begin
includes := includes + ';';
end;
includes := includes + DataPath+ScriptIncludes[i];
end;
Result := Result + ' -i="'+includes+'"';
Result := Result + ' -f="'+DefaultFlagsFile+'" -o="'+outputDir+'"';
Result := Result + ' -op -r';
end;
{
Attempts to compile the given inputFile.
outputDir is the directory where the resulting pex should end up.
}
function doCompile(inputFile: string; outputDir: string): boolean;
var
exePath, paramStr: string;
resultCode: LongWord;
begin
Result := true;
exePath := PapyrusCompiler;
paramStr := assembleCompilerParamsForVanilla(inputFile, DataPath+'SCRIPTS');
// try to mkdir
if(not DirectoryExists(outputDir)) then begin
if(not ForceDirectories(outputDir)) then begin
AddMessage('Could not create '+outputDir);
Result := false;
exit;
end;
end;
AddMessage('Command: '+exePath+' '+paramStr);
resultCode := ShellExecuteWait(
TForm(frmMain).Handle, // parent window handle, use 0 for none
'open', // verb
exePath, // application
paramStr, // parameters
'', // working directory
0 // SW_SHOWNORMAL = window mode
);
//AddMessage('code '+inttostr(resultCode));
if(resultCode <> 0) then begin
AddMessage('Error compiling '+inputFile);
Result := false;
end else begin
AddMessage('Compiling successful!');
end;
end;
function compileScriptByName(name: string): boolean;
var
sourcePath, outputDir, resultPex: string;
begin
sourcePath := scriptNameToSourcePath(name);
resultPex := scriptNameToPexPath(name);
outputDir := scriptNameToOutputDir(name);
if(not fileExists(sourcePath)) then begin
AddMessage('Source '+sourcePath+' not found!');
AddMessage('Nothing to compile!');
result := false;
exit;
end;
Result := doCompile(sourcePath, outputDir);
if(not FileExists(resultPex)) then begin
AddMessage('Pex file not generated! '+resultPex);
Result := false;
end;
end;
{
returns the substring of str up to the first occurence of separator
}
function getStringUpTo(str: string; separator: string): string;
var
i: integer;
char: string;
begin
Result := str;
for i := 1 to length(str) do begin
char := copy(str, i, 1);
if(char = separator) then begin
Result := copy(str, 1, i-1);
exit;
end;
end;
end;
{
returns the substring of str up to the last occurence of separator
}
function getStringUpToLast(str: string; separator: string): string;
var
i: integer;
char: string;
begin
Result := str;
for i := length(str) downto 1 do begin
char := copy(str, i, 1);
if(char = separator) then begin
Result := copy(str, 1, i-1);
exit;
end;
end;
end;
{
etracts the directory in which the given file is
}
function getFileDir(curPath: string): string;
begin
Result := getStringUpToLast(curPath, '\');
end;
{
extracts the first namespace of a script name
}
function getScriptNamespace(scriptName: string): string;
begin
Result := getStringUpTo(scriptName, ':');
end;
{
converts a script name to the path of the corresponding PSC file
}
function scriptNameToSourcePath(name: string): string;
begin
Result := StringReplace(name, ':', '\', [rfReplaceAll]);
Result := DataPath + 'SCRIPTS\Source\user\' + Result + '.psc';
end;
{
converts a script name to the path of the corresponding PEX file
}
function scriptNameToPexPath(name: string): string;
begin
Result := StringReplace(name, ':', '\', [rfReplaceAll]);
Result := DataPath + 'SCRIPTS\' + Result + '.pex';
end;
{
for a script name, gets the directory where the corresponding PEX file should be put
}
function scriptNameToOutputDir(name: string): string;
begin
Result := StringReplace(name, ':', '\', [rfReplaceAll]);
// strip off the last part
Result := getFileDir(DataPath + 'SCRIPTS\' + Result);
end;
{
Makes sure the directory where this file exists.
}
function ensureDirectory(filename: string): boolean;
var
dirPath: string;
begin
Result := true;
dirPath := getFileDir(filename);
if(not DirectoryExists(dirPath)) then begin
Result := ForceDirectories(dirPath);
end;
end;
end.