Skip to content

Commit

Permalink
renamed class, misc tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
hollowstrawberry committed Jun 10, 2020
1 parent 2fa9087 commit e88cfe0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
22 changes: 11 additions & 11 deletions PolyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public class PolyConverter
public const string JsonExtension = ".layout.json";
public const string BackupExtension = ".layout.backup";

public static readonly Regex LayoutExtensionRegex = new Regex(LayoutExtension.Replace(".", "\\.") + "$");
public static readonly Regex JsonExtensionRegex = new Regex(JsonExtension.Replace(".", "\\.") + "$");
public static readonly Regex BackupExtensionRegex = new Regex(BackupExtension.Replace(".", "\\.") + "$");
public static readonly Regex LayoutRegex = new Regex(LayoutExtension.Replace(".", "\\.") + "$");
public static readonly Regex JsonRegex = new Regex(JsonExtension.Replace(".", "\\.") + "$");
public static readonly Regex BackupRegex = new Regex(BackupExtension.Replace(".", "\\.") + "$");

static readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
PreserveReferencesHandling = PreserveReferencesHandling.None,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Converters = new JsonConverter[] { new VectorJsonConverter(), new PolyJsonConverter() },
Converters = new JsonConverter[] { new VectorJsonConverter(), new ProxyJsonConverter() },
};

static readonly List<char> logChars = new List<char> { 'F', 'E', '+', '@', '*', '>' };
Expand All @@ -41,23 +41,23 @@ public void ConvertAll()

foreach (string path in files)
{
if (BackupExtensionRegex.IsMatch(path))
if (BackupRegex.IsMatch(path))
{
backups++;
continue;
}
else if (JsonExtensionRegex.IsMatch(path))
else if (JsonRegex.IsMatch(path))
{
string layoutPath = JsonExtensionRegex.Replace(path, LayoutExtension);
string backupPath = JsonExtensionRegex.Replace(path, BackupExtension);
string layoutPath = JsonRegex.Replace(path, LayoutExtension);
string backupPath = JsonRegex.Replace(path, BackupExtension);

resultLog.Add(JsonToLayout(path, layoutPath, backupPath));

fileCount++;
}
else if (LayoutExtensionRegex.IsMatch(path))
else if (LayoutRegex.IsMatch(path))
{
string newPath = LayoutExtensionRegex.Replace(path, JsonExtension);
string newPath = LayoutRegex.Replace(path, JsonExtension);
if (File.Exists(newPath)) continue;

resultLog.Add(LayoutToJson(path, newPath));
Expand All @@ -67,7 +67,7 @@ public void ConvertAll()
}

resultLog = resultLog
.Where(s => !string.IsNullOrWhiteSpace(s))
.Where(s => !string.IsNullOrWhiteSpace(s) && s.Length >= 2)
.OrderBy(s => logChars.Contains(s[1]) ? logChars.IndexOf(s[1]) : logChars.Last())
.ToList();

Expand Down
14 changes: 6 additions & 8 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using Microsoft.Win32;
using static PolyConverter.PolyConverter;

namespace PolyConverter
{
Expand Down Expand Up @@ -88,10 +87,10 @@ public static int Main(string[] args)
{
string filePath = string.Join(' ', args).Trim();

if (JsonExtensionRegex.IsMatch(filePath))
if (PolyConverter.JsonRegex.IsMatch(filePath))
{
string newPath = JsonExtensionRegex.Replace(filePath, LayoutExtension);
string backupPath = JsonExtensionRegex.Replace(filePath, BackupExtension);
string newPath = PolyConverter.JsonRegex.Replace(filePath, PolyConverter.LayoutExtension);
string backupPath = PolyConverter.JsonRegex.Replace(filePath, PolyConverter.BackupExtension);

string result = new PolyConverter().JsonToLayout(filePath, newPath, backupPath);

Expand All @@ -101,9 +100,9 @@ public static int Main(string[] args)
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else if (LayoutExtensionRegex.IsMatch(filePath))
else if (PolyConverter.LayoutRegex.IsMatch(filePath))
{
string newPath = LayoutExtensionRegex.Replace(filePath, JsonExtension);
string newPath = PolyConverter.LayoutRegex.Replace(filePath, PolyConverter.JsonExtension);

string result = new PolyConverter().LayoutToJson(filePath, newPath);

Expand All @@ -114,7 +113,7 @@ public static int Main(string[] args)
}
else
{
Console.WriteLine($"[Error] File extension must be either {LayoutExtension} or {JsonExtension}");
Console.WriteLine($"[Error] File extension must be either {PolyConverter.LayoutExtension} or {PolyConverter.JsonExtension}");
return ExitCodeFileError;
}
}
Expand Down Expand Up @@ -157,7 +156,6 @@ static string GetPolyBridge2SteamPath()
const int ExitCodeConversionError = 2;
const int ExitCodeFileError = 3;
const int ExitCodeGamePathError = 4;
const int ExitCodeUnexpectedError = 5;

const string ManualGamePath = "gamepath.txt";

Expand Down
15 changes: 6 additions & 9 deletions PolyJsonConverter.cs → ProxyJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@

namespace PolyConverter
{
// Handles Poly Bridge 2 objects that don't have a default constructor

public class PolyJsonConverter : JsonConverter
/// <summary> Handles SandboxLayoutData fields whose type don't have a default constructor.</summary>
public class ProxyJsonConverter : JsonConverter
{
public override bool CanRead => true;
public override bool CanWrite => false;

public override bool CanConvert(Type objectType)
{
if (!objectType.Name.EndsWith("Proxy")) return false;
var constructors = objectType.GetConstructors();
return constructors.Length == 2 && constructors.All(c => c.GetParameters().Length > 0);
}
public override bool CanConvert(Type type) =>
type.Assembly == Program.PolyBridge2Assembly
&& type.Name.EndsWith("Proxy")
&& type.GetConstructors().All(c => c.GetParameters().Length > 0);

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
Expand Down

0 comments on commit e88cfe0

Please sign in to comment.