From c21e9e98d23cfe082b460ed71dc7be39fa43de5a Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Mon, 27 Sep 2021 22:44:52 -0600 Subject: [PATCH] much simpler component handling in NAT --- .../nat-archive-tool/src/nat/Archive.kiss | 14 +-------- .../src/nat/ArchiveController.kiss | 2 +- projects/nat-archive-tool/src/nat/Lib.kiss | 31 ++++++------------- 3 files changed, 11 insertions(+), 36 deletions(-) diff --git a/projects/nat-archive-tool/src/nat/Archive.kiss b/projects/nat-archive-tool/src/nat/Archive.kiss index d6d5100e..3acd84d4 100644 --- a/projects/nat-archive-tool/src/nat/Archive.kiss +++ b/projects/nat-archive-tool/src/nat/Archive.kiss @@ -47,20 +47,8 @@ (joinPath archiveDir "entries" (e.id.withExtension "json")) (Json.stringify e))) -(method :Dynamic componentData [:Entry e :String componentType] - (haxe.Json.parse (File.getContent (joinPath archiveDir "components" "$(dictGet e.components componentType).json")))) - -(method fullData [:Entry e] - (object - id e.id - components - (for =>type id e.components - =>type (componentData e type)) - files - e.files)) - (method fullString [:Entry e] - (haxe.Json.stringify (fullData e) null "\t")) + (haxe.Json.stringify e null "\t")) (function :Entry _newEntry [] (object diff --git a/projects/nat-archive-tool/src/nat/ArchiveController.kiss b/projects/nat-archive-tool/src/nat/ArchiveController.kiss index e6cf6597..a7be3c0e 100644 --- a/projects/nat-archive-tool/src/nat/ArchiveController.kiss +++ b/projects/nat-archive-tool/src/nat/ArchiveController.kiss @@ -184,7 +184,7 @@ (defCommand PrintComponent [entries (SelectedEntries null null) componentType (Text null)] - (doFor e entries (ui.displayMessage (Std.string (archive.componentData e componentType)))) []) + (doFor e entries (ui.displayMessage (dictGet e.components componentType))) []) (defCommand CreateEntry [name (Text null)] [(archive.createEntry ->e diff --git a/projects/nat-archive-tool/src/nat/Lib.kiss b/projects/nat-archive-tool/src/nat/Lib.kiss index f5040156..2c3ddf1f 100644 --- a/projects/nat-archive-tool/src/nat/Lib.kiss +++ b/projects/nat-archive-tool/src/nat/Lib.kiss @@ -6,32 +6,19 @@ (defMacro hasComponent [e componentType] `(.exists .components ,e ,(symbolName componentType))) -(defMacro _componentPath [archive e componentType] - `(joinPath .archiveDir (the nat.Archive ,archive) "components" (+ (dictGet (the Map .components ,e) ,(symbolName componentType)) ".json"))) - // Changes to the object returned by (readComponent) will not be saved! Use (withWritableComponents) for making changes (defMacro readComponent [archive e componentType] - // TODO add to the documentation a hint that macros should use fully qualified type paths so macro caller classes don't need to import everything - `(the nat.components ,componentType - (tink.Json.parse - (sys.io.File.getContent (_componentPath ,archive ,e ,componentType))))) - -// Components have to be saved individually after writing because the Entity only knows their ids, -// not the data they contain. This is more ergonomically done by using (withWritableComponents...) -(defMacro writeComponent [archive e componentType c] - `{ - (print (+ "saving " (the nat.components ,componentType ,c) " as " ,(symbolName componentType) " for " .id ,e)) - (sys.io.File.saveContent - (_componentPath ,archive ,e ,componentType) - (tink.Json.stringify - (the nat.components ,componentType ,c)))}) + `(let [componentData (dictGet (the Map .components ,e) ,(symbolName componentType))] + (print (+ "reading " componentData " as " ,(symbolName componentType) " for " .id ,e)) + (the nat.components ,componentType + // TODO add to the documentation a hint that macros should use fully qualified type paths so macro caller classes don't need to import everything + (tink.Json.parse componentData)))) // TODO check not overwriting a component (defMacro addComponent [archive e componentType c] - `(withWritableEntry ,archive ,e (let [componentId (uuid.Uuid.v4)] - (dictSet .components ,e ,(symbolName componentType) componentId) - (writeComponent ,archive ,e ,componentType ,c) - ,e))) + `(withWritableEntry ,archive ,e + (print (+ "adding " (the nat.components ,componentType ,c) " as " ,(symbolName componentType) " for " .id ,e)) + (dictSet .components ,e ,(symbolName componentType) (tink.Json.stringify (the nat.components ,componentType ,c))))) // Retrieve multiple components from an Entity with mutable access. // All components will be serialized after the block is done. @@ -47,7 +34,7 @@ (doFor [name type] bindingPairs (bindingList.push `&mut ,name) (bindingList.push `(readComponent ,archive ,e ,type)) - (saveList.push `(writeComponent ,archive ,e ,type ,name))) + (saveList.push `(dictSet .components ,e ,(symbolName type) (tink.Json.stringify (the nat.components ,type ,name))))) `(let [,@bindingList ,retValSymbol {,@body}] ,@saveList