Ejemplo n.º 1
0
        public clsResult Load(string path)
        {
            var returnResult = new clsResult(string.Format("Loading FMap from \"{0}\"", path), false);
            logger.Info(string.Format("Loading FMap from \"{0}\"", path));

            using ( var zip = ZipFile.Read(path) )
            {
                /*
                 * Info.ini loading
                 */
                var infoIniEntry = zip["info.ini"]; // Case insensetive.
                if ( infoIniEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"info.ini\".");
                    return returnResult;
                }

                FMapInfo resultInfo = null;
                using (var reader = new StreamReader(infoIniEntry.OpenReader()))
                {
                    var text = reader.ReadToEnd ();
                    resultInfo = new FMapInfo ();
                    returnResult.Add(read_FMap_Info(text, ref resultInfo));
                    if (returnResult.HasProblems)
                    {
                        return returnResult;
                    }

                    var newTerrainSize = resultInfo.TerrainSize;
                    map.Tileset = resultInfo.Tileset;

                    if ( newTerrainSize.X <= 0 | newTerrainSize.X > Constants.MapMaxSize )
                    {
                        returnResult.ProblemAdd(string.Format("Map width of {0} is not valid.", newTerrainSize.X));
                    }
                    if ( newTerrainSize.Y <= 0 | newTerrainSize.Y > Constants.MapMaxSize )
                    {
                        returnResult.ProblemAdd(string.Format("Map height of {0} is not valid.", newTerrainSize.Y));
                    }
                    if ( returnResult.HasProblems )
                    {
                        return returnResult;
                    }

                    map.SetPainterToDefaults(); //depends on tileset. must be called before loading the terrains.
                    map.TerrainBlank(newTerrainSize);
                    map.TileType_Reset();
                }

                // vertexheight.dat
                var vhEntry = zip["vertexheight.dat"]; // Case insensetive.
                if ( vhEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"vertexheight.dat\".");
                }
                else
                {
                    using ( Stream s = vhEntry.OpenReader() )
                    {
                        var reader = new BinaryReader(s);
                        returnResult.Add(read_FMap_VertexHeight(reader));
                        reader.Close();
                    }
                }

                // vertexterrain.dat
                var vtEntry = zip["vertexterrain.dat"]; // Case insensetive.
                if ( vtEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"vertexterrain.dat\".");
                }
                else
                {
                    using ( Stream s = vtEntry.OpenReader() )
                    {
                        var reader = new BinaryReader(s);
                        returnResult.Add(read_FMap_VertexTerrain(reader));
                        reader.Close();
                    }
                }

                // tiletexture.dat
                var ttEntry = zip["tiletexture.dat"]; // Case insensetive.
                if ( vtEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"tiletexture.dat\".");
                }
                else
                {
                    using ( Stream s = ttEntry.OpenReader() )
                    {
                        var reader = new BinaryReader(s);
                        returnResult.Add(read_FMap_TileTexture(reader));
                        reader.Close();
                    }
                }

                // tileorientation.dat
                var toEntry = zip["tileorientation.dat"]; // Case insensetive.
                if ( toEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"tileorientation.dat\".");
                }
                else
                {
                    using ( Stream s = toEntry.OpenReader() )
                    {
                        var reader = new BinaryReader(s);
                        returnResult.Add(read_FMap_TileOrientation(reader));
                        reader.Close();
                    }
                }

                // tilecliff.dat
                var tcEntry = zip["tilecliff.dat"]; // Case insensetive.
                if ( tcEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"tilecliff.dat\".");
                }
                else
                {
                    using ( Stream s = tcEntry.OpenReader() )
                    {
                        var reader = new BinaryReader(s);
                        returnResult.Add(read_FMap_TileCliff(reader));
                        reader.Close();
                    }
                }

                // roads.dat
                var roEntry = zip["roads.dat"]; // Case insensetive.
                if ( roEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"roads.dat\".");
                }
                else
                {
                    using ( Stream s = roEntry.OpenReader() )
                    {
                        var reader = new BinaryReader(s);
                        returnResult.Add(read_FMap_Roads(reader));
                        reader.Close();
                    }
                }

                // objects.ini
                var obEntry = zip["objects.ini"]; // Case insensetive.
                if ( obEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"objects.ini\".");
                }
                else
                {
                    using ( var reader = new StreamReader(obEntry.OpenReader()) )
                    {
                        var text = reader.ReadToEnd();
                        returnResult.Add(read_FMap_Objects(text));
                    }
                }

                // gateways.ini
                var gaEntry = zip["gateways.ini"]; // Case insensetive.
                if ( gaEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"gateways.ini\".");
                    return returnResult;
                }
                using ( var reader = new StreamReader(gaEntry.OpenReader()) )
                {
                    var text = reader.ReadToEnd();
                    returnResult.Add(read_FMap_Gateways(text));
                }

                // tiletypes.dat
                var tileTypesEntry = zip["tiletypes.dat"]; // Case insensetive.
                if ( tileTypesEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"tiletypes.dat\".");
                }
                else
                {
                    using ( Stream s = tileTypesEntry.OpenReader() )
                    {
                        var reader = new BinaryReader(s);
                        returnResult.Add(read_FMap_TileTypes(reader));
                        reader.Close();
                    }
                }

                // scriptlabels.ini
                var scriptLabelsEntry = zip["scriptlabels.ini"]; // Case insensetive.
                if ( scriptLabelsEntry == null )
                {
                    returnResult.ProblemAdd("Unable to find file \"scriptlabels.ini\".");
                    return returnResult;
                }
                if ( scriptLabelsEntry != null )
                {
                    using ( var reader = new StreamReader(scriptLabelsEntry.OpenReader()) )
                    {
                        var text = reader.ReadToEnd();
                        returnResult.Add(read_INI_Labels(text));
                    }
                }

                map.InterfaceOptions = resultInfo.InterfaceOptions;
            }

            return returnResult;
        }
