summaryrefslogtreecommitdiff
path: root/server/chunk.lua
diff options
context:
space:
mode:
Diffstat (limited to 'server/chunk.lua')
-rw-r--r--server/chunk.lua36
1 files changed, 36 insertions, 0 deletions
diff --git a/server/chunk.lua b/server/chunk.lua
new file mode 100644
index 0000000..4fb2960
--- /dev/null
+++ b/server/chunk.lua
@@ -0,0 +1,36 @@
+local class = require"common.class"
+local Chunk = require"common.chunk".Chunk
+local json = require"common.dkjson"
+
+local ChunkS = class.extend(Chunk)
+function ChunkS.make(cls,...)
+ local self = Chunk.make(cls,...)
+ self.dirty = true
+ return self
+end
+function ChunkS.load_from_disk(cls,cp)
+ -- returns nil if not there
+ local filename = cp:filename()
+ local f = io.open(filename,"r")
+ if not f then return nil end
+ local j = json.decode(f:read("a"))
+ self = cls:from_packet_data(j)
+ f:close()
+ return self
+end
+function ChunkS.save_if_dirty(self)
+ if self.dirty then
+ print("saving chunk",self.cp)
+ local filename = self.cp:filename()
+ local f = io.open(filename,"w")
+ f:write(self:data_packet())
+ f:flush()
+ f:close()
+ end
+end
+function ChunkS.set_at(self,...)
+ Chunk.set_at(self,...)
+ self.dirty = true
+end
+
+return {ChunkS=ChunkS}