Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Yafc.Model.Tests/Blueprints/BlueprintUtilitiesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Text.Json;
using Xunit;
using Yafc.Blueprints;

namespace Yafc.Model.Tests.Blueprints;

public class BlueprintUtilitiesTests {
[Fact]
public void ExportConstantCombinators_WithEmptyGoods_ReturnsEmptyBlueprint() {
string blueprintString = BlueprintUtilities.ExportConstantCombinators(
"empty",
Array.Empty<(IObjectWithQuality<Goods> item, int amount)>());

AssertEmptyBlueprint(blueprintString, "empty");
}

[Fact]
public void ExportRequesterChests_WithEmptyGoods_ReturnsEmptyBlueprint() {
EntityContainer chest = new() {
logisticSlotsCount = 10,
};

string blueprintString = BlueprintUtilities.ExportRequesterChests(
"empty",
Array.Empty<(IObjectWithQuality<Item> item, int amount)>(),
chest);

AssertEmptyBlueprint(blueprintString, "empty");
}

private static void AssertEmptyBlueprint(string blueprintString, string name) {
Assert.StartsWith("0", blueprintString);
using JsonDocument document = JsonDocument.Parse(DecodeBlueprintString(blueprintString));
JsonElement blueprint = document.RootElement.GetProperty("blueprint");

Assert.Equal("blueprint", blueprint.GetProperty("item").GetString());
Assert.Equal(name, blueprint.GetProperty("label").GetString());
Assert.Empty(blueprint.GetProperty("entities").EnumerateArray());
}

private static string DecodeBlueprintString(string blueprintString) {
byte[] compressed = Convert.FromBase64String(blueprintString[1..]);
using MemoryStream input = new MemoryStream(compressed);
using ZLibStream zlib = new ZLibStream(input, CompressionMode.Decompress);
using MemoryStream output = new MemoryStream();
zlib.CopyTo(output);
return Encoding.UTF8.GetString(output.ToArray());
}
}
11 changes: 11 additions & 0 deletions Yafc.Model/Blueprints/BlueprintUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace Yafc.Blueprints;

public static class BlueprintUtilities {
public static string ExportConstantCombinators(string name, IReadOnlyList<(IObjectWithQuality<Goods> item, int amount)> goods) {
if (goods.Count == 0) {
return ExportEmptyBlueprint(name);
}

int combinatorCount = ((goods.Count - 1) / Database.constantCombinatorCapacity) + 1;
int offset = -combinatorCount / 2;
BlueprintString blueprint = new BlueprintString(name);
Expand Down Expand Up @@ -45,6 +49,10 @@ public static string ExportRequesterChests(string name, IReadOnlyList<(IObjectWi
throw new ArgumentException("Chest does not have logistic slots");
}

if (goods.Count == 0) {
return ExportEmptyBlueprint(name);
}

int combinatorCount = ((goods.Count - 1) / chest.logisticSlotsCount) + 1;
int offset = -chest.size * combinatorCount / 2;
BlueprintString blueprint = new BlueprintString(name);
Expand Down Expand Up @@ -76,6 +84,9 @@ public static string ExportRequesterChests(string name, IReadOnlyList<(IObjectWi
return blueprint.ToBpString();
}

private static string ExportEmptyBlueprint(string name)
=> new BlueprintString(name).ToBpString();

private class PlacedEntity {
public RecipeRow Recipe { get; }
public int X { get; } // Top-left X coordinate
Expand Down
2 changes: 1 addition & 1 deletion Yafc/Windows/ShoppingListScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public override void Build(ImGui gui) {
Decompose();
}

if (gui.BuildButton(LSs.ShoppingListExportBlueprint, SchemeColor.Grey)) {
if (gui.BuildButton(LSs.ShoppingListExportBlueprint, SchemeColor.Grey, active: list.data.Count > 0)) {
gui.ShowDropDown(ExportBlueprintDropdown);
}
}
Expand Down
Loading