Skip to content

Commit

Permalink
Merge branch 'master' into feature/autosave
Browse files Browse the repository at this point in the history
  • Loading branch information
shihan42 authored Feb 21, 2024
2 parents e0379cf + c44f888 commit bcdb2e4
Show file tree
Hide file tree
Showing 113 changed files with 4,013 additions and 6,926 deletions.
8 changes: 6 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ csharp_style_inlined_variable_declaration = true:suggestion
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_open_brace = none
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
Expand All @@ -133,4 +133,8 @@ csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_blocks = true
###############################
# YAFC-CE Code Style #
###############################
dotnet_diagnostic.IDE0055.severity = error
41 changes: 14 additions & 27 deletions CommandLineToolExample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using System;
using System.Globalization;
using System.Threading;
using YAFC;
using YAFC.Model;
using YAFC.Parser;

namespace CommandLineToolExample
{
namespace CommandLineToolExample {
// If you wish to embed yafc or make a command-line tool using YAFC, here is an example on how to do that
// However, I can't make any promises about not changing signatures
public static class Program
{
public static void Main(string[] args)
{
if (args.Length == 0)
{
public static class Program {
public static void Main(string[] args) {
if (args.Length == 0) {
Console.WriteLine("Pass FactorioData path as command-line argument");
return;
}
Expand All @@ -23,42 +17,35 @@ public static void Main(string[] args)
var factorioPath = args[0];
var errorCollector = new ErrorCollector();
Project project;
try
{
try {
// Load YAFC project.
// Empty project path loads default project (with one empty page).
// Project is irrelevant if you just need data, but you need it to perform sheet calculations
// Set to not render any icons
project = FactorioDataSource.Parse(factorioPath, "", "", false, new ConsoleProgressReport(), errorCollector, "en", false);
}
catch (Exception ex)
{
catch (Exception ex) {
// Critical errors that make project un-loadable will be thrown as exceptions
Console.Error.WriteException(ex);
return;
}
if (errorCollector.severity != ErrorSeverity.None)
{
if (errorCollector.severity != ErrorSeverity.None) {
// Some non-critical errors were found while loading project, for example missing recipe or analysis warnings
foreach (var (error, _) in errorCollector.GetArrErrors())
{
foreach (var (error, _) in errorCollector.GetArrErrors()) {
Console.Error.WriteLine(error);
}

}

// To confirm project loading, enumerate all objects
foreach (var obj in Database.objects.all)
{
foreach (var obj in Database.objects.all) {
Console.WriteLine(obj.locName);
}
}

private class ConsoleProgressReport : IProgress<(string, string)>
{
public void Report((string, string) value)
{
Console.WriteLine(value.Item1 +" - " + value.Item2);
private class ConsoleProgressReport : IProgress<(string, string)> {
public void Report((string, string) value) {
Console.WriteLine(value.Item1 + " - " + value.Item2);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ YAFC is a desktop app. The Windows build is the most tested, but OSX and Linux a
4. Run either `./YAFC` or `./YAFC.exe` (OS-dependent),
5. Once YAFC is opened, make sure to locate your mod folder. Refer to the [wiki](https://wiki.factorio.com/Application_directory#Locations) for your given OS.

For more examples, you can check out the [Gifs](/Docs/Gifs.md), but beware that the gifs are traffic-heavy.
For tricky use cases, please refer to [Tips and Tricks](/Docs/TipsAndTricks.md). Feel free to add to it.
For more examples, you can check out the [Gifs](/Docs/Gifs.md), but beware that they are traffic-heavy.
For tricky use cases, please refer to the [Tips and Tricks](/Docs/TipsAndTricks.md) and the in-built [tips](https://github.com/have-fun-was-taken/yafc-ce/blob/master/YAFC/Data/Tips.txt). Feel free to add to them.

## Project features
- Works with any combination of mods for Factorio 0.17+.
Expand Down
17 changes: 6 additions & 11 deletions YAFC/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@
using YAFC.Model;
using YAFC.UI;

namespace YAFC
{
public static class Program
{
namespace YAFC {
public static class Program {
public static bool hasOverriddenFont;
static void Main(string[] args)
{
static void Main(string[] args) {
YafcLib.Init();
YafcLib.RegisterDefaultAnalysis();
Ui.Start();
var overrideFont = Preferences.Instance.overrideFont;
FontFile overriddenFontFile = null;
try
{
try {
if (!string.IsNullOrEmpty(overrideFont) && File.Exists(overrideFont))
overriddenFontFile = new FontFile(overrideFont);
}
catch (Exception ex)
{
catch (Exception ex) {
Console.Error.WriteException(ex);
}
hasOverriddenFont = overriddenFontFile != null;
Expand All @@ -33,4 +28,4 @@ static void Main(string[] args)
Ui.MainLoop();
}
}
}
}
31 changes: 10 additions & 21 deletions YAFC/Utils/Preferences.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.Json;
using YAFC.Model;
using YAFC.Parser;

namespace YAFC
{
public class Preferences
{
namespace YAFC {
public class Preferences {
public static readonly Preferences Instance;
public static readonly string appDataFolder;
private static readonly string fileName;

public static readonly string autosaveFilename;

static Preferences()
{
static Preferences() {
appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
appDataFolder = Path.Combine(appDataFolder, "YAFC");
Expand All @@ -27,23 +22,19 @@ static Preferences()

autosaveFilename = Path.Combine(appDataFolder, "autosave.yafc");
fileName = Path.Combine(appDataFolder, "yafc.config");
if (File.Exists(fileName))
{
try
{
if (File.Exists(fileName)) {
try {
Instance = JsonSerializer.Deserialize<Preferences>(File.ReadAllBytes(fileName));
return;
}
catch (Exception ex)
{
catch (Exception ex) {
Console.Error.WriteException(ex);
}
}
Instance = new Preferences();
}

public void Save()
{
public void Save() {
var data = JsonSerializer.SerializeToUtf8Bytes(this, JsonUtils.DefaultOptions);
File.WriteAllBytes(fileName, data);
}
Expand All @@ -52,16 +43,14 @@ public void Save()
public string language { get; set; } = "en";
public string overrideFont { get; set; }

public void AddProject(string path, string dataPath, string modsPath, bool expensiveRecipes)
{
public void AddProject(string path, string dataPath, string modsPath, bool expensiveRecipes) {
recentProjects = recentProjects.Where(x => string.Compare(path, x.path, StringComparison.InvariantCultureIgnoreCase) != 0)
.Prepend(new RecentProject {path = path, modsPath = modsPath, dataPath = dataPath, expensive = expensiveRecipes}).ToArray();
.Prepend(new RecentProject { path = path, modsPath = modsPath, dataPath = dataPath, expensive = expensiveRecipes }).ToArray();
Save();
}
}

public struct RecentProject
{
public struct RecentProject {
public string path { get; set; }
public string dataPath { get; set; }
public string modsPath { get; set; }
Expand Down
35 changes: 13 additions & 22 deletions YAFC/Utils/WindowsClipboard.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using YAFC.UI;

namespace YAFC
{
public static class WindowsClipboard
{
namespace YAFC {
public static class WindowsClipboard {
[DllImport("user32.dll")] static extern bool OpenClipboard(IntPtr handle);
[DllImport("user32.dll")] static extern bool EmptyClipboard();
[DllImport("user32.dll")] static extern IntPtr SetClipboardData(uint format, IntPtr data);
[DllImport("user32.dll")] static extern bool CloseClipboard();

private static unsafe void CopyToClipboard<T>(uint format, in T header, Span<byte> data) where T : unmanaged
{
private static unsafe void CopyToClipboard<T>(uint format, in T header, Span<byte> data) where T : unmanaged {
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return;
var headersize = Unsafe.SizeOf<T>();
var ptr = Marshal.AllocHGlobal(headersize + data.Length);
OpenClipboard(IntPtr.Zero);
try
{
try {
Marshal.StructureToPtr(header, ptr, false);
var targetSpan = new Span<byte>((void*) (ptr + headersize), data.Length);
var targetSpan = new Span<byte>((void*)(ptr + headersize), data.Length);
data.CopyTo(targetSpan);
EmptyClipboard();
SetClipboardData(format, ptr);
ptr = IntPtr.Zero;
}
finally
{
finally {
Marshal.FreeHGlobal(ptr);
CloseClipboard();
}
}

[StructLayout(LayoutKind.Sequential)]
private struct BitmapInfoHeader
{
private struct BitmapInfoHeader {
public uint biSize;
public int biWidth;
public int biHeight;
Expand All @@ -52,23 +45,21 @@ private struct BitmapInfoHeader
public uint biClrImportant;
}

public static unsafe void CopySurfaceToClipboard(MemoryDrawingSurface surface)
{
public static unsafe void CopySurfaceToClipboard(MemoryDrawingSurface surface) {
ref var surfaceinfo = ref RenderingUtils.AsSdlSurface(surface.surface);
var width = surfaceinfo.w;
var height = surfaceinfo.h;
var pitch = surfaceinfo.pitch;
var size = pitch * surfaceinfo.h;

// Windows expect images starting at bottom
var flippedPixels = new Span<byte>(new byte[size]);
var originalPixels = new Span<byte>((void*) surfaceinfo.pixels, size);
var originalPixels = new Span<byte>((void*)surfaceinfo.pixels, size);
for (var i = 0; i < surfaceinfo.h; i++)
originalPixels.Slice(i*pitch, pitch).CopyTo(flippedPixels.Slice((height-i-1)*pitch, pitch));
originalPixels.Slice(i * pitch, pitch).CopyTo(flippedPixels.Slice((height - i - 1) * pitch, pitch));

var header = new BitmapInfoHeader
{
biSize = (uint) Unsafe.SizeOf<BitmapInfoHeader>(),
var header = new BitmapInfoHeader {
biSize = (uint)Unsafe.SizeOf<BitmapInfoHeader>(),
biWidth = width,
biHeight = height,
biPlanes = 1,
Expand Down
Loading

0 comments on commit bcdb2e4

Please sign in to comment.