Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ChunkReference.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8 using SiliconStudio.Core.IO;
9 using SiliconStudio.Core.Serialization.Serializers;
10 
11 namespace SiliconStudio.Core.Serialization.Contents
12 {
13  /// <summary>
14  /// Describe a reference between an object and another.
15  /// </summary>
16  /// <remarks>This class is IEquatable, and equality is true if and only if Location and ObjType properties match</remarks>
17  [DataSerializer(typeof(ChunkReference.Serializer))]
18  public struct ChunkReference : IEquatable<ChunkReference>
19  {
20  public readonly string Location;
21 
22  public readonly Type ObjectType;
23 
24  public const int NullIdentifier = -1;
25 
26  public ChunkReference(Type objectType, string location)
27  {
28  ObjectType = objectType;
29  Location = location;
30  }
31 
32  public bool Equals(ChunkReference other)
33  {
34  return string.Equals(Location, other.Location) && ObjectType == other.ObjectType;
35  }
36 
37  public override bool Equals(object obj)
38  {
39  if (ReferenceEquals(null, obj)) return false;
40  return obj is ChunkReference && Equals((ChunkReference)obj);
41  }
42 
43  public override int GetHashCode()
44  {
45  unchecked
46  {
47  return (Location.GetHashCode()*397) ^ ObjectType.GetHashCode();
48  }
49  }
50 
51  public static bool operator ==(ChunkReference left, ChunkReference right)
52  {
53  return left.Equals(right);
54  }
55 
56  public static bool operator !=(ChunkReference left, ChunkReference right)
57  {
58  return !left.Equals(right);
59  }
60 
61  internal class Serializer : DataSerializer<ChunkReference>
62  {
63  public override Type SerializationType
64  {
65  get { return typeof(ChunkReference); }
66  }
67 
68  public override void Serialize(ref ChunkReference chunkReference, ArchiveMode mode, SerializationStream stream)
69  {
70  if (mode == ArchiveMode.Serialize)
71  {
72  stream.Write(chunkReference.ObjectType.AssemblyQualifiedName);
73  stream.Write(chunkReference.Location);
74  }
75  else if (mode == ArchiveMode.Deserialize)
76  {
77  string typeName = stream.ReadString();
78  chunkReference = new ChunkReference(Type.GetType(typeName), stream.ReadString());
79  }
80  }
81  }
82  }
83 }
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.
Describes how to serialize and deserialize an object without knowing its type. Used as a common base ...
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Definition: ArchiveMode.cs:8
Describe a reference between an object and another.