mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 03:06:55 -04:00
parent
eaaf524f99
commit
df99bfc804
@ -27,6 +27,7 @@ pub const Assets = struct {
|
||||
blocks: ZonHashMap,
|
||||
blockMigrations: AddonNameToZonMap,
|
||||
items: ZonHashMap,
|
||||
itemMigrations: ZonHashMap,
|
||||
tools: ZonHashMap,
|
||||
biomes: ZonHashMap,
|
||||
biomeMigrations: AddonNameToZonMap,
|
||||
@ -41,6 +42,7 @@ pub const Assets = struct {
|
||||
.blocks = .{},
|
||||
.blockMigrations = .{},
|
||||
.items = .{},
|
||||
.itemMigrations = .{},
|
||||
.tools = .{},
|
||||
.biomes = .{},
|
||||
.biomeMigrations = .{},
|
||||
@ -55,6 +57,7 @@ pub const Assets = struct {
|
||||
self.blocks.deinit(allocator.allocator);
|
||||
self.blockMigrations.deinit(allocator.allocator);
|
||||
self.items.deinit(allocator.allocator);
|
||||
self.itemMigrations.deinit(allocator.allocator);
|
||||
self.tools.deinit(allocator.allocator);
|
||||
self.biomes.deinit(allocator.allocator);
|
||||
self.biomeMigrations.deinit(allocator.allocator);
|
||||
@ -69,6 +72,7 @@ pub const Assets = struct {
|
||||
.blocks = self.blocks.clone(allocator.allocator) catch unreachable,
|
||||
.blockMigrations = self.blockMigrations.clone(allocator.allocator) catch unreachable,
|
||||
.items = self.items.clone(allocator.allocator) catch unreachable,
|
||||
.itemMigrations = self.itemMigrations.clone(allocator.allocator) catch unreachable,
|
||||
.tools = self.tools.clone(allocator.allocator) catch unreachable,
|
||||
.biomes = self.biomes.clone(allocator.allocator) catch unreachable,
|
||||
.biomeMigrations = self.biomeMigrations.clone(allocator.allocator) catch unreachable,
|
||||
@ -86,7 +90,7 @@ pub const Assets = struct {
|
||||
|
||||
for(addons.items) |addon| {
|
||||
addon.readAllZon(allocator, "blocks", true, &self.blocks, &self.blockMigrations);
|
||||
addon.readAllZon(allocator, "items", true, &self.items, null);
|
||||
addon.readAllZon(allocator, "items", true, &self.items, &self.itemMigrations);
|
||||
addon.readAllZon(allocator, "tools", true, &self.tools, null);
|
||||
addon.readAllZon(allocator, "biomes", true, &self.biomes, &self.biomeMigrations);
|
||||
addon.readAllZon(allocator, "recipes", false, &self.recipes, null);
|
||||
@ -98,8 +102,8 @@ pub const Assets = struct {
|
||||
}
|
||||
fn log(self: *Assets, typ: enum {common, world}) void {
|
||||
std.log.info(
|
||||
"Finished {s} assets reading with {} blocks ({} migrations), {} items, {} tools, {} biomes ({} migrations), {} recipes, {} structure building blocks, {} blueprints and {} particles",
|
||||
.{@tagName(typ), self.blocks.count(), self.blockMigrations.count(), self.items.count(), self.tools.count(), self.biomes.count(), self.biomeMigrations.count(), self.recipes.count(), self.structureBuildingBlocks.count(), self.blueprints.count(), self.particles.count()},
|
||||
"Finished {s} assets reading with {} blocks ({} migrations), {} items ({} migrations), {} tools, {} biomes ({} migrations), {} recipes, {} structure building blocks, {} blueprints and {} particles",
|
||||
.{@tagName(typ), self.blocks.count(), self.blockMigrations.count(), self.items.count(), self.itemMigrations.count(), self.tools.count(), self.biomes.count(), self.biomeMigrations.count(), self.recipes.count(), self.structureBuildingBlocks.count(), self.blueprints.count(), self.particles.count()},
|
||||
);
|
||||
}
|
||||
|
||||
@ -323,6 +327,7 @@ fn createAssetStringID(
|
||||
pub fn init() void {
|
||||
biomes_zig.init();
|
||||
blocks_zig.init();
|
||||
migrations_zig.init();
|
||||
|
||||
commonAssetArena = .init(main.globalAllocator);
|
||||
commonAssetAllocator = commonAssetArena.allocator();
|
||||
@ -494,6 +499,9 @@ pub fn loadWorldAssets(assetFolder: []const u8, blockPalette: *Palette, itemPale
|
||||
migrations_zig.registerAll(.block, &worldAssets.blockMigrations);
|
||||
migrations_zig.apply(.block, blockPalette);
|
||||
|
||||
migrations_zig.registerAll(.item, &worldAssets.itemMigrations);
|
||||
migrations_zig.apply(.item, itemPalette);
|
||||
|
||||
migrations_zig.registerAll(.biome, &worldAssets.biomeMigrations);
|
||||
migrations_zig.apply(.biome, biomePalette);
|
||||
|
||||
@ -653,6 +661,7 @@ pub fn unloadAssets() void { // MARK: unloadAssets()
|
||||
sbb.reset();
|
||||
blocks_zig.reset();
|
||||
items_zig.reset();
|
||||
migrations_zig.reset();
|
||||
biomes_zig.reset();
|
||||
migrations_zig.reset();
|
||||
main.models.reset();
|
||||
|
@ -5,14 +5,16 @@ const ZonElement = @import("zon.zig").ZonElement;
|
||||
const Palette = @import("assets.zig").Palette;
|
||||
const Assets = main.assets.Assets;
|
||||
|
||||
var arenaAllocator = main.heap.NeverFailingArenaAllocator.init(main.globalAllocator);
|
||||
const migrationAllocator = arenaAllocator.allocator();
|
||||
var arenaAllocator: main.heap.NeverFailingArenaAllocator = undefined;
|
||||
const migrationAllocator: main.heap.NeverFailingAllocator = arenaAllocator.allocator();
|
||||
|
||||
var blockMigrations: std.StringHashMap([]const u8) = .init(migrationAllocator.allocator);
|
||||
var biomeMigrations: std.StringHashMap([]const u8) = .init(migrationAllocator.allocator);
|
||||
var blockMigrations: std.StringHashMapUnmanaged([]const u8) = undefined;
|
||||
var itemMigrations: std.StringHashMapUnmanaged([]const u8) = undefined;
|
||||
var biomeMigrations: std.StringHashMapUnmanaged([]const u8) = undefined;
|
||||
|
||||
const MigrationType = enum {
|
||||
block,
|
||||
item,
|
||||
biome,
|
||||
};
|
||||
|
||||
@ -20,6 +22,7 @@ pub fn registerAll(comptime typ: MigrationType, migrations: *Assets.AddonNameToZ
|
||||
std.log.info("Registering {} {s} migrations", .{migrations.count(), @tagName(typ)});
|
||||
const collection = switch(typ) {
|
||||
.block => &blockMigrations,
|
||||
.item => &itemMigrations,
|
||||
.biome => &biomeMigrations,
|
||||
};
|
||||
var migrationIterator = migrations.iterator();
|
||||
@ -30,7 +33,7 @@ pub fn registerAll(comptime typ: MigrationType, migrations: *Assets.AddonNameToZ
|
||||
|
||||
fn register(
|
||||
comptime typ: MigrationType,
|
||||
collection: *std.StringHashMap([]const u8),
|
||||
collection: *std.StringHashMapUnmanaged([]const u8),
|
||||
addonName: []const u8,
|
||||
migrationZon: ZonElement,
|
||||
) void {
|
||||
@ -65,7 +68,7 @@ fn register(
|
||||
}
|
||||
|
||||
const oldAssetId = std.fmt.allocPrint(migrationAllocator.allocator, "{s}:{s}", .{addonName, oldZon}) catch unreachable;
|
||||
const result = collection.getOrPut(oldAssetId) catch unreachable;
|
||||
const result = collection.getOrPut(migrationAllocator.allocator, oldAssetId) catch unreachable;
|
||||
|
||||
if(result.found_existing) {
|
||||
std.log.err("Skipping name collision in {s} migration: '{s}' -> '{s}:{s}'", .{@tagName(typ), oldAssetId, addonName, newZon});
|
||||
@ -86,6 +89,7 @@ fn register(
|
||||
pub fn apply(comptime typ: MigrationType, palette: *Palette) void {
|
||||
const migrations = switch(typ) {
|
||||
.block => blockMigrations,
|
||||
.item => itemMigrations,
|
||||
.biome => biomeMigrations,
|
||||
};
|
||||
std.log.info("Applying {} migrations to {s} palette", .{migrations.count(), @tagName(typ)});
|
||||
@ -97,12 +101,23 @@ pub fn apply(comptime typ: MigrationType, palette: *Palette) void {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init() void {
|
||||
biomeMigrations = .{};
|
||||
blockMigrations = .{};
|
||||
itemMigrations = .{};
|
||||
arenaAllocator = .init(main.globalAllocator);
|
||||
}
|
||||
|
||||
pub fn reset() void {
|
||||
blockMigrations.clearAndFree();
|
||||
biomeMigrations.clearAndFree();
|
||||
biomeMigrations = .{};
|
||||
blockMigrations = .{};
|
||||
itemMigrations = .{};
|
||||
_ = arenaAllocator.reset(.free_all);
|
||||
}
|
||||
|
||||
pub fn deinit() void {
|
||||
biomeMigrations = undefined;
|
||||
blockMigrations = undefined;
|
||||
itemMigrations = undefined;
|
||||
arenaAllocator.deinit();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user