Files
nat-archive-tool/src/nat/Archive.kiss
2021-06-27 18:59:59 -06:00

55 lines
2.0 KiB
Plaintext

(defnew [&prop :String archiveDir]
[:Array<System> systems
[]
:Map<String,Entry> entries
(let [entryDir (Path.join [archiveDir "entries"])
componentDir (Path.join [archiveDir "components"])]
(FileSystem.createDirectory entryDir)
(FileSystem.createDirectory componentDir)
(let [entryFiles (FileSystem.readDirectory entryDir)]
(for file entryFiles =>(file.withoutExtension) (the Entry (Json.parse (File.getContent (Path.join [archiveDir "entries" file])))))))])
(defmethod addSystem [:System system]
// Assign entries to the Systems that care about them
(doFor =>id entry entries
(system.checkEntryInOrOut this entry))
(systems.push system)
system)
(defmethod :Entry createEntry [:Entry->Dynamic initializer] // initializer returns Dynamic so ->:Void isn't required
(let [e (_newEntry)]
(initializer e)
(dictSet entries e.id e)
(refreshEntry e)
e))
// After modifying an entry, this must be called. If you are writing in a createEntry initializer or a system's processEntry function, this will happen automatically.
// Otherwise, you can guarantee it happens automatically by using the (withWritableEntry) macro in Lib.kiss
(defmethod refreshEntry [:Entry e]
(_saveEntry e)
(doFor system systems
(system.checkEntryInOrOut this e)))
(defmethod _saveEntry [:Entry e]
(File.saveContent
(Path.join [archiveDir "entries" (e.id.withExtension "json")])
(Json.stringify e)))
(defmethod fullData [:Entry e]
(object
id e.id
components
(for =>type id e.components
=>type (haxe.Json.parse (File.getContent (haxe.io.Path.join [archiveDir "components" "$(dictGet e.components type).json"]))))
files
e.files))
(defmethod fullString [:Entry e]
(haxe.Json.stringify (fullData e) null "\t"))
(defun :Entry _newEntry []
(object
id (Uuid.v4)
components (new Map)
files []))