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
19 changes: 16 additions & 3 deletions CheckControl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Material_Editor
Expand All @@ -7,6 +8,7 @@ public partial class CheckControl : CustomControl
{
private Label lbLabel;
private CheckBox check;
public static Func<Color> OffForegroundProvider { get; set; } = () => System.Drawing.Color.Red;

public override Label LabelControl
{
Expand Down Expand Up @@ -50,23 +52,34 @@ public override void CreateControls()
Tag = this
};
check.CheckedChanged += new EventHandler(Check_CheckedChanged);
UpdateCheckVisual(check);
}

private void Check_CheckedChanged(object sender, EventArgs e)
{
var check = sender as CheckBox;
UpdateCheckVisual(check);

InvokeChangedCallback();
}

internal static void UpdateCheckVisual(CheckBox check)
{
if (check == null)
return;

if (check.Checked)
{
check.ForeColor = System.Drawing.Color.FromName("green");
check.Text = "On";
check.Font = new System.Drawing.Font(check.Font, System.Drawing.FontStyle.Regular);
}
else
{
check.ForeColor = System.Drawing.Color.FromName("red");
check.ForeColor = OffForegroundProvider();
check.Text = "Off";
check.Font = new System.Drawing.Font(check.Font, System.Drawing.FontStyle.Regular);
}

InvokeChangedCallback();
}

public override object GetProperty()
Expand Down
13 changes: 13 additions & 0 deletions CustomControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ public static CustomControl Find(string name)
return null;
}

public static IEnumerable<string> GetRegisteredNames()
{
return customControls.Keys;
}

public static string GetTooltip(string name)
{
if (customControls.TryGetValue(name, out CustomControl control))
return control.BaseToolTip;

return null;
}

public static bool GetProperty(string name, out object property)
{
if (customControls.TryGetValue(name, out CustomControl control))
Expand Down
150 changes: 150 additions & 0 deletions FieldOverwriteTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using MaterialLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Material_Editor
{
public enum FieldCopyStatus
{
Success,
Skipped,
Failed
}

public sealed class FieldCopyResult
{
public string TargetPath { get; }
public FieldCopyStatus Status { get; }
public string Message { get; }

public FieldCopyResult(string targetPath, FieldCopyStatus status, string message)
{
TargetPath = targetPath;
Status = status;
Message = message;
}
}

public sealed class FieldOverwriteTool
{
public IReadOnlyList<FieldCopyResult> Run(BaseMaterialFile sourceState, IReadOnlyList<MaterialFieldDescriptor> descriptors, IReadOnlyList<string> targetFiles, bool backupBeforeWrite)
{
var results = new List<FieldCopyResult>();

foreach (var target in targetFiles)
{
var result = ProcessTarget(sourceState, descriptors, target, backupBeforeWrite);
results.Add(result);
}

return results;
}

private FieldCopyResult ProcessTarget(BaseMaterialFile sourceState, IReadOnlyList<MaterialFieldDescriptor> descriptors, string filePath, bool backupBeforeWrite)
{
if (!File.Exists(filePath))
return new FieldCopyResult(filePath, FieldCopyStatus.Failed, "Target file does not exist.");

if (!TryLoadMaterial(filePath, out var targetMaterial, out var isJson, out var loadError))
return new FieldCopyResult(filePath, FieldCopyStatus.Failed, loadError ?? "Failed to load material.");

if (targetMaterial.GetType() != sourceState.GetType())
return new FieldCopyResult(filePath, FieldCopyStatus.Skipped, "Skipped incompatible material type.");

var supportedDescriptors = descriptors.Where(d => d.IsSupported(targetMaterial)).ToList();
if (supportedDescriptors.Count == 0)
return new FieldCopyResult(filePath, FieldCopyStatus.Skipped, "No compatible fields for target version.");

foreach (var descriptor in supportedDescriptors)
{
descriptor.SetValue(targetMaterial, descriptor.GetValue(sourceState));
}

try
{
if (backupBeforeWrite)
{
File.Copy(filePath, $"{filePath}.bak", true);
}

SaveMaterial(filePath, targetMaterial, isJson);
}
catch (Exception ex)
{
return new FieldCopyResult(filePath, FieldCopyStatus.Failed, ex.Message);
}

return new FieldCopyResult(filePath, FieldCopyStatus.Success, "Updated successfully.");
}

private static bool TryLoadMaterial(string filePath, out BaseMaterialFile material, out bool isJson, out string errorMessage)
{
material = null;
isJson = false;
errorMessage = null;

try
{
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
int start = stream.ReadByte();
if (start == -1)
{
errorMessage = "Target file is empty.";
return false;
}

stream.Position = 0;

BaseMaterialFile candidate = Path.GetExtension(filePath).Equals(".bgem", StringComparison.OrdinalIgnoreCase)
? new BGEM()
: new BGSM();

if (start == '{' || start == '[')
{
isJson = true;
var serializer = new DataContractJsonSerializer(candidate.GetType(), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true });
material = (BaseMaterialFile)serializer.ReadObject(stream);
}
else
{
stream.Position = 0;
if (!candidate.Open(stream))
{
errorMessage = "Failed to read binary material.";
return false;
}

material = candidate;
}

return true;
}
catch (Exception ex)
{
errorMessage = ex.Message;
return false;
}
}

private static void SaveMaterial(string filePath, BaseMaterialFile material, bool asJson)
{
using var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
if (asJson)
{
using var writer = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8, true, true, " ");
var serializer = new DataContractJsonSerializer(material.GetType(), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true });
serializer.WriteObject(writer, material);
writer.Flush();
}
else
{
if (!material.Save(stream))
throw new IOException("Failed to write binary material.");
}
}
}
}
Loading