﻿// Decompiled with JetBrains decompiler
// Type: MongoDB.Bson.Serialization.Serializers.IPEndPointSerializer
// Assembly: MongoDB.Bson, Version=1.6.1.21837, Culture=neutral, PublicKeyToken=f686731cfb9cc103
// MVID: 93880221-3735-46FB-8891-84D897158E28
// Assembly location: C:\Users\CGA Computer\Desktop\LEGO Universe Stuff\LOCO Server\Unity Web Player Extractor\Saved Files\LOCO Server.unity3d_unpacked\MongoDB.Bson.dll

using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;

namespace MongoDB.Bson.Serialization.Serializers
{
  public class IPEndPointSerializer : BsonBaseSerializer
  {
    private static IPEndPointSerializer __instance = new IPEndPointSerializer();

    public static IPEndPointSerializer Instance
    {
      get
      {
        return IPEndPointSerializer.__instance;
      }
    }

    public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
    {
      this.VerifyTypes(nominalType, actualType, typeof (IPEndPoint));
      BsonType currentBsonType = bsonReader.GetCurrentBsonType();
      switch (currentBsonType)
      {
        case BsonType.String:
          string input = bsonReader.ReadString();
          Match match = Regex.Match(input, "^(?<address>(.+|\\[.*\\]))\\:(?<port>\\d+)$");
          IPAddress address;
          int result;
          if (match.Success && IPAddress.TryParse(match.Groups["address"].Value, out address) && int.TryParse(match.Groups["port"].Value, out result))
            return (object) new IPEndPoint(address, result);
          throw new FileFormatException(string.Format("Invalid IPEndPoint value '{0}'.", (object) input));
        case BsonType.Null:
          bsonReader.ReadNull();
          return (object) null;
        default:
          throw new FileFormatException(string.Format("Cannot deserialize IPEndPoint from BsonType {0}.", (object) currentBsonType));
      }
    }

    public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
    {
      if (value == null)
      {
        bsonWriter.WriteNull();
      }
      else
      {
        IPEndPoint ipEndPoint = (IPEndPoint) value;
        string str = ipEndPoint.AddressFamily != AddressFamily.InterNetwork ? string.Format("[{0}]:{1}", (object) ipEndPoint.Address, (object) ipEndPoint.Port) : string.Format("{0}:{1}", (object) ipEndPoint.Address, (object) ipEndPoint.Port);
        bsonWriter.WriteString(str);
      }
    }
  }
}
