Skip to content

Commit

Permalink
Ignore game directory prefixes (#81)
Browse files Browse the repository at this point in the history
* Fixed hiding buttons on empty

* Single use specification, ignore directory prefixes

* Updated changelogs
  • Loading branch information
KYPremco authored Jul 26, 2023
1 parent 935cafa commit 4ca30a8
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 17 deletions.
54 changes: 41 additions & 13 deletions Core/TimberApi/AssetSystem/AssetLoader.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,54 @@
using System;
using System.Diagnostics;
using System.Linq;
using TimberApi.AssetSystem.Exceptions;
using TimberApi.ShaderSystem;
using Timberborn.Persistence;
using Timberborn.SingletonSystem;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;

namespace TimberApi.AssetSystem
{
internal class AssetLoader : IAssetLoader
internal class AssetLoader : IAssetLoader, ILoadableSingleton
{
private readonly ISpecificationService _specificationService;

private readonly AssetSpecificationDeserializer _assetSpecificationDeserializer;

private readonly AssetRepository _assetRepository;

private readonly ShaderService _shaderService;

public AssetLoader(AssetRepository assetRepository, ShaderService shaderService)
private AssetSpecification _assetSpecification = null!;

public AssetLoader(AssetRepository assetRepository, ShaderService shaderService, ISpecificationService specificationService, AssetSpecificationDeserializer assetSpecificationDeserializer)
{
_assetRepository = assetRepository;
_shaderService = shaderService;
_specificationService = specificationService;
_assetSpecificationDeserializer = assetSpecificationDeserializer;
}

public void Load()
{
_assetSpecification = _specificationService.GetSpecifications(_assetSpecificationDeserializer).Single();
}

public T Load<T>(string path) where T : Object
{
string[] slicedPath = path.Split("/");
string prefix = slicedPath.First();
string newPath = string.Join("/", slicedPath.Skip(1));
var assetPath = RemoveDirectoryPrefixes(path);
var slicedPath = assetPath.Split("/");
var prefix = slicedPath.First();
var newPath = string.Join("/", slicedPath.Skip(1));
return Load<T>(prefix, newPath);
}

public T Load<T>(string prefix, string path) where T : Object
{
string[] slicedPath = path.Split("/");
string name = slicedPath[^1];
var slicedPath = path.Split("/");
var name = slicedPath[^1];
return Load<T>(prefix, string.Join('/', slicedPath.SkipLast(1)), name);
}

Expand All @@ -41,9 +59,9 @@ public T Load<T>(string prefix, string pathToFile, string name) where T : Object
throw new PrefixNotFoundException(prefix);
}

T asset = assetFolder.GetAssetBundleAtPath(pathToFile).Load<T>(name) ??
throw new InvalidOperationException(
$"Failed to load asset ({prefix}/{pathToFile}/{name}). Asset name does not exists inside bundle");
var asset = assetFolder.GetAssetBundleAtPath(pathToFile).Load<T>(name) ??
throw new InvalidOperationException(
$"Failed to load asset ({prefix}/{pathToFile}/{name}). Asset name does not exists inside bundle");

switch (asset)
{
Expand All @@ -60,9 +78,9 @@ public T Load<T>(string prefix, string pathToFile, string name) where T : Object

public T[] LoadAll<T>(string path) where T : Object
{
string[] slicedPath = path.Split("/");
string prefix = slicedPath.First();
string newPath = string.Join("/", slicedPath.Skip(1));
var slicedPath = path.Split("/");
var prefix = slicedPath.First();
var newPath = string.Join("/", slicedPath.Skip(1));
return LoadAll<T>(prefix, newPath);
}

Expand All @@ -75,5 +93,15 @@ public T[] LoadAll<T>(string prefix, string pathToFile) where T : Object

return assetFolder.GetAssetBundleAtPath(pathToFile).LoadAll<T>();
}

private string RemoveDirectoryPrefixes(string path)
{
foreach (var directoryPrefix in _assetSpecification.IgnoreDirectoryPrefixes)
{
path = path.Replace(directoryPrefix, "");
}

return path;
}
}
}
14 changes: 14 additions & 0 deletions Core/TimberApi/AssetSystem/AssetSpecification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Immutable;

