Skip to content

Commit

Permalink
Merge pull request #80 from ToniMacaroni/development
Browse files Browse the repository at this point in the history
2.3.6
  • Loading branch information
ToniMacaroni authored Sep 1, 2021
2 parents b2a6319 + 97e1800 commit 030b7be
Show file tree
Hide file tree
Showing 31 changed files with 492 additions and 204 deletions.
4 changes: 2 additions & 2 deletions SaberFactory/Configuration/ConfigBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public abstract class ConfigBase : IInitializable, IDisposable

private readonly Dictionary<PropertyInfo, object> _originalValues = new Dictionary<PropertyInfo, object>();

protected ConfigBase(SFDirectories sfDirs, string fileName)
protected ConfigBase(PluginDirectories pluginDirs, string fileName)
{
ConfigFile = sfDirs.SaberFactoryDir.GetFile(fileName);
ConfigFile = pluginDirs.SaberFactoryDir.GetFile(fileName);
}

public void Revert()
Expand Down
2 changes: 1 addition & 1 deletion SaberFactory/Configuration/TrailConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TrailConfig : ConfigBase

public bool OnlyUseVertexColor { get; set; } = true;

public TrailConfig(SFDirectories sfDirs) : base(sfDirs, "TrailConfig.json")
public TrailConfig(PluginDirectories pluginDirs) : base(pluginDirs, "TrailConfig.json")
{ }
}
}
78 changes: 41 additions & 37 deletions SaberFactory/DataStore/MainAssetStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -11,6 +10,7 @@
using SaberFactory.Models;
using SaberFactory.Models.CustomSaber;
using SiraUtil.Tools;
using UnityEngine;

namespace SaberFactory.DataStore
{
Expand All @@ -22,28 +22,41 @@ internal class MainAssetStore : IDisposable
public bool IsLoading { get; private set; }
public Task CurrentTask;

public List<string> AdditionalCustomSaberFolders { get; } = new List<string>();

private readonly CustomSaberAssetLoader _customSaberAssetLoader;
private readonly CustomSaberModelLoader _customSaberModelLoader;

private readonly PluginConfig _config;
private readonly SiraLog _logger;
private readonly PluginDirectories _pluginDirs;

private readonly Dictionary<string, ModelComposition> _modelCompositions;
private readonly Dictionary<string, PreloadMetaData> _metaData;

private MainAssetStore(
PluginConfig config,
SiraLog logger,
CustomSaberModelLoader customSaberModelLoader)
CustomSaberModelLoader customSaberModelLoader,
PluginDirectories pluginDirs)
{
_config = config;
_logger = logger;
_pluginDirs = pluginDirs;

_customSaberAssetLoader = new CustomSaberAssetLoader();
_customSaberModelLoader = customSaberModelLoader;

_modelCompositions = new Dictionary<string, ModelComposition>();
_metaData = new Dictionary<string, PreloadMetaData>();

foreach (var directory in pluginDirs.CustomSaberDir.GetDirectories("*", SearchOption.AllDirectories))
{
var relPath = PathTools.ToRelativePath(directory.FullName);
relPath = PathTools.CorrectRelativePath(relPath);
relPath = relPath.Substring(relPath.IndexOf('\\')+1);
AdditionalCustomSaberFolders.Add(relPath);
}
}

public Task<ModelComposition> this[string path] => GetCompositionByPath(path);
Expand Down Expand Up @@ -77,7 +90,7 @@ public async Task LoadAllCustomSaberMetaDataAsync()
if (!IsLoading)
{
IsLoading = true;
CurrentTask = LoadAllCustomSaberMetaDataAsyncInternal();
CurrentTask = LoadAllMetaDataForLoader(_customSaberAssetLoader, true);
}

await CurrentTask;
Expand Down Expand Up @@ -127,7 +140,7 @@ public void Unload(string path)
public async Task Reload(string path)
{
Unload(path);
await LoadMetaData(path);
LoadMetaData(path);
await LoadComposition(path);
}

Expand All @@ -149,74 +162,65 @@ public void Delete(string path)
File.Delete(filePath);
}

private async Task LoadAllCustomSaberMetaDataAsyncInternal()
{
await LoadAllMetaDataForLoader(_customSaberAssetLoader, true);
}

