From 826d115cb1012f2e6285c4ae29da7fdb9a96fd41 Mon Sep 17 00:00:00 2001 From: Marco Hladik Date: Sun, 6 Jun 2021 12:27:36 +0200 Subject: [PATCH] Gracefully deal with entity attributes of the same name. This is needed for the Input/Output system. We append numerations separated by a '#' character and strip the when saving the map. The editor will intelligently pick a name for us. --- libs/entitylib.h | 72 +++++++++++++++++++++++++++++++++++++++-- plugins/mapq3/write.cpp | 8 +++-- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/libs/entitylib.h b/libs/entitylib.h index 09ba84d..5725031 100644 --- a/libs/entitylib.h +++ b/libs/entitylib.h @@ -374,6 +374,7 @@ typedef MemberCallerassign( value ); + + /* does the key already exist */ + if (i != m_keyValues.end() ) { + /* re-assign only when we're a special field, else pick a new name */ + if (dupecheck) { + ( *i ).second->assign( value ); + printf("[ENTLIB]: dupe found, setting %s to %s\n", key, value); + } else { + bool b = true; + unsigned int num = 0; + StringOutputStream new_key(64); + + /* loop through and generate an enumerated variant */ + do { + /* keep incrementing num until we find a free slot */ + num++; + new_key.clear(); + new_key << key << "#" << Unsigned(num); + i = m_keyValues.find(new_key.c_str()); + + if (i == m_keyValues.end()) { + insert(new_key.c_str(), value); + b = false; + } + } while (b != false); + } } else { m_undo.save(); insert( key, KeyValuePtr( new KeyValue( value, EntityClass_valueForKey( *m_eclass, key ) ) ) ); + printf("[ENTLIB]: inserting key %s = %s\n", key, value); } } diff --git a/plugins/mapq3/write.cpp b/plugins/mapq3/write.cpp index f37b5b0..6228a24 100644 --- a/plugins/mapq3/write.cpp +++ b/plugins/mapq3/write.cpp @@ -49,9 +49,11 @@ void Entity_ExportTokens(const Entity &entity, TokenWriter &writer) void visit(const char *key, const char *value) { - m_writer.writeString(key); - m_writer.writeString(value); - m_writer.nextLine(); + /* cut anything after # including the symbol itself */ + StringTokeniser st(key, "#"); + m_writer.writeString(st.getToken()); + m_writer.writeString(value); + m_writer.nextLine(); } } visitor(writer);