1+ /*
2+ Yaml.cs is part of the Experica.
3+ Copyright (c) 2016 Li Alex Zhang and Contributors
4+
5+ Permission is hereby granted, free of charge, to any person obtaining a
6+ copy of this software and associated documentation files (the "Software"),
7+ to deal in the Software without restriction, including without limitation
8+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+ and/or sell copies of the Software, and to permit persons to whom the
10+ Software is furnished to do so, subject to the following conditions:
11+
12+ The above copyright notice and this permission notice shall be included
13+ in all copies or substantial portions of the Software.
14+
15+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20+ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+ */
22+ using UnityEngine ;
23+ using System ;
24+ using YamlDotNet . Serialization ;
25+ using YamlDotNet . Core ;
26+ using YamlDotNet . Core . Events ;
27+ using System . Runtime . Serialization ;
28+ using System . IO ;
29+
30+ namespace Experica
31+ {
32+ public class YamlTypeConverter : IYamlTypeConverter
33+ {
34+ public bool Accepts ( Type type )
35+ {
36+ if ( type == typeof ( Vector2 ) || type == typeof ( Vector3 ) || type == typeof ( Vector4 ) || type == typeof ( Color ) )
37+ {
38+ return true ;
39+ }
40+ return false ;
41+ }
42+
43+ public object ReadYaml ( IParser parser , Type type )
44+ {
45+ var o = ( ( Scalar ) parser . Current ) . Value . Convert ( type ) ;
46+ parser . MoveNext ( ) ;
47+ return o ;
48+ }
49+
50+ public void WriteYaml ( IEmitter emitter , object value , Type type )
51+ {
52+ emitter . Emit ( new Scalar ( value . Convert < string > ( ) ) ) ;
53+ }
54+ }
55+
56+ public class YAMLFormat : IFormat
57+ {
58+ ISerializer serializer ;
59+ IDeserializer deserializer ;
60+
61+ public YAMLFormat ( )
62+ {
63+ var yamlvlabconverter = new YamlTypeConverter ( ) ;
64+ serializer = new SerializerBuilder ( ) . DisableAliases ( ) . EmitDefaults ( ) . IgnoreFields ( ) . WithTypeConverter ( yamlvlabconverter ) . Build ( ) ;
65+ deserializer = new DeserializerBuilder ( ) . IgnoreUnmatchedProperties ( ) . IgnoreFields ( ) . WithTypeConverter ( yamlvlabconverter ) . Build ( ) ;
66+ }
67+
68+ //public static void WriteYamlFile<T>(this string path, T data)
69+ //{
70+ // File.WriteAllText(path, serializer.Serialize(data));
71+ //}
72+
73+ //public static T ReadYamlFile<T>(string path)
74+ //{
75+ // return deserializer.Deserialize<T>(File.ReadAllText(path));
76+ //}
77+
78+ //public static T DeserializeYaml<T>(string data)
79+ //{
80+ // return deserializer.Deserialize<T>(data);
81+ //}
82+
83+ public string Serialize < T > ( T obj )
84+ {
85+ return serializer . Serialize ( obj ) ;
86+ }
87+
88+ public T Deserialize < T > ( string data )
89+ {
90+ return deserializer . Deserialize < T > ( data ) ;
91+ }
92+ }
93+ }
0 commit comments