Skip to content

Commit

Permalink
[HS-1341] Migrate server constants to XML config
Browse files Browse the repository at this point in the history
  • Loading branch information
baughj committed Aug 22, 2023
1 parent 1fb1dce commit 57fafb4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
36 changes: 36 additions & 0 deletions src/Extensions/ServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,40 @@ public void InitializeClientSettings()

public string GetSettingLabel(byte number) => SettingsNumberIndex[number].Value;
public byte GetSettingNumber(string key) => SettingsKeyIndex[key.ToLower()].Number;

private Dictionary<byte, string> ClassNames { get; set; } = new();
private Dictionary<string, byte> ClassIds { get; set; } = new();

private void GenerateIndex()
{
ClassNames.Clear();
ClassNames[0] = Constants.ClassName0;
ClassNames[1] = Constants.ClassName1;
ClassNames[2] = Constants.ClassName2;
ClassNames[3] = Constants.ClassName3;
ClassNames[4] = Constants.ClassName4;
ClassNames[5] = Constants.ClassName5;
ClassIds[Constants.ClassName0] = 0;
ClassIds[Constants.ClassName1] = 1;
ClassIds[Constants.ClassName2] = 2;
ClassIds[Constants.ClassName3] = 3;
ClassIds[Constants.ClassName4] = 4;
ClassIds[Constants.ClassName5] = 5;
}

public byte GetClassId(string name)
{
if (!ClassNames.Any())
GenerateIndex();
return ClassIds[name];

}

public string GetClassName(byte id)
{
if (!ClassIds.Any())
GenerateIndex();
return ClassNames[id];
}

}
4 changes: 2 additions & 2 deletions src/Hybrasyl.Xml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>Hybrasyl.Xml</AssemblyName>
<RootNamespace>Hybrasyl.Xml</RootNamespace>
<PackageVersion>0.9.3.17</PackageVersion>
<Version>0.9.3.17</Version>
<PackageVersion>0.9.3.18</PackageVersion>
<Version>0.9.3.18</Version>
<BuildDocFx Condition="'$(Configuration)'=='Debug'">false</BuildDocFx>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Authors>ERISCO LLC</Authors>
Expand Down
1 change: 1 addition & 0 deletions src/Objects/ServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public partial class ServerConfig : HybrasylEntity<ServerConfig>

public ServerConfig()
{
_constants = new ServerConstants();
_apiEndpoints = new ApiEndpoints();
_network = new Network();
_dataStore = new DataStore();
Expand Down
2 changes: 1 addition & 1 deletion src/XSD/ServerConfig.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
<xs:element name="Motd" type="hyb:String16" minOccurs="0" maxOccurs="1" />
<xs:element name="Plugins" type="hyb:ServerPlugins" minOccurs="0" maxOccurs="1" />
<xs:element name="ClientSettings" type="hyb:ClientSettings" minOccurs="0" maxOccurs="1" />
<xs:element name="Constants" type="hyb:ServerConstants" minOccurs="0" maxOccurs="1" />
<xs:element name="Constants" type="hyb:ServerConstants" minOccurs="1" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="Name" type="xs:string" default="default" />
<xs:attribute name="ElementTable" type="xs:string" default="default" />
Expand Down
8 changes: 8 additions & 0 deletions tests/XmlEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,12 @@ public void EquipmentRestrictionWeaponTypeDefaultNone()
Assert.Equal(WeaponType.None, castable.Restrictions.First().WeaponType);
}

[Fact]
public void ServerClassNames()
{
var config = fixture.SyncManager.Values<ServerConfig>().First();
Assert.NotNull(config);
Assert.Equal(config.Constants.ClassName0, config.GetClassName(0));
Assert.Equal(0,config.GetClassId(config.Constants.ClassName0));
}
}

0 comments on commit 57fafb4

Please sign in to comment.