From 81bf2d653902860c5d28ccade19ac6e1fd56acaf Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 26 May 2015 14:06:52 +0800 Subject: add layer and param --- io/init.lua | 21 +++++++++++++++++++++ io/param.c | 8 ++++---- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'io') diff --git a/io/init.lua b/io/init.lua index 1288bd4..2fa38e6 100644 --- a/io/init.lua +++ b/io/init.lua @@ -5,3 +5,24 @@ function nerv.ParamFile:write_chunkdata(metadata, writer) end return self:_write_chunkdata(table.tostring(metadata), writer) end + +function nerv.ParamFile:write_param(param) + local id = param.id + local type = param.__typename + if id == nil then + nerv_error("id of param %s must be specified", type) + end + self:write_chunkdata({id = id, + type = type, + info = param:get_info()}, param) +end + +function nerv.ParamFile:read_param(id) + local metadata = self.metadata[id] + if metadata == nil then + nerv_error("param with id %s does not exist", id) + end + local param = assert(loadstring("return " .. metadata.type .. "(" .. id .. ")"))() + param:set_info(metadata.info) + param:read(self:get_chunkdata(id)) +end diff --git a/io/param.c b/io/param.c index 627c815..477df28 100644 --- a/io/param.c +++ b/io/param.c @@ -220,12 +220,12 @@ int nerv_param_file_write_chunkdata(lua_State *L) { write_param_metadata(pfh->fp, metadata_str, &status); CHECK_WRITE(status); lua_pushvalue(L, 3); - lua_getfield(L, -1, "save"); + lua_getfield(L, -1, "write"); if (!lua_isfunction(L, -1)) - nerv_error(L, "\"save\" method must be implemented"); + nerv_error(L, "\"write\" method must be implemented"); lua_pushvalue(L, -2); - lua_pushvalue(L, 4); /* pass handle as parameter to save() */ - lua_call(L, 2, 0); /* let the save() to write */ + lua_pushvalue(L, 4); /* pass handle as parameter to write() */ + lua_call(L, 2, 0); /* let the write() to write */ lua_pop(L, 1); size = ftello(pfh->fp) - start; fseeko(pfh->fp, start, SEEK_SET); -- cgit v1.2.3