-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad30801
commit 86d15bb
Showing
8 changed files
with
536 additions
and
18 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project> | ||
<PropertyGroup> | ||
<Urho3DNetVersion>0.3.7.802</Urho3DNetVersion> | ||
<Urho3DNetVersion>0.3.7.805</Urho3DNetVersion> | ||
</PropertyGroup> | ||
</Project> |
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,113 @@ | ||
// Copyright (c) 2024-2024 the rbfx project. | ||
// This work is licensed under the terms of the MIT license. | ||
// For a copy, see <https://opensource.org/licenses/MIT> or the accompanying LICENSE file. | ||
|
||
using RbfxTemplate.Tests; | ||
using RbfxTemplate.Utils; | ||
using Xunit; | ||
|
||
namespace Urho3DNet.Tests | ||
{ | ||
public enum TestEnum | ||
{ | ||
A,B,C | ||
} | ||
public class TestContainer | ||
{ | ||
public Vector2 Vector2 { get; set; } | ||
public Vector3 Vector3 { get; set; } | ||
public Vector4 Vector4 { get; set; } | ||
public Quaternion Quaternion { get; set; } | ||
public IntVector2 IntVector2 { get; set; } | ||
public IntVector3 IntVector3 { get; set; } | ||
public BoundingBox BoundingBox { get; set; } | ||
public TestEnum Enum { get; set; } | ||
public Material? Material { get; set; } | ||
|
||
public TestContainer() | ||
{ | ||
} | ||
|
||
public TestContainer(Material mat) | ||
{ | ||
Vector2 = new Vector2(1, 2); | ||
Vector3 = new Vector3(1, 2, 3); | ||
Vector4 = new Vector4(1, 2, 3, 4); | ||
Quaternion = new Quaternion(10, Vector3.Down); | ||
IntVector2 = new IntVector2(1, 2); | ||
IntVector3 = new IntVector3(1, 2, 3); | ||
BoundingBox = new BoundingBox(new Vector3(-1, -2, -3), new Vector3(1, 2, 3)); | ||
Enum = TestEnum.B; | ||
Material = mat; | ||
} | ||
} | ||
|
||
[ObjectFactory] | ||
public class TestResource : JsonResource<TestContainer> | ||
{ | ||
public TestResource(Context context) : base(context) | ||
{ | ||
} | ||
} | ||
public class JsonResourceTests | ||
{ | ||
[Fact] | ||
public async Task SaveSimpleResource() | ||
{ | ||
await RbfxTestFramework.Context.ToMainThreadAsync(); | ||
|
||
var mat = new Material(RbfxTestFramework.Context); | ||
mat.Name = "TestResource.material"; | ||
RbfxTestFramework.Context.ResourceCache.AddManualResource(mat); | ||
|
||
var res = new TestResource(RbfxTestFramework.Context); | ||
var testContainer = new TestContainer(mat); | ||
|
||
res.Value = testContainer; | ||
var fileIdentifier = new FileIdentifier("conf", "SaveJson.json"); | ||
res.SaveFile(fileIdentifier); | ||
res.Value = default; | ||
res.LoadFile(fileIdentifier); | ||
Assert.NotNull(res.Value); | ||
Assert.Equal(testContainer.IntVector2, res.Value.IntVector2); | ||
Assert.Equal(testContainer.IntVector3, res.Value.IntVector3); | ||
Assert.Equal(testContainer.Vector2, res.Value.Vector2); | ||
Assert.Equal(testContainer.Vector3, res.Value.Vector3); | ||
Assert.Equal(testContainer.Vector4, res.Value.Vector4); | ||
Assert.Equal(testContainer.Quaternion, res.Value.Quaternion); | ||
Assert.Equal(testContainer.BoundingBox, res.Value.BoundingBox); | ||
Assert.Equal(testContainer.Enum, res.Value.Enum); | ||
Assert.Equal(testContainer.Material, res.Value.Material); | ||
var path = RbfxTestFramework.Context.VirtualFileSystem.GetAbsoluteNameFromIdentifier(fileIdentifier); | ||
System.IO.File.Delete(path); | ||
} | ||
|
||
[Fact] | ||
public async Task SaveConfigResource() | ||
{ | ||
await RbfxTestFramework.Context.ToMainThreadAsync(); | ||
|
||
var mat = new Material(RbfxTestFramework.Context); | ||
mat.Name = "TestResource2.material"; | ||
RbfxTestFramework.Context.ResourceCache.AddManualResource(mat); | ||
|
||
using var conf1 = new ConfigFileContainer<TestContainer>(RbfxTestFramework.Context); | ||
conf1.Value = new TestContainer(mat); | ||
conf1.SaveConfig(); | ||
|
||
using var conf2 = ConfigFileContainer<TestContainer>.LoadConfig(RbfxTestFramework.Context); | ||
|
||
Assert.Equal(conf1.Value.IntVector2, conf2.Ptr.Value.IntVector2); | ||
Assert.Equal(conf1.Value.IntVector3, conf2.Ptr.Value.IntVector3); | ||
Assert.Equal(conf1.Value.Vector2, conf2.Ptr.Value.Vector2); | ||
Assert.Equal(conf1.Value.Vector3, conf2.Ptr.Value.Vector3); | ||
Assert.Equal(conf1.Value.Vector4, conf2.Ptr.Value.Vector4); | ||
Assert.Equal(conf1.Value.Quaternion, conf2.Ptr.Value.Quaternion); | ||
Assert.Equal(conf1.Value.BoundingBox, conf2.Ptr.Value.BoundingBox); | ||
Assert.Equal(conf1.Value.Enum, conf2.Ptr.Value.Enum); | ||
Assert.Equal(conf1.Value.Material, conf2.Ptr.Value.Material); | ||
var path = RbfxTestFramework.Context.VirtualFileSystem.GetAbsoluteNameFromIdentifier(new FileIdentifier("conf", conf2.Ptr.Name)); | ||
System.IO.File.Delete(path); | ||
} | ||
} | ||
} |
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
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,58 @@ | ||
using Urho3DNet; | ||
|
||
namespace RbfxTemplate.Utils | ||
{ | ||
/// <summary> | ||
/// Non-abstract wrapper for config files. | ||
/// This will never work as a resource because generic types can't be registered in engine reflection. | ||
/// Use this only via LoadConfig/SaveConfig API. | ||
/// </summary> | ||
public sealed class ConfigFileContainer<TValue> : JsonResource<TValue> where TValue : new() | ||
{ | ||
public ConfigFileContainer(Context context) : base(context) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Load resource from config folder. | ||
/// </summary> | ||
/// <param name="context">Engine context.</param> | ||
/// <param name="configFileName">Config file name. If null then config value type is used as a file name.</param> | ||
public static SharedPtr<ConfigFileContainer<TValue>> LoadConfig(Context context, string configFileName = null) | ||
{ | ||
configFileName = configFileName ?? GetDefaultFileName(); | ||
var fileId = new FileIdentifier("conf", configFileName); | ||
|
||
SharedPtr<ConfigFileContainer<TValue>> ptr = new ConfigFileContainer<TValue>(context); | ||
ptr.Ptr.Name = configFileName; | ||
if (context.VirtualFileSystem.Exists(fileId)) | ||
{ | ||
if (ptr.Ptr.LoadFile(fileId) && ptr.Ptr.Value != null) | ||
{ | ||
return ptr; | ||
} | ||
} | ||
|
||
ptr.Ptr.Value = new TValue(); | ||
return ptr; | ||
} | ||
|
||
/// <summary> | ||
/// Save value as configuration file. | ||
/// </summary> | ||
public void SaveConfig() | ||
{ | ||
var name = Name; | ||
if (string.IsNullOrWhiteSpace(name)) | ||
name = GetDefaultFileName(); | ||
var fileId = new FileIdentifier("conf", name); | ||
SaveFile(fileId); | ||
} | ||
|
||
private static string GetDefaultFileName() | ||
{ | ||
return typeof(TValue).Name + ".json"; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.