Skip to content

Commit a23a811

Browse files
committed
Unhardcode the rules folder path
1 parent 656cb46 commit a23a811

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"program": "${workspaceFolder}/Cortex-Command-Mod-Converter-Engine",
1212
"args": [
1313
"./input",
14-
"./output"
14+
"./output",
15+
"./rules"
1516
],
1617
"cwd": "${workspaceFolder}",
1718
"console": "integratedTerminal",

src/main.zig

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ pub fn main() !void {
154154
_ = program_name;
155155
const input_folder_path = args.next() orelse return error.ExpectedInputFolderPath;
156156
const output_folder_path = args.next() orelse return error.ExpectedOutputFolderPath;
157+
const rules_folder_path = args.next() orelse return error.ExpectedRulesFolderPath;
157158

158159
var diagnostics: Diagnostics = .{};
159160
convert(
160161
input_folder_path,
161162
output_folder_path,
163+
rules_folder_path,
162164
allocator,
163165
&diagnostics,
164166
) catch |err| switch (err) {
@@ -209,22 +211,24 @@ pub fn main() !void {
209211

210212
/// For every mod directory in `input_folder_path`, it creates a copy of the mod directory in `output_folder_path` with the required changes to make it compatible with the latest version of the game.
211213
/// If `convert()` crashed, the `diagnostics` argument allows you to know why and where it did.
212-
pub fn convert(input_folder_path_: []const u8, output_folder_path_: []const u8, allocator: Allocator, diagnostics: *Diagnostics) !void {
214+
pub fn convert(input_folder_path_: []const u8, output_folder_path_: []const u8, rules_folder_path_: []const u8, allocator: Allocator, diagnostics: *Diagnostics) !void {
213215
// Necessary solely because of .OBJECT_NAME_INVALID => unreachable in the std lib:
214216
// https://github.com/ziglang/zig/issues/15607#issue-1698930560
215217
if (!try isValidDirPath(input_folder_path_)) return error.InvalidInputPath;
216218
if (!try isValidDirPath(output_folder_path_)) return error.InvalidOutputPath;
219+
if (!try isValidDirPath(rules_folder_path_)) return error.InvalidOutputPath;
217220

218221
const input_folder_path = try std.fs.realpathAlloc(allocator, input_folder_path_);
219222
const output_folder_path = try std.fs.realpathAlloc(allocator, output_folder_path_);
223+
const rules_folder_path = try std.fs.realpathAlloc(allocator, rules_folder_path_);
220224

221225
std.log.info("Making all output dirs...\n", .{});
222226
try makeOutputDirs(input_folder_path, output_folder_path, allocator);
223227

224228
std.log.info("Copying files...\n", .{});
225229
try copyFiles(input_folder_path, output_folder_path, allocator);
226230

227-
const lua_rules = try parseLuaRules(allocator);
231+
const lua_rules = try parseLuaRules(rules_folder_path, allocator);
228232
std.log.info("Applying Lua rules...\n", .{});
229233
try applyLuaRules(lua_rules, output_folder_path, allocator);
230234

@@ -257,27 +261,27 @@ pub fn convert(input_folder_path_: []const u8, output_folder_path_: []const u8,
257261
std.log.info("Wav extension to flac...\n", .{});
258262
try applyOnNodesAlloc(wavExtensionToFlac, &file_tree, allocator);
259263

260-
const ini_copy_of_rules = try parseIniCopyOfRules(allocator);
264+
const ini_copy_of_rules = try parseIniCopyOfRules(rules_folder_path, allocator);
261265
std.log.info("Applying INI CopyOf rules...\n", .{});
262266
applyIniCopyOfRules(ini_copy_of_rules, &file_tree);
263267

264-
const ini_file_path_rules = try parseIniFilePathRules(allocator);
268+
const ini_file_path_rules = try parseIniFilePathRules(rules_folder_path, allocator);
265269
std.log.info("Applying INI FilePath rules...\n", .{});
266270
applyIniFilePathRules(ini_file_path_rules, &file_tree);
267271

268-
const ini_script_path_rules = try parseIniScriptPathRules(allocator);
272+
const ini_script_path_rules = try parseIniScriptPathRules(rules_folder_path, allocator);
269273
std.log.info("Applying INI ScriptPath rules...\n", .{});
270274
applyIniScriptPathRules(ini_script_path_rules, &file_tree);
271275

272-
const ini_property_rules = try parseIniPropertyRules(allocator);
276+
const ini_property_rules = try parseIniPropertyRules(rules_folder_path, allocator);
273277
std.log.info("Applying INI property rules...\n", .{});
274278
applyIniPropertyRules(ini_property_rules, &file_tree);
275279

276-
const ini_rules = try parseIniRules(allocator);
280+
const ini_rules = try parseIniRules(rules_folder_path, allocator);
277281
std.log.info("Applying INI rules...\n", .{});
278282
applyIniRules(ini_rules, &file_tree);
279283

280-
const ini_sound_container_rules = try parseIniSoundContainerRules(allocator);
284+
const ini_sound_container_rules = try parseIniSoundContainerRules(rules_folder_path, allocator);
281285
std.log.info("Applying INI SoundContainer rules...\n", .{});
282286
applyIniSoundContainerRules(ini_sound_container_rules, &file_tree);
283287

@@ -481,8 +485,9 @@ fn convertWavToFlac(input_file_path: []const u8, output_file_path: []const u8, a
481485
// }
482486
}
483487

484-
fn parseLuaRules(allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
485-
const text = try readFile("rules/lua_rules.json", allocator);
488+
fn parseLuaRules(rules_folder_path: []const u8, allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
489+
const lua_rules_path = try join(allocator, &.{ rules_folder_path, "lua_rules.json" });
490+
const text = try readFile(lua_rules_path, allocator);
486491

487492
var scanner = Scanner.initCompleteInput(allocator, text);
488493

@@ -1166,8 +1171,9 @@ fn wavExtensionToFlac(node: *Node, allocator: Allocator) !void {
11661171
}
11671172
}
11681173

1169-
fn parseIniCopyOfRules(allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
1170-
const text = try readFile("rules/ini_copy_of_rules.json", allocator);
1174+
fn parseIniCopyOfRules(rules_folder_path: []const u8, allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
1175+
const ini_copy_of_rules_path = try join(allocator, &.{ rules_folder_path, "ini_copy_of_rules.json" });
1176+
const text = try readFile(ini_copy_of_rules_path, allocator);
11711177

11721178
var scanner = Scanner.initCompleteInput(allocator, text);
11731179

@@ -1213,8 +1219,9 @@ fn applyIniValueReplacementRulesRecursivelyNode(node: *Node, comptime property:
12131219
}
12141220
}
12151221

1216-
fn parseIniFilePathRules(allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
1217-
const text = try readFile("rules/ini_file_path_rules.json", allocator);
1222+
fn parseIniFilePathRules(rules_folder_path: []const u8, allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
1223+
const ini_file_path_rules_path = try join(allocator, &.{ rules_folder_path, "ini_file_path_rules.json" });
1224+
const text = try readFile(ini_file_path_rules_path, allocator);
12181225

12191226
var scanner = Scanner.initCompleteInput(allocator, text);
12201227

@@ -1232,8 +1239,9 @@ fn applyIniFilePathRules(rules: std.json.ArrayHashMap([]const u8), file_tree: *I
12321239
}
12331240
}
12341241

1235-
fn parseIniScriptPathRules(allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
1236-
const text = try readFile("rules/ini_script_path_rules.json", allocator);
1242+
fn parseIniScriptPathRules(rules_folder_path: []const u8, allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
1243+
const ini_script_path_rules_path = try join(allocator, &.{ rules_folder_path, "ini_script_path_rules.json" });
1244+
const text = try readFile(ini_script_path_rules_path, allocator);
12371245

12381246
var scanner = Scanner.initCompleteInput(allocator, text);
12391247

@@ -1251,8 +1259,9 @@ fn applyIniScriptPathRules(rules: std.json.ArrayHashMap([]const u8), file_tree:
12511259
}
12521260
}
12531261

1254-
fn parseIniPropertyRules(allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
1255-
const text = try readFile("rules/ini_property_rules.json", allocator);
1262+
fn parseIniPropertyRules(rules_folder_path: []const u8, allocator: Allocator) !std.json.ArrayHashMap([]const u8) {
1263+
const ini_property_rules_path = try join(allocator, &.{ rules_folder_path, "ini_property_rules.json" });
1264+
const text = try readFile(ini_property_rules_path, allocator);
12561265

12571266
var scanner = Scanner.initCompleteInput(allocator, text);
12581267

@@ -1294,8 +1303,9 @@ fn applyIniPropertyRulesRecursivelyNode(node: *Node, old_property: []const u8, n
12941303
}
12951304
}
12961305

1297-
fn parseIniRules(allocator: Allocator) ![]Rule {
1298-
const text = try readFile("rules/ini_rules.json", allocator);
1306+
fn parseIniRules(rules_folder_path: []const u8, allocator: Allocator) ![]Rule {
1307+
const ini_rules_path = try join(allocator, &.{ rules_folder_path, "ini_rules.json" });
1308+
const text = try readFile(ini_rules_path, allocator);
12991309
return try parseFromSliceLeaky([]Rule, allocator, text, .{});
13001310
}
13011311

@@ -1334,8 +1344,9 @@ fn applyIniRulesRecursivelyNode(node: *Node, rule: *Rule) void {
13341344
}
13351345
}
13361346

1337-
fn parseIniSoundContainerRules(allocator: Allocator) ![][]const u8 {
1338-
const text = try readFile("rules/ini_sound_container_rules.json", allocator);
1347+
fn parseIniSoundContainerRules(rules_folder_path: []const u8, allocator: Allocator) ![][]const u8 {
1348+
const ini_sound_container_rules_path = try join(allocator, &.{ rules_folder_path, "ini_sound_container_rules.json" });
1349+
const text = try readFile(ini_sound_container_rules_path, allocator);
13391350
return try parseFromSliceLeaky([][]const u8, allocator, text, .{});
13401351
}
13411352

@@ -2396,8 +2407,10 @@ fn testDirectory(comptime directory_name: []const u8, is_invalid_test: bool) !vo
23962407
var tmpdir_output_folder_path_buffer: [MAX_PATH_BYTES]u8 = undefined;
23972408
const tmpdir_output_folder_path = try tmpdir_output_folder.dir.realpath(".", &tmpdir_output_folder_path_buffer);
23982409

2410+
const rules_folder_path = "./rules";
2411+
23992412
var diagnostics: Diagnostics = .{};
2400-
convert(input_folder_path, tmpdir_output_folder_path, allocator, &diagnostics) catch |err| {
2413+
convert(input_folder_path, tmpdir_output_folder_path, rules_folder_path, allocator, &diagnostics) catch |err| {
24012414
if (is_invalid_test) {
24022415
const error_path = try join(allocator, &.{ test_folder_path, "expected_error.txt" });
24032416

0 commit comments

Comments
 (0)