Skip to content

Commit

Permalink
Added a new function to resources that reloads a data asset.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorks committed Mar 13, 2024
1 parent 25f299c commit 1379553
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
11 changes: 11 additions & 0 deletions include/topaz/modules/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ topazAsset_t * topaz_resources_create_data_asset_from_path(
);


/// Reads bytes from the filesystem and replaces an existing
/// data asset's contents with these bytes.
///
/// Success is returned.
int topaz_resources_read_data_asset_from_path(
topazResources_t * res,
/// The asset to modify.
topazAsset_t * asset,
/// The path to the asset.
const topazString_t * path
);


/// Converts an existing data asset
Expand Down
26 changes: 22 additions & 4 deletions src/modules_resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ topazAsset_t * topaz_resources_create_data_asset_from_path(
return NULL;
}

if(!topaz_resources_read_data_asset_from_path(
r,
a,
path
)) {
topaz_asset_destroy(a);
topaz_table_remove(r->name2asset, name);
}

return a;
}

int topaz_resources_read_data_asset_from_path(
topazResources_t * r,
/// The asset to modify.
topazAsset_t * a,
/// The path to the asset.
const topazString_t * path
) {

// at this point, we want a data buffer
topazFilesystem_t * fs = topaz_context_get_filesystem(r->ctx);
const topazFilesystem_Path_t * p = topaz_filesystem_get_path_from_string(fs, r->path, path);
Expand All @@ -227,9 +247,7 @@ topazAsset_t * topaz_resources_create_data_asset_from_path(

// check to see if read failed.
if (!data) {
topaz_asset_destroy(a);
topaz_table_remove(r->name2asset, name);
return NULL;
return 0;
}

// populate data asset
Expand All @@ -242,7 +260,7 @@ topazAsset_t * topaz_resources_create_data_asset_from_path(
);

topaz_rbuffer_destroy(data);
return a;
return 1;
}

//// PRIVATE:
Expand Down
18 changes: 18 additions & 0 deletions src/script_native__resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ TSO_SCRIPT_API_FN(resources_api__create_data_asset_from_path) {
return object;
}

TSO_SCRIPT_API_FN(resources_api__read_data_asset_from_path) {
TSO_ARG_0;
TSO_ARG_1;
TSO_NATIVIZE(topazAsset_t *, TSO_OBJECT_TYPE__ASSET);

topazResources_t * r = topaz_context_get_resources(((topazScriptManager_t*)context)->ctx);

return topaz_script_object_from_int(
script,
topaz_resources_read_data_asset_from_path(
r,
native,
topaz_script_object_as_string(arg1)
)
);
}




Expand Down Expand Up @@ -420,6 +437,7 @@ static void add_refs__resources_api(topazScript_t * script, topazScriptManager_t
TS_MAP_NATIVE_FN("topaz_resources__fetch_asset", resources_api__fetch_asset, 1);

TS_MAP_NATIVE_FN("topaz_resources__create_data_asset_from_path", resources_api__create_data_asset_from_path, 2);
TS_MAP_NATIVE_FN("topaz_resources__read_data_asset_from_path", resources_api__read_data_asset_from_path, 2);

TS_MAP_NATIVE_FN("topaz_resources__convert_asset", resources_api__convert_asset, 2);
TS_MAP_NATIVE_FN("topaz_resources__write_asset", resources_api__write_asset, 3);
Expand Down
4 changes: 4 additions & 0 deletions system/script/lua/src/topaz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2862,6 +2862,10 @@ Topaz = {};
return out;
end,

readDataAssetFromPath = function(asset, path)
return topaz_resources__read_data_asset_from_path(asset, path);
end,

convertAsset = function(fileType, asset)
local out = topaz_resources__convert_asset(fileType, asset);
if (out == nil) then return nil end;
Expand Down
6 changes: 6 additions & 0 deletions system/script/matte/src/topaz.mt
Original file line number Diff line number Diff line change
Expand Up @@ -3105,6 +3105,7 @@
@:topaz_resources__fetch_asset = getExternalFunction(name:'topaz_resources__fetch_asset');
@:topaz_resources__create_asset = getExternalFunction(name:'topaz_resources__create_asset');
@:topaz_resources__create_data_asset_from_path = getExternalFunction(name:'topaz_resources__create_data_asset_from_path');
@:topaz_resources__read_data_asset_from_path = getExternalFunction(name:'topaz_resources__read_data_asset_from_path');
@:topaz_resources__convert_asset = getExternalFunction(name:'topaz_resources__convert_asset');
@:topaz_resources__write_asset = getExternalFunction(name:'topaz_resources__write_asset');
@:topaz_resources__remove_asset = getExternalFunction(name:'topaz_resources__remove_asset');
Expand Down Expand Up @@ -3163,6 +3164,11 @@
initializer__data(d:out);
return out;
},

readDataAssetFromPath ::(asset, path) {
return topaz_resources__read_data_asset_from_path(a:asset, b:path);
},


convertAsset ::(fileType, asset) {
@:out = topaz_resources__convert_asset(a:fileType, b:asset);
Expand Down

0 comments on commit 1379553

Please sign in to comment.