From a9e4265c19c82a1c16d9de2b9bbf8599cd4d11f3 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Wed, 7 Dec 2022 16:11:32 +0300 Subject: [PATCH] Gradually adding support for all features --- tpl/macro.twig | 71 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/tpl/macro.twig b/tpl/macro.twig index 06594a4..d5f8c65 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -334,8 +334,26 @@ public class {{o.name}} : IRpc {%- macro diffable_support(o) -%} + public bool GetDiff({{o.name}} old, {{o.name}} diff = null, List removed = null) + { + if(diff != null) + diff.reset(); + bool no_changes = true; + + {% set field_idx = -1 %} + {%- for f in o.fields ~%} + {% set field_idx = field_idx + 1 %} + + {%- if not has_token(f, 'nodiff') -%} + {{_self.field_diff(f, field_idx)}} + {%- endif -%} + {% endfor %} + + return !no_changes; + } + {% for u in get_diff_related_units(o) %} - {{_self.diff_methods(u)}} + {{_self.diff_methods_for(u)}} {% endfor %} static public List GetClassRemovedIds(List removed_ids, uint class_id) @@ -369,7 +387,7 @@ public class {{o.name}} : IRpc {% endmacro %} -{%- macro diff_methods(u) -%} +{%- macro diff_methods_for(u) -%} static public bool DiffOne(ref {{u.name}} curr, {{u.name}} old) { @@ -482,3 +500,52 @@ public class {{o.name}} : IRpc {% macro compare_func_name(o) %} {{has_token(u, 'bitfields')?'CompareAndMarkFields':'IsEqual'}} {% endmacro %} + +{%- macro field_diff(f, field_idx) -%} + +{% if f.type is instanceof('\\mtgMetaStruct') %} + {%- if not has_token(f.type, 'POD') -%} + Error("Diffable struct '" ~ f.type ~ "' must be POD"); + {%- endif -%} + + { + var __tmp = {{f.name}}; + if(DiffOne(ref __tmp, old.{{f.name}})) + { + no_changes &= false; + if(diff != null) + { + diff.{{f.name}} = __tmp; + Meta.SetFieldDirty(ref diff.fields_mask, {{field_idx}}); + } + } + } +{% elseif f.type is instanceof('\\mtgArrType') %} + {%- if not has_token(f.type.value, 'POD') -%} + Error("Diffable struct '" ~ f.type.value ~ "' must be POD"); + {%- endif -%} + + if(DiffCollection({{f.name}}, old.{{f.name}}, + diff == null ? null : diff.{{f.name}}, + removed + )) + { + no_changes &= false; + if(diff != null) + Meta.SetFieldDirty(ref diff.fields_mask, {{field_idx}}); + } +{% elseif f.type is instanceof('\\mtgBuiltinType') %} + if(diff != null) + diff.{{f.name}} = {{f.name}}; + + if({{f.name}} != old.{{f.name}}) + { + no_changes &= false; + if(diff != null) + Meta.SetFieldDirty(ref diff.fields_mask, {{field_idx}}); + } +{% else %} + Error("Diff for field '"~ f.name ~"' is not supported"); +{% endif %} + +{% endmacro %}