-
-
Notifications
You must be signed in to change notification settings - Fork 910
/
ContentStorageHeader.cs
110 lines (97 loc) · 3.46 KB
/
ContentStorageHeader.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
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Runtime.Serialization;
using Stride.Core.Serialization;
namespace Stride.Core.Streaming
{
/// <summary>
/// Header with description of streamable resource data storage.
/// </summary>
public struct ContentStorageHeader
{
/// <summary>
/// Describies single data chunk storage information.
/// </summary>
public struct ChunkEntry
{
/// <summary>
/// The location (adress in file).
/// </summary>
public int Location;
/// <summary>
/// The size in bytes.
/// </summary>
public int Size;
}
/// <summary>
/// True if data is followed by initial low resolution image.
/// </summary>
public bool InitialImage;
/// <summary>
/// The data container url.
/// </summary>
public string DataUrl;
/// <summary>
/// Time when package has been created (in UTC).
/// </summary>
public DateTime PackageTime;
/// <summary>
/// The hash code for the package header. Used to ensure data consistency.
/// </summary>
public int HashCode;
/// <summary>
/// The data chunks.
/// </summary>
public ChunkEntry[] Chunks;
/// <summary>
/// Gets the amount of data chunks.
/// </summary>
public int ChunksCount => Chunks.Length;
/// <summary>
/// Writes this instance to a stream.
/// </summary>
/// <param name="stream">The destination stream.</param>
public void Write(SerializationStream stream)
{
stream.Write(1);
stream.Write(InitialImage);
stream.Write(DataUrl);
stream.Write(PackageTime.Ticks);
stream.Write(ChunksCount);
for (int i = 0; i < Chunks.Length; i++)
{
var e = Chunks[i];
stream.Write(e.Location);
stream.Write(e.Size);
}
stream.Write(HashCode);
}
/// <summary>
/// Reads header instance from a stream.
/// </summary>
/// <param name="stream">The source stream.</param>
/// <param name="result">Result data</param>
public static void Read(SerializationStream stream, out ContentStorageHeader result)
{
result = new ContentStorageHeader();
var version = stream.ReadInt32();
if (version == 1)
{
result.InitialImage = stream.ReadBoolean();
result.DataUrl = stream.ReadString();
result.PackageTime = new DateTime(stream.ReadInt64());
int chunksCount = stream.ReadInt32();
result.Chunks = new ChunkEntry[chunksCount];
for (int i = 0; i < chunksCount; i++)
{
result.Chunks[i].Location = stream.ReadInt32();
result.Chunks[i].Size = stream.ReadInt32();
}
result.HashCode = stream.ReadInt32();
return;
}
throw new SerializationException($"Invald {nameof(ContentStorageHeader)} version.");
}
}
}