From 6fbd6b5b6d3f43d5b1a07a6cb4533b69aebaa691 Mon Sep 17 00:00:00 2001 From: Paul Hirch Date: Wed, 15 May 2024 00:44:49 +0200 Subject: [PATCH] update --- .../Collections/KeyedCollection.cs | 2 +- .../IO/{ => Binary}/TerrainV9Serializer.cs | 5 +- .../IO/{ => Resources}/ZipFileManager.cs | 2 +- .../IO/{ => Text}/BeamJsonSerializer.cs | 2 +- Grille.BeamNG.Lib/IO/Text/JsonDict.cs | 88 +++++++++++++++++++ .../IO/{ => Text}/JsonDictSerializer.cs | 2 +- .../IO/{ => Text}/SimItemsJsonSerializer.cs | 2 +- Grille.BeamNG.Lib/LevelBuilder.cs | 5 +- Grille.BeamNG.Lib/README.md | 5 +- .../SceneTree/Art/ArtItemsCollection.cs | 3 +- .../SceneTree/JsonDictWrapper.cs | 1 - Grille.BeamNG.Lib/SceneTree/Main/SimGroup.cs | 4 +- .../SceneTree/Main/SimItemCollection.cs | 3 +- .../JsonDictPropertyTypeRegistry.cs | 1 + Grille.BeamNG.Lib/Terrain.cs | 3 +- Grille.BeamNG.Lib/TerrainTemplate.cs | 2 +- Grille.BeamNG.Lib/usings.cs | 2 +- Grille.BeamNG.Lib_Tests/Terrain.cs | 2 - .../Assets/AssetLibaryLoader.cs | 12 +-- .../Assets/LevelObjectsAsset.cs | 4 - LevelTemplateCreator/EnvironmentInfo.cs | 3 +- LevelTemplateCreator/usings.cs | 2 +- README.md | 14 ++- 23 files changed, 120 insertions(+), 49 deletions(-) rename Grille.BeamNG.Lib/IO/{ => Binary}/TerrainV9Serializer.cs (97%) rename Grille.BeamNG.Lib/IO/{ => Resources}/ZipFileManager.cs (98%) rename Grille.BeamNG.Lib/IO/{ => Text}/BeamJsonSerializer.cs (98%) create mode 100644 Grille.BeamNG.Lib/IO/Text/JsonDict.cs rename Grille.BeamNG.Lib/IO/{ => Text}/JsonDictSerializer.cs (99%) rename Grille.BeamNG.Lib/IO/{ => Text}/SimItemsJsonSerializer.cs (98%) diff --git a/Grille.BeamNG.Lib/Collections/KeyedCollection.cs b/Grille.BeamNG.Lib/Collections/KeyedCollection.cs index 5a2d6bf..6e90519 100644 --- a/Grille.BeamNG.Lib/Collections/KeyedCollection.cs +++ b/Grille.BeamNG.Lib/Collections/KeyedCollection.cs @@ -9,7 +9,7 @@ public class KeyedCollection : ICollection where T : class, IKeyed public KeyedCollection() { - _dict = new Dictionary(); + _dict = new(); } public int Count => _dict.Count; diff --git a/Grille.BeamNG.Lib/IO/TerrainV9Serializer.cs b/Grille.BeamNG.Lib/IO/Binary/TerrainV9Serializer.cs similarity index 97% rename from Grille.BeamNG.Lib/IO/TerrainV9Serializer.cs rename to Grille.BeamNG.Lib/IO/Binary/TerrainV9Serializer.cs index 359d1f6..deefd46 100644 --- a/Grille.BeamNG.Lib/IO/TerrainV9Serializer.cs +++ b/Grille.BeamNG.Lib/IO/Binary/TerrainV9Serializer.cs @@ -1,8 +1,7 @@ using System.Text; using Grille.IO; -using Grille.BeamNG.IO.Binary; -namespace Grille.BeamNG.IO; +namespace Grille.BeamNG.IO.Binary; public class TerrainV9Serializer { @@ -124,6 +123,6 @@ public static float GetSingleHeight(ushort u16height, float maxHeight) float height = u16height; float u16max = ushort.MaxValue; - return (height / u16max) * maxHeight; + return height / u16max * maxHeight; } } diff --git a/Grille.BeamNG.Lib/IO/ZipFileManager.cs b/Grille.BeamNG.Lib/IO/Resources/ZipFileManager.cs similarity index 98% rename from Grille.BeamNG.Lib/IO/ZipFileManager.cs rename to Grille.BeamNG.Lib/IO/Resources/ZipFileManager.cs index 2edc7a3..7f4c106 100644 --- a/Grille.BeamNG.Lib/IO/ZipFileManager.cs +++ b/Grille.BeamNG.Lib/IO/Resources/ZipFileManager.cs @@ -1,7 +1,7 @@ using Grille.BeamNG.Logging; using System.IO.Compression; -namespace Grille.BeamNG.IO; +namespace Grille.BeamNG.IO.Resources; public static class ZipFileManager { diff --git a/Grille.BeamNG.Lib/IO/BeamJsonSerializer.cs b/Grille.BeamNG.Lib/IO/Text/BeamJsonSerializer.cs similarity index 98% rename from Grille.BeamNG.Lib/IO/BeamJsonSerializer.cs rename to Grille.BeamNG.Lib/IO/Text/BeamJsonSerializer.cs index 870a9b7..38c3e5a 100644 --- a/Grille.BeamNG.Lib/IO/BeamJsonSerializer.cs +++ b/Grille.BeamNG.Lib/IO/Text/BeamJsonSerializer.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; -namespace Grille.BeamNG.IO; +namespace Grille.BeamNG.IO.Text; public class BeamJsonSerializer { public static IEnumerable Load(string filePath) diff --git a/Grille.BeamNG.Lib/IO/Text/JsonDict.cs b/Grille.BeamNG.Lib/IO/Text/JsonDict.cs new file mode 100644 index 0000000..fcc0430 --- /dev/null +++ b/Grille.BeamNG.Lib/IO/Text/JsonDict.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Grille.BeamNG.IO.Text; +public class JsonDict : IDictionary +{ + SortedList _dict; + + public JsonDict() + { + _dict = new SortedList(); + } + + public JsonDict(JsonDict dict) + { + _dict = new SortedList(dict._dict); + } + + public object this[string key] { get => _dict[key]; set => _dict[key] = value; } + + public ICollection Keys => _dict.Keys; + + public ICollection Values => _dict.Values; + + public int Count => _dict.Count; + + public bool IsReadOnly => ((ICollection>)_dict).IsReadOnly; + + public void Add(string key, object value) + { + _dict.Add(key, value); + } + + public void Add(KeyValuePair item) + { + ((ICollection>)_dict).Add(item); + } + + public void Clear() + { + _dict.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return _dict.Contains(item); + } + + public bool ContainsKey(string key) + { + return _dict.ContainsKey(key); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + ((ICollection>)_dict).CopyTo(array, arrayIndex); + } + + public IEnumerator> GetEnumerator() + { + return _dict.GetEnumerator(); + } + + public bool Remove(string key) + { + return _dict.Remove(key); + } + + public bool Remove(KeyValuePair item) + { + return ((ICollection>)_dict).Remove(item); + } + + public bool TryGetValue(string key, [MaybeNullWhen(false)] out object value) + { + return _dict.TryGetValue(key, out value); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)_dict).GetEnumerator(); + } +} diff --git a/Grille.BeamNG.Lib/IO/JsonDictSerializer.cs b/Grille.BeamNG.Lib/IO/Text/JsonDictSerializer.cs similarity index 99% rename from Grille.BeamNG.Lib/IO/JsonDictSerializer.cs rename to Grille.BeamNG.Lib/IO/Text/JsonDictSerializer.cs index a80e61e..0573210 100644 --- a/Grille.BeamNG.Lib/IO/JsonDictSerializer.cs +++ b/Grille.BeamNG.Lib/IO/Text/JsonDictSerializer.cs @@ -1,6 +1,6 @@ using System.Text.Json; -namespace Grille.BeamNG.IO; +namespace Grille.BeamNG.IO.Text; public class JsonDictSerializer { diff --git a/Grille.BeamNG.Lib/IO/SimItemsJsonSerializer.cs b/Grille.BeamNG.Lib/IO/Text/SimItemsJsonSerializer.cs similarity index 98% rename from Grille.BeamNG.Lib/IO/SimItemsJsonSerializer.cs rename to Grille.BeamNG.Lib/IO/Text/SimItemsJsonSerializer.cs index 5cf07f7..a073e71 100644 --- a/Grille.BeamNG.Lib/IO/SimItemsJsonSerializer.cs +++ b/Grille.BeamNG.Lib/IO/Text/SimItemsJsonSerializer.cs @@ -1,7 +1,7 @@ using Grille.BeamNG.SceneTree; using System.Text; -namespace Grille.BeamNG.IO; +namespace Grille.BeamNG.IO.Text; public static class SimItemsJsonSerializer { diff --git a/Grille.BeamNG.Lib/LevelBuilder.cs b/Grille.BeamNG.Lib/LevelBuilder.cs index 13d7f33..8778c4c 100644 --- a/Grille.BeamNG.Lib/LevelBuilder.cs +++ b/Grille.BeamNG.Lib/LevelBuilder.cs @@ -1,5 +1,4 @@ -using Grille.BeamNG.IO; -using Grille.BeamNG.IO.Resources; +using Grille.BeamNG.IO.Resources; using Grille.BeamNG.SceneTree.Art; using Grille.BeamNG.SceneTree.Main; using System.IO; @@ -21,8 +20,6 @@ public class LevelBuilder public Resource? Preview { get; set; } - bool _setup; - public LevelBuilder(string @namespace) { Namespace = @namespace; diff --git a/Grille.BeamNG.Lib/README.md b/Grille.BeamNG.Lib/README.md index a8b8452..6b87648 100644 --- a/Grille.BeamNG.Lib/README.md +++ b/Grille.BeamNG.Lib/README.md @@ -6,18 +6,15 @@ Its currently focusing on level data and is generally very WIP. If you find a piece of code useful but don’t want to include the whole library, feel free to copy paste. Credits to this project are appreciated but not necessary. - ## Links - [\[GitHub\] https://github.com/Grille/BeamNG_LevelTemplateCreator/tree/main/Grille.BeamNG.Lib](https://github.com/Grille/BeamNG_LevelTemplateCreator/tree/main/Grille.BeamNG.Lib) - [\[NuGet\] https://www.nuget.org/packages/Grille.BeamNG.Lib](https://www.nuget.org/packages/Grille.BeamNG.Lib) -- [\[BeamNG Forum\] https://www.beamng.com/threads/beamng_leveltemplatecreator-pbr-v0-2.98334/](https://www.beamng.com/threads/beamng_leveltemplatecreator-pbr-v0-2.98334/) - +- [\[BeamNG Forum\] https://www.beamng.com/threads/c-grille-beamng-lib-v0-1.98413/](https://www.beamng.com/threads/c-grille-beamng-lib-v0-1.98413/) ## Features - Read/Write BeamNG terrain binary files (testet againt file version 9 used by 0.32) - Parsing/Writing of BeamNG scene-tree json - ## Disclaimer This library is an independent project and is not affiliated with, endorsed by, or in any way officially connected to BeamNG GmbH. All trademarks, service marks, product names, and logos appearing in this library are the property of their respective owners. \ No newline at end of file diff --git a/Grille.BeamNG.Lib/SceneTree/Art/ArtItemsCollection.cs b/Grille.BeamNG.Lib/SceneTree/Art/ArtItemsCollection.cs index 122efad..ccabdeb 100644 --- a/Grille.BeamNG.Lib/SceneTree/Art/ArtItemsCollection.cs +++ b/Grille.BeamNG.Lib/SceneTree/Art/ArtItemsCollection.cs @@ -1,5 +1,4 @@ -using Grille.BeamNG.IO; -using Grille.BeamNG.SceneTree.Main; +using Grille.BeamNG.SceneTree.Main; using Grille.BeamNG.SceneTree.Registry; namespace Grille.BeamNG.SceneTree.Art; diff --git a/Grille.BeamNG.Lib/SceneTree/JsonDictWrapper.cs b/Grille.BeamNG.Lib/SceneTree/JsonDictWrapper.cs index 800f704..430da54 100644 --- a/Grille.BeamNG.Lib/SceneTree/JsonDictWrapper.cs +++ b/Grille.BeamNG.Lib/SceneTree/JsonDictWrapper.cs @@ -1,5 +1,4 @@ using Grille.BeamNG.Collections; -using Grille.BeamNG.IO; using Grille.BeamNG.SceneTree.Main; namespace Grille.BeamNG.SceneTree; diff --git a/Grille.BeamNG.Lib/SceneTree/Main/SimGroup.cs b/Grille.BeamNG.Lib/SceneTree/Main/SimGroup.cs index 16c4711..afcc27e 100644 --- a/Grille.BeamNG.Lib/SceneTree/Main/SimGroup.cs +++ b/Grille.BeamNG.Lib/SceneTree/Main/SimGroup.cs @@ -1,5 +1,5 @@ -using Grille.BeamNG.Collections; -using Grille.BeamNG.IO; +using Grille.BeamNG.IO; +using Grille.BeamNG.IO.Text; using Grille.BeamNG.SceneTree.Art; using Grille.BeamNG.SceneTree.Registry; diff --git a/Grille.BeamNG.Lib/SceneTree/Main/SimItemCollection.cs b/Grille.BeamNG.Lib/SceneTree/Main/SimItemCollection.cs index 09d0cae..8af1021 100644 --- a/Grille.BeamNG.Lib/SceneTree/Main/SimItemCollection.cs +++ b/Grille.BeamNG.Lib/SceneTree/Main/SimItemCollection.cs @@ -1,5 +1,4 @@ -using Grille.BeamNG.IO; -using Grille.BeamNG.SceneTree.Registry; +using Grille.BeamNG.SceneTree.Registry; using System; using System.Collections.Generic; using System.Linq; diff --git a/Grille.BeamNG.Lib/SceneTree/TypeRegistry/JsonDictPropertyTypeRegistry.cs b/Grille.BeamNG.Lib/SceneTree/TypeRegistry/JsonDictPropertyTypeRegistry.cs index 912fdb5..d4471d3 100644 --- a/Grille.BeamNG.Lib/SceneTree/TypeRegistry/JsonDictPropertyTypeRegistry.cs +++ b/Grille.BeamNG.Lib/SceneTree/TypeRegistry/JsonDictPropertyTypeRegistry.cs @@ -1,4 +1,5 @@ using Grille.BeamNG.Collections; +using Grille.BeamNG.IO.Text; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; diff --git a/Grille.BeamNG.Lib/Terrain.cs b/Grille.BeamNG.Lib/Terrain.cs index 7304add..a7b194f 100644 --- a/Grille.BeamNG.Lib/Terrain.cs +++ b/Grille.BeamNG.Lib/Terrain.cs @@ -1,5 +1,4 @@ -using Grille.BeamNG.IO; -using Grille.BeamNG.IO.Binary; +using Grille.BeamNG.IO.Binary; using System.Collections; using System.Diagnostics.CodeAnalysis; using System.Drawing; diff --git a/Grille.BeamNG.Lib/TerrainTemplate.cs b/Grille.BeamNG.Lib/TerrainTemplate.cs index 4bdd630..de49a61 100644 --- a/Grille.BeamNG.Lib/TerrainTemplate.cs +++ b/Grille.BeamNG.Lib/TerrainTemplate.cs @@ -1,4 +1,4 @@ -using Grille.BeamNG.IO; +using Grille.BeamNG.IO.Binary; namespace Grille.BeamNG; diff --git a/Grille.BeamNG.Lib/usings.cs b/Grille.BeamNG.Lib/usings.cs index 2c7b819..2b82afa 100644 --- a/Grille.BeamNG.Lib/usings.cs +++ b/Grille.BeamNG.Lib/usings.cs @@ -3,4 +3,4 @@ global using System.IO; global using System.Numerics; -global using JsonDict = System.Collections.Generic.Dictionary; \ No newline at end of file +global using Grille.BeamNG.IO.Text; \ No newline at end of file diff --git a/Grille.BeamNG.Lib_Tests/Terrain.cs b/Grille.BeamNG.Lib_Tests/Terrain.cs index 3321244..d71724c 100644 --- a/Grille.BeamNG.Lib_Tests/Terrain.cs +++ b/Grille.BeamNG.Lib_Tests/Terrain.cs @@ -4,8 +4,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; - -using Grille.BeamNG.IO; using Grille.BeamNG; using System.IO.Enumeration; diff --git a/LevelTemplateCreator/Assets/AssetLibaryLoader.cs b/LevelTemplateCreator/Assets/AssetLibaryLoader.cs index b27d315..e1fc562 100644 --- a/LevelTemplateCreator/Assets/AssetLibaryLoader.cs +++ b/LevelTemplateCreator/Assets/AssetLibaryLoader.cs @@ -1,5 +1,4 @@ -using Grille.BeamNG.IO; -using LevelTemplateCreator.IO.Resources; +using LevelTemplateCreator.IO.Resources; using LevelTemplateCreator.Properties; using Grille.BeamNG.SceneTree; using Grille.BeamNG.SceneTree.Art; @@ -8,6 +7,7 @@ using System.Text; using System.Xml.Linq; using Grille.BeamNG.Logging; +using Grille.BeamNG.IO.Resources; namespace LevelTemplateCreator.Assets; @@ -26,7 +26,6 @@ public override string ToString() const string JsonConstant = "Constant"; const string JsonInclude = "Include"; - public int MaxWrongFileCount { get; set; } = 20; int _wrongFileCount = 0; bool _exit = false; @@ -148,13 +147,6 @@ void LoadJsonFile(string path) Deserialize(stream); } - void Deserialize(string text) - { - var bytes = Encoding.UTF8.GetBytes(text); - using var stream = new MemoryStream(bytes); - Deserialize(stream); - } - void Deserialize(Stream stream) { var dict = JsonDictSerializer.Deserialize(stream); diff --git a/LevelTemplateCreator/Assets/LevelObjectsAsset.cs b/LevelTemplateCreator/Assets/LevelObjectsAsset.cs index 1cb07c4..a3d8610 100644 --- a/LevelTemplateCreator/Assets/LevelObjectsAsset.cs +++ b/LevelTemplateCreator/Assets/LevelObjectsAsset.cs @@ -25,10 +25,6 @@ public LevelObjectsAsset(JsonDictWrapper data, AssetSource info) : base(data, in foreach (var rawitem in rawitems) { var item = new SimItem(rawitem, (string)rawitem["class"]); - foreach (var pair in rawitem) - { - item[pair.Key] = pair.Value; - } Items.Add(item); } } diff --git a/LevelTemplateCreator/EnvironmentInfo.cs b/LevelTemplateCreator/EnvironmentInfo.cs index 2a3e419..08b2789 100644 --- a/LevelTemplateCreator/EnvironmentInfo.cs +++ b/LevelTemplateCreator/EnvironmentInfo.cs @@ -1,5 +1,4 @@ -using Grille.BeamNG.IO; -using Grille.BeamNG.IO.Resources; +using Grille.BeamNG.IO.Resources; using System; using System.Collections.Generic; using System.Linq; diff --git a/LevelTemplateCreator/usings.cs b/LevelTemplateCreator/usings.cs index 2eea4e4..079e091 100644 --- a/LevelTemplateCreator/usings.cs +++ b/LevelTemplateCreator/usings.cs @@ -4,4 +4,4 @@ global using System.Windows.Forms; global using System.Drawing; -global using JsonDict = System.Collections.Generic.Dictionary; \ No newline at end of file +global using Grille.BeamNG.IO.Text; \ No newline at end of file diff --git a/README.md b/README.md index 4d33cdc..8c3457e 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ Setting up a new scenario from scratch can be a bit tiresome, especially if you The goal of this project is to provide an easy solution to create empty map templates. I also aimed to make it easily extendable for people with deeper knowledge of BeamNG. +## Links +- [\[GitHub\] https://github.com/Grille/BeamNG_LevelTemplateCreator](https://github.com/Grille/BeamNG_LevelTemplateCreator) +- [\[NuGet\] https://www.nuget.org/packages/Grille.BeamNG.Lib](https://www.nuget.org/packages/Grille.BeamNG.Lib) +- [\[BeamNG Forum\] https://www.beamng.com/threads/beamng_leveltemplatecreator-pbr-v0-2.98334/](https://www.beamng.com/threads/beamng_leveltemplatecreator-pbr-v0-2.98334/) + ## Features * Setup of needed level folder structure without manual renaming. * Asset System based on BeamNG's Json structure. @@ -47,15 +52,18 @@ Object material, indirectly added if used by any other object. ### Paths -* `.` -Relative path from the folder containing the Json file. - * `/` Absolute path either from the local package folder, or alternatively if beginning with `/level` and contains an valid BeamNG-level name e.g `/levels/driver_training/` an pointer to an BeamNG resource. +* `.` +Relative path from the folder containing the Json file. + * `#` Hex color code `#ffffff` used to generate a single-color texture file on export. +* `$` +Variable + Each part must start with one of the above characters. ## Requirements