Skip to content

Commit

Permalink
refactor(tesseratos): handleImport
Browse files Browse the repository at this point in the history
  • Loading branch information
Scarface1809 committed Oct 4, 2024
1 parent 2c3a2b3 commit 0954d7a
Showing 1 changed file with 64 additions and 63 deletions.
127 changes: 64 additions & 63 deletions tools/tesseratos/src/tesseratos/importer/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,98 +55,99 @@ namespace

static bool handleImport(Assets& assets, ImportState& state)
{
// Find the palette asset using the provided file path.
auto paletteHandle = assets.find(state.paletteFilePath);
if (paletteHandle.isNull())
{
auto model = assets.read<VoxelModel>(state.currentAsset);
if (paletteHandle.isNull())
CUBOS_INFO("Failed to get asset handle for paletteFilePath: {}", state.paletteFilePath);
CUBOS_INFO("Creating new palette asset.");

// Create a new empty VoxelPalette and assign a handle to it.
paletteHandle = assets.create(VoxelPalette{});
assets.writeMeta(paletteHandle)->set("path", state.paletteFilePath);
assets.save(paletteHandle);
}

std::vector<AnyAsset> gridHandles;
std::vector<std::string> gridPaths;
gridHandles.resize(state.gridCount);
gridPaths.resize(state.gridCount);
for (std::size_t i = 0; i < state.gridCount; ++i)
{
if (state.gridFilePaths.contains(i))
{
CUBOS_INFO("Failed to get asset handle for paletteFilePath: {}", state.paletteFilePath);
CUBOS_INFO("Creating new palette asset.");
// Should create new palette asset.
paletteHandle = assets.create(VoxelPalette{});
assets.writeMeta(paletteHandle)->set("path", state.paletteFilePath);
assets.save(paletteHandle);
if (paletteHandle.isNull())
{
// Should not happen.
CUBOS_ERROR("Failed to create palette asset.");
return false;
}
gridPaths[i] = state.gridFilePaths.at(i);
gridHandles[i] = assets.find(gridPaths[i]);
}
}

{
// Read the current VoxelModel asset.
auto model = assets.read<VoxelModel>(state.currentAsset);

// Write to the palette asset.
auto palette = assets.write<VoxelPalette>(paletteHandle);

// Iterate over the grids which will be exported.
for (std::size_t i = 0; i < state.gridCount; ++i)
{
auto palette = assets.write<VoxelPalette>(paletteHandle);
// Iterate over the grids which will be exported.
for (std::size_t i = 0; i < model.get().gridCount(); ++i)
if (state.gridFilePaths.contains(i))
{
if (state.gridFilePaths.contains(i))
if (gridHandles[i].isNull())
{
auto gridPath = state.gridFilePaths.at(i);
auto gridHandle = assets.find(gridPath);
if (gridHandle.isNull())
{
CUBOS_INFO("Failed to get asset handle for gridPath: {}", gridPath);
CUBOS_INFO("Creating new grid asset.");
gridHandle = assets.create(VoxelGrid{});
assets.writeMeta(gridHandle)->set("path", gridPath);
assets.save(gridHandle);
if (gridHandle.isNull())
{
// Should not happen.
CUBOS_ERROR("Failed to create grid asset.");
return false;
}
}
// Merge this palette with the main one.
palette.get().merge(model.get().palette(), 1.0F);
CUBOS_INFO("Failed to get asset handle for gridPath: {}", gridPaths[i]);
CUBOS_INFO("Creating new grid asset.");

{
auto grid = assets.write<VoxelGrid>(gridHandle);
// Convert the grid to the main palette.
if (!grid.get().convert(model.get().palette(), palette.get(), 1.0F))
{
CUBOS_ERROR("Error converting grid to main palette");
return false;
}
}
// Create a new empty VoxelGrid and assign a handle to it.
gridHandles[i] = assets.create(VoxelGrid{});
assets.writeMeta(gridHandles[i])->set("path", gridPaths[i]);
assets.save(gridHandles[i]);
}

CUBOS_INFO("Saving grid to path: {}", gridPath);
// Merge the current model's palette into the main palette.
palette.get().merge(model.get().palette(), 1.0F);

// Save the grid to the given path.
if (!assets.save(gridHandle))
{
auto grid = assets.write<VoxelGrid>(gridHandles[i]);

// Convert the grid using the main palette.
if (!grid.get().convert(model.get().palette(), palette.get(), 1.0F))
{
CUBOS_ERROR("Error saving grid to path: {}", gridPath);
CUBOS_ERROR("Error converting grid to main palette");
return false;
}
} // Release lock on gridHandle in order to save it.

// CUBOS_INFO("Setting grid path to asset meta");
// Not sure if needed (would be helpful probably). Causes deadlock for now.
// assets.writeMeta(state.currentAsset)->set("grid_path", gridPath);
// Save the grid to the given path.
CUBOS_INFO("Saving grid to path: {}", gridPaths[i]);
if (!assets.save(gridHandles[i]))
{
CUBOS_ERROR("Error saving grid to path: {}", gridPaths[i]);
return false;
}
}
}
}

CUBOS_INFO("Saving palette to path: {}", state.paletteFilePath);
} // Release lock on paletteHandle in order to save it.

// Save the palette to the asset system.
CUBOS_INFO("Saving palette to path: {}", state.paletteFilePath);
if (!assets.save(paletteHandle))
{
CUBOS_ERROR("Error saving palette to asset system");
return false;
}

// CUBOS_INFO("Setting palette path to asset meta");
// Not sure if needed (would be helpful probably).
// assets.writeMeta(state.currentAsset)->set("palette_path", state.paletteFilePath);
assets.writeMeta(state.currentAsset)->set("palette_id", uuids::to_string(paletteHandle.getId()));

// Save the model to the asset system.
if (!assets.save(state.currentAsset))
for (std::size_t i = 0; i < state.gridCount; ++i)
{
CUBOS_ERROR("Error saving model to asset system");
return false;
assets.writeMeta(state.currentAsset)
->set("grid_id_" + std::to_string(i), uuids::to_string(gridHandles[i].getId()));
}

CUBOS_INFO("handleImport completed successfully");
assets.saveMeta(state.currentAsset);

CUBOS_INFO("Import completed successfully");
return true;
}

Expand Down

0 comments on commit 0954d7a

Please sign in to comment.