Files
h-school/src/HSchool.Content/PackPaths.cs
T

110 lines
3.3 KiB
C#

namespace HSchool.Content;
internal static class PackPaths
{
public static string Normalize(string relativePath) => relativePath.Replace('\\', '/').TrimStart('/');
public static bool IsJsonc(string relativePath)
{
var path = Normalize(relativePath);
return path.EndsWith(".jsonc", StringComparison.OrdinalIgnoreCase)
|| path.EndsWith(".json", StringComparison.OrdinalIgnoreCase);
}
public static bool TryGetDefKind(string relativePath, out DefKind kind)
{
kind = default;
var parts = Normalize(relativePath).Split('/');
if (parts.Length < 3 || !parts[0].Equals("defs", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return TryMapFolder(parts[1], out kind);
}
public static bool IsPatch(string relativePath)
{
var path = Normalize(relativePath);
return path.StartsWith("patches/", StringComparison.OrdinalIgnoreCase) && IsJsonc(path);
}
public static bool TryGetLocaleLanguage(string relativePath, out string language)
{
language = string.Empty;
var path = Normalize(relativePath);
if (!path.StartsWith("localizations/", StringComparison.OrdinalIgnoreCase) || !IsJsonc(path))
{
return false;
}
var file = Path.GetFileNameWithoutExtension(path);
if (file.Equals("ru", StringComparison.OrdinalIgnoreCase))
{
language = "ru";
return true;
}
if (file.Equals("en", StringComparison.OrdinalIgnoreCase))
{
language = "en";
return true;
}
return false;
}
public static bool IsDefaultMap(string relativePath) =>
Normalize(relativePath).Equals("maps/default.jsonc", StringComparison.OrdinalIgnoreCase)
|| Normalize(relativePath).Equals("maps/default.json", StringComparison.OrdinalIgnoreCase);
private static bool TryMapFolder(string folder, out DefKind kind)
{
switch (folder.ToLowerInvariant())
{
case "actions":
kind = DefKind.Action;
return true;
case "things":
kind = DefKind.Thing;
return true;
case "positions":
kind = DefKind.Position;
return true;
case "works":
kind = DefKind.Work;
return true;
case "rooms":
kind = DefKind.Room;
return true;
case "buildings":
kind = DefKind.Building;
return true;
case "floors":
kind = DefKind.Floor;
return true;
case "territories":
kind = DefKind.Territory;
return true;
case "skills":
kind = DefKind.Skill;
return true;
case "traits":
kind = DefKind.Trait;
return true;
case "bodies":
kind = DefKind.BodyAttribute;
return true;
case "needs":
kind = DefKind.Need;
return true;
case "namesets":
kind = DefKind.NameSet;
return true;
default:
kind = default;
return false;
}
}
}