Skip to content
Draft
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ obj/
desktop.ini
Build/
*DotSettings
*DotSettings.user
*DotSettings.user
.vs
2 changes: 2 additions & 0 deletions YAFC/Windows/PreferencesScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public override void Build(ImGui gui)
Close();
if (prefs.justChanged)
MainScreen.Instance.RebuildProjectView();
if (settings.justChanged)
Project.current.RecalculateDisplayPages();
}

private void ChoiceObject<T>(ImGui gui, string text, T[] list, T current, Action<T> select) where T:FactorioObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public override void Build(ImGui gui)

if (gui.BuildButton("Done"))
Close();
if (Project.current.settings.justChanged)
Project.current.RecalculateDisplayPages();
}
}
}
14 changes: 14 additions & 0 deletions YAFC/Workspace/ProductionTable/ProductionTableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ public override void BuildMenu(ImGui gui)
SelectObjectPanel.Select(Database.recipes.all, "Select raw recipe", r => view.AddRecipe(view.model, r));
}

if (gui.BuildButton("Remove unused recipes") && gui.CloseDropdown())
{
view.model.RecordUndo().RemoveUnusedRecipes(false);
view.Rebuild();
}

var padding = ImGuiUtils.DefaultButtonPadding;
padding = new Padding(padding.left + 1, padding.right, padding.top, padding.bottom);
if (gui.BuildButton("... and unpack empty tables", indentLevel: 1) && gui.CloseDropdown())
{
view.model.RecordUndo().RemoveUnusedRecipes(true);
view.Rebuild();
}

gui.BuildText("Export inputs and outputs to blueprint with constant combinators:", wrap: true);
using (gui.EnterRow())
{
Expand Down
35 changes: 35 additions & 0 deletions YAFCmodel/Model/ProductionTable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Google.OrTools.LinearSolver;
Expand Down Expand Up @@ -228,6 +229,40 @@ private void CalculateFlow(RecipeRow include)
flow = flowArr;
}

public void RemoveUnusedRecipes(bool unpackToo)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried spamming calls to RecordUndo in this method, but that sent the undo system into a stack overflow loop, both with and without unpacking.

