Skip to content

Commit

Permalink
Improve .tscn VCS
Browse files Browse the repository at this point in the history
Serialize dictionaries adding newlines between key-value pairs
Serialize group lists also with newlines in between
Serialize string properties escaping only " and \ (needed for a good diff experience with built-in scripts and shaders)

Bonus:
Make AnimationPlayer serialize its blend times always sorted so their order is predictable in the .tscn file.

This PR is back-compat; won't break the load of existing files.
  • Loading branch information
RandomShaper committed Jan 16, 2017
1 parent 681575f commit 7dbb1c0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
11 changes: 10 additions & 1 deletion core/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3286,8 +3286,17 @@ String String::c_escape() const {
escaped=escaped.replace("\t","\\t");
escaped=escaped.replace("\v","\\v");
escaped=escaped.replace("\'","\\'");
escaped=escaped.replace("\"","\\\"");
escaped=escaped.replace("\?","\\?");
escaped=escaped.replace("\"","\\\"");

return escaped;
}

String String::c_escape_multiline() const {

String escaped=*this;
escaped=escaped.replace("\\","\\\\");
escaped=escaped.replace("\"","\\\"");

return escaped;
}
Expand Down
1 change: 1 addition & 0 deletions core/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class String : public Vector<CharType> {
String http_escape() const;
String http_unescape() const;
String c_escape() const;
String c_escape_multiline() const;
String c_unescape() const;
String json_escape() const;
String word_wrap(int p_chars_per_line) const;
Expand Down
10 changes: 5 additions & 5 deletions core/variant_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str

String str=p_variant;

str="\""+str.c_escape()+"\"";
str="\""+str.c_escape_multiline()+"\"";
p_store_string_func(p_store_string_ud, str );
} break;
case Variant::VECTOR2: {
Expand Down Expand Up @@ -2123,20 +2123,20 @@ Error VariantWriter::write(const Variant& p_variant, StoreStringFunc p_store_str
dict.get_key_list(&keys);
keys.sort();

p_store_string_func(p_store_string_ud,"{ ");
p_store_string_func(p_store_string_ud,"{\n");
for(List<Variant>::Element *E=keys.front();E;E=E->next()) {

//if (!_check_type(dict[E->get()]))
// continue;
write(E->get(),p_store_string_func,p_store_string_ud,p_encode_res_func,p_encode_res_ud);
p_store_string_func(p_store_string_ud,":");
p_store_string_func(p_store_string_ud,": ");
write(dict[E->get()],p_store_string_func,p_store_string_ud,p_encode_res_func,p_encode_res_ud);
if (E->next())
p_store_string_func(p_store_string_ud,", ");
p_store_string_func(p_store_string_ud,",\n");
}


p_store_string_func(p_store_string_ud," }");
p_store_string_func(p_store_string_ud,"\n}");


} break;
Expand Down
19 changes: 11 additions & 8 deletions scene/animation/animation_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,20 @@ bool AnimationPlayer::_get(const StringName& p_name,Variant &r_ret) const {

} else if (name=="blend_times") {

Array array;

array.resize(blend_times.size()*3);
int idx=0;
Vector<BlendKey> keys;
for(Map<BlendKey, float >::Element *E=blend_times.front();E;E=E->next()) {

array.set(idx*3+0,E->key().from);
array.set(idx*3+1,E->key().to);
array.set(idx*3+2,E->get());
idx++;
keys.ordered_insert(E->key());
}

Array array;
for(int i=0;i<keys.size();i++) {

array.push_back(keys[i].from);
array.push_back(keys[i].to);
array.push_back(blend_times[keys[i]]);
}

r_ret=array;
} else if (name=="autoplay") {
r_ret=autoplay;
Expand Down
2 changes: 1 addition & 1 deletion scene/animation/animation_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class AnimationPlayer : public Node {

StringName from;
StringName to;
bool operator<(const BlendKey& bk) const { return from==bk.from?to<bk.to:from<bk.from; }
bool operator<(const BlendKey& bk) const { return from==bk.from?String(to)<String(bk.to):String(from)<String(bk.from); }
};


Expand Down
10 changes: 4 additions & 6 deletions scene/resources/scene_format_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ void ResourceFormatSaverTextInstance::_find_resources(const Variant& p_variant,b
static String _valprop(const String& p_name) {

if (p_name.find("\"")!=-1 || p_name.find("=")!=-1 || p_name.find(" ")!=-1)
return "\""+p_name.c_escape()+"\"";
return "\""+p_name.c_escape_multiline()+"\"";
return p_name;
}

Expand Down Expand Up @@ -1358,13 +1358,11 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re
}

if (groups.size()) {
String sgroups=" groups=[ ";
String sgroups=" groups=[\n";
for(int j=0;j<groups.size();j++) {
if (j>0)
sgroups+=", ";
sgroups+="\""+groups[j].operator String().c_escape()+"\"";
sgroups+="\""+String(groups[j]).c_escape()+"\",\n";
}
sgroups+=" ]";
sgroups+="]";
header+=sgroups;
}

Expand Down

0 comments on commit 7dbb1c0

Please sign in to comment.