namespace TimberApi.AssetSystem
{
public class AssetSpecification
{
public AssetSpecification(ImmutableArray<string> ignoreDirectoryPrefixes)
{
IgnoreDirectoryPrefixes = ignoreDirectoryPrefixes;
}

public ImmutableArray<string> IgnoreDirectoryPrefixes { get; }
}
}
20 changes: 20 additions & 0 deletions Core/TimberApi/AssetSystem/AssetSpecificationDeserializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Immutable;
using Timberborn.Persistence;

namespace TimberApi.AssetSystem
{
public class AssetSpecificationDeserializer : IObjectSerializer<AssetSpecification>
{
public void Serialize(AssetSpecification value, IObjectSaver objectSaver)
{
throw new System.NotImplementedException();
}

public Obsoletable<AssetSpecification> Deserialize(IObjectLoader objectLoader)
{
return new AssetSpecification(
objectLoader.GetValueOrDefault(new ListKey<string>("IgnoreDirectoryPrefixes")).ToImmutableArray()
);
}
}
}
21 changes: 21 additions & 0 deletions Core/TimberApi/AssetSystem/AssetSpecificationGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using TimberApi.SpecificationSystem;
using TimberApi.SpecificationSystem.SpecificationTypes;

namespace TimberApi.AssetSystem
{
public class AssetSpecificationGenerator : ISpecificationGenerator
{
public IEnumerable<ISpecification> Generate()
{
var json = JsonConvert.SerializeObject(new
{
IgnoreDirectoryPrefixes = Array.Empty<string>(),
});

yield return new GeneratedSpecification(json, "", "assetspecification");
}
}
}
3 changes: 3 additions & 0 deletions Core/TimberApi/AssetSystem/AssetSystemConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Bindito.Core;
using TimberApi.ConfiguratorSystem;
using TimberApi.SceneSystem;
using TimberApi.SpecificationSystem;

namespace TimberApi.AssetSystem
{
Expand All @@ -11,6 +12,8 @@ public void Configure(IContainerDefinition containerDefinition)
{
containerDefinition.Bind<IAssetLoader>().To<AssetLoader>().AsSingleton();
containerDefinition.Bind<AssetSceneLoader>().AsSingleton();
containerDefinition.Bind<AssetSpecificationDeserializer>().AsSingleton();
containerDefinition.MultiBind<ISpecificationGenerator>().To<AssetSpecificationGenerator>().AsSingleton();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
using System.IO;
using UnityEngine;

namespace TimberApi.SpecificationSystem.SpecificationTypes
{
internal class FileSpecification : ISpecification
{
public FileSpecification(string filePath)
{
string fileName = Path.GetFileNameWithoutExtension(filePath);
var fileName = Path.GetFileNameWithoutExtension(filePath);

FilePath = filePath;
FullName = fileName.Replace(".original", "").Replace(".replace", "").ToLower();
Name = FullName[(FullName.IndexOf('.') + 1)..].ToLower();
SpecificationName = fileName[..fileName.IndexOf('.')].ToLower();

if(FullName.Contains('.'))
{
Name = FullName[(FullName.IndexOf('.') + 1)..].ToLower();
SpecificationName = fileName[..fileName.IndexOf('.')].ToLower();
}
else
{
Name = "";
SpecificationName = FullName[(FullName.IndexOf('.') + 1)..].ToLower();
}

IsOriginal = fileName.ToLower().EndsWith("original");
IsReplace = fileName.ToLower().EndsWith("replace");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public GeneratedSpecification(string json, string name, string specificationName
_json = json;
Name = name;
SpecificationName = specificationName.ToLower();
FullName = $"{SpecificationName}.{name.ToLower()}";
FullName = string.IsNullOrWhiteSpace(name) ? SpecificationName : $"{SpecificationName}.{name.ToLower()}";
IsOriginal = isOriginal;
IsReplace = false;
}
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Unreleased

### Changes
- Added sinle type specificaiton eg. `AssetSpecification.original.json`
- Added `AssetSpecification` with `IgnoreDirectoryPrefixes` to remove directory prefixes from the game
- Changed `ContainTools` prefix so that groups will be hidden when empty in/out devmode

## TimberAPI v0.5.5.8

Expand Down

0 comments on commit 4ca30a8

Please sign in to comment.