public static MapFile ReadFromFile(this IMapFormat mapFormat, string fileName)
 {
     using (var fo = File.OpenRead(fileName))
     {
         return(mapFormat.Read(fo));
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Loads and returns a Map instance using the given file path. The map
        /// format is determined automatically from the file extension and the
        /// map is loaded using the corresponding IMapFormat implementation if
        /// available.
        /// </summary>
        /// <param name="filePath">Path to the map file to load</param>
        /// <returns>a loaded Map instance</returns>
        public Map LoadMap(string filePath)
        {
            try
            {
                if (filePath == null)
                {
                    throw new Exception("A null file path was specified");
                }

                string fileExtension
                    = Path.GetExtension(filePath).Replace(".", "");
                if (fileExtension.Length == 0)
                {
                    throw new Exception("Cannot determine map format without a file extension");
                }

                IMapFormat mapFormat = GetMapFormatByExtension(fileExtension);
                if (mapFormat == null)
                {
                    throw new Exception("No IMapFormat implementation for files with extension '" + fileExtension + "'");
                }

                using (Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    Map map = mapFormat.Load(fileStream);
                    fileStream.Close();

                    return(map);
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Unable to load map with file path '" + filePath + "'", exception);
            }
        }
Esempio n. 3
0
 public ushort GetId(IMapFormat inst)
 {
     if (inst == null)
     {
         return(0);
     }
     return(inst.Identifier);
 }
Esempio n. 4
0
 public string GetName(IMapFormat inst)
 {
     if (inst == null)
     {
         return(null);
     }
     return(inst.Name);
 }
Esempio n. 5
0
 public MANAGER_ERROR AddInstance(IMapFormat instance)
 {
     if (instance == null)
     {
         return(MANAGER_ERROR.E_FAULT);
     }
     return(AddInstance(instance.GetType()));
 }
Esempio n. 6
0
        /// <summary>
        /// Registers the given map format
        /// </summary>
        /// <param name="mapFormat">Map format implementation to register</param>
        public void RegisterMapFormat(IMapFormat mapFormat)
        {
            if (m_mapFormats.ContainsKey(mapFormat.Name))
            {
                throw new Exception("Map format '" + mapFormat.Name + "' is already registered");
            }

            m_mapFormats[mapFormat.Name] = mapFormat;
        }
Esempio n. 7
0
        /// <summary>
        /// Unregisters the given map format
        /// </summary>
        /// <param name="mapFormat">Map format implementation to unregister</param>
        public void UnregisterMapFormat(IMapFormat mapFormat)
        {
            if (!m_mapFormats.ContainsKey(mapFormat.Name))
            {
                throw new Exception("Map format '" + mapFormat.Name + "' is is not registered");
            }

            m_mapFormats.Remove(mapFormat.Name);
        }
Esempio n. 8
0
        public ushort GetId(Type inst)
        {
            if (inst == null)
            {
                return(0);
            }
            IMapFormat instance = (IMapFormat)Activator.CreateInstance(inst);

            return(GetId(instance));
        }
Esempio n. 9
0
        public string GetName(Type inst)
        {
            if (inst == null)
            {
                return(null);
            }
            if (implementations.Contains(inst))
            {
                if (names.Count > implementations.IndexOf(inst))
                {
                    return(names[implementations.IndexOf(inst)]);
                }
            }
            IMapFormat instance = (IMapFormat)Activator.CreateInstance(inst);

            return(GetName(instance));
        }
Esempio n. 10
0
        public Map(Stream input, uint owVersion, bool leaveOpen = false)
        {
            if (input == null)
            {
                return;
            }
            using (BinaryReader reader = new BinaryReader(input, Encoding.Default, leaveOpen)) {
                Header         = reader.Read <MapHeader>();
                input.Position = Header.offset;
                Records        = new IMapFormat[Header.recordCount];
                CommonHeaders  = new MapCommonHeader[Header.recordCount];
                for (uint i = 0; i < Header.recordCount; ++i)
                {
                    try {
                        CommonHeaders[i] = reader.Read <MapCommonHeader>();
                        long before = reader.BaseStream.Position;
                        long nps    = input.Position + CommonHeaders[i].size - 24;
                        if (Manager.InitializeInstance(CommonHeaders[i].type, input, out Records[i]) !=
                            MANAGER_ERROR.E_SUCCESS)
                        {
                            if (Debugger.IsAttached)
                            {
                                Debugger.Log(0, "STULib.Types.Map", $"[STULib.Types.Map.Map]: Error reading Map type {CommonHeaders[i].type:X} (offset: {before})\n");
                            }
                        }
                        input.Position = nps;
                    }
                    catch (OutOfMemoryException) {
                        CommonHeaders = null;
                        Records       = null;
                        return;
                    }
                    catch (ArgumentOutOfRangeException) {
                        CommonHeaders = null;
                        Records       = null;
                        return;
                    }
                }

                if (Records.Length > 0 && Records[0] != null && Records[0].HasSTUD)
                {
                    AlignPosition(input, input.Position);
                    while (true)
                    {
                        if (input.Position >= input.Length)
                        {
                            break;
                        }
                        ISTU tmp;
                        tmp = ISTU.NewInstance(input, owVersion);

                        AlignPositionNew(reader, tmp as Version1);
                        STUInstances = new HashSet <uint>(STUInstances.Concat(tmp.TypeHashes).ToList());
                        STUs.Add(tmp);
                    }
                }

                int stuIndex = 0;
                foreach (IMapFormat record in Records)
                {
                    if (record?.GetType() != typeof(MapEntity))
                    {
                        continue;
                    }

                    MapEntity mapEntity = (MapEntity)record;
                    mapEntity.STUContainers = new List <object>();
                    foreach (MapEntity.MapEntitySTUBinding binding in mapEntity.STUBindings)
                    {
                        mapEntity.STUContainers.Add(STUs[stuIndex]);
                        stuIndex++;
                    }
                }
            }
        }
Esempio n. 11
0
 public MANAGER_ERROR AddInstance(IMapFormat instance)
 {
     return(instance == null ? MANAGER_ERROR.E_FAULT : AddInstance(instance.GetType()));
 }
Esempio n. 12
0
 public MANAGER_ERROR InitializeInstance(ushort id, Stream input, out IMapFormat instance)
 {
     return(InitializeInstance(GetInstance(id), input, out instance));
 }
Esempio n. 13
0
 public static void Register(IMapFormat loader)
 {
     _formats.Add(loader);
 }
Esempio n. 14
0
        /// <summary>
        /// Unregisters the given map format
        /// </summary>
        /// <param name="mapFormat">Map format implementation to unregister</param>
        public void UnregisterMapFormat(IMapFormat mapFormat)
        {
            if (!m_mapFormats.ContainsKey(mapFormat.Name))
                throw new Exception("Map format '" + mapFormat.Name + "' is is not registered");

            m_mapFormats.Remove(mapFormat.Name);
        }
Esempio n. 15
0
        /// <summary>
        /// Registers the given map format
        /// </summary>
        /// <param name="mapFormat">Map format implementation to register</param>
        public void RegisterMapFormat(IMapFormat mapFormat)
        {
            if (m_mapFormats.ContainsKey(mapFormat.Name))
                throw new Exception("Map format '" + mapFormat.Name+ "' is already registered");

            m_mapFormats[mapFormat.Name] = mapFormat;
        }