Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
FourCC.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 //
4 // Copyright (c) 2010-2012 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 using System;
24 using System.Runtime.InteropServices;
25 
26 using SiliconStudio.Core.Serialization;
27 using SiliconStudio.Core.Serialization.Serializers;
28 
29 namespace SiliconStudio.Paradox
30 {
31  /// <summary>
32  /// A FourCC descriptor.
33  /// </summary>
34  [DataSerializer(typeof(FourCC.Serializer))]
35  [StructLayout(LayoutKind.Sequential, Size = 4)]
36  internal struct FourCC : IEquatable<FourCC>
37  {
38  private readonly uint value;
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="FourCC" /> struct.
42  /// </summary>
43  /// <param name="fourCC">The fourCC value as a string .</param>
44  public FourCC(string fourCC)
45  {
46  if (fourCC.Length != 4)
47  throw new ArgumentException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid length for FourCC(\"{0}\". Must be be 4 characters long ", fourCC), "fourCC");
48  this.value = ((uint)fourCC[3]) << 24 | ((uint)fourCC[2]) << 16 | ((uint)fourCC[1]) << 8 | ((uint)fourCC[0]);
49  }
50 
51  /// <summary>
52  /// Initializes a new instance of the <see cref="FourCC" /> struct.
53  /// </summary>
54  /// <param name="byte1">The byte1.</param>
55  /// <param name="byte2">The byte2.</param>
56  /// <param name="byte3">The byte3.</param>
57  /// <param name="byte4">The byte4.</param>
58  public FourCC(char byte1, char byte2, char byte3, char byte4)
59  {
60  this.value = ((uint)byte4) << 24 | ((uint)byte3) << 16 | ((uint)byte2) << 8 | ((uint)byte1);
61  }
62 
63  /// <summary>
64  /// Initializes a new instance of the <see cref="FourCC" /> struct.
65  /// </summary>
66  /// <param name="fourCC">The fourCC value as an uint.</param>
67  public FourCC(uint fourCC)
68  {
69  this.value = fourCC;
70  }
71 
72  /// <summary>
73  /// Initializes a new instance of the <see cref="FourCC" /> struct.
74  /// </summary>
75  /// <param name="fourCC">The fourCC value as an int.</param>
76  public FourCC(int fourCC)
77  {
78  this.value = unchecked((uint)fourCC);
79  }
80 
81  /// <summary>
82  /// Performs an implicit conversion from <see cref="SharpDX.Multimedia.FourCC"/> to <see cref="System.Int32"/>.
83  /// </summary>
84  /// <param name="d">The d.</param>
85  /// <returns>
86  /// The result of the conversion.
87  /// </returns>
88  public static implicit operator uint(FourCC d)
89  {
90  return d.value;
91  }
92 
93  /// <summary>
94  /// Performs an implicit conversion from <see cref="SharpDX.Multimedia.FourCC"/> to <see cref="System.Int32"/>.
95  /// </summary>
96  /// <param name="d">The d.</param>
97  /// <returns>
98  /// The result of the conversion.
99  /// </returns>
100  public static implicit operator int(FourCC d)
101  {
102  return unchecked((int)d.value);
103  }
104 
105  /// <summary>
106  /// Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="SharpDX.Multimedia.FourCC"/>.
107  /// </summary>
108  /// <param name="d">The d.</param>
109  /// <returns>
110  /// The result of the conversion.
111  /// </returns>
112  public static implicit operator FourCC(uint d)
113  {
114  return new FourCC(d);
115  }
116 
117  /// <summary>
118  /// Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="SharpDX.Multimedia.FourCC"/>.
119  /// </summary>
120  /// <param name="d">The d.</param>
121  /// <returns>
122  /// The result of the conversion.
123  /// </returns>
124  public static implicit operator FourCC(int d)
125  {
126  return new FourCC(d);
127  }
128 
129  /// <summary>
130  /// Performs an implicit conversion from <see cref="SharpDX.Multimedia.FourCC"/> to <see cref="System.String"/>.
131  /// </summary>
132  /// <param name="d">The d.</param>
133  /// <returns>
134  /// The result of the conversion.
135  /// </returns>
136  public static implicit operator string(FourCC d)
137  {
138  return d.ToString();
139  }
140 
141  /// <summary>
142  /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="SharpDX.Multimedia.FourCC"/>.
143  /// </summary>
144  /// <param name="d">The d.</param>
145  /// <returns>
146  /// The result of the conversion.
147  /// </returns>
148  public static implicit operator FourCC(string d)
149  {
150  return new FourCC(d);
151  }
152 
153  public override string ToString()
154  {
155  return string.Format("{0}", new string(new[]
156  {
157  (char) (value & 0xFF),
158  (char) ((value >> 8) & 0xFF),
159  (char) ((value >> 16) & 0xFF),
160  (char) ((value >> 24) & 0xFF),
161  }));
162  }
163 
164  public bool Equals(FourCC other)
165  {
166  return value == other.value;
167  }
168 
169  public override bool Equals(object obj)
170  {
171  if (ReferenceEquals(null, obj)) return false;
172  return obj is FourCC && Equals((FourCC) obj);
173  }
174 
175  public override int GetHashCode()
176  {
177  return (int) value;
178  }
179 
180  public static bool operator ==(FourCC left, FourCC right)
181  {
182  return left.Equals(right);
183  }
184 
185  public static bool operator !=(FourCC left, FourCC right)
186  {
187  return !left.Equals(right);
188  }
189 
190  public class Serializer : DataSerializer<FourCC>
191  {
192  public override void Serialize(ref FourCC fourCC, ArchiveMode mode, SerializationStream stream)
193  {
194  if (mode == ArchiveMode.Serialize)
195  stream.Write(fourCC.value);
196  else
197  fourCC = new FourCC(stream.ReadUInt32());
198  }
199  }
200  }
201 }
override void Serialize(ref FourCC fourCC, ArchiveMode mode, SerializationStream stream)
Definition: FourCC.cs:192
Base class for implementation of SerializationStream.
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