Skip to content

Commit

Permalink
Merge branch 'develop' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Dec 21, 2020
2 parents 7ce929a + db456ad commit 8150e14
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
31 changes: 30 additions & 1 deletion StardewXnbHack/Framework/Writers/MapWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using StardewModdingAPI.Toolkit.Utilities;
Expand All @@ -6,6 +7,7 @@
using xTile;
using xTile.Dimensions;
using xTile.Layers;
using xTile.Tiles;

namespace StardewXnbHack.Framework.Writers
{
Expand Down Expand Up @@ -58,16 +60,43 @@ public override bool TryWriteFile(object asset, string toPathWithoutExtension, s
layer.TileSize = new Size(MapWriter.TileSize, MapWriter.TileSize);
}

// fix image sources (game overrides them in-memory)
IDictionary<TileSheet, string> imageSources = new Dictionary<TileSheet, string>();
foreach (var sheet in map.TileSheets)
{
imageSources[sheet] = sheet.ImageSource;
sheet.ImageSource = this.GetOriginalImageSource(relativePath, sheet.ImageSource);
}

// save file
using (Stream stream = File.Create($"{toPathWithoutExtension}.tmx"))
this.Format.Store(map, stream, DataEncodingType.CSV);

// undo tile size changes
// undo changes
foreach (var layer in map.Layers)
layer.TileSize = tileSizes[layer];
foreach (var sheet in map.TileSheets)
sheet.ImageSource = imageSources[sheet];

error = null;
return true;
}


/*********
** Public methods
*********/
/// <summary>Get the image source for a map tilesheet without the game's automatic path changes.</summary>
/// <param name="relativeMapPath">The relative path to the map file within the content folder.</param>
/// <param name="imageSource">The tilesheet image source.</param>
private string GetOriginalImageSource(string relativeMapPath, string imageSource)
{
string mapDirPath = PathUtilities.NormalizePath(Path.GetDirectoryName(relativeMapPath));
string normalizedImageSource = PathUtilities.NormalizePath(imageSource);

return normalizedImageSource.StartsWith($"{mapDirPath}{PathUtilities.PreferredPathSeparator}", StringComparison.OrdinalIgnoreCase)
? imageSource.Substring(mapDirPath.Length + 1)
: imageSource;
}
}
}
10 changes: 5 additions & 5 deletions StardewXnbHack/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal static void Main()
/// <param name="gamePath">The absolute path to the game folder, or <c>null</c> to auto-detect it.</param>
/// <param name="getLogger">Get a custom progress update logger, or <c>null</c> to use the default console logging. Receives the unpack context and default logger as arguments.</param>
/// <param name="showPressAnyKeyToExit">Whether the default logger should show a 'press any key to exit' prompt when it finishes.</param>
public static void Run(Game1 game = null, string gamePath = null, Func<IUnpackContext, IProgressLogger, IProgressLogger> getLogger = null, bool showPressAnyKeyToExit = true)
public static void Run(GameRunner game = null, string gamePath = null, Func<IUnpackContext, IProgressLogger, IProgressLogger> getLogger = null, bool showPressAnyKeyToExit = true)
{
// init logging
UnpackContext context = new UnpackContext();
Expand Down Expand Up @@ -251,20 +251,20 @@ private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEven
/// <summary>Create a temporary game instance for the unpacker.</summary>
/// <param name="platform">The platform-specific context.</param>
/// <param name="contentPath">The absolute path to the content folder to import.</param>
private static Game1 CreateTemporaryGameInstance(PlatformContext platform, string contentPath)
private static GameRunner CreateTemporaryGameInstance(PlatformContext platform, string contentPath)
{
var foregroundColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkGray;

try
{

Game1 game = new Game1();
GameRunner game = new GameRunner();
GameRunner.instance = game;

if (platform.Is(Platform.Windows))
{
game.Content.RootDirectory = contentPath;
MethodInfo startGameLoop = typeof(Game1).GetMethod("StartGameLoop", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
MethodInfo startGameLoop = typeof(GameRunner).GetMethod("StartGameLoop", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
if (startGameLoop == null)
throw new InvalidOperationException("Can't locate game method 'StartGameLoop' to initialise internal game instance.");
startGameLoop.Invoke(game, new object[0]);
Expand Down
2 changes: 1 addition & 1 deletion StardewXnbHack/StardewXnbHack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/Pathoschild/StardewXnbHack</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>1.0.2</Version>
<Version>1.0.3</Version>

<!--build-->
<TargetFramework>net452</TargetFramework>
Expand Down
5 changes: 5 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[← back to readme](README.md)

# Release notes
## 1.0.3
Released 21 December 2020.

* Updated for Stardew Valley 1.5.

## 1.0.2
Released 07 December 2020.

Expand Down

0 comments on commit 8150e14

Please sign in to comment.