Skip to content

Commit

Permalink
Merge pull request #617 from TheTrackerCouncil/update-plando-config
Browse files Browse the repository at this point in the history
Update plando config
  • Loading branch information
MattEqualsCoder authored Dec 16, 2024
2 parents 0add745 + 33074e2 commit 9bfeadc
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 130 deletions.
51 changes: 35 additions & 16 deletions src/TrackerCouncil.Smz3.Data.SchemaGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using System.ComponentModel;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
using NJsonSchema.Generation;
using NJsonSchema.NewtonsoftJson.Generation;
using Serilog;
using Serilog.Events;
using TrackerCouncil.Smz3.Data.Configuration;
using TrackerCouncil.Smz3.Data.Configuration.ConfigFiles;
using TrackerCouncil.Smz3.Data.Configuration.ConfigTypes;
using TrackerCouncil.Smz3.Data.Options;
using YamlDotNet.Serialization;
using JsonSchemaGenerator = NJsonSchema.Generation.JsonSchemaGenerator;

namespace TrackerCouncil.Smz3.Data.SchemaGenerator;

Expand Down Expand Up @@ -60,6 +65,34 @@ public static void Main(string[] args)

CreateSchemas(outputPath);
CreateTemplates(outputPath);
CreatePlandoSchema(outputPath);
}

private static void CreatePlandoSchema(string outputPath)
{
var plandoConfig = new PlandoConfig();
var serializer = new SerializerBuilder()
.DisableAliases()
.Build();
var newHashCode = serializer.Serialize(plandoConfig).GetHashCode();
if (newHashCode == PlandoConfig.SHashCode)
{
Log.Information("PlandoConfig default object matches SHashCode value of {Code}", newHashCode);
return;
}

Log.Information(
"New PlandoConfig HashCode {NewHashCode} does not match prior PlandoConfig.SHashCode value of {OldHashCode}",
newHashCode, PlandoConfig.SHashCode);

var schemaPath = Path.Combine(outputPath, "Schemas");
var generator = new JSchemaGenerator();
generator.GenerationProviders.Add(new StringEnumGenerationProvider());
var schema = generator.Generate(typeof(PlandoConfig), false);
var path = Path.Combine(schemaPath, "plando.json");
File.WriteAllText(path, schema.ToString());

Log.Information("Wrote {Type} schema to {Path}. Update PlandoConfig SHashCode value to {Hash}", typeof(PlandoConfig).FullName, path, newHashCode);
}

private static void CreateSchemas(string outputPath)
Expand All @@ -81,6 +114,7 @@ private static void CreateSchemas(string outputPath)
{
var path = Path.Combine(schemaPath, type.Item2);
var schema = generator.Generate(type.Item1);

var text = schrodingersStringReplacement.Replace(schema.ToJson(), "").Replace("\\n", " ");

for (var i = 0; i < 5; i++)
Expand Down Expand Up @@ -255,7 +289,7 @@ private static SchrodingersString GetPopulatedSchrodingersString(string name, bo

private static string GetOutputPath()
{
var slnDirectory = new DirectoryInfo(SolutionPath);
var slnDirectory = new DirectoryInfo(RandomizerDirectories.SolutionPath);
if (slnDirectory.Parent?.GetDirectories().Any(x => x.Name == "SMZ3CasConfigs") == true)
{
return slnDirectory.Parent.GetDirectories().First(x => x.Name == "SMZ3CasConfigs").FullName;
Expand All @@ -265,19 +299,4 @@ private static string GetOutputPath()
return Path.Combine(slnDirectory.FullName, "src", "SchemaGenerator", "Output");
}
}

private static string SolutionPath
{
get
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());

while (directory != null && !directory.GetFiles("*.sln").Any())
{
directory = directory.Parent;
}

return Path.Combine(directory!.FullName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json.Schema" Version="4.0.1" />
<PackageReference Include="NJsonSchema" Version="11.0.2" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.2" />
<PackageReference Include="Serilog" Version="4.1.0" />
Expand Down
28 changes: 1 addition & 27 deletions src/TrackerCouncil.Smz3.Data/Configuration/ConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,7 @@ public partial class ConfigProvider
/// </summary>
public ConfigProvider(ILogger<ConfigProvider>? logger)
{
#if DEBUG
var parentDir = new DirectoryInfo(SolutionPath).Parent;
var configRepo = parentDir?.GetDirectories().FirstOrDefault(x => x.Name == "SMZ3CasConfigs");
_basePath = Path.Combine(configRepo?.FullName ?? "", "Profiles");

if (!Directory.Exists(_basePath))
{
_basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SMZ3CasRandomizer", "Configs");
}
#else
_basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SMZ3CasRandomizer", "Configs");
#endif
_basePath = RandomizerDirectories.ConfigPath;
_logger = logger;
}

Expand Down Expand Up @@ -432,21 +421,6 @@ private static T GetBuiltInConfig<T>(string fileName)
?? throw new InvalidOperationException("The embedded tracker configuration could not be loaded.");
}

private static string SolutionPath
{
get
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());

while (directory != null && !directory.GetFiles("*.sln").Any())
{
directory = directory.Parent;
}

return Path.Combine(directory!.FullName);
}
}

