public void saveDirtyMaterial(TerrainMaterial mat)
        {
            // Skip over obviously bad cases.
            if (!mat.isObject())
                return;

            GuiTextEditCtrl matNameCtrl = this.findObjectByInternalName("matNameCtrl", true);
            GuiBitmapCtrl baseTexCtrl = this.findObjectByInternalName("baseTexCtrl", true);
            GuiBitmapCtrl detailTexCtrl = this.findObjectByInternalName("detailTexCtrl", true);
            GuiBitmapCtrl macroTexCtrl = this.findObjectByInternalName("macroTexCtrl", true);
            GuiBitmapCtrl normTexCtrl = this.findObjectByInternalName("normTexCtrl", true);
            GuiTextEditCtrl detSizeCtrl = this.findObjectByInternalName("detSizeCtrl", true);
            GuiTextEditCtrl baseSizeCtrl = this.findObjectByInternalName("baseSizeCtrl", true);
            GuiTextEditCtrl detStrengthCtrl = this.findObjectByInternalName("detStrengthCtrl", true);
            GuiTextEditCtrl detDistanceCtrl = this.findObjectByInternalName("detDistanceCtrl", true);
            GuiCheckBoxCtrl sideProjectionCtrl = this.findObjectByInternalName("sideProjectionCtrl", true);
            GuiTextEditCtrl parallaxScaleCtrl = this.findObjectByInternalName("parallaxScaleCtrl", true);

            GuiTextEditCtrl macroSizeCtrl = this.findObjectByInternalName("macroSizeCtrl", true);
            GuiTextEditCtrl macroStrengthCtrl = this.findObjectByInternalName("macroStrengthCtrl", true);
            GuiTextEditCtrl macroDistanceCtrl = this.findObjectByInternalName("macroDistanceCtrl", true);

            //TerrainPainterAddition
            GuiTextEditCtrl terrainFolderCtrl = FOT("terrainFolderCtrl");

            // Read out properties from the dialog.
            string newDiffuse = "";
            string newNormal = "";
            string newDetail = "";
            string newMacro = "";

            string newName = matNameCtrl.getText();

            if (baseTexCtrl.bitmap != "tools/materialeditor/gui/unknownImage")
                newDiffuse = baseTexCtrl.bitmap;

            if (normTexCtrl.bitmap != "tools/materialeditor/gui/unknownImage")
                newNormal = normTexCtrl.bitmap;

            if (detailTexCtrl.bitmap != "tools/materialeditor/gui/unknownImage")
                newDetail = detailTexCtrl.bitmap;

            if (macroTexCtrl.bitmap != "tools/materialeditor/gui/unknownImage")
                newMacro = macroTexCtrl.bitmap;

            string detailSize = detSizeCtrl.getText();
            string diffuseSize = baseSizeCtrl.getText();
            string detailStrength = detStrengthCtrl.getText();
            string detailDistance = detDistanceCtrl.getText();
            string useSideProjection = sideProjectionCtrl.getValue();
            string parallaxScale = parallaxScaleCtrl.getText();

            string macroSize = macroSizeCtrl.getText();
            string macroStrength = macroStrengthCtrl.getText();
            string macroDistance = macroDistanceCtrl.getText();

            string terrainFolder = terrainFolderCtrl.getText();

            if (mat.internalName == newName && mat.diffuseMap == newDiffuse && mat.normalMap == newNormal && mat.detailMap == newDetail && mat["macroMap"] == newMacro && mat["detailSize"] == detailSize && mat["diffuseSize"] == diffuseSize && mat["detailStrength"] == detailStrength && mat["detailDistance"] == detailDistance && mat["useSideProjection"] == useSideProjection && mat["macroSize"] == macroSize && mat["macroStrength"] == macroStrength && mat["macroDistance"] == macroDistance && mat.parallaxScale.AsString() == parallaxScale && mat["terrainFolder"] == terrainFolder)
                return;

            // Make sure the material name is unique.
            if (mat.internalName != newName)
                {
                TerrainMaterial existingMat = ((SimSet) "TerrainMaterialSet").findObjectByInternalName(newName, true);
                if (existingMat.isObject())
                    {
                    messageBox.MessageBoxOK("Error", "There already is a terrain material called '" + newName + "'.");
                    // Reset the name edit control to the old name.
                    matNameCtrl.setText(mat.internalName);
                    }
                else
                    mat.setInternalName(newName);
                }
            mat["diffuseMap"] = newDiffuse;
            mat["normalMap"] = newNormal;
            mat["detailMap"] = newDetail;
            mat["macroMap"] = newMacro;
            mat["detailSize"] = detailSize;
            mat["diffuseSize"] = diffuseSize;
            mat["detailStrength"] = detailStrength;
            mat["detailDistance"] = detailDistance;
            mat["macroSize"] = macroSize;
            mat["macroStrength"] = macroStrength;
            mat["macroDistance"] = macroDistance;
            mat["useSideProjection"] = useSideProjection;
            mat["parallaxScale"] = parallaxScale;

            //TerrainPainterAddition
            mat["terrainFolder"] = terrainFolder;

            // Mark the material as dirty and needing saving.

            string fileName = mat.getFilename();
            if (fileName == "")
                fileName = "art/terrains/materials.cs";

            PersistenceManager ETerrainMaterialPersistMan = "ETerrainMaterialPersistMan";
            ETerrainMaterialPersistMan.setDirty(mat, fileName);
        }