Extract createAssetStringID (#1229)

* Revert "Remove createAssetStringID"

This reverts commit 2c20a593e55bd84e202b9bfcbf13affb6f83fb96.

* Undo boy scout behavior

* Apply createAssetStringID to readAllObjFilesInAddonsHashmap

* Generialize ID generation

* Fix formatting issues

* Only remove the extension

* Apply review requests
This commit is contained in:
Krzysztof Wiśniewski 2025-03-24 19:06:08 +01:00 committed by GitHub
parent 1559c9d9e8
commit d9a4e7cdd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -112,19 +112,7 @@ pub fn readAllZonFilesInAddons(
!std.ascii.startsWithIgnoreCase(entry.path, "textures") and
!std.ascii.eqlIgnoreCase(entry.basename, "_migrations.zig.zon"))
{
const fileSuffixLen = if(std.ascii.endsWithIgnoreCase(entry.basename, ".zig.zon")) ".zig.zon".len else ".zon".len;
const folderName = addonName;
const id: []u8 = externalAllocator.alloc(u8, folderName.len + 1 + entry.path.len - fileSuffixLen);
errdefer externalAllocator.free(id);
@memcpy(id[0..folderName.len], folderName);
id[folderName.len] = ':';
for(0..entry.path.len - fileSuffixLen) |i| {
if(entry.path[i] == '\\') { // Convert windows path seperators
id[folderName.len + 1 + i] = '/';
} else {
id[folderName.len + 1 + i] = entry.path[i];
}
}
const id = createAssetStringID(externalAllocator, addonName, entry.path);
const zon = main.files.Dir.init(dir).readToZon(externalAllocator, entry.path) catch |err| {
std.log.err("Could not open {s}/{s}: {s}", .{subPath, entry.path, @errorName(err)});
@ -161,7 +149,30 @@ pub fn readAllZonFilesInAddons(
}
}
}
fn createAssetStringID(
externalAllocator: NeverFailingAllocator,
addonName: []const u8,
relativeFilePath: []const u8,
) []u8 {
const baseNameEndIndex = if(std.ascii.endsWithIgnoreCase(relativeFilePath, ".zig.zon")) relativeFilePath.len - ".zig.zon".len else std.mem.lastIndexOfScalar(u8, relativeFilePath, '.') orelse relativeFilePath.len;
const pathNoExtension: []const u8 = relativeFilePath[0..baseNameEndIndex];
const assetId: []u8 = externalAllocator.alloc(u8, addonName.len + 1 + pathNoExtension.len);
@memcpy(assetId[0..addonName.len], addonName);
assetId[addonName.len] = ':';
// Convert from windows to unix style separators.
for(0..pathNoExtension.len) |i| {
if(pathNoExtension[i] == '\\') {
assetId[addonName.len + 1 + i] = '/';
} else {
assetId[addonName.len + 1 + i] = pathNoExtension[i];
}
}
return assetId;
}
/// Reads obj files recursively from all subfolders.
pub fn readAllObjFilesInAddonsHashmap(
externalAllocator: NeverFailingAllocator,
@ -187,18 +198,7 @@ pub fn readAllObjFilesInAddonsHashmap(
break :blk null;
}) |entry| {
if(entry.kind == .file and std.ascii.endsWithIgnoreCase(entry.basename, ".obj")) {
const folderName = addonName;
const id: []u8 = externalAllocator.alloc(u8, folderName.len + 1 + entry.path.len - 4);
errdefer externalAllocator.free(id);
@memcpy(id[0..folderName.len], folderName);
id[folderName.len] = ':';
for(0..entry.path.len - 4) |i| {
if(entry.path[i] == '\\') { // Convert windows path seperators
id[folderName.len + 1 + i] = '/';
} else {
id[folderName.len + 1 + i] = entry.path[i];
}
}
const id: []u8 = createAssetStringID(externalAllocator, addonName, entry.path);
const string = dir.readFileAlloc(externalAllocator.allocator, entry.path, std.math.maxInt(usize)) catch |err| {
std.log.err("Could not open {s}/{s}: {s}", .{subPath, entry.path, @errorName(err)});