private async Task LoadAllMetaDataForLoader(AssetBundleLoader loader, bool createIfNotExisting = false)
{
var sw = Stopwatch.StartNew();
var sw = DebugTimer.StartNew("Loading Metadata");

await Task.Run(async () =>
foreach (var assetMetaPath in loader.CollectFiles(_pluginDirs))
{
foreach (var assetMetaPath in await loader.CollectFiles())
{
var relativePath = PathTools.ToRelativePath(assetMetaPath.MetaDataPath);
if (_metaData.TryGetValue(relativePath, out _)) continue;
var relativePath = PathTools.ToRelativePath(assetMetaPath.MetaDataPath);
if (_metaData.TryGetValue(relativePath, out _)) continue;

if (!assetMetaPath.HasMetaData)
if (!assetMetaPath.HasMetaData)
{
if (createIfNotExisting)
{
if (createIfNotExisting)
{
var comp = await await UnityMainThreadTaskScheduler.Factory.StartNew(() => this[PathTools.ToRelativePath(assetMetaPath.Path)]);
var comp = await this[PathTools.ToRelativePath(assetMetaPath.Path)];

if (comp == null) continue;
if (comp == null) continue;

var metaData = new PreloadMetaData(assetMetaPath, comp, comp.AssetTypeDefinition);
await metaData.SaveToFile();
_metaData.Add(relativePath, metaData);
}
}
else
{
var metaData = new PreloadMetaData(assetMetaPath);
await metaData.LoadFromFile();
metaData.IsFavorite = _config.IsFavorite(PathTools.ToRelativePath(assetMetaPath.Path));
var metaData = new PreloadMetaData(assetMetaPath, comp, comp.AssetTypeDefinition);
metaData.SaveToFile();
_metaData.Add(relativePath, metaData);
}
}
});
else
{
var metaData = new PreloadMetaData(assetMetaPath);
metaData.LoadFromFile();
metaData.IsFavorite = _config.IsFavorite(PathTools.ToRelativePath(assetMetaPath.Path));
_metaData.Add(relativePath, metaData);
}
}

sw.Stop();
_logger.Info($"Loaded Metadata in {sw.Elapsed.Seconds}.{sw.Elapsed.Milliseconds}s");
sw.Print(_logger);
}

public async Task<ModelComposition> CreateMetaData(AssetMetaPath assetMetaPath)
{
var relativePath = PathTools.ToRelativePath(assetMetaPath.MetaDataPath);
if (_metaData.TryGetValue(relativePath, out _)) return null;

var comp = await await UnityMainThreadTaskScheduler.Factory.StartNew(() => this[PathTools.ToRelativePath(assetMetaPath.Path)]);
var comp = await this[PathTools.ToRelativePath(assetMetaPath.Path)];

if (comp == null) return null;

var metaData = new PreloadMetaData(assetMetaPath, comp, comp.AssetTypeDefinition);
await metaData.SaveToFile();
metaData.SaveToFile();
_metaData.Add(relativePath, metaData);

return comp;
}

private async Task LoadMetaData(string pieceRelativePath)
private void LoadMetaData(string pieceRelativePath)
{
var assetMetaPath = new AssetMetaPath(new FileInfo(PathTools.ToFullPath(pieceRelativePath)));
if (_metaData.TryGetValue(assetMetaPath.RelativeMetaDataPath, out _)) return;
if (!File.Exists(assetMetaPath.MetaDataPath)) return;

var metaData = new PreloadMetaData(assetMetaPath);
metaData.IsFavorite = _config.IsFavorite(assetMetaPath.RelativePath);
await metaData.LoadFromFile();
metaData.LoadFromFile();
_metaData.Add(assetMetaPath.RelativeMetaDataPath, metaData);
}

Expand Down
13 changes: 9 additions & 4 deletions SaberFactory/DataStore/StoreAsset.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using UnityEngine;
using System.IO;
using System.Linq;
using SaberFactory.Helpers;
using UnityEngine;

namespace SaberFactory.DataStore
{
Expand All @@ -11,6 +14,7 @@ internal class StoreAsset
public readonly string Name;
public readonly string NameWithoutExtension;
public readonly string Extension;
public readonly string SubDirName;

public GameObject Prefab;

Expand All @@ -19,9 +23,10 @@ internal class StoreAsset
public StoreAsset(string relativePath, GameObject prefab, AssetBundle assetBundle)
{
RelativePath = relativePath;
Name = System.IO.Path.GetFileName(RelativePath);
NameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(Name);
Extension = System.IO.Path.GetExtension(Name);
Name = Path.GetFileName(RelativePath);
NameWithoutExtension = Path.GetFileNameWithoutExtension(Name);
Extension = Path.GetExtension(Name);
SubDirName = PathTools.GetSubDir(relativePath);

Prefab = prefab;
AssetBundle = assetBundle;
Expand Down
4 changes: 2 additions & 2 deletions SaberFactory/DataStore/TextureStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ internal class TextureStore
private readonly DirectoryInfo _textureDirectory;
private Task _currentLoadingTask;

private TextureStore(SFDirectories sfDirs)
private TextureStore(PluginDirectories pluginDirs)
{
_textureAssets = new Dictionary<string, TextureAsset>();
_textureDirectory = sfDirs.SaberFactoryDir.CreateSubdirectory("Textures");
_textureDirectory = pluginDirs.SaberFactoryDir.CreateSubdirectory("Textures");
}

public Task<TextureAsset> this[string path] => GetTexture(path);
Expand Down
10 changes: 10 additions & 0 deletions SaberFactory/Helpers/BaseGameTypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,15 @@ public static void SetMaterial(this Renderer renderer, int index, Material mater
mats[index] = material;
renderer.materials = mats;
}

public static Vector2 With(this Vector2 vec, float? x, float? y)
{
return new Vector2(x ?? vec.x, y ?? vec.y);
}

public static Vector3 With(this Vector3 vec, float? x, float? y, float? z)
{
return new Vector3(x ?? vec.x, y ?? vec.y, z ?? vec.z);
}
}
}
44 changes: 43 additions & 1 deletion SaberFactory/Helpers/DebugTools.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using UnityEngine;
using System.Diagnostics;
using SiraUtil.Tools;
using Debug = UnityEngine.Debug;

