-
Notifications
You must be signed in to change notification settings - Fork 10
/
Node.cs
284 lines (246 loc) · 9.95 KB
/
Node.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) 2019 SceneGate
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace Yarhl.FileSystem
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Yarhl.FileFormat;
using Yarhl.IO;
/// <summary>
/// Node in the FileSystem with an associated format.
/// </summary>
[DebuggerDisplay("{Name} [{Format}]")]
public partial class Node : NavigableNode<Node>
{
/// <summary>
/// Initializes a new instance of the <see cref="Node"/> class.
/// </summary>
/// <param name="name">Node name.</param>
public Node(string name)
: base(name)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Node"/> class.
/// </summary>
/// <param name="name">Node name.</param>
/// <param name="initialFormat">Node format.</param>
public Node(string name, IFormat initialFormat)
: this(name)
{
ChangeFormat(initialFormat);
}
/// <summary>
/// Initializes a new instance of the <see cref="Node"/> class.
/// </summary>
/// <param name="node">The original node.</param>
/// <remarks><para>It makes a copy of the original node.
/// The original format is deep copied. See <see cref="ICloneableFormat"/> for details.</para>
/// </remarks>
/// <remarks><para>If the node has children, it must be a <see cref="NodeContainerFormat"/> to clone them.
/// In other case, the format must implement <see cref="ICloneableFormat"/> and clone the children explicitly.
/// </para></remarks>
public Node(Node node)
: this(node != null ? node.Name : string.Empty)
{
if (node!.Format != null && node.Format is not ICloneableFormat) {
throw new InvalidOperationException("Format does not implement ICloneableFormat interface.");
}
ICloneableFormat? newFormat = null;
if (node.Format != null) {
var oldFormat = node.Format as ICloneableFormat;
newFormat = (ICloneableFormat)oldFormat!.DeepClone();
}
ChangeFormat(newFormat);
foreach (KeyValuePair<string, dynamic> tag in node.Tags)
{
Tags[tag.Key] = tag.Value;
}
}
/// <summary>
/// Gets the current format of the node.
/// </summary>
/// <value>The current format.</value>
public IFormat? Format {
get;
private set;
}
/// <summary>
/// Gets the node associated DataStream if the format is IBinary.
/// </summary>
/// <value>
/// DataStream if the format is IBinary, null otherwise.
/// </value>
public DataStream? Stream => GetFormatAs<IBinary>()?.Stream;
/// <summary>
/// Gets a value indicating whether the format is a container of nodes.
/// </summary>
/// <value>
/// <see langword="true"/> if the format is a container; otherwise,
/// <see langword="false"/>.
/// </value>
public bool IsContainer => Format is NodeContainerFormat;
/// <summary>
/// Gets the format as the specified type.
/// </summary>
/// <returns>The format casted to the type or null if not possible.</returns>
/// <typeparam name="T">The format type.</typeparam>
public T? GetFormatAs<T>()
where T : class, IFormat
{
if (Disposed) {
throw new ObjectDisposedException(nameof(Node));
}
return Format as T;
}
/// <summary>
/// Change the format of the current node.
/// </summary>
/// <returns>This node.</returns>
/// <remarks>
/// <para>If the previous format was a container, this method will
/// remove the children of the node.
/// If the new format is a container, this method will add the format
/// children to the node.</para>
/// <para>If the new format is the same reference as the current format
/// the method is a no-op.</para>
/// </remarks>
/// <param name="newFormat">The new format to assign.</param>
/// <param name="disposePreviousFormat">
/// If <see langword="true" /> the method will dispose the previous
/// format.
/// </param>
public Node ChangeFormat(IFormat? newFormat, bool disposePreviousFormat = true)
{
if (Disposed) {
throw new ObjectDisposedException(nameof(Node));
}
if (newFormat == Format) {
return this;
}
// If it was a container, clean children
if (IsContainer) {
RemoveChildren();
}
if (disposePreviousFormat) {
(Format as IDisposable)?.Dispose();
}
Format = newFormat;
// If now it's a container, add its children
if (IsContainer) {
AddContainerChildren();
}
return this;
}
/// <summary>
/// Transform the node format to another format with a given converter.
/// </summary>
/// <returns>This node.</returns>
/// <typeparam name="TConv">The type of the converter to use.</typeparam>
public Node TransformWith<TConv>()
where TConv : IConverter, new()
{
if (Disposed) {
throw new ObjectDisposedException(nameof(Node));
}
// This is a valid use of ConvertFormat.With as we don't care about
// the returned type. There isn't anything to do type safety.
return TransformWith(typeof(TConv));
}
/// <summary>
/// Transforms the node format with the specified converter.
/// </summary>
/// <returns>This node.</returns>
/// <param name="converterType">The type of the converter to use.</param>
/// <param name="args">
/// Arguments for the constructor of the type if any.
/// </param>
public Node TransformWith(Type converterType, params object?[] args)
{
if (Disposed) {
throw new ObjectDisposedException(nameof(Node));
}
if (converterType == null) {
throw new ArgumentNullException(nameof(converterType));
}
if (Format is null) {
throw new InvalidOperationException(
"Cannot transform a node without format");
}
object result = ConvertFormat.With(converterType, Format, args);
CastAndChangeFormat(result);
return this;
}
/// <summary>
/// Transform the node format to another format using a converter.
/// </summary>
/// <param name="converter">Convert to use.</param>
/// <returns>This node.</returns>
public Node TransformWith(IConverter converter)
{
if (Disposed) {
throw new ObjectDisposedException(nameof(Node));
}
if (converter == null) {
throw new ArgumentNullException(nameof(converter));
}
if (Format is null) {
throw new InvalidOperationException(
"Cannot transform a node without format");
}
ConvertFormat.ValidateConverterType(converter.GetType(), Format.GetType());
dynamic converterDyn = converter;
dynamic source = Format;
object newFormat = converterDyn.Convert(source);
CastAndChangeFormat(newFormat);
return this;
}
/// <summary>
/// Releases all resource used by the <see cref="Node"/>
/// object.
/// </summary>
/// <param name="freeManagedResourcesAlso">
/// If set to <see langword="true"/> free managed resources also.
/// </param>
protected override void Dispose(bool freeManagedResourcesAlso)
{
base.Dispose(freeManagedResourcesAlso);
if (freeManagedResourcesAlso) {
(Format as IDisposable)?.Dispose();
}
}
void AddContainerChildren()
{
RemoveChildren();
GetFormatAs<NodeContainerFormat>()?.MoveChildrenTo(this);
}
void CastAndChangeFormat(object newFormat)
{
if (newFormat == null) {
// Null may be an acceptable format, for now.
ChangeFormat(null);
} else if (newFormat is IFormat format) {
ChangeFormat(format);
} else {
throw new InvalidOperationException(
"Result format does not implement the IFormat interface. " +
"Cannot assign to the Format property.");
}
}
}
}