Skip to content

Commit

Permalink
write reference config for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
compujuckel committed Jan 21, 2024
1 parent aaf6249 commit 1fccd20
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 40 deletions.
9 changes: 7 additions & 2 deletions AssettoServer/Server/Configuration/ACServerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public ACServerConfiguration(string? preset, ConfigurationLocations locations, b

var extraCfgSchemaPath = ConfigurationSchemaGenerator.WriteExtraCfgSchema();
LoadExtraConfig(locations.ExtraCfgPath, extraCfgSchemaPath);
ACExtraConfiguration.WriteReferenceConfig(extraCfgSchemaPath);
ReferenceConfigurationHelper.WriteReferenceConfiguration("extra_cfg.reference.yml",
extraCfgSchemaPath,
ACExtraConfiguration.ReferenceConfiguration,
$"AssettoServer {ThisAssembly.AssemblyInformationalVersion}");

var parsedTrackOptions = CSPTrackOptions = CSPTrackOptions.Parse(Server.Track);
if (Extra.MinimumCSPVersion.HasValue)
Expand Down Expand Up @@ -208,9 +211,11 @@ internal void LoadPluginConfiguration(ACPluginLoader loader, ContainerBuilder bu
{
foreach (var plugin in loader.LoadedPlugins)
{
if (plugin.ConfigurationType == null) continue;
if (!plugin.HasConfiguration) continue;

var schemaPath = ConfigurationSchemaGenerator.WritePluginConfigurationSchema(plugin);
ReferenceConfigurationHelper.WriteReferenceConfiguration(plugin.ReferenceConfigurationFileName, schemaPath, plugin.ReferenceConfiguration, plugin.Name);

var configPath = Path.Join(BaseFolder, plugin.ConfigurationFileName);
if (File.Exists(configPath))
{
Expand Down
37 changes: 3 additions & 34 deletions AssettoServer/Server/Configuration/Extra/ACExtraConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,15 @@ public partial class ACExtraConfiguration : ObservableObject
[YamlIgnore] public int MaxAfkTimeMilliseconds => MaxAfkTimeMinutes * 60_000;
[YamlIgnore] internal bool ContainsObsoletePluginConfiguration { get; private set; }

public void ToFile(string path, bool full = false)
public void ToFile(string path)
{
using var writer = File.CreateText(path);
ToStream(writer, full);
ToStream(writer);
}

public void ToStream(StreamWriter writer, bool full = false)
public void ToStream(StreamWriter writer)
{
var builder = new SerializerBuilder();
if (full)
{
builder.WithoutEmissionPhaseObjectGraphVisitor<DefaultValuesObjectGraphVisitor>();
}
builder.Build().Serialize(writer, this);
}

Expand All @@ -142,33 +138,6 @@ public static ACExtraConfiguration FromFile(string path)

return extraCfg;
}

public static void WriteReferenceConfig(string schemaPath)
{
const string baseFolder = "cfg";
var path = Path.Join(baseFolder, "extra_cfg.reference.yml");

FileInfo? info = null;
if (File.Exists(path))
{
info = new FileInfo(path);
info.IsReadOnly = false;
}

using (var writer = File.CreateText(path))
{
ConfigurationSchemaGenerator.WriteModeLine(writer, baseFolder, schemaPath);
writer.WriteLine($"# AssettoServer {ThisAssembly.AssemblyInformationalVersion} Reference Configuration");
writer.WriteLine("# This file serves as an overview of all possible options with their default values.");
writer.WriteLine("# It is NOT read by the server - edit extra_cfg.yml instead!");
writer.WriteLine();

ReferenceConfiguration.ToStream(writer, true);
}

info ??= new FileInfo(path);
info.IsReadOnly = true;
}

public static readonly ACExtraConfiguration ReferenceConfiguration = new()
{
Expand Down
41 changes: 41 additions & 0 deletions AssettoServer/Server/Configuration/ReferenceConfigurationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.ObjectGraphVisitors;

namespace AssettoServer.Server.Configuration;

public static class ReferenceConfigurationHelper
{
private const string BaseFolder = "cfg/reference";

public static void WriteReferenceConfiguration(string filename, string schemaPath, object config, string name)
{
Directory.CreateDirectory(BaseFolder);

var path = Path.Join(BaseFolder, filename);

FileInfo? info = null;
if (File.Exists(path))
{
info = new FileInfo(path);
info.IsReadOnly = false;
}

using (var writer = File.CreateText(path))
{
ConfigurationSchemaGenerator.WriteModeLine(writer, BaseFolder, schemaPath);
writer.WriteLine($"# {name} Reference Configuration");
writer.WriteLine("# This file serves as an overview of all possible options with their default values.");
writer.WriteLine();

var builder = new SerializerBuilder()
.WithoutEmissionPhaseObjectGraphVisitor<DefaultValuesObjectGraphVisitor>()
.Build();

builder.Serialize(writer, config);
}

info ??= new FileInfo(path);
info.IsReadOnly = true;
}
}
4 changes: 3 additions & 1 deletion AssettoServer/Server/Plugin/ACPluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ private void LoadPlugin(string name)
ConfigurationType = configType,
ValidatorType = validatorType,
ConfigurationFileName = configType != null ? ConfigurationTypeToFilename(configType.Name) : null,
SchemaFileName = configType != null ? ConfigurationTypeToFilename(configType.Name, "schema.json") : null
SchemaFileName = configType != null ? ConfigurationTypeToFilename(configType.Name, "schema.json") : null,
ReferenceConfigurationFileName = configType != null ? ConfigurationTypeToFilename(configType.Name, "reference.yml") : null,
ReferenceConfiguration = instance.ReferenceConfiguration
});
}
}
Expand Down
6 changes: 4 additions & 2 deletions AssettoServer/Server/Plugin/AssettoServerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ namespace AssettoServer.Server.Plugin;
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public abstract class AssettoServerModule : Module
{
public virtual object? ReferenceConfiguration => null;

public virtual void ConfigureServices(IServiceCollection services)
{

}
}

public abstract class AssettoServerModule<T> : AssettoServerModule
public abstract class AssettoServerModule<TConfig> : AssettoServerModule where TConfig : new()
{

public override object? ReferenceConfiguration => new TConfig();
}
4 changes: 3 additions & 1 deletion AssettoServer/Server/Plugin/LoadedPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ public class LoadedPlugin
public required string Name { get; init; }
public required Assembly Assembly { get; init; }
public required AssettoServerModule Instance { get; init; }
[MemberNotNullWhen(true, nameof(ConfigurationFileName), nameof(SchemaFileName), nameof(ConfigurationType))]
[MemberNotNullWhen(true, nameof(ConfigurationFileName), nameof(SchemaFileName), nameof(ConfigurationType), nameof(ReferenceConfigurationFileName), nameof(ReferenceConfiguration))]
public bool HasConfiguration => ConfigurationType != null;
public Type? ConfigurationType { get; init; }
public Type? ValidatorType { get; init; }
public string? ConfigurationFileName { get; init; }
public string? SchemaFileName { get; init; }
public string? ReferenceConfigurationFileName { get; init; }
public object? ReferenceConfiguration { get; init; }
}

0 comments on commit 1fccd20

Please sign in to comment.