sinmai-mods/FixLocaleIssues/Manager/patch_SlideManager.cs
2024-05-22 22:31:19 +07:00

224 lines
11 KiB
C#

// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using MonoMod;
using UnityEngine;
namespace Manager;
public class patch_SlideManager : SlideManager
{
[MonoModIgnore]
private struct SlidePath
{
public List<ControlPoint> Points;
}
[MonoModIgnore]
private static readonly char[] RemoveChars = ['\r', '\n', '\t'];
[MonoModReplace]
private bool loadXML(SlidePath path, string dataPath)
{
if (!File.Exists(dataPath))
{
return false;
}
var xmlDocument = new XmlDocument { XmlResolver = null };
try
{
xmlDocument.Load(dataPath);
var regex = new Regex(@"([MCLSclsvVhH(points)])((-?\d+(\.\d+)?[,-]?\d+(\.\d+)?),?)+", RegexOptions.Multiline);
var regex2 = new Regex(@"(-?\d+(\.\d+)?),?(-?\d+(\.\d+)?)", RegexOptions.Multiline);
var regex3 = new Regex(@"(-?\d+(\.\d+)?),?(-?\d+(\.\d+)?)", RegexOptions.Multiline);
var controlPoint = new ControlPoint();
foreach (var obj in xmlDocument.GetElementsByTagName("line"))
{
var xmlElement = (XmlElement)obj;
var x1 = float.Parse(xmlElement.GetAttribute("x1"), NumberStyles.Float, CultureInfo.InvariantCulture);
var y1 = float.Parse(xmlElement.GetAttribute("y1"), NumberStyles.Float, CultureInfo.InvariantCulture);
var vector = new Vector2(x1, y1);
var controlPoint2 = new ControlPoint
{
Handle1 = new Vector3(vector.x, vector.y, 0),
Handle2 = new Vector3(vector.x, vector.y, 0),
Anchore = new Vector3(vector.x, vector.y, 0)
};
path.Points.Add(controlPoint2);
var x2 = float.Parse(xmlElement.GetAttribute("x2"), NumberStyles.Float, CultureInfo.InvariantCulture);
var y2 = float.Parse(xmlElement.GetAttribute("y2"), NumberStyles.Float, CultureInfo.InvariantCulture);
var vector2 = new Vector2(x2, y2);
var controlPoint3 = new ControlPoint
{
Handle1 = new Vector3(vector2.x, vector2.y, 0),
Handle2 = new Vector3(vector2.x, vector2.y, 0),
Anchore = new Vector3(vector2.x, vector2.y, 0)
};
path.Points.Add(controlPoint3);
}
foreach (var obj in xmlDocument.GetElementsByTagName("polyline"))
{
var attribute = ((XmlElement)obj).GetAttribute("points");
foreach (var matchObj in regex3.Matches(attribute))
{
var match = (Match)matchObj;
var vector = new Vector2(
float.Parse(match.Groups[1].Value, NumberStyles.Float, CultureInfo.InvariantCulture),
float.Parse(match.Groups[3].Value, NumberStyles.Float, CultureInfo.InvariantCulture));
var controlPoint4 = new ControlPoint
{
Handle1 = new Vector3(vector.x, vector.y, 0),
Handle2 = new Vector3(vector.x, vector.y, 0),
Anchore = new Vector3(vector.x, vector.y, 0)
};
path.Points.Add(controlPoint4);
}
}
foreach (var obj in xmlDocument.GetElementsByTagName("path"))
{
var d = ((XmlElement)obj).GetAttribute("d");
var vectors = new List<Vector2>();
foreach (var c in RemoveChars)
{
d = d.Replace(c.ToString(), "");
}
foreach (var matchObj in regex.Matches(d))
{
var match2 = (Match)matchObj;
var controlPoint5 = new ControlPoint();
vectors.Clear();
foreach (var matchObj2 in regex2.Matches(match2.Value))
{
var match3 = (Match)matchObj2;
switch (match2.Groups[1].Value)
{
case "h" or "H":
{
var x = float.Parse(match3.Value, NumberStyles.Float, CultureInfo.InvariantCulture);
vectors.Add(new Vector2(x, 0));
break;
}
case "V" or "V":
{
var y = float.Parse(match3.Value, NumberStyles.Float, CultureInfo.InvariantCulture);
vectors.Add(new Vector2(0, y));
break;
}
default:
{
var x = float.Parse(match3.Groups[1].Value, NumberStyles.Float,
CultureInfo.InvariantCulture);
var y = float.Parse(match3.Groups[3].Value, NumberStyles.Float,
CultureInfo.InvariantCulture);
vectors.Add(new Vector2(x, y));
break;
}
}
}
switch (match2.Groups[1].Value)
{
case "C":
controlPoint5.Handle1 = new Vector3(vectors[0].x, vectors[0].y, 0f);
controlPoint5.Handle2 = new Vector3(vectors[1].x, vectors[1].y, 0f);
controlPoint5.Anchore = new Vector3(vectors[2].x, vectors[2].y, 0f);
break;
case "c":
controlPoint5.Handle1 = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y + vectors[0].y, 0f);
controlPoint5.Handle2 = new Vector3(controlPoint.Anchore.x + vectors[1].x, controlPoint.Anchore.y + vectors[1].y, 0f);
controlPoint5.Anchore = new Vector3(controlPoint.Anchore.x + vectors[2].x, controlPoint.Anchore.y + vectors[2].y, 0f);
break;
case "s" when path.Points.Count > 0:
{
var vector3 = controlPoint.Anchore - (controlPoint.Handle2 - controlPoint.Anchore);
controlPoint5.Handle1 = new Vector3(vector3.x, vector3.y, 0f);
controlPoint5.Handle2 = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y + vectors[0].y, 0f);
controlPoint5.Anchore = new Vector3(controlPoint.Anchore.x + vectors[1].x, controlPoint.Anchore.y + vectors[1].y, 0f);
break;
}
case "s":
controlPoint5.Handle1 = new Vector3(controlPoint.Anchore.x + vectors[1].x, controlPoint.Anchore.y + vectors[1].y, 0f);
controlPoint5.Handle2 = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y + vectors[0].y, 0f);
controlPoint5.Anchore = new Vector3(controlPoint.Anchore.x + vectors[1].x, controlPoint.Anchore.y + vectors[1].y, 0f);
break;
case "S" when path.Points.Count > 0:
{
var vector4 = controlPoint.Anchore - (controlPoint.Handle2 - controlPoint.Anchore);
controlPoint5.Handle1 = new Vector3(vector4.x, vector4.y, 0f);
controlPoint5.Handle2 = new Vector3(vectors[0].x, vectors[0].y, 0f);
controlPoint5.Anchore = new Vector3(vectors[1].x, vectors[1].y, 0f);
break;
}
case "S":
controlPoint5.Handle1 = new Vector3(vectors[1].x, vectors[1].y, 0f);
controlPoint5.Handle2 = new Vector3(vectors[0].x, vectors[0].y, 0f);
controlPoint5.Anchore = new Vector3(vectors[1].x, vectors[1].y, 0f);
break;
case "l":
controlPoint5.Handle1 = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y + vectors[0].y, 0f);
controlPoint5.Handle2 = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y + vectors[0].y, 0f);
controlPoint5.Anchore = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y + vectors[0].y, 0f);
break;
case "L":
controlPoint5.Handle1 = new Vector3(vectors[0].x, vectors[0].y, 0f);
controlPoint5.Handle2 = new Vector3(vectors[0].x, vectors[0].y, 0f);
controlPoint5.Anchore = new Vector3(vectors[0].x, vectors[0].y, 0f);
break;
case "v":
controlPoint5.Handle1 = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y, 0f);
controlPoint5.Handle2 = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y, 0f);
controlPoint5.Anchore = new Vector3(controlPoint.Anchore.x + vectors[0].x, controlPoint.Anchore.y, 0f);
break;
case "V":
controlPoint5.Handle1 = new Vector3(controlPoint.Anchore.x, vectors[0].y, 0f);
controlPoint5.Handle2 = new Vector3(controlPoint.Anchore.x, vectors[0].y, 0f);
controlPoint5.Anchore = new Vector3(controlPoint.Anchore.x, vectors[0].y, 0f);
break;
case "h":
controlPoint5.Handle1 = new Vector3(controlPoint.Anchore.x, controlPoint.Anchore.y + vectors[0].y, 0f);
controlPoint5.Handle2 = new Vector3(controlPoint.Anchore.x, controlPoint.Anchore.y + vectors[0].y, 0f);
controlPoint5.Anchore = new Vector3(controlPoint.Anchore.x, controlPoint.Anchore.y + vectors[0].y, 0f);
break;
case "H":
controlPoint5.Handle1 = new Vector3(vectors[0].x, controlPoint.Anchore.y, 0f);
controlPoint5.Handle2 = new Vector3(vectors[0].x, controlPoint.Anchore.y, 0f);
controlPoint5.Anchore = new Vector3(vectors[0].x, controlPoint.Anchore.y, 0f);
break;
default:
controlPoint5.Anchore = new Vector3(vectors[0].x, vectors[0].y, 0f);
controlPoint5.Handle1 = new Vector3(vectors[0].x, vectors[0].y, 0f);
controlPoint5.Handle2 = new Vector3(vectors[0].x, vectors[0].y, 0f);
break;
}
path.Points.Add(controlPoint5);
controlPoint = controlPoint5;
}
}
}
catch
{
return false;
}
return true;
}
}