[GeneratedRegex("[^\\.]+\\.(?<mood>.+)\\.yml")]
private static partial Regex ProfileFileNameWithMoodRegex();
}
4 changes: 2 additions & 2 deletions src/TrackerCouncil.Smz3.Data/Logic/LogicConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public LogicConfig(bool enableAllCasOptions, bool enableAllTricks, WallJumpDiffi
WallJumpDifficulty = wallJumpDifficulty;
}

[YamlIgnore]
[YamlIgnore, Newtonsoft.Json.JsonIgnore]
[DynamicFormFieldText(groupName: "CasTop")]
public string CasLogicDescription => "Logic settings that will make the experience more relaxed and easier to play.";

Expand Down Expand Up @@ -76,7 +76,7 @@ public LogicConfig(bool enableAllCasOptions, bool enableAllTricks, WallJumpDiffi
[DynamicFormFieldCheckBox(checkBoxText: "Include Quarter Magic", toolTipText: "Adds an additional progressive half magic to the item pool.", groupName: "CasMiddle")]
public bool QuarterMagic { get; set; }

[YamlIgnore]
[YamlIgnore, Newtonsoft.Json.JsonIgnore]
[DynamicFormFieldText(groupName: "TricksTop")]
public string TricksDescription => "Logic settings that will make the game more difficult by requiring you to do techniques or maneuvers not typically required in the vanilla games.";

Expand Down
33 changes: 29 additions & 4 deletions src/TrackerCouncil.Smz3.Data/Options/PlandoConfig.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using TrackerCouncil.Smz3.Shared;
using TrackerCouncil.Smz3.Data.Logic;
using TrackerCouncil.Smz3.Data.WorldData;
using TrackerCouncil.Smz3.Data.WorldData.Regions;
using TrackerCouncil.Smz3.Shared.Enums;
using YamlDotNet.Core;
using YamlDotNet.Serialization;

namespace TrackerCouncil.Smz3.Data.Options;
Expand All @@ -14,6 +16,12 @@ namespace TrackerCouncil.Smz3.Data.Options;
/// </summary>
public class PlandoConfig // TODO: Consider using this instead of SeedData?
{
/// <summary>
/// Number that represents the hash code the serialized string this class. Used to prevent unnecessary generation
/// of the schema file to prevent going over usage limits.
/// </summary>
public static int SHashCode = 0;

/// <summary>
/// Initializes a new empty instance of the <see cref="PlandoConfig"/>
/// class.
Expand All @@ -37,28 +45,35 @@ public PlandoConfig(World world)
TourianBossCount = world.Config.TourianBossCount;
Items = world.Locations
.ToDictionary(x => x.ToString(), x => x.Item.Type);
Rewards = world.Regions.Where(x => x is IHasReward)
Rewards = world.Regions.Where(x => x is IHasReward r && !r.RewardType.IsInCategory(RewardCategory.NonRandomized) && r.RewardType.IsInCategory(RewardCategory.Zelda))
.ToDictionary(x => x.ToString(), x => ((IHasReward)x).RewardType);
Medallions = world.Regions.Where(x => x is IHasPrerequisite)
.ToDictionary(x => x.ToString(), x => ((IHasPrerequisite)x).RequiredItem);
Logic = world.Config.LogicConfig.Clone();
StartingInventory = world.Config.ItemOptions;
var prizes = DropPrizes.GetPool(world.Config.CasPatches.ZeldaDrops);
var prizes = DropPrizes.GetPool(world.Config.CasPatches.ZeldaDrops).ToList();
ZeldaPrizes.EnemyDrops = prizes.Take(56).ToList();
ZeldaPrizes.TreePulls = prizes.Skip(56).Take(3).ToList();
ZeldaPrizes.CrabBaseDrop = prizes.Skip(59).First();
ZeldaPrizes.CrabEightDrop = prizes.Skip(60).First();
ZeldaPrizes.StunPrize = prizes.Skip(61).First();
ZeldaPrizes.FishPrize = prizes.Skip(62).First();

var bottleItems = Enum.GetValues<ItemType>().Where(x => x.IsInCategory(ItemCategory.Bottle))
.Shuffle(new Random().Sanitize()).Take(2)
.ToList();
WaterfallFairyTrade = bottleItems.First();
PyramidFairyTrade = bottleItems.Last();
}

/// <summary>
/// Gets or sets the name of the file from which the plando config was
/// deserialized.
/// </summary>
[YamlIgnore]
[YamlIgnore, Newtonsoft.Json.JsonIgnore]
public string FileName { get; set; } = "";

[YamlMember(ScalarStyle = ScalarStyle.DoubleQuoted)]
public string Seed { get; set; } = "";

/// <summary>
Expand Down Expand Up @@ -119,6 +134,16 @@ public PlandoConfig(World world)
/// </summary>
public PlandoZeldaPrizeConfig ZeldaPrizes { get; set; } = new();

/// <summary>
/// Bottle trade offer with the waterfall fairy
/// </summary>
public ItemType? WaterfallFairyTrade { get; set; }

/// <summary>
/// Bottle trade offer with the pyramid fairy
/// </summary>
public ItemType? PyramidFairyTrade { get; set; }

/// <summary>
/// Item Options for the starting inventory
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/TrackerCouncil.Smz3.Data/Options/PlandoTextConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class PlandoTextConfig

public string? HintTileSouthEastDarkworldCave { get; init; }

[YamlIgnore]
[YamlIgnore, Newtonsoft.Json.JsonIgnore]
public bool HasHintTileText => !string.IsNullOrEmpty(HintTileEasternPalace) ||
!string.IsNullOrEmpty(HintTileTowerOfHeraFloor4) ||
!string.IsNullOrEmpty(HintTileSpectacleRock) ||
Expand Down
37 changes: 1 addition & 36 deletions src/TrackerCouncil.Smz3.Data/Options/Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,40 +117,5 @@ public override string ToString()
return string.IsNullOrEmpty(Author) ? Name : $"{Name} by {Author}";
}