{
for (int i = recipes.Count - 1; i >= 0; i--)
if (recipes[i].subgroup != null)
{
recipes[i].subgroup.RemoveUnusedRecipes(unpackToo);
if (recipes[i].recipesPerSecond == 0 && recipes[i].subgroup.recipes.Count == 0)
recipes.Remove(recipes[i]);
else if (recipes[i].subgroup.recipes.Count == 0 && unpackToo)
recipes[i].subgroup = null;
else if (recipes[i].recipesPerSecond == 0)
// This table header needs to be deleted, without deleting its children.
if (recipes[i].subgroup.recipes.FirstOrDefault(r => r.subgroup == null || r.subgroup.recipes.Count == 0) is RecipeRow newParent)
{
// Promote the first child with no children
recipes[i].subgroup.recipes.Remove(newParent);
newParent.subgroup = recipes[i].subgroup;
recipes[i] = newParent;
}
else
{
// or just the first child, if necessary
newParent = recipes[i].subgroup.recipes[0];
recipes[i].subgroup.recipes.Remove(newParent);
newParent.subgroup.recipes.AddRange(recipes[i].subgroup.recipes);
newParent.subgroup.links.AddRange(recipes[i].subgroup.links);
newParent.subgroup.RebuildLinkMap();
recipes[i] = newParent;
}
}
else if (recipes[i].recipesPerSecond == 0)
recipes.Remove(recipes[i]);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AddLinkCoef(Constraint cst, Variable var, ProductionLink link, RecipeRow recipe, float amount)
{
Expand Down
6 changes: 6 additions & 0 deletions YAFCmodel/Model/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public void Save(string fileName)
lastSavedVersion = projectVersion;
}

public void RecalculateDisplayPages()
{
foreach (var page in displayPages)
FindPage(page)?.SetToRecalculate();
}

public (float multiplier, string suffix) ResolveUnitOfMeasure(UnitOfMeasure unit)
{
switch (unit)
Expand Down
9 changes: 6 additions & 3 deletions YAFCparser/FactorioDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ public static Project Parse(string factorioPath, string modPath, string projectP
var modSettingsPath = Path.Combine(modPath, "mod-settings.dat");
progress.Report(("Initializing", "Loading mod list"));
var modListPath = Path.Combine(modPath, "mod-list.json");
Dictionary<string, Version> versionSpecifiers = new Dictionary<string, Version>();
if (File.Exists(modListPath))
{
var mods = JsonSerializer.Deserialize<ModList>(File.ReadAllText(modListPath));
allMods = mods.mods.Where(x => x.enabled).Select(x => x.name).ToDictionary(x => x, x => (ModInfo) null);
versionSpecifiers = mods.mods.Where(x => x.enabled && !string.IsNullOrEmpty(x.version)).ToDictionary(x => x.name, x => Version.Parse(x.version));
}
else
allMods = new Dictionary<string, ModInfo> {{"base", null}};
Expand Down Expand Up @@ -168,7 +170,7 @@ public static Project Parse(string factorioPath, string modPath, string projectP
foreach (var mod in allFoundMods)
{
currentLoadingMod = mod.name;
if (mod.ValidForFactorioVersion(factorioVersion) && (allMods.TryGetValue(mod.name, out var existing) && (existing == null || mod.parsedVersion > existing.parsedVersion || (mod.parsedVersion == existing.parsedVersion && existing.zipArchive != null && mod.zipArchive == null))))
if (mod.ValidForFactorioVersion(factorioVersion) && allMods.TryGetValue(mod.name, out var existing) && (existing == null || mod.parsedVersion > existing.parsedVersion || (mod.parsedVersion == existing.parsedVersion && existing.zipArchive != null && mod.zipArchive == null)) && (!versionSpecifiers.TryGetValue(mod.name, out var version) || mod.parsedVersion == version))
{
existing?.Dispose();
allMods[mod.name] = mod;
Expand All @@ -182,8 +184,8 @@ public static Project Parse(string factorioPath, string modPath, string projectP
throw new NotSupportedException("Mod not found: "+name+". Try loading this pack in Factorio first.");
mod.ParseDependencies();
}


var modsToDisable = new List<string>();
do
{
Expand Down Expand Up @@ -304,6 +306,7 @@ internal class ModEntry
{
public string name { get; set; }
public bool enabled { get; set; }
public string version { get; set; }
}

internal class ModList
Expand Down
12 changes: 8 additions & 4 deletions YAFCui/ImGui/ImGuiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@ public static bool OnClick(this ImGui gui, Rect rect)
return false;
}

public static bool BuildButton(this ImGui gui, string text, SchemeColor color = SchemeColor.Primary, Padding? padding = null, bool active = true)
public static bool BuildButton(this ImGui gui, string text, SchemeColor color = SchemeColor.Primary, Padding? padding = null, bool active = true, int indentLevel = 0)
{
if (!active)
color = SchemeColor.Grey;
using (gui.EnterGroup(padding ?? DefaultButtonPadding, active ? color+2 : color+3))
gui.BuildText(text, Font.text, align:RectAlignment.Middle);

return gui.BuildButton(gui.lastRect, color, color + 1) && active;
using (gui.EnterGroup(new Padding(indentLevel * 1.5f, 0, 0, 0)))
{
using (gui.EnterGroup(padding ?? DefaultButtonPadding, active ? color + 2 : color + 3))
gui.BuildText(text, Font.text, align: RectAlignment.Middle);

return gui.BuildButton(gui.lastRect, color, color + 1) && active;
}
}

public static ButtonEvent BuildContextMenuButton(this ImGui gui, string text, string rightText = null, Icon icon = default, bool disabled = false)
Expand Down