Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: add content type code generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Jan 19, 2024
1 parent a454f1c commit 69184dc
Show file tree
Hide file tree
Showing 11 changed files with 1,492 additions and 1,363 deletions.
20 changes: 8 additions & 12 deletions RotationSolver.Basic/Data/ObjectListDelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ namespace RotationSolver.Basic.Data;
/// A class to delay the object list checking.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObjectListDelay<T> : IEnumerable<T> where T : GameObject
/// <remarks>
/// Constructer.
/// </remarks>
/// <param name="getRange"></param>
public class ObjectListDelay<T>(Func<(float min, float max)> getRange)
: IEnumerable<T> where T : GameObject
{
IEnumerable<T> _list = Array.Empty<T>();
readonly Func<(float min, float max)> _getRange;
SortedList<uint, DateTime> _revealTime = new();
readonly Func<(float min, float max)> _getRange = getRange;
SortedList<uint, DateTime> _revealTime = [];
readonly Random _ran = new(DateTime.Now.Millisecond);

/// <summary>
/// Constructer.
/// </summary>
/// <param name="getRange"></param>
public ObjectListDelay(Func<(float min, float max)> getRange)
{
_getRange = getRange;
}

/// <summary>
/// The delayed list.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions RotationSolver.Basic/Data/StatusID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// <summary>
/// The status Id.
/// </summary>
[Obsolete]
public enum StatusID : ushort
{
/// <summary>
Expand Down
136 changes: 0 additions & 136 deletions RotationSolver.Basic/Data/TerritoryContentType.cs

This file was deleted.

23 changes: 23 additions & 0 deletions RotationSolver.GameData/ContentTypeGetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Lumina.Excel.GeneratedSheets;

namespace RotationSolver.GameData;
internal class ContentTypeGetter(Lumina.GameData gameData)
: ExcelRowGetter<ContentType>(gameData)
{
protected override bool AddToList(ContentType item)
{
if (string.IsNullOrEmpty(item.Name.RawString)) return false;
return true;
}

protected override string ToCode(ContentType item)
{
var name = item.Name.RawString.ToPascalCase();
return $"""
/// <summary>
///
/// </summary>
{name} = {item.RowId},
""";
}
}
19 changes: 19 additions & 0 deletions RotationSolver.GameData/ExcelRowGetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Lumina.Excel;

namespace RotationSolver.GameData;
internal abstract class ExcelRowGetter<T>(Lumina.GameData gameData) where T :ExcelRow
{
protected abstract bool AddToList(T item);
protected abstract string ToCode(T item);

protected virtual void BeforeCreating() { }

public string GetCode()
{
var items = gameData.GetExcelSheet<T>();

if (items == null) return string.Empty;
BeforeCreating();
return string.Join("\n", items.Where(AddToList).Select(ToCode));
}
}
66 changes: 4 additions & 62 deletions RotationSolver.GameData/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// See https://aka.ms/new-console-template for more information
using Lumina;
using Lumina.Excel.GeneratedSheets;
using RotationSolver.GameData;
using System.Resources.NetStandard;
using System.Text.RegularExpressions;

var gameData = new GameData("C:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack", new LuminaOptions
{
Expand All @@ -12,70 +11,13 @@
DefaultExcelLanguage = Lumina.Data.Language.English,
});

var statuses = gameData.GetExcelSheet<Status>();

if (statuses == null) return;

Dictionary<string, byte> _count = [];
var status =string.Join("\n", statuses.Where(UseStatus).Select(StatusString));

var dirInfo = new DirectoryInfo(typeof(Program).Assembly.Location);
dirInfo = dirInfo.Parent!.Parent!.Parent!.Parent!.Parent!;

using var res = new ResXResourceWriter(dirInfo.FullName + "\\RotationSolver.SourceGenerators\\Properties\\Resources.resx");

res.AddResource("StatusId", status);
res.AddResource("StatusId", new StatusGetter(gameData).GetCode());
res.AddResource("ContentType", new ContentTypeGetter(gameData).GetCode());
res.Generate();

Console.WriteLine(status);

static bool UseStatus(Status status)
{
if (status.ClassJobCategory.Row == 0) return false;
var name = status.Name.RawString;
if (string.IsNullOrEmpty(name)) return false;
if (!name.All(char.IsAscii)) return false;
return true;
}

string StatusString(Status status)
{
var name = ConvertToPascalCase(status.Name.RawString);
if (_count.ContainsKey(name))
{
name += "_" + (++_count[name]).ToString();
}
else
{
_count[name] = 1;
}

var desc = new string(status.Description.RawString.Where(char.IsAscii).ToArray());

return $"""
/// <summary>
/// {desc.Replace("\n", "\n/// ")}
/// </summary>
{name} = {status.RowId},
""";
}


static string ConvertToPascalCase(string input)
{
Regex invalidCharsRgx = new(@"[^_a-zA-Z0-9]");
Regex whiteSpace = new(@"(?<=\s)");
Regex startsWithLowerCaseChar = new("^[a-z]");
Regex firstCharFollowedByUpperCasesOnly = new("(?<=[A-Z])[A-Z0-9]+$");
Regex lowerCaseNextToNumber = new("(?<=[0-9])[a-z]");
Regex upperCaseInside = new("(?<=[A-Z])[A-Z]+?((?=[A-Z][a-z])|(?=[0-9]))");

var pascalCase = invalidCharsRgx.Replace(whiteSpace.Replace(input, "_"), string.Empty)
.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries)
.Select(w => startsWithLowerCaseChar.Replace(w, m => m.Value.ToUpper()))
.Select(w => firstCharFollowedByUpperCasesOnly.Replace(w, m => m.Value.ToLower()))
.Select(w => lowerCaseNextToNumber.Replace(w, m => m.Value.ToUpper()))
.Select(w => upperCaseInside.Replace(w, m => m.Value.ToLower()));

return string.Concat(pascalCase);
}
Console.WriteLine("Finished!");
43 changes: 43 additions & 0 deletions RotationSolver.GameData/StatusGetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Lumina.Excel.GeneratedSheets;

namespace RotationSolver.GameData;
internal class StatusGetter(Lumina.GameData gameData)
: ExcelRowGetter<Status>(gameData)
{
private readonly List<string> _addedNames = [];

protected override void BeforeCreating()
{
_addedNames.Clear();
base.BeforeCreating();
}

protected override bool AddToList(Status item)
{
if (item.ClassJobCategory.Row == 0) return false;
var name = item.Name.RawString;
if (string.IsNullOrEmpty(name)) return false;
if (!name.All(char.IsAscii)) return false;
return true;
}

protected override string ToCode(Status item)
{
var name = item.Name.RawString.ToPascalCase();
if (_addedNames.Contains(name))
{
name += "_" + item.RowId.ToString();
}
else
{
_addedNames.Add(name);
}

return $"""
/// <summary>
/// {item.Description.RawString.OnlyAscii().Replace("\n", "\n/// ")}
/// </summary>
{name} = {item.RowId},
""";
}
}
32 changes: 32 additions & 0 deletions RotationSolver.GameData/Util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Text.RegularExpressions;

namespace RotationSolver.GameData;
internal static partial class Util
{
public static string OnlyAscii(this string input) => new string(input.Where(char.IsAscii).ToArray());

public static string ToPascalCase(this string input)
{
var pascalCase = InvalidCharsRgx().Replace(WhiteSpace().Replace(input, "_"), string.Empty)
.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries)
.Select(w => StartWithLowerCaseChar().Replace(w, m => m.Value.ToUpper()))
.Select(w => FirstCharFollowedByUpperCasesOnly().Replace(w, m => m.Value.ToLower()))
.Select(w => LowerCaseNextToNumber().Replace(w, m => m.Value.ToUpper()))
.Select(w => UpperCaseInside().Replace(w, m => m.Value.ToLower()));

return string.Concat(pascalCase);
}

[GeneratedRegex("[^_a-zA-Z0-9]")]
private static partial Regex InvalidCharsRgx();
[GeneratedRegex("(?<=\\s)")]
private static partial Regex WhiteSpace();
[GeneratedRegex("^[a-z]")]
private static partial Regex StartWithLowerCaseChar();
[GeneratedRegex("(?<=[A-Z])[A-Z0-9]+$")]
private static partial Regex FirstCharFollowedByUpperCasesOnly();
[GeneratedRegex("(?<=[0-9])[a-z]")]
private static partial Regex LowerCaseNextToNumber();
[GeneratedRegex("(?<=[A-Z])[A-Z]+?((?=[A-Z][a-z])|(?=[0-9]))")]
private static partial Regex UpperCaseInside();
}
Loading

0 comments on commit 69184dc

Please sign in to comment.