public static string SpritePath
{
get
{
#if DEBUG
var parentDir = new DirectoryInfo(SolutionPath).Parent;
var spriteRepo = parentDir?.GetDirectories().FirstOrDefault(x => x.Name == "SMZ3CasSprites");
var path = Path.Combine(spriteRepo?.FullName ?? "", "Sprites");

if (!Directory.Exists(path) || path == "Sprites")
{
return Path.Combine(AppContext.BaseDirectory, "Sprites");
}

return path;
#else
return Path.Combine(AppContext.BaseDirectory, "Sprites");
#endif
}

}

private static string SolutionPath
{
get
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());

while (directory != null && !directory.GetFiles("*.sln").Any())
{
directory = directory.Parent;
}

return Path.Combine(directory!.FullName);
}
}
public static string SpritePath => RandomizerDirectories.SpritePath;
}
69 changes: 69 additions & 0 deletions src/TrackerCouncil.Smz3.Data/RandomizerDirectories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.IO;
using System.Linq;

namespace TrackerCouncil.Smz3.Data;

public class RandomizerDirectories
{
public static string SolutionPath
{
get
{
#if DEBUG
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());

while (directory != null && !directory.GetFiles("*.sln").Any())
{
directory = directory.Parent;
}

return Path.Combine(directory!.FullName);
#else
throw new InvalidOperationException("This method should only be called in debug mode.");
#endif
}
}

public static string ConfigPath
{
get
{
#if DEBUG
var parentDir = new DirectoryInfo(SolutionPath).Parent;
var configRepo = parentDir?.GetDirectories().FirstOrDefault(x => x.Name == "SMZ3CasConfigs");
var basePath = Path.Combine(configRepo?.FullName ?? "", "Profiles");

if (!Directory.Exists(basePath))
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SMZ3CasRandomizer", "Configs");
}

return basePath;
#else
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SMZ3CasRandomizer", "Configs");
#endif
}
}

public static string SpritePath
{
get
{
#if DEBUG
var parentDir = new DirectoryInfo(SolutionPath).Parent;
var spriteRepo = parentDir?.GetDirectories().FirstOrDefault(x => x.Name == "SMZ3CasSprites");
var path = Path.Combine(spriteRepo?.FullName ?? "", "Sprites");

if (!Directory.Exists(path) || path == "Sprites")
{
return Path.Combine(AppContext.BaseDirectory, "Sprites");
}

return path;
#else
return Path.Combine(AppContext.BaseDirectory, "Sprites");
#endif
}
}
}
Loading

0 comments on commit 9bfeadc

Please sign in to comment.