-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from mbbsemu/modules-config-basepath
Modules Config "BasePath"
- Loading branch information
Showing
8 changed files
with
265 additions
and
4 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
MBBSEmu.Tests/Assets/Module_Single_NoPatch_BasePath_Linux.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"BasePath" : "/dos/", | ||
"Modules": [ | ||
{ | ||
"Identifier": "MBBSEMU", | ||
"Path": "modules/mbbsemu/", | ||
"Enabled": "true" | ||
} | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
MBBSEmu.Tests/Assets/Module_Single_NoPatch_BasePath_Windows.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"BasePath" : "c:\\dos\\", | ||
"Modules": [ | ||
{ | ||
"Identifier": "MBBSEMU", | ||
"Path": "modules\\mbbsemu\\", | ||
"Enabled": "true" | ||
} | ||
] | ||
} |
105 changes: 105 additions & 0 deletions
105
MBBSEmu.Tests/Converters/JsonModuleConfigurationFileConverter_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using MBBSEmu.Converters; | ||
using MBBSEmu.Module; | ||
using MBBSEmu.Resources; | ||
using System; | ||
using System.Text.Json; | ||
using Xunit; | ||
|
||
namespace MBBSEmu.Tests.Converters | ||
{ | ||
public class JsonModuleConfigurationFileConverter_Tests : TestBase | ||
{ | ||
[Fact] | ||
public void Module_Single_NoPatch_BasePath() | ||
{ | ||
var resourceManager = ResourceManager.GetTestResourceManager(); | ||
var jsonToDeserialize = string.Empty; | ||
var expectedPath = ""; | ||
|
||
//Determine Platform and using a switch, set the file to be opened using resourceManager and the resulting BasePath | ||
switch (Environment.OSVersion.Platform) | ||
{ | ||
case PlatformID.Win32Windows: | ||
case PlatformID.Win32S: | ||
case PlatformID.WinCE: | ||
case PlatformID.Win32NT: | ||
|
||
jsonToDeserialize = | ||
resourceManager.GetString("MBBSEmu.Tests.Assets.Module_Single_NoPatch_BasePath_Windows.json"); | ||
expectedPath = @"c:\dos\modules\mbbsemu\"; | ||
break; | ||
case PlatformID.MacOSX: | ||
case PlatformID.Unix: | ||
jsonToDeserialize = | ||
resourceManager.GetString("MBBSEmu.Tests.Assets.Module_Single_NoPatch_BasePath_Linux.json"); | ||
expectedPath = "/dos/modules/mbbsemu/"; | ||
break; | ||
default: | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
|
||
var options = new JsonSerializerOptions | ||
{ | ||
Converters = | ||
{ | ||
new JsonModuleConfigurationFileConverter(), | ||
new JsonBooleanConverter() | ||
} | ||
}; | ||
|
||
var result = JsonSerializer.Deserialize<ModuleConfigurationFile>(jsonToDeserialize, options); | ||
Assert.NotNull(result); | ||
Assert.Single(result.Modules); | ||
|
||
var module = result.Modules[0]; | ||
Assert.Equal(expectedPath, module.ModulePath); | ||
} | ||
|
||
[Fact] | ||
public void Module_Single_NoPatch_BasePath_NoConverter() | ||
{ | ||
var resourceManager = ResourceManager.GetTestResourceManager(); | ||
var jsonToDeserialize = string.Empty; | ||
var expectedPath = ""; | ||
|
||
//Determine Platform and using a switch, set the file to be opened using resourceManager and the resulting BasePath | ||
switch (Environment.OSVersion.Platform) | ||
{ | ||
case PlatformID.Win32Windows: | ||
case PlatformID.Win32S: | ||
case PlatformID.WinCE: | ||
case PlatformID.Win32NT: | ||
|
||
jsonToDeserialize = | ||
resourceManager.GetString("MBBSEmu.Tests.Assets.Module_Single_NoPatch_BasePath_Windows.json"); | ||
expectedPath = @"modules\mbbsemu\"; | ||
break; | ||
case PlatformID.MacOSX: | ||
case PlatformID.Unix: | ||
jsonToDeserialize = | ||
resourceManager.GetString("MBBSEmu.Tests.Assets.Module_Single_NoPatch_BasePath_Linux.json"); | ||
expectedPath = "modules/mbbsemu/"; | ||
break; | ||
default: | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
var options = new JsonSerializerOptions | ||
{ | ||
Converters = | ||
{ | ||
new JsonBooleanConverter() | ||
} | ||
}; | ||
|
||
|
||
var result = JsonSerializer.Deserialize<ModuleConfigurationFile>(jsonToDeserialize, options); | ||
Assert.NotNull(result); | ||
Assert.Single(result.Modules); | ||
|
||
var module = result.Modules[0]; | ||
Assert.Equal(expectedPath, module.ModulePath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using MBBSEmu.Module; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MBBSEmu.Converters | ||
{ | ||
/// <summary> | ||
/// Custom JSON Converter used to load Module Configuration Files and propagate overriding values (Path, etc.) to | ||
/// each Module defined in the configuration file. | ||
/// </summary> | ||
public class JsonModuleConfigurationFileConverter : JsonConverter<ModuleConfigurationFile> | ||
{ | ||
public override ModuleConfigurationFile Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException("Expected a JSON object."); | ||
} | ||
|
||
var config = new ModuleConfigurationFile(); | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
// Ensure that the modules are initialized even if not present in the JSON. | ||
config.Modules ??= new List<ModuleConfiguration>(); | ||
return config; | ||
} | ||
|
||
// Get the property name. | ||
if (reader.TokenType != JsonTokenType.PropertyName) | ||
{ | ||
throw new JsonException("Expected a property name."); | ||
} | ||
|
||
var propertyName = reader.GetString(); | ||
reader.Read(); // Move to the property value. | ||
|
||
switch (propertyName) | ||
{ | ||
case nameof(ModuleConfigurationFile.BasePath): | ||
{ | ||
config.BasePath = JsonSerializer.Deserialize<string>(ref reader, options); | ||
|
||
//Set to PWD if "." is specified | ||
if (config.BasePath == ".") | ||
config.BasePath = System.IO.Directory.GetCurrentDirectory(); | ||
} | ||
break; | ||
case nameof(ModuleConfigurationFile.Modules): | ||
var modules = JsonSerializer.Deserialize<List<JsonElement>>(ref reader, options); | ||
if (modules != null) | ||
{ | ||
config.Modules = new List<ModuleConfiguration>(modules.Count); | ||
foreach (var element in modules) | ||
{ | ||
var moduleJson = element.GetRawText(); | ||
var module = JsonSerializer.Deserialize<ModuleConfiguration>(moduleJson, options); | ||
module.BasePath = config.BasePath ?? ""; | ||
config.Modules.Add(module); | ||
} | ||
} | ||
break; | ||
default: | ||
throw new JsonException($"Property '{propertyName}' is not supported."); | ||
} | ||
} | ||
|
||
throw new JsonException("Expected a JSON object end."); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, ModuleConfigurationFile value, JsonSerializerOptions options) | ||
{ | ||
throw new NotSupportedException("This converter does not support writing JSON."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters