Skip to content

Commit

Permalink
Custom sample values
Browse files Browse the repository at this point in the history
  • Loading branch information
Equinox- committed Jun 4, 2024
1 parent 5ba608a commit 1f04f17
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 35 deletions.
14 changes: 14 additions & 0 deletions SchemaBuilder/SchemaConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,25 @@ public class MemberPatch : IInheritable<MemberPatch>
[XmlElement]
public string Documentation;

public const string HiddenSampleValue = "__omit__";

[XmlAttribute]
public bool HideSample
{
get => Sample == HiddenSampleValue;
set => Sample = HiddenSampleValue;
}

[XmlElement]
public string Sample;

public void InheritFrom(MemberPatch other)
{
Delete = Delete.OrInherit(other.Delete);
Optional = Optional.OrInherit(other.Optional);
HideSample = HideSample.OrInherit(other.HideSample);
Documentation ??= other.Documentation;
Sample ??= other.Sample;
}
}

Expand Down
2 changes: 1 addition & 1 deletion SchemaBuilder/SchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task Generate(string name)
Postprocess(postprocessArgs);

var ir = SchemaIrCompiler.Compile(postprocessArgs.Schema);
SchemaIrConfig.ApplyConfig(ir, config);
SchemaIrConfig.ApplyConfig(ir, info, config);
SchemaIrDocumentation.InjectXmlDocumentation(ir, info, _docs);
// Write the IR file.
WriteIr(ir, Path.Combine("schemas", name + ".json"));
Expand Down
35 changes: 22 additions & 13 deletions SchemaBuilder/SchemaIr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public sealed class ObjectTypeIr : TypeIr
public Dictionary<string, PropertyIr> Attributes = new Dictionary<string, PropertyIr>();
}

public sealed class PropertyIr : BaseElementIr
{
[JsonPropertyName("type")]
public TypeReferenceIr Type;

[JsonPropertyName("default")]
public string DefaultValue;

[JsonPropertyName("sample")]
public string SampleValue;
}

public sealed class EnumTypeIr : TypeIr
{
[JsonPropertyName("flags")]
Expand Down Expand Up @@ -71,6 +83,12 @@ public abstract class TypeReferenceIr : IEquatable<TypeReferenceIr>
public bool Equals(TypeReferenceIr other) => Equals((object)other);
}

[JsonDerivedType(typeof(PrimitiveTypeReferenceIr), "primitive")]
[JsonDerivedType(typeof(CustomTypeReferenceIr), "custom")]
public abstract class ItemTypeReferenceIr : TypeReferenceIr
{
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum PrimitiveTypeIr
{
Expand All @@ -80,7 +98,7 @@ public enum PrimitiveTypeIr
Integer,
}

public sealed class PrimitiveTypeReferenceIr : TypeReferenceIr, IEquatable<PrimitiveTypeReferenceIr>
public sealed class PrimitiveTypeReferenceIr : ItemTypeReferenceIr, IEquatable<PrimitiveTypeReferenceIr>
{
[JsonPropertyName("type")]
public PrimitiveTypeIr Type;
Expand All @@ -94,7 +112,7 @@ public sealed class PrimitiveTypeReferenceIr : TypeReferenceIr, IEquatable<Primi
public override string ToString() => $"Primitive[{Type}]";
}

public sealed class CustomTypeReferenceIr : TypeReferenceIr, IEquatable<CustomTypeReferenceIr>
public sealed class CustomTypeReferenceIr : ItemTypeReferenceIr, IEquatable<CustomTypeReferenceIr>
{
[JsonPropertyName("name")]
public string Name;
Expand All @@ -111,7 +129,7 @@ public sealed class CustomTypeReferenceIr : TypeReferenceIr, IEquatable<CustomTy
public sealed class ArrayTypeReferenceIr : TypeReferenceIr, IEquatable<ArrayTypeReferenceIr>
{
[JsonPropertyName("item")]
public TypeReferenceIr Item;
public ItemTypeReferenceIr Item;

[JsonPropertyName("wrapperElement")]
public string WrapperElement;
Expand All @@ -128,7 +146,7 @@ public sealed class ArrayTypeReferenceIr : TypeReferenceIr, IEquatable<ArrayType
public sealed class OptionalTypeReferenceIr : TypeReferenceIr, IEquatable<OptionalTypeReferenceIr>
{
[JsonPropertyName("item")]
public TypeReferenceIr Item;
public ItemTypeReferenceIr Item;

public bool Equals(OptionalTypeReferenceIr other) => other != null && Equals(Item, other.Item);

Expand All @@ -138,13 +156,4 @@ public sealed class OptionalTypeReferenceIr : TypeReferenceIr, IEquatable<Option

public override string ToString() => $"Optional[{Item}]";
}

public sealed class PropertyIr : BaseElementIr
{
[JsonPropertyName("type")]
public TypeReferenceIr Type;

[JsonPropertyName("default")]
public string DefaultValue;
}
}
31 changes: 18 additions & 13 deletions SchemaBuilder/SchemaIrCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,34 @@ void IndexAttributes(XmlSchemaObjectCollection attributes)

private static PropertyIr CompileElement(XmlSchemaElement element)
{
var itemType = CompileTypeReference(element.SchemaTypeName);
var result = new PropertyIr
{
DefaultValue = element.DefaultValue,
Type = CompileTypeReference(element.SchemaTypeName)
DefaultValue = element.DefaultValue
};
AttachDocumentation(result, element);
if (element.MaxOccurs > 1)
result.Type = new ArrayTypeReferenceIr { Item = result.Type };
result.Type = new ArrayTypeReferenceIr { Item = itemType };
else if (element.MinOccurs == 0)
result.Type = new OptionalTypeReferenceIr { Item = result.Type };
result.Type = new OptionalTypeReferenceIr { Item = itemType };
else
result.Type = itemType;
return result;
}

private static PropertyIr CompileAttribute(XmlSchemaAttribute attribute)
{
var itemType = CompileTypeReference(attribute.SchemaTypeName);
var result = new PropertyIr
{
DefaultValue = attribute.DefaultValue,
Type = CompileTypeReference(attribute.SchemaTypeName)
Type = attribute.Use switch
{
XmlSchemaUse.Optional => new OptionalTypeReferenceIr { Item = itemType },
_ => itemType
}
};
AttachDocumentation(result, attribute);
result.Type = attribute.Use switch
{
XmlSchemaUse.Optional => new OptionalTypeReferenceIr { Item = result.Type },
_ => result.Type
};
return result;
}

Expand Down Expand Up @@ -141,7 +143,7 @@ TypeIr CompileSimpleTypeList(XmlSchemaSimpleTypeList list)
}
}

private static TypeReferenceIr CompileTypeReference(XmlQualifiedName typeName)
private static ItemTypeReferenceIr CompileTypeReference(XmlQualifiedName typeName)
{
if (typeName.TryPrimitiveIrFromXsd(out var primitive))
return new PrimitiveTypeReferenceIr { Type = primitive };
Expand Down Expand Up @@ -219,10 +221,13 @@ TypeReferenceIr FixRef(TypeReferenceIr reference)
switch (replacement(reference))
{
case ArrayTypeReferenceIr array:
array.Item = FixRef(array.Item);
array.Item = (ItemTypeReferenceIr)FixRef(array.Item);
return array;
case OptionalTypeReferenceIr optional:
optional.Item = FixRef(optional.Item);
var fixedRef = FixRef(optional.Item);
if (!(fixedRef is ItemTypeReferenceIr fixedItem))
return fixedRef;
optional.Item = fixedItem;
return optional;
case PrimitiveTypeReferenceIr primitive:
return primitive;
Expand Down
17 changes: 9 additions & 8 deletions SchemaBuilder/SchemaIrConfig.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace SchemaBuilder
{
public static class SchemaIrConfig
{
public static void ApplyConfig(SchemaIr ir, SchemaConfig config)
public static void ApplyConfig(SchemaIr ir, XmlInfo info, SchemaConfig config)
{
foreach (var type in ir.Types)
{
var patch = config.TypePatch(type.Key);
if (patch == null && info.TryGetTypeByXmlName(type.Key, out var typeInfo))
patch = config.TypePatch(typeInfo.Type.FullName) ?? config.TypePatch(typeInfo.Type.Name);
if (patch != null)
ApplyType(patch, type.Value);
}
Expand Down Expand Up @@ -64,17 +65,17 @@ void ApplyProperties(Dictionary<string, PropertyIr> properties, Func<string, Mem
foreach (var prop in properties)
{
var patch = getter(prop.Key);
if (patch == null) continue;
if (patch.Delete == InheritableTrueFalse.True) removing.Add(prop.Key);
if (!string.IsNullOrEmpty(patch.Documentation)) prop.Value.Documentation = patch.Documentation;
if (patch?.Delete == InheritableTrueFalse.True) removing.Add(prop.Key);
if (!string.IsNullOrEmpty(patch?.Documentation)) prop.Value.Documentation = patch.Documentation;
if (!string.IsNullOrEmpty(patch?.Sample)) prop.Value.SampleValue = patch.Sample;

switch (patch.Optional.OrInherit(config.AllOptional))
switch ((patch?.Optional).OrInherit(config.AllOptional))
{
case InheritableTrueFalse.Inherit:
break;
case InheritableTrueFalse.True:
if (!(prop.Value.Type is OptionalTypeReferenceIr))
prop.Value.Type = new OptionalTypeReferenceIr { Item = prop.Value.Type };
if (prop.Value.Type is ItemTypeReferenceIr item)
prop.Value.Type = new OptionalTypeReferenceIr { Item = item };
break;
case InheritableTrueFalse.False:
if (prop.Value.Type is OptionalTypeReferenceIr opt)
Expand Down
1 change: 1 addition & 0 deletions SchemaGenerator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Patches", "Patches", "{E470
patches\medieval.xml = patches\medieval.xml
patches\space.xml = patches\space.xml
patches\medieval-modded.xml = patches\medieval-modded.xml
patches\shared.xml = patches\shared.xml
EndProjectSection
EndProject
Global
Expand Down
2 changes: 2 additions & 0 deletions patches/medieval.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<Patches>
<Include>shared</Include>

<Game>medieval-engineers</Game>
<SteamBranch>communityedition</SteamBranch>

Expand Down
12 changes: 12 additions & 0 deletions patches/shared.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='utf-8'?>
<Patches>
<Type Name="SerializableVector3">
<Member Name="element:X" HideSample="true" />
<Member Name="element:Y" HideSample="true" />
<Member Name="element:Z" HideSample="true" />

<Member Name="attribute:x" Sample="1.2" />
<Member Name="attribute:y" Sample="3.4" />
<Member Name="attribute:z" Sample="5.6" />
</Type>
</Patches>
2 changes: 2 additions & 0 deletions patches/space.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<Patches>
<Include>shared</Include>

<Game>space-engineers</Game>
<SteamBranch>public</SteamBranch>

Expand Down

0 comments on commit 1f04f17

Please sign in to comment.