Skip to content

Commit

Permalink
fix: Fixes reading prof.txt (#1745)
Browse files Browse the repository at this point in the history
### Summary
* Fixes reading the professions file.
  • Loading branch information
kamronbatman committed Apr 28, 2024
1 parent c4fbc6b commit d23d92d
Showing 1 changed file with 155 additions and 136 deletions.
291 changes: 155 additions & 136 deletions Projects/UOContent/Misc/ProfessionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,88 @@
using System.Collections.Generic;
using System.IO;

namespace Server
namespace Server;

public class ProfessionInfo
{
public class ProfessionInfo
public static ProfessionInfo[] Professions { get; }

private static bool TryGetSkillName(string name, out SkillName skillName)
{
public static ProfessionInfo[] Professions { get; }
if (Enum.TryParse(name, out skillName))
{
return true;
}

var lowerName = name?.ToLowerInvariant().RemoveOrdinal(" ");

private static bool TryGetSkillName(string name, out SkillName skillName)
if (!string.IsNullOrEmpty(lowerName))
{
if (Enum.TryParse(name, out skillName))
foreach (var so in SkillInfo.Table)
{
return true;
if (lowerName == so.ProfessionSkillName.ToLowerInvariant())
{
skillName = (SkillName)so.SkillID;
return true;
}
}
}

var lowerName = name?.ToLowerInvariant().RemoveOrdinal(" ");
return false;
}

if (!string.IsNullOrEmpty(lowerName))
static ProfessionInfo()
{
var profs = new List<ProfessionInfo>
{
new()
{
foreach (var so in SkillInfo.Table)
{
if (lowerName == so.ProfessionSkillName.ToLowerInvariant())
{
skillName = (SkillName)so.SkillID;
return true;
}
}
ID = 0, // Custom
Name = "Advanced",
TopLevel = false,
GumpID = 5571
}
};

return false;
var file = Core.FindDataFile("prof.txt", false);
if (!File.Exists(file))
{
var parent = Path.Combine(Core.BaseDirectory, "Data/Professions");
file = Path.Combine(ExpansionInfo.GetEraFolder(parent), "prof.txt");
}

static ProfessionInfo()
var maxProf = 0;

if (File.Exists(file))
{
var profs = new List<ProfessionInfo>
using var s = File.OpenText(file);

while (!s.EndOfStream)
{
new()
var line = s.ReadLine();

if (string.IsNullOrWhiteSpace(line))
{
ID = 0, // Custom
Name = "Advanced",
TopLevel = false,
GumpID = 5571
continue;
}
};

var file = Core.FindDataFile("prof.txt", false);
if (!File.Exists(file))
{
var parent = Path.Combine(Core.BaseDirectory, "Data/Professions");
file = Path.Combine(ExpansionInfo.GetEraFolder(parent), "prof.txt");
}
line = line.Trim();

var maxProf = 0;
if (!line.InsensitiveStartsWith("Begin"))
{
continue;
}

if (File.Exists(file))
{
using var s = File.OpenText(file);
var prof = new ProfessionInfo();

var statIndex = 0;
var totalStats = 0;
var skillIndex = 0;
var totalSkill = 0;

while (!s.EndOfStream)
{
var line = s.ReadLine();
line = s.ReadLine();

if (string.IsNullOrWhiteSpace(line))
{
Expand All @@ -69,139 +92,135 @@ static ProfessionInfo()

line = line.Trim();

if (!line.InsensitiveStartsWith("Begin"))
if (line.InsensitiveStartsWith("End"))
{
continue;
}
if (prof.ID > 0 && totalStats >= 80 && totalSkill >= 100)
{
prof.FixSkills(); // Adjust skills array in case there are fewer skills than the default 4
profs.Add(prof);
}

var prof = new ProfessionInfo();
break;
}

int stats;
int valid;
var skills = stats = valid = 0;
var cols = line.Split('\t', StringSplitOptions.RemoveEmptyEntries);
var key = cols[0].ToLowerInvariant();
var value = cols[1].Trim('"');

while (!s.EndOfStream)
if (key == "type" && !value.InsensitiveEquals("profession"))
{
line = s.ReadLine();

if (string.IsNullOrWhiteSpace(line))
{
continue;
}

line = line.Trim();
break;
}

if (line.InsensitiveStartsWith("End"))
{
if (valid >= 4)
switch (key)
{
case "truename":
{
profs.Add(prof);
prof.Name = value;
}

break;
}

var cols = line.Split('\t', StringSplitOptions.RemoveEmptyEntries);
var key = cols[0].ToLowerInvariant();
var value = cols[1].Trim('"');

if (key == "type" && !value.InsensitiveEquals("profession"))
{
break;
}

switch (key)
{
case "truename":
{
prof.Name = value;
++valid;
}
break;
case "nameid":
case "nameid":
{
prof.NameID = Utility.ToInt32(value);
break;
case "descid":
}
case "descid":
{
prof.DescID = Utility.ToInt32(value);
break;
case "desc":
}
case "desc":
{
prof.ID = Utility.ToInt32(value);
if (prof.ID > maxProf)
{
prof.ID = Utility.ToInt32(value);
if (prof.ID > maxProf)
{
maxProf = prof.ID;
}

++valid;
maxProf = prof.ID;
}
break;
case "toplevel":
}
break;
case "toplevel":
{
prof.TopLevel = Utility.ToBoolean(value);
break;
case "gump":
}
case "gump":
{
prof.GumpID = Utility.ToInt32(value);
break;
case "skill":
}
case "skill":
{
if (!TryGetSkillName(value, out var skillName))
{
if (!TryGetSkillName(value, out var skillName))
{
break;
}

prof.Skills[skills++] = new SkillNameValue(skillName, Utility.ToInt32(cols[2]));

if (skills == prof.Skills.Length)
{
++valid;
}
break;
}
break;
case "stat":
{
if (!Enum.TryParse(value, out StatType stat))
{
break;
}

prof.Stats[stats++] = new StatNameValue(stat, Utility.ToInt32(cols[2]));

if (stats == prof.Stats.Length)
{
++valid;
}
var skillValue = Utility.ToInt32(cols[2]);
prof.Skills[skillIndex++] = new SkillNameValue(skillName, skillValue);
totalSkill += skillValue;
}
break;
case "stat":
{
if (!Enum.TryParse(value, out StatType stat))
{
break;
}
break;
}

var statValue = Utility.ToInt32(cols[2]);
prof.Stats[statIndex++] = new StatNameValue(stat, statValue);
totalStats += statValue;
}
break;
}
}
}
}

Professions = new ProfessionInfo[1 + maxProf];

foreach (var p in profs)
{
Professions[p.ID] = p;
}
Professions = new ProfessionInfo[1 + maxProf];

profs.Clear();
profs.TrimExcess();
foreach (var p in profs)
{
Professions[p.ID] = p;
}

private ProfessionInfo()
profs.Clear();
profs.TrimExcess();
}

private SkillNameValue[] _skills;

private ProfessionInfo()
{
Name = string.Empty;

_skills = new SkillNameValue[4];
Stats = new StatNameValue[3];
}

public int ID { get; private set; }
public string Name { get; private set; }
public int NameID { get; private set; }
public int DescID { get; private set; }
public bool TopLevel { get; private set; }
public int GumpID { get; private set; }
public SkillNameValue[] Skills => _skills;
public StatNameValue[] Stats { get; }

public void FixSkills()
{
var index = _skills.Length - 1;
while (index >= 0)
{
Name = string.Empty;
var skill = _skills[index];
if (skill is not { Name: SkillName.Alchemy, Value: 0 })
{
break;
}

Skills = new SkillNameValue[4];
Stats = new StatNameValue[3];
index--;
}

public int ID { get; private set; }
public string Name { get; private set; }
public int NameID { get; private set; }
public int DescID { get; private set; }
public bool TopLevel { get; private set; }
public int GumpID { get; private set; }
public SkillNameValue[] Skills { get; }
public StatNameValue[] Stats { get; }
Array.Resize(ref _skills, index + 1);
}
}

0 comments on commit d23d92d

Please sign in to comment.