mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-05 12:17:53 -04:00
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:
parent
1559c9d9e8
commit
d9a4e7cdd9
@ -112,19 +112,7 @@ pub fn readAllZonFilesInAddons(
|
|||||||
!std.ascii.startsWithIgnoreCase(entry.path, "textures") and
|
!std.ascii.startsWithIgnoreCase(entry.path, "textures") and
|
||||||
!std.ascii.eqlIgnoreCase(entry.basename, "_migrations.zig.zon"))
|
!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 id = createAssetStringID(externalAllocator, addonName, entry.path);
|
||||||
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 zon = main.files.Dir.init(dir).readToZon(externalAllocator, entry.path) catch |err| {
|
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)});
|
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.
|
/// Reads obj files recursively from all subfolders.
|
||||||
pub fn readAllObjFilesInAddonsHashmap(
|
pub fn readAllObjFilesInAddonsHashmap(
|
||||||
externalAllocator: NeverFailingAllocator,
|
externalAllocator: NeverFailingAllocator,
|
||||||
@ -187,18 +198,7 @@ pub fn readAllObjFilesInAddonsHashmap(
|
|||||||
break :blk null;
|
break :blk null;
|
||||||
}) |entry| {
|
}) |entry| {
|
||||||
if(entry.kind == .file and std.ascii.endsWithIgnoreCase(entry.basename, ".obj")) {
|
if(entry.kind == .file and std.ascii.endsWithIgnoreCase(entry.basename, ".obj")) {
|
||||||
const folderName = addonName;
|
const id: []u8 = createAssetStringID(externalAllocator, addonName, entry.path);
|
||||||
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 string = dir.readFileAlloc(externalAllocator.allocator, entry.path, std.math.maxInt(usize)) catch |err| {
|
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)});
|
std.log.err("Could not open {s}/{s}: {s}", .{subPath, entry.path, @errorName(err)});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user