コード例 #1
0
ファイル: Preset.cs プロジェクト: AzureFox4/PrepareLanding
        private XElement GetTopElement(out XDocument xDocument, bool fileMustExist)
        {
            var filePath = PresetManager.FullPresetPathFromPresetName(PresetName, fileMustExist);

            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentException($"[PrepareLanding] presetName ({PresetName}) doesn't lead to a full path.");
            }

            XElement xPreset;

            if (fileMustExist)
            {
                // load the document and check if there's a root node.
                xDocument = XDocument.Load(filePath);
                if (xDocument.Root == null)
                {
                    throw new Exception("No root node");
                }

                // get the root element
                xPreset = xDocument.Element(RootName);
                if (xPreset == null)
                {
                    throw new Exception($"No root node named '{RootName}'");
                }
            }
            else
            {
                // create document
                xDocument = new XDocument();

                // add root node
                xPreset = new XElement(RootName);
                xDocument.Add(xPreset);
            }

            return(xPreset);
        }
コード例 #2
0
ファイル: Preset.cs プロジェクト: AzureFox4/PrepareLanding
        public void SavePreset(string description = null, bool saveOptions = false)
        {
            try
            {
                var xRoot = GetTopElement(out var xDocument, false);
                if (xRoot == null)
                {
                    return;
                }

                // preset info
                PresetInfo.SavePresetInfo(xRoot);

                /*
                 * filters
                 */
                var xFilter = new XElement(FilterNode);
                xRoot.Add(xFilter);

                // Terrain
                var xTerrainFilters = new XElement(TerrainNode);
                xFilter.Add(xTerrainFilters);

                SaveDef(xTerrainFilters, "Biome", _gameData.UserData.ChosenBiome);
                SaveHilliness(xTerrainFilters, "Hilliness", _gameData.UserData.ChosenHilliness);
                SaveThreeStateItemContainer(xTerrainFilters, "Roads", "Road", _gameData.UserData.SelectedRoadDefs);
                SaveThreeStateItemContainer(xTerrainFilters, "Rivers", "River", _gameData.UserData.SelectedRiverDefs);
                SaveUsableMinMax(xTerrainFilters, "CurrentMovementTime", _gameData.UserData.CurrentMovementTime);
                SaveUsableMinMax(xTerrainFilters, "SummerMovementTime", _gameData.UserData.SummerMovementTime);
                SaveUsableMinMax(xTerrainFilters, "WinterMovementTime", _gameData.UserData.WinterMovementTime);
                if (_gameData.UserData.StoneTypesNumberOnly)
                {
                    SaveBoolean(xTerrainFilters, "StoneTypesNumberOnly", _gameData.UserData.StoneTypesNumberOnly);
                    Save(xTerrainFilters, "StoneTypesNumber", _gameData.UserData.StoneTypesNumber);
                }
                else
                {
                    SaveThreeStateItemContainerOrdered(xTerrainFilters, "Stones", "Stone",
                                                       _gameData.UserData.SelectedStoneDefs);
                }
                SaveThreeState(xTerrainFilters, "CoastalTile", _gameData.UserData.ChosenCoastalTileState);
                SaveThreeState(xTerrainFilters, "CoastalLakeTile", _gameData.UserData.ChosenCoastalLakeTileState);
                SaveUsableFromList(xTerrainFilters, "CoastalRotation", _gameData.UserData.CoastalRotation);
                SaveUsableMinMax(xTerrainFilters, "Elevation", _gameData.UserData.Elevation);
                SaveUsableMinMax(xTerrainFilters, "TimeZone", _gameData.UserData.TimeZone);

                SaveThreeState(xTerrainFilters, "HasCave", _gameData.UserData.HasCaveState);

                // Temperature
                var xTemperatureFilters = new XElement("Temperature");
                xFilter.Add(xTemperatureFilters);

                SaveUsableMinMax(xTemperatureFilters, "AverageTemperature", _gameData.UserData.AverageTemperature);
                SaveUsableMinMax(xTemperatureFilters, "SummerTemperature", _gameData.UserData.SummerTemperature);
                SaveUsableMinMax(xTemperatureFilters, "WinterTemperature", _gameData.UserData.WinterTemperature);
                SaveMinMaxFromRestrictedList(xTemperatureFilters, "GrowingPeriod", _gameData.UserData.GrowingPeriod);
                SaveUsableMinMax(xTemperatureFilters, "RainFall", _gameData.UserData.RainFall);
                SaveThreeState(xTemperatureFilters, "AnimalsCanGrazeNow", _gameData.UserData.ChosenAnimalsCanGrazeNowState);
                SaveMostLeastItem(xTemperatureFilters, "MostLeastFeature", _gameData.UserData.MostLeastItem);

                /*
                 * Options
                 */
                var xOption = new XElement(OptionNode);
                xRoot.Add(xOption);

                // save options if specifically asked for
                if (saveOptions)
                {
                    SaveBoolean(xOption, "AllowImpassableHilliness", _gameData.UserData.Options.AllowImpassableHilliness);
                    SaveBoolean(xOption, "AllowInvalidTilesForNewSettlement",
                                _gameData.UserData.Options.AllowInvalidTilesForNewSettlement);
                    SaveBoolean(xOption, "AllowLiveFiltering", _gameData.UserData.Options.AllowLiveFiltering);
                    SaveBoolean(xOption, "BypassMaxHighlightedTiles", _gameData.UserData.Options.BypassMaxHighlightedTiles);
                    SaveBoolean(xOption, "DisablePreFilterCheck", _gameData.UserData.Options.DisablePreFilterCheck);
                    SaveBoolean(xOption, "ViewPartialOffNoSelect", _gameData.UserData.Options.ViewPartialOffNoSelect);
                    SaveBoolean(xOption, "ResetAllFieldsOnNewGeneratedWorld", _gameData.UserData.Options.ResetAllFieldsOnNewGeneratedWorld);
                    SaveBoolean(xOption, "DisableTileHighlighting", _gameData.UserData.Options.DisableTileHighlighting);
                    SaveBoolean(xOption, "DisableTileBlinking", _gameData.UserData.Options.DisableTileBlinking);
                    SaveBoolean(xOption, "ShowDebugTileId", _gameData.UserData.Options.ShowDebugTileId);
                    SaveBoolean(xOption, "ShowFilterHeaviness", _gameData.UserData.Options.ShowFilterHeaviness);
                }

                // save the document
                xDocument.Save(PresetManager.FullPresetPathFromPresetName(PresetName, false));
            }
            catch (Exception e)
            {
                Log.Error($"Failed to save preset file. error:\n\t{e}\n\t{e.Message}");
                throw;
            }
        }