Skip to content

Commit

Permalink
0.3.7.805
Browse files Browse the repository at this point in the history
  • Loading branch information
gleblebedev committed May 8, 2024
1 parent ad30801 commit 86d15bb
Show file tree
Hide file tree
Showing 8 changed files with 536 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"unofficial.Urho3DNet.Editor": {
"version": "0.3.7.802",
"version": "0.3.7.805",
"commands": [
"rbfx"
]
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
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>
113 changes: 113 additions & 0 deletions RbfxTemplate.Tests/JsonResourceTests.cs
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);
}
}
}
2 changes: 1 addition & 1 deletion RbfxTemplate.Tests/RbfxTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public RbfxTestFramework(IMessageSink messageSink)
/// Await this to continue execution in the application main thread.
/// </summary>
/// <returns>Main thread awaitable.</returns>
public static ConfiguredTaskAwaitable<bool> ToMainThreadAsync(ITestOutputHelper testOutputHelper = null)
public static ConfiguredTaskAwaitable<bool> ToMainThreadAsync(ITestOutputHelper? testOutputHelper = null)
{
var tcs = new TaskCompletionSource<bool>();
var workQueue = Context.GetSubsystem<WorkQueue>();
Expand Down
26 changes: 13 additions & 13 deletions RbfxTemplate/UrhoApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void Setup()
{
// Set up engine parameters
EngineParameters[Urho3D.EpFullScreen] = false;
EngineParameters[Urho3D.EpWindowResizable] = true;
EngineParameters[Urho3D.EpWindowResizable] = false;
EngineParameters[Urho3D.EpWindowTitle] = "RbfxTemplate";
EngineParameters[Urho3D.EpApplicationName] = "RbfxTemplate";
EngineParameters[Urho3D.EpOrganizationName] = "RbfxTemplate";
Expand Down Expand Up @@ -62,19 +62,19 @@ private void ApplyCommandLineArguments()
case "--opengl": EngineParameters[Urho3D.EpRenderBackend] = (int)RenderBackend.OpenGl; break;
case "--vulkan": EngineParameters[Urho3D.EpRenderBackend] = (int)RenderBackend.Vulkan; break;
case "--fullscreen":
{
EngineParameters[Urho3D.EpFullScreen] = true;
EngineParameters[Urho3D.EpWindowResizable] = false;
EngineParameters[Urho3D.EpBorderless] = true;
break;
}
{
EngineParameters[Urho3D.EpFullScreen] = true;
EngineParameters[Urho3D.EpWindowResizable] = false;
EngineParameters[Urho3D.EpBorderless] = true;
break;
}
case "--windowed":
{
EngineParameters[Urho3D.EpFullScreen] = false;
EngineParameters[Urho3D.EpWindowResizable] = true;
EngineParameters[Urho3D.EpBorderless] = false;
break;
}
{
EngineParameters[Urho3D.EpFullScreen] = false;
EngineParameters[Urho3D.EpWindowResizable] = true;
EngineParameters[Urho3D.EpBorderless] = false;
break;
}
default: Log.Warning("Unknown argument " + commandLineArgs[index]); break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions RbfxTemplate/UrhoPluginApplication.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Urho3DNet;
using RbfxTemplate.Utils;
using Urho3DNet;

namespace RbfxTemplate
{
Expand Down Expand Up @@ -140,7 +141,6 @@ public void ToNewGame()
public void ContinueGame()
{
if (_gameState) _stateStack.Push(_gameState);
;
}

public void Quit()
Expand Down
58 changes: 58 additions & 0 deletions RbfxTemplate/Utils/ConfigFileContainer.cs
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";
}

}
}
Loading

0 comments on commit 86d15bb

Please sign in to comment.