Ejemplo n.º 2
0
        private clsResult read_FMap_Info(string text, ref FMapInfo resultInfo)
        {
            var returnResult = new clsResult("Read general map info", false);
            logger.Info("Read general map info");

            var infoIni = IniReader.ReadString (text);

            var invalid = true;
            foreach (var iniSection in infoIni) {
                invalid = false;

                foreach (var iniToken in iniSection.Data) {
                    if (invalid) {
                        break;
                    }

                    try {
                        switch (iniToken.Name.ToLower()) {
                        case "tileset":
                            switch (iniToken.Data.ToLower()) {
                            case "arizona":
                                resultInfo.Tileset = App.Tileset_Arizona;
                                break;
                            case "urban":
                                resultInfo.Tileset = App.Tileset_Urban;
                                break;
                            case "rockies":
                                resultInfo.Tileset = App.Tileset_Rockies;
                                break;
                            default:
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            break;
                        case "size":
                            resultInfo.TerrainSize = XYInt.FromString(iniToken.Data);
                            if (resultInfo.TerrainSize.X < 1 || resultInfo.TerrainSize.Y < 1 || resultInfo.TerrainSize.X > Constants.MapMaxSize || resultInfo.TerrainSize.Y > Constants.MapMaxSize) {
                                invalid = true;
                                returnResult.WarningAdd (string.Format ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data));
                                logger.Warn ("#{0} {1} \"{2}\" is not valid.", iniSection.Name, iniToken.Name, iniToken.Data);
                                continue;
                            }
                            break;
                        case "autoscrolllimits":
                            resultInfo.InterfaceOptions.AutoScrollLimits = bool.Parse(iniToken.Data);
                            break;
                        case "scrollminx":
                            resultInfo.InterfaceOptions.ScrollMin.X = int.Parse(iniToken.Data);
                            break;
                        case "scrollminy":
                            resultInfo.InterfaceOptions.ScrollMin.Y = int.Parse(iniToken.Data);
                            break;
                        case "scrollmaxx":
                            resultInfo.InterfaceOptions.ScrollMax.X = uint.Parse(iniToken.Data);
                            break;
                        case "scrollmaxy":
                            resultInfo.InterfaceOptions.ScrollMax.Y = uint.Parse(iniToken.Data);
                            break;
                        case "name":
                            resultInfo.InterfaceOptions.CompileName = iniToken.Data;
                            break;
                        case "players":
                            resultInfo.InterfaceOptions.CompileMultiAuthor = iniToken.Data;
                            break;
                        case "xplayerlev":
                            // ignored.
                            break;
                        case "author":
                            resultInfo.InterfaceOptions.CompileMultiAuthor = iniToken.Data;
                            break;
                        case "license":
                            resultInfo.InterfaceOptions.CompileMultiLicense = iniToken.Data;
                            break;
                        case "camptime":
                            // ignore
                            break;
                        case "camptype":
                            resultInfo.InterfaceOptions.CampaignGameType = int.Parse(iniToken.Data);
                            break;
                        }
                    }
                    catch (Exception ex) {
                        invalid = true;
                        returnResult.WarningAdd(
                            string.Format("#{0} invalid {2}: \"{3}\", got exception: {2}", iniSection.Name, iniToken.Name, iniToken.Data, ex.Message), false);
                        logger.WarnException(string.Format("#{0} invalid {2} \"{1}\"", iniSection.Name, iniToken.Name, iniToken.Data), ex);
                    }
                }
            }

            if ( resultInfo.TerrainSize.X < 0 | resultInfo.TerrainSize.Y < 0 )
            {
                returnResult.ProblemAdd("Map size was not specified or was invalid.");
            }

            return returnResult;
        }