I'm trying to serialize a text who looks like that :
m_list 2 1035 6 16 204 206 501
pdtse 1 6
m_list 3 1 1027 10 1035 1 2038 8 -1
pdtse 1 16
m_list 3 1 1027 8 1035 1 2042 6 -1
pdtse 1 204
m_list 3 1 1027 15 1035 1 2042 10 -1
pdtse 1 206
m_list 3 1 1027 8 1035 1 2046 7 -1
pdtse 1 501
m_list 3 1 500 1 1027 14 1035 1 -1
pdtclose
however, after few research & debug I'm still getting the same results and can't figure out from where does the error come from.
here is the result I got :
but there is missing items & random things such as *o0 & &o0 and I'm unable to figure out why
here is my code :
string[] lines = File.ReadAllLines(Path.Combine(_configuration.BasePath, _configuration.RecipeFile));
List<Recipe> recipes = new();
Recipe tempRecipe = new();
long vnumToInsert = 0;
foreach (string line in lines)
{
string[] parts = line.Split(' ');
if (line.StartsWith("m_list 2"))
{
foreach (string part in parts.Skip(3))
{
if (tempRecipe.Recipes == null)
{
tempRecipe.Recipes = new List<RecipeAttributes>()
{
new RecipeAttributes
{
ItemVnum = Convert.ToInt64(part),
ProducerItemVnum = Convert.ToInt64(parts[2]),
Amount = 1
}
};
}
else
{
tempRecipe.Recipes.Add(new RecipeAttributes
{
ItemVnum = Convert.ToInt64(part),
ProducerItemVnum = Convert.ToInt64(parts[2]),
Amount = 1
});
}
}
}
if (line.StartsWith("pdtse"))
{
vnumToInsert = Convert.ToInt64(parts[2]);
}
if (line.StartsWith("m_list 3"))
{
for (int i = 3; i < parts.Length; i += 2)
{
if (Convert.ToInt64(parts[i]) < 0) continue;
if (tempRecipe.Recipes == null) continue;
var findRecipeRelatedToVnum = tempRecipe.Recipes.FirstOrDefault(s => s.ItemVnum == vnumToInsert);
if (findRecipeRelatedToVnum == null) break;
if (findRecipeRelatedToVnum.Items == null)
{
findRecipeRelatedToVnum.Items = new List<ItemAttributes>()
{
new ItemAttributes()
{
ItemVNum = Convert.ToInt64(parts[i]),
Amount = Convert.ToInt64(parts[i + 1])
}
};
}
else
{
if (findRecipeRelatedToVnum.Items.FirstOrDefault(s => s.ItemVNum == Convert.ToInt64(parts[i])) != null) continue;
findRecipeRelatedToVnum.Items.Add(new ItemAttributes()
{
ItemVNum = Convert.ToInt64(parts[i]),
Amount = Convert.ToInt64(parts[i + 1])
});
}
}
}
if (line.StartsWith("pdtclose"))
{
recipes.Add(tempRecipe);
tempRecipe = new();
}
}
Recipe toSerialize = new();
List<long> alreadyDone = new();
foreach (Recipe recipe in recipes)
{
toSerialize = new();
if (recipe.Recipes == null) continue;
if (alreadyDone.Contains(recipe.Recipes[0].ProducerItemVnum)) continue;
foreach (Recipe r in recipes.Where(s => s.Recipes?.First().ProducerItemVnum == recipe.Recipes?.First().ProducerItemVnum))
{
toSerialize = r;
}
alreadyDone.Add(recipe.Recipes[0].ProducerItemVnum);
var serializer = new SerializerBuilder().Build();
var yaml = serializer.Serialize(toSerialize);
Console.WriteLine(yaml);
}
and there is my yaml class :
using YamlDotNet.Serialization;
namespace YamlParser.Entities
{
public record Recipe
{
[YamlMember(Alias = "recipes")]
public List<RecipeAttributes> Recipes { get; set; }
}
public record RecipeAttributes
{
[YamlMember(Alias = "item_vnum")]
public long ItemVnum { get; set; }
[YamlMember(Alias = "quantity")]
public long Amount { get; set; }
[YamlMember(Alias = "producer_item_vnum")]
public long ProducerItemVnum { get; set; }
[YamlMember(Alias = "items")]
public List<ItemAttributes> Items { get; set; }
}
public record ItemAttributes
{
[YamlMember(Alias = "item_vnum")]
public long ItemVNum { get; set; }
[YamlMember(Alias = "quantity")]
public long Amount { get; set; }
}
}
Thanks in advance for your help & have a good day !