From 631c2397173d7c7631c58270676b32aa45e0bfe0 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 10 May 2017 22:01:47 +0200 Subject: [PATCH] Merge function for proplists --- planet/System.ocg/Proplists.c | 47 ++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/planet/System.ocg/Proplists.c b/planet/System.ocg/Proplists.c index 3540b4b50..1e96ab74f 100644 --- a/planet/System.ocg/Proplists.c +++ b/planet/System.ocg/Proplists.c @@ -2,9 +2,54 @@ Proplists.c General helper functions that create or work with proplists. - @author Guenther, Maikel + @author Guenther, Maikel, Marky */ +/* + Adds the contents of one proplist to the other. + Usually you can do this by using a prototype, but + this is not possible when you receive a second proplist + from another function. + + @par original This proplist will be expanded. + @par additional These are the additional contents. + @par no_overwrite By default the function overwrites + existing properties in the original proplist, + as it would happen if you derive from a prototype. + If this parameter is 'true' the function will report + an error instead of overwriting a parameter. + + @return proplist The original proplist. This is in fact the same + proplist that was passed as an argument, the functions just + returns it for convenience. + */ +global func AddProperties(proplist original, proplist additional, bool no_overwrite) +{ + if (original == nil) + { + FatalError("Cannot add properties to 'nil'"); + } + if (additional == nil) + { + FatalError("Adding proplist 'nil' to another proplist makes no sense."); + } + + for (var property in GetProperties(additional)) + { + if (no_overwrite && (original[property] != nil)) // about to overwrite? + { + FatalError(Format("Cancelling overwrite of property '%s', original value %v, with new value %v", property, original[property], additional[property])); + } + else + { + original[property] = additional[property]; + } + } + + return original; +} + + // Turns a property into a writable one. This for example useful if an object // inherits a property from a definition which should not stay read-only. global func MakePropertyWritable(name, obj)