Skip to content

Commit

Permalink
[adds]
Browse files Browse the repository at this point in the history
+ Minimap Reset() method which minimaps with dynamic overlays must implement

[fixes]
+ Dynamic minimap overlay saving for area overlays (issue #163)

[changes]
+ Uses Scribe.EnterNode() properly for determining an xml nodes existance deprecating XML_Helper
  • Loading branch information
ForsakenShell committed Aug 23, 2016
1 parent 4343ac7 commit d70bd19
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 33 deletions.
2 changes: 1 addition & 1 deletion DLL_Project/Classes/Static/XML_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CommunityCoreLibrary
public static class XML_Helper
{

// TODO: Move this method to an extension class (question, under what though?)
// TODO: Remove this as Scribe.EnterNode returns a bool and does the same job
public static bool HasChildNode( this XmlNode xmlNode, string childNode )
{
var xmlEnumerator = xmlNode.ChildNodes.GetEnumerator();
Expand Down
63 changes: 36 additions & 27 deletions DLL_Project/MapComponents/MiniMapController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

using RimWorld;
Expand All @@ -27,7 +28,7 @@ public class MiniMapController : MapComponent
public static List<MiniMap> visibleMiniMaps = new List<MiniMap>();

private static string regExPattern = "\\W";
public static Regex regEx;
private static Regex regEx;

#endregion Fields

Expand Down Expand Up @@ -317,16 +318,25 @@ public static MiniMapOverlay FindOverlay( string overlayDefName )

#region Save/Load Minimap and Overlays

public static string GenSaveKey( string inputString )
{
// Remove non-valid xml tag characters from the string
var regExed = regEx.Replace( inputString, "" );
Log.Message( string.Format( "GenSaveKey( '{0}' ) = '{1}'", inputString, regExed ) );
return regExed;
}

private void ExposeDataSave()
{
bool hidden;
foreach( var minimap in Controller.Data.MiniMaps )
{

#region Minimap Header

Scribe.EnterNode( minimap.SaveKey );

if( !Scribe.EnterNode( minimap.SaveKey ) )
{
continue;
}
#endregion

hidden = minimap.Hidden;
Expand All @@ -337,27 +347,28 @@ private void ExposeDataSave()
foreach( var overlay in minimap.overlayWorkers )
{
#region Overlay Header

Scribe.EnterNode( overlay.SaveKey );

var saveKey = overlay.SaveKey;
if(
( string.IsNullOrEmpty( saveKey ) )||
( !Scribe.EnterNode( overlay.SaveKey ) )
)
{
continue;
}
#endregion

hidden = overlay.Hidden;
Scribe_Values.LookValue( ref hidden, "hidden", overlay.overlayDef.hiddenByDefault, true );

#region Finalize Overlay

Scribe.ExitNode();

#endregion
}

#endregion

#region Finalize Minimap

Scribe.ExitNode();

#endregion
}
}
Expand All @@ -367,44 +378,42 @@ private void ExposeDataLoad()
bool hidden = true; // Don't really need to set this but the compiler complains if we don't
foreach( var minimap in Controller.Data.MiniMaps )
{
if( !Scribe.curParent.HasChildNode( minimap.SaveKey ) )

#region Minimap Header
if( !Scribe.EnterNode( minimap.SaveKey ) )
{ // No saved data for this minimap
continue;
}

#region Minimap Header

Scribe.EnterNode( minimap.SaveKey );

#endregion

Scribe_Values.LookValue( ref hidden, "hidden", minimap.miniMapDef.hiddenByDefault, true );
minimap.Hidden = hidden;

if( minimap.miniMapDef.dynamicOverlays )
{ // Rebuild overlays for minimap
minimap.Reset();
}

#region Handle all MiniMap Overlays

foreach( var overlay in minimap.overlayWorkers )
{

//if( !Scribe.curParent.HasChildNode( overlay.overlayDef.defName ) )
if( !Scribe.curParent.HasChildNode( overlay.SaveKey ) )
#region Overlay Header
var saveKey = overlay.SaveKey;
if(
( string.IsNullOrEmpty( saveKey ) )||
( !Scribe.EnterNode( saveKey ) )
)
{ // No saved data for this overlay
continue;
}

#region Overlay Header

Scribe.EnterNode( overlay.SaveKey );

#endregion

Scribe_Values.LookValue( ref hidden, "hidden", overlay.overlayDef.hiddenByDefault, true );
overlay.Hidden = hidden;

#region Finalize Overlay

Scribe.ExitNode();

#endregion
}

Expand Down
4 changes: 2 additions & 2 deletions DLL_Project/MiniMapOverlays/MiniMapOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ public virtual bool Hidden
}
}

public string SaveKey
public virtual string SaveKey
{
get
{
if( minimap.miniMapDef.dynamicOverlays )
{
return MiniMapController.regEx.Replace( overlayDef.label, "" );
throw new Exception( "Dynamic minimap overlays must override SaveKey to provide a save-safe key!" );
}
return overlayDef.defName;
}
Expand Down
8 changes: 8 additions & 0 deletions DLL_Project/MiniMapOverlays/MiniMapOverlay_Area.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public override void Update()
}
}

public override string SaveKey
{
get
{
return MiniMapController.GenSaveKey( area.GetUniqueLoadID() );
}
}

#endregion Methods
}
}
6 changes: 5 additions & 1 deletion DLL_Project/MiniMaps/MiniMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public virtual Texture2D Icon
}
}

public string SaveKey
public virtual string SaveKey
{
get
{
Expand Down Expand Up @@ -218,6 +218,10 @@ public void ClearTextures( bool apply = false )
}
}

public virtual void Reset()
{
}

public virtual void Update()
{
}
Expand Down
8 changes: 7 additions & 1 deletion DLL_Project/MiniMaps/MiniMap_Areas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public MiniMap_Areas( MiniMapDef def ) : base( def ) { }

#region Methods

public override void Reset()
{
overlayWorkers.Clear();
UpdateAreaOverlays();
}

public override void Update()
{
UpdateAreaOverlays();
Expand All @@ -31,7 +37,7 @@ private void UpdateAreaOverlays()
return;

// check if we need to add area overlays
foreach ( var area in Find.AreaManager.AllAreas )
foreach( var area in Find.AreaManager.AllAreas )
{
if ( !overlayWorkers.Any( w => ((MiniMapOverlay_Area)w).area == area ) )
overlayWorkers.Add( new MiniMapOverlay_Area( this, new MiniMapOverlayDef(), area ) );
Expand Down
9 changes: 8 additions & 1 deletion DLL_Project/ModConfigurationMenus/MCM_MiniMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ public override void ExposeData()
if( iOverlay != null )
{
#region Overlay Header
Scribe.EnterNode( overlay.SaveKey );
var saveKey = overlay.SaveKey;
if(
( string.IsNullOrEmpty( saveKey ) )||
( !Scribe.EnterNode( saveKey ) )
)
{
continue;
}
#endregion

#region Handle Overlay IConfigurable
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit d70bd19

Please sign in to comment.