namespace SaberFactory.Helpers
{
Expand All @@ -9,4 +11,44 @@ public static void LogError(object message)
Debug.LogError(message);
}
}

public class DebugTimer
{
private readonly string _taskName;
private readonly Stopwatch _stopwatch;

public DebugTimer(string taskName = null)
{
_taskName = taskName ?? "Task";
_stopwatch = new Stopwatch();
}

public static DebugTimer StartNew(string taskName = null)
{
var sw = new DebugTimer(taskName);
sw.Start();
return sw;
}

public void Start()
{
_stopwatch.Start();
}

public void Print()
{
Debug.LogError(GetString());
}

public void Print(SiraLog logger)
{
logger.Info(GetString());
}

private string GetString()
{
_stopwatch.Stop();
return $"{_taskName} finished in {_stopwatch.Elapsed.Seconds}.{_stopwatch.Elapsed.Milliseconds}s";
}
}
}
39 changes: 39 additions & 0 deletions SaberFactory/Helpers/PathTools.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,60 @@
using System.IO;
using IPA.Utilities;
using UnityEngine;

namespace SaberFactory.Helpers
{
public static class PathTools
{
public static string RelativeExtension;

public static string SaberFactoryUserPath => Path.Combine(UnityGame.UserDataPath, "Saber Factory");

public static string ToFullPath(string relativePath) => Path.Combine(UnityGame.InstallPath, relativePath);

public static string ToRelativePath(string path) => path.Substring(UnityGame.InstallPath.Length+1);

public static string GetSubDir(string relPath)
{
relPath = CorrectRelativePath(relPath);

var split = relPath.Split(Path.DirectorySeparatorChar);
if (split.Length < 3) return "";
string output = "";
for (int i = 1; i < split.Length - 1; i++)
{
output += split[i];
if (i != split.Length - 2) output += "\\";
}

return output;
}

public static string CorrectRelativePath(string path)
{
if (!string.IsNullOrEmpty(RelativeExtension) && path.StartsWith(RelativeExtension))
{
return path.Substring(RelativeExtension.Length);
}

return path;
}

public static FileInfo GetFile(this DirectoryInfo dir, string fileName)
{
return new FileInfo(Path.Combine(dir.FullName, fileName));
}

public static DirectoryInfo GetDirectory(this DirectoryInfo dir, string dirName, bool create = false)
{
if (create)
{
return dir.CreateSubdirectory(dirName);
}

return new DirectoryInfo(Path.Combine(dir.FullName, dirName));
}

public static void WriteText(this FileInfo file, string text)
{
File.WriteAllText(file.FullName, text);
Expand Down
3 changes: 1 addition & 2 deletions SaberFactory/Installers/PluginAppInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public override void InstallBindings()

Container.BindInstance(rtOptions).AsSingle();

var dirs = SFDirectories.Create();
Container.BindInstance(dirs);
Container.Bind<PluginDirectories>().AsSingle();

Container.BindLoggerAsSiraLogger(_logger);
Container.BindInstance(_config).AsSingle();
Expand Down
2 changes: 1 addition & 1 deletion SaberFactory/Instances/Trail/TrailHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void CreateTrail(TrailConfig trailConfig, bool editor)

if (_backupTrail is null) return;

var trailStart = TrailInstance.gameObject.CreateGameObject("Trail Start");
var trailStart = TrailInstance.gameObject.CreateGameObject("Trail StartNew");
var trailEnd = TrailInstance.gameObject.CreateGameObject("TrailEnd");
trailEnd.transform.localPosition = new Vector3(0, 0, 1);

Expand Down
Loading

0 comments on commit 030b7be

Please sign in to comment.