-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathEditorSession.cs
More file actions
151 lines (137 loc) · 3.49 KB
/
Copy pathEditorSession.cs
File metadata and controls
151 lines (137 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using RPGCore.DataEditor.Files;
using RPGCore.DataEditor.Manifest;
using System;
using System.Text;
namespace RPGCore.DataEditor;
/// <summary>
/// Provides configuration for data editing.
/// </summary>
public class EditorSession
{
/// <summary>
/// The manifest used for configuring the data editor.
/// </summary>
public ProjectManifest Manifest { get; }
/// <summary>
/// The serializer used to load and save values.
/// </summary>
public IEditorSerializer Serializer { get; }
/// <summary>
/// Creates a new instance of the <see cref="EditorSession"/> object.
/// </summary>
/// <param name="manifest">A manifest used to drive data behaviour.</param>
/// <param name="serializer">The serializer used to serialize values.</param>
public EditorSession(ProjectManifest manifest, IEditorSerializer serializer)
{
Manifest = manifest;
Serializer = serializer;
}
/// <summary>
/// Starts editing a file using configuration provided by this <see cref="EditorSession"/>.
/// </summary>
/// <returns>A new <see cref="EditorFile"/> used for editing.</returns>
public EditorFileBuilder EditFile()
{
return new EditorFileBuilder(this);
}
/// <summary>
/// Gets the underlying type information from a qualified type.
/// </summary>
/// <param name="schemaQualifiedType"></param>
/// <returns></returns>
public SchemaType? ResolveType(TypeName schemaQualifiedType)
{
var type = Manifest.GetTypeInformation(schemaQualifiedType.Identifier);
return type;
}
internal IEditorValue CreateDefaultValue(TypeName type)
{
return type.IsNullable
? new EditorNull(this)
: CreateInstatedValue(type);
}
internal IEditorValue CreateInstatedValue(TypeName type)
{
if (type.IsDictionary)
{
return new EditorDictionary(this, type);
}
else if (type.IsArray)
{
return new EditorList(this, type);
}
else
{
var typeInfo = Manifest.GetTypeInformation(type.Identifier);
if (typeInfo == null)
{
throw new InvalidOperationException($"Cannot create an instance of an object of type \"{type.Identifier}\".");
}
if (string.IsNullOrEmpty(typeInfo.InstatedValue))
{
return new EditorNull(this);
}
else
{
byte[]? data = Encoding.UTF8.GetBytes(typeInfo.InstatedValue);
var scalarInnerValue = Serializer.DeserializeValue(this, type, new ArraySegment<byte>(data));
return scalarInnerValue;
}
}
}
internal bool IsTypeSubtypeOf(TypeName subType, TypeName baseType)
{
return true;
}
internal bool IsValueOfType(IEditorValue value, TypeName type)
{
if (type.IsDictionary)
{
if (value is EditorDictionary editorDictionary)
{
bool isKeyMatch = editorDictionary.KeyType == type.TemplateTypes[0];
bool isValueMatch = editorDictionary.ValueType == type.TemplateTypes[1];
return isKeyMatch && isValueMatch;
}
else
{
return false;
}
}
else if (type.IsArray)
{
if (value is EditorList editorList)
{
return editorList.ElementType == type.TemplateTypes[0];
}
else
{
return false;
}
}
else
{
var typeInfo = Manifest.GetTypeInformation(type.Identifier);
if (typeInfo == null)
{
throw new InvalidOperationException($"Cannot create an instance of an object of type \"{typeInfo}\".");
}
if (value is EditorNull)
{
return type.IsNullable;
}
else if (value is EditorScalarValue scalarValue)
{
return scalarValue.Type == type;
}
else if (value is EditorObject editorObject)
{
return editorObject.Type == type;
}
else
{
return false;
}
}
}
}