-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializableTransformObject.cs
171 lines (144 loc) · 5.33 KB
/
SerializableTransformObject.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
// <copyright file="SerializableTransformObject.cs" company="HiVR">
// Copyright (c) 2016 HiVR All Rights Reserved
// </copyright>
namespace SerializedObjects
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// Serializable data class; contains position, scale and rotation vectors.
/// Can be Serialized and DeSerialized with supplied methods.
/// </summary>
[Serializable]
public class SerializableTransformObject
{
#region Fields
/// <summary>
/// Private instance of property below.
/// </summary>
[NonSerialized]
private Socket socket;
/// <summary>
/// Private instance of property below.
/// </summary>
[NonSerialized]
private List<byte> transmissionBuffer = new List<byte>();
/// <summary>
/// Private instance of property below.
/// </summary>
[NonSerialized]
private byte[] buffer = new byte[2048];
#endregion Fields
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SerializableTransformObject"/> class.
/// </summary>
public SerializableTransformObject()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SerializableTransformObject"/> class.
/// </summary>
/// <param name="id">the id of the object</param>
/// <param name="type">the type of the object</param>
/// <param name="isStatic">whether the object is static or not</param>
/// <param name="position">the position vector of the object</param>
/// <param name="scale">the scale vector of the object</param>
/// <param name="rotation">the rotation vector of the object</param>
/// <param name="character">the serializable character object, if the type is character</param>
public SerializableTransformObject(int id, SerializableType type, bool isStatic, SerializableVector3 position, SerializableVector3 scale, SerializableVector3 rotation, SerializableCharacter character)
{
this.Id = id;
this.Type = type;
this.IsStatic = isStatic;
this.Position = position;
this.Scale = scale;
this.Rotation = rotation;
this.Character = character;
}
#endregion Constructors
#region Properties
/// <summary>
/// Gets or sets byte buffer used to receive part of binary stream.
/// </summary>
public byte[] Buffer
{
get { return this.buffer; }
set { this.buffer = value; }
}
/// <summary>
/// Gets or sets list of bytes used for retrieving binary stream of unknown length.
/// </summary>
public List<byte> TransmissionBuffer
{
get { return this.transmissionBuffer; }
set { this.transmissionBuffer = value; }
}
/// <summary>
/// Gets or sets socket used for sending/receiving binary data.
/// </summary>
public Socket Socket
{
get { return this.socket; }
set { this.socket = value; }
}
/// <summary>
/// Gets or sets the id of the object.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Gets or sets the type of the object.
/// </summary>
public SerializableType Type { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the object is static or not.
/// </summary>
public bool IsStatic { get; set; }
/// <summary>
/// Gets or sets the position vector of the object.
/// </summary>
public SerializableVector3 Position { get; set; }
/// <summary>
/// Gets or sets the scale vector of the object.
/// </summary>
public SerializableVector3 Scale { get; set; }
/// <summary>
/// Gets or sets the rotation vector of the object.
/// </summary>
public SerializableVector3 Rotation { get; set; }
/// <summary>
/// Gets or sets the character object.
/// </summary>
public SerializableCharacter Character { get; set; }
#endregion Properties
#region Methods
/// <summary>
/// Serialize this object into a byte buffer.
/// </summary>
/// <returns>serialized object</returns>
public byte[] Serialize()
{
var bin = new BinaryFormatter();
var mem = new MemoryStream();
bin.Serialize(mem, this);
return mem.GetBuffer();
}
/// <summary>
/// DeSerialize this object from a byte buffer.
/// </summary>
/// <returns>deserialized object</returns>
public SerializableTransformObject DeSerialize()
{
var dataBuffer = this.transmissionBuffer.ToArray();
var bin = new BinaryFormatter();
var mem = new MemoryStream();
mem.Write(dataBuffer, 0, dataBuffer.Length);
mem.Seek(0, 0);
return (SerializableTransformObject)bin.Deserialize(mem);
}
#endregion Methods
}
}