PR004: implement asset status-first lifecycle surface
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
use crate::memory_banks::{SoundBankPoolInstaller, TileBankPoolInstaller};
|
||||
use prometeu_hal::AssetBridge;
|
||||
use prometeu_hal::asset::{
|
||||
AssetEntry, BankStats, BankType, HandleId, LoadStatus, PreloadEntry, SlotRef, SlotStats,
|
||||
AssetEntry, AssetLoadError, AssetOpStatus, BankStats, BankType, HandleId, LoadStatus,
|
||||
PreloadEntry, SlotRef, SlotStats,
|
||||
};
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::sample::Sample;
|
||||
@@ -139,16 +140,16 @@ impl AssetBridge for AssetManager {
|
||||
) {
|
||||
self.initialize_for_cartridge(assets, preload, assets_data)
|
||||
}
|
||||
fn load(&self, asset_name: &str, slot: SlotRef) -> Result<HandleId, String> {
|
||||
fn load(&self, asset_name: &str, slot: SlotRef) -> Result<HandleId, AssetLoadError> {
|
||||
self.load(asset_name, slot)
|
||||
}
|
||||
fn status(&self, handle: HandleId) -> LoadStatus {
|
||||
self.status(handle)
|
||||
}
|
||||
fn commit(&self, handle: HandleId) {
|
||||
fn commit(&self, handle: HandleId) -> AssetOpStatus {
|
||||
self.commit(handle)
|
||||
}
|
||||
fn cancel(&self, handle: HandleId) {
|
||||
fn cancel(&self, handle: HandleId) -> AssetOpStatus {
|
||||
self.cancel(handle)
|
||||
}
|
||||
fn apply_commits(&self) {
|
||||
@@ -293,19 +294,22 @@ impl AssetManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(&self, asset_name: &str, slot: SlotRef) -> Result<HandleId, String> {
|
||||
pub fn load(&self, asset_name: &str, slot: SlotRef) -> Result<HandleId, AssetLoadError> {
|
||||
if slot.index >= 16 {
|
||||
return Err(AssetLoadError::SlotIndexInvalid);
|
||||
}
|
||||
let entry = {
|
||||
let assets = self.assets.read().unwrap();
|
||||
let name_to_id = self.name_to_id.read().unwrap();
|
||||
let id = name_to_id
|
||||
.get(asset_name)
|
||||
.ok_or_else(|| format!("Asset not found: {}", asset_name))?;
|
||||
assets.get(id).ok_or_else(|| format!("Asset ID {} not found in table", id))?.clone()
|
||||
.ok_or(AssetLoadError::AssetNotFound)?;
|
||||
assets.get(id).ok_or(AssetLoadError::BackendError)?.clone()
|
||||
};
|
||||
let asset_id = entry.asset_id;
|
||||
|
||||
if slot.asset_type != entry.bank_type {
|
||||
return Err("INCOMPATIBLE_SLOT_KIND".to_string());
|
||||
return Err(AssetLoadError::SlotKindMismatch);
|
||||
}
|
||||
|
||||
let mut next_id = self.next_handle_id.lock().unwrap();
|
||||
@@ -533,21 +537,38 @@ impl AssetManager {
|
||||
}
|
||||
|
||||
pub fn status(&self, handle: HandleId) -> LoadStatus {
|
||||
self.handles.read().unwrap().get(&handle).map(|h| h.status).unwrap_or(LoadStatus::ERROR)
|
||||
self.handles
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&handle)
|
||||
.map(|h| h.status)
|
||||
.unwrap_or(LoadStatus::UnknownHandle)
|
||||
}
|
||||
|
||||
pub fn commit(&self, handle: HandleId) {
|
||||
pub fn commit(&self, handle: HandleId) -> AssetOpStatus {
|
||||
let mut handles_map = self.handles.write().unwrap();
|
||||
if let Some(h) = handles_map.get_mut(&handle) {
|
||||
if h.status == LoadStatus::READY {
|
||||
self.pending_commits.lock().unwrap().push(handle);
|
||||
}
|
||||
let Some(h) = handles_map.get_mut(&handle) else {
|
||||
return AssetOpStatus::UnknownHandle;
|
||||
};
|
||||
if h.status == LoadStatus::READY {
|
||||
self.pending_commits.lock().unwrap().push(handle);
|
||||
AssetOpStatus::Ok
|
||||
} else {
|
||||
AssetOpStatus::InvalidState
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cancel(&self, handle: HandleId) {
|
||||
pub fn cancel(&self, handle: HandleId) -> AssetOpStatus {
|
||||
let mut final_status = AssetOpStatus::UnknownHandle;
|
||||
let mut handles_map = self.handles.write().unwrap();
|
||||
if let Some(h) = handles_map.get_mut(&handle) {
|
||||
final_status = match h.status {
|
||||
LoadStatus::PENDING | LoadStatus::LOADING | LoadStatus::READY => {
|
||||
AssetOpStatus::Ok
|
||||
}
|
||||
LoadStatus::CANCELED => AssetOpStatus::Ok,
|
||||
_ => AssetOpStatus::InvalidState,
|
||||
};
|
||||
match h.status {
|
||||
LoadStatus::PENDING | LoadStatus::LOADING | LoadStatus::READY => {
|
||||
h.status = LoadStatus::CANCELED;
|
||||
@@ -557,6 +578,7 @@ impl AssetManager {
|
||||
}
|
||||
self.gfx_policy.take_staging(handle);
|
||||
self.sound_policy.take_staging(handle);
|
||||
final_status
|
||||
}
|
||||
|
||||
pub fn apply_commits(&self) {
|
||||
|
||||
Reference in New Issue
Block a user