Skip to content

Commit

Permalink
Add save and load of map mirror tiles to json (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
drogoganor committed Mar 4, 2024
1 parent 6a407e6 commit 8cce1c2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
68 changes: 67 additions & 1 deletion OpenRA.Mods.Common/Traits/World/MirrorLayerOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
Expand Down Expand Up @@ -47,8 +49,18 @@ public override object Create(ActorInitializer init)
}
}

public class MirrorLayerOverlay : IRenderAnnotations, INotifyActorDisposing
public class MirrorLayerOverlay : IRenderAnnotations, INotifyActorDisposing, IWorldLoaded
{
public class MirrorLayerFile
{
public Dictionary<int, List<int>> Tiles { get; set; }
public MirrorTileMode MirrorMode { get; set; }
public int NumSides { get; set; }
public int AxisAngle { get; set; }
public bool ShowAxisGuide { get; set; }
public int TileAlpha { get; set; }
}

const double DegreesToRadians = Math.PI / 180;

readonly int[] validFlipModeSides = { 2, 4 };
Expand Down Expand Up @@ -101,6 +113,60 @@ public MirrorLayerOverlay(Actor self, MirrorLayerOverlayInfo info)
mapCenter = GetMapCenterWPos();
}

public void WorldLoaded(World w, WorldRenderer wr)
{
try
{
var modData = Game.ModData;
var mod = modData.Manifest.Metadata;
var directory = Path.Combine(Platform.SupportDir, "Editor", modData.Manifest.Id, mod.Version);
if (!Directory.Exists(directory))
return;

if (string.IsNullOrWhiteSpace(world.Map.Package.Name))
return;

var mirrorTileFilename = $"{Path.GetFileNameWithoutExtension(world.Map.Package.Name)}.json";
var mirrorTilePath = Path.Combine(directory, mirrorTileFilename);
if (!File.Exists(mirrorTilePath))
return;

using (var streamReader = new StreamReader(mirrorTilePath))
{
var content = streamReader.ReadToEnd();
var file = JsonConvert.DeserializeObject<MirrorLayerFile>(content);

TileAlpha = file.TileAlpha;
MirrorMode = file.MirrorMode;
NumSides = file.NumSides;
AxisAngle = file.AxisAngle;
ShowAxisGuide = file.ShowAxisGuide;

var savedTilesHashSetDictionary = file.Tiles.ToDictionary(x => x.Key, x => x.Value.Select(bits => new CPos(bits)).ToHashSet());
SetAll(savedTilesHashSetDictionary);
}
}
catch (Exception e)
{
Log.Write("debug", "Failed to load map editor mirror tiles.");
Log.Write("debug", e);
}
}

public MirrorLayerFile ToFile()
{
var tilesBitsDictionary = Tiles.ToDictionary(x => x.Key, x => x.Value.Select(cpos => cpos.Bits).ToList());
return new MirrorLayerFile
{
Tiles = tilesBitsDictionary,
TileAlpha = TileAlpha,
MirrorMode = MirrorMode,
NumSides = NumSides,
AxisAngle = AxisAngle,
ShowAxisGuide = ShowAxisGuide,
};
}

void UpdateTileAlpha()
{
for (var i = 0; i < Info.Colors.Length; i++)
Expand Down
31 changes: 31 additions & 0 deletions OpenRA.Mods.Common/Widgets/Logic/Editor/SaveMapLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using OpenRA.FileSystem;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
Expand Down Expand Up @@ -300,6 +301,8 @@ public static void SaveMap(ModData modData, World world, Map map, string combine
}

saveMap(combinedPath);

SaveMapMirrorTiles(map, modData, world);
}

public static void SaveMapInner(Map map, IReadWritePackage package, World world, ModData modData)
Expand Down Expand Up @@ -344,5 +347,33 @@ static void SaveMapFailed(Exception e, ModData modData, World world)
},
confirmText: SaveMapFailedConfirm);
}

static void SaveMapMirrorTiles(Map map, ModData modData, World world)
{
try
{
var mirrorLayerOverlay = world.WorldActor.Trait<MirrorLayerOverlay>();
if (mirrorLayerOverlay.Tiles.Count == 0)
return;

var mod = modData.Manifest.Metadata;
var directory = Path.Combine(Platform.SupportDir, "Editor", modData.Manifest.Id, mod.Version);
Directory.CreateDirectory(directory);

var mirrorTilesFile = mirrorLayerOverlay.ToFile();
var mirrorTilesContent = JsonConvert.SerializeObject(mirrorTilesFile);

var mirrorTileFilename = $"{Path.GetFileNameWithoutExtension(map.Package.Name)}.json";
using (var streamWriter = new StreamWriter(Path.Combine(directory, mirrorTileFilename), false))
{
streamWriter.Write(mirrorTilesContent);
}
}
catch (Exception e)
{
Log.Write("debug", "Failed to save map editor mirror tiles.");
Log.Write("debug", e);
}
}
}
}
4 changes: 2 additions & 2 deletions mods/common/chrome/editor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Background@SAVE_MAP_PANEL:
Y: 21
Width: 250
Height: 25
Text: label-save-map-panel-title.label
Text: label-save-map-panel-heading
Align: Center
Font: Bold
Label@TITLE_LABEL:
Expand All @@ -93,7 +93,7 @@ Background@SAVE_MAP_PANEL:
Width: 95
Height: 25
Align: Right
Text: label-save-map-panel-title.label
Text: label-save-map-panel-title
TextField@TITLE:
X: 110
Y: 60
Expand Down
6 changes: 2 additions & 4 deletions mods/common/languages/chrome/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ label-new-map-bg-width = Width:
label-new-map-bg-height = Height:
button-new-map-bg-create = Create
label-save-map-panel-title =
.label = Save Map
.label = Title:
label-save-map-panel-heading = Save Map
label-save-map-panel-title = Title:
label-save-map-panel-author = Author:
label-save-map-panel-visibility = Visibility:
dropdownbutton-save-map-panel-visibility-dropdown = Map Visibility
Expand Down

0 comments on commit 8cce1c2

Please sign in to comment.