Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Grille committed May 9, 2024
1 parent 89f4ea5 commit 2c9f1cd
Show file tree
Hide file tree
Showing 89 changed files with 1,212 additions and 949 deletions.
Binary file added Assets/Grille.BeamNG.ico
Binary file not shown.
Binary file added Assets/Grille.BeamNG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Grille.BeamNGLib/Collections/IKeyed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Grille.BeamNgLib.Collections;

public interface IKeyed
{
public string? Key { get; }
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using LevelTemplateCreator.Assets;
using System;
using Grille.BeamNgLib.SceneTree.Art;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LevelTemplateCreator.Collections;
namespace Grille.BeamNgLib.Collections;

internal class KeyedCollection<T> : ICollection<T> where T : class, IKeyed
public class KeyedCollection<T> : ICollection<T> where T : class, IKeyed
{
readonly Dictionary<string, T> _dict;

Expand All @@ -20,6 +14,8 @@ public KeyedCollection()

public int Count => _dict.Count;

public bool IgnoreFalseDuplicates { get; set; } = false;

public bool IsReadOnly => false;

public Dictionary<string, T>.KeyCollection Keys => _dict.Keys;
Expand All @@ -40,7 +36,7 @@ public void Add(T item)

if (TryGetValue(key, out T old))
{
if (old == item)
if (old == item || IgnoreFalseDuplicates)
{
return;
}
Expand Down Expand Up @@ -98,6 +94,17 @@ public bool Remove(T item)
return true;
}

public IEnumerable<TItem> EnumerateItems<TItem>() where TItem : T
{
foreach (var item in _dict.Values)
{
if (item is TItem)
{
yield return (TItem)item;
}
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
Expand Down
29 changes: 29 additions & 0 deletions Grille.BeamNGLib/Grille.BeamNgLib.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<PackageId>Grille.BeamNgLib</PackageId>
<Title>Grille.BeamNgLib</Title>
<Owners>Grille</Owners>
<RepositoryUrl>https://github.com/Grille/BeamNG_LevelTemplateCreator</RepositoryUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Copyright>Copyright (c) 2024 Paul Hirch</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>Grille.BeamNG.png</PackageIcon>
<Authors>Grille</Authors>
</PropertyGroup>

<ItemGroup>
<None Include="..\Assets\Grille.BeamNG.png" Pack="true" PackagePath="\" Visible="false" />
<None Include="..\README.md" Pack="true" PackagePath="\" Visible="false" />
<None Include="..\LICENSE" Pack="true" PackagePath="\" Visible="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="GGL.BinaryView" Version="2.4.2" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions Grille.BeamNGLib/IO/Binary/TerrainBinary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Grille.BeamNgLib.IO.Binary;

public class TerrainBinary
{
public struct TerrainData
{
public ushort Height;
public byte Material;

public bool IsHole => Material == byte.MaxValue;

public void SetHeight(float value, float maxHeight) => Height = TerrainV9Serializer.GetU16Height(value, maxHeight);
public float GetHeight(float maxHeight) => TerrainV9Serializer.GetSingleHeight(Height, maxHeight);
}

public int Size { get; }

public TerrainData[] Data { get; }

public string[] MaterialNames { get; set; }

public int TotalSize => Size * Size;

public TerrainBinary(int size) : this(size, Array.Empty<string>()) { }

public TerrainBinary(int size, string[] materialNames) : this(size, materialNames, new TerrainData[size * size]) { }

public TerrainBinary(int size, string[] materialNames, TerrainData[] data)
{
Size = size;
Data = data;
MaterialNames = materialNames;

if (data.Length != TotalSize)
{
throw new ArgumentException($"data.Length must be {TotalSize}", nameof(data));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Text.Json;

namespace LevelTemplateCreator.IO;
namespace Grille.BeamNgLib.IO;

static class JsonDictSerializer
public class JsonDictSerializer
{
public const string ArrayClassName = "Template_Array";
public static void Serialize<T>(Stream stream, T value, bool intended = false) where T : IDictionary<string, object>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Grille.BeamNgLib.IO;

namespace LevelTemplateCreator.IO;

static class LevelInfoSerializer
public class LevelInfoSerializer
{
public static void Serialize(Level level, string path)
{
Expand Down
16 changes: 16 additions & 0 deletions Grille.BeamNGLib/IO/Resources/FileResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Grille.BeamNgLib.IO.Resources;

public class FileResource : Resource
{
public string Path { get; }

public FileResource(string name, string path, bool isGameResource) : base(name, isGameResource)
{
Path = path;
}

public override Stream Open()
{
return new FileStream(Path, FileMode.Open);
}
}
14 changes: 14 additions & 0 deletions Grille.BeamNGLib/IO/Resources/GroupResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Grille.BeamNgLib.IO.Resources;

internal class GroupResource : Resource
{
public GroupResource(string name, bool isGameResource, Resource[] resources) : base(name, isGameResource)
{

}

public override Stream Open()
{
throw new NotImplementedException();
}
}
38 changes: 38 additions & 0 deletions Grille.BeamNGLib/IO/Resources/PathEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Grille.BeamNgLib.IO.Resources;

public static class PathEvaluator
{
static public Resource Get(string entry, string gamePath)
{
return Get(entry, gamePath, string.Empty);
}

static public Resource Get(string entry, string gamePath, string userPath)
{
entry = entry.Replace("\\", "/");

string path = entry.StartsWith('/') ? entry.Substring(1) : entry;
return ParseAbsolute(path, gamePath, userPath);
}

static Resource ParseAbsolute(string entry, string gamePath, string userPath)
{
var split = entry.ToLower().Split([Path.PathSeparator, Path.AltDirectorySeparatorChar]);

if (split[0] == "levels")
{
var level = split[1];
var filename = split[split.Length - 1];
var key = $"beamng.{level}.{filename}";
var zippath = $"{gamePath}/content/levels/{level}.zip";
if (File.Exists(zippath))
{
return new ZipFileResource(key, zippath, entry, true);
}
}

throw new NotImplementedException();
}


}
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
using LevelTemplateCreator.Collections;
using LevelTemplateCreator.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LevelTemplateCreator.IO.Resources;

internal abstract class Resource : IKeyed
using Grille.BeamNgLib.Collections;

namespace Grille.BeamNgLib.IO.Resources;

public abstract class Resource : IKeyed
{
string IKeyed.Key => Name;

public bool IsGameResource { get; }

public string Name { get; }

public string DynamicName { get; protected set; }

public Resource(string name)
public Resource(string name, bool isGameResource)
{
Name = name;
DynamicName = name;
IsGameResource = isGameResource;
}

public abstract Stream OpenStream();
public abstract Stream Open();

public void SaveToDirectory(string directory)
{
using var stream = OpenStream();
using var stream = Open();
var dstpath = Path.Combine(directory, DynamicName);
using var file = File.OpenWrite(dstpath);
stream.CopyTo(file);
}

public void Save(string path)
{
using var src = OpenStream();
using var src = Open();
using var dst = new FileStream(path, FileMode.Create);
src.CopyTo(dst);
}
Expand Down
21 changes: 21 additions & 0 deletions Grille.BeamNGLib/IO/Resources/ResourceCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Grille.BeamNgLib.Collections;

namespace Grille.BeamNgLib.IO.Resources;

public class ResourceCollection : KeyedCollection<Resource>
{
public ResourceCollection() { }

public ResourceCollection(bool ignoreFalseDuplicates)
{
IgnoreFalseDuplicates = ignoreFalseDuplicates;
}

public void Save(string path)
{
foreach (var resource in this)
{
resource.SaveToDirectory(path);
}
}
}
4 changes: 4 additions & 0 deletions Grille.BeamNGLib/IO/Resources/StaticPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Grille.BeamNgLib.IO.Resources;
internal class StaticPath
{
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Grille.BeamNgLib.IO.Resources;

namespace LevelTemplateCreator.IO.Resources;

internal class ZipFileResource : Resource
public class ZipFileResource : Resource
{
public string ZipFilePath { get; }
public string EntryPath { get; }

public ZipFileResource(string name, string zipFilePath, string path) : base(name)
public ZipFileResource(string name, string zipFilePath, string path, bool isGameResource) : base(name, isGameResource)
{
ZipFilePath = zipFilePath;
EntryPath = path;
}

public override Stream OpenStream()
public override Stream Open()
{
var archive = ZipFileManager.Open(ZipFilePath);
var entry = archive.GetEntry(EntryPath);
Expand All @@ -46,5 +38,5 @@ public override Stream OpenStream()
public void Find()
{

}
}
}
Loading

0 comments on commit 2c9f1cd

Please sign in to comment.