Fix Sidekick tool startup and path handling
This commit is contained in:
@@ -230,6 +230,42 @@ namespace Synty.SidekickCharacters.API
|
||||
await runtime.PopulatePresetLibrary();
|
||||
}
|
||||
|
||||
private static List<string> GetSidekickPartFiles()
|
||||
{
|
||||
string packageRootPath = DatabaseManager.GetPackageRootAbsolutePath();
|
||||
if (!string.IsNullOrEmpty(packageRootPath))
|
||||
{
|
||||
string meshesRootPath = Path.Combine(packageRootPath, "Resources", "Meshes");
|
||||
if (Directory.Exists(meshesRootPath))
|
||||
{
|
||||
return Directory
|
||||
.GetFiles(meshesRootPath, "SK_*_*_*_*_*.fbx", SearchOption.AllDirectories)
|
||||
.Select(ToAssetPath)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
return Directory.GetFiles("Assets", "SK_*_*_*_*_*.fbx", SearchOption.AllDirectories).ToList();
|
||||
}
|
||||
|
||||
private static string ToAssetPath(string fullPath)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(fullPath))
|
||||
{
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
string normalizedFullPath = fullPath.Replace('\\', '/');
|
||||
string normalizedAssetsPath = Application.dataPath.Replace('\\', '/');
|
||||
|
||||
if (!normalizedFullPath.StartsWith(normalizedAssetsPath, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return normalizedFullPath;
|
||||
}
|
||||
|
||||
return "Assets" + normalizedFullPath.Substring(normalizedAssetsPath.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes all the parts selected in the window, and combines them into a single model in the scene.
|
||||
/// </summary>
|
||||
@@ -512,7 +548,7 @@ namespace Synty.SidekickCharacters.API
|
||||
_partOutfitToggleMap = new Dictionary<string, bool>();
|
||||
_partCount = 0;
|
||||
|
||||
List<string> files = Directory.GetFiles("Assets", "SK_*_*_*_*_*.fbx", SearchOption.AllDirectories).ToList();
|
||||
List<string> files = GetSidekickPartFiles();
|
||||
|
||||
foreach (CharacterPartType partType in Enum.GetValues(typeof(CharacterPartType)))
|
||||
{
|
||||
@@ -610,7 +646,7 @@ namespace Synty.SidekickCharacters.API
|
||||
_speciesDictionary = new Dictionary<string, SidekickSpecies>();
|
||||
_partCount = 0;
|
||||
|
||||
List<string> files = Directory.GetFiles("Assets", "SK_*_*_*_*_*.fbx", SearchOption.AllDirectories).ToList();
|
||||
List<string> files = GetSidekickPartFiles();
|
||||
Dictionary<string, string> filesOnDisk = new Dictionary<string, string>();
|
||||
foreach (string file in files)
|
||||
{
|
||||
|
||||
@@ -154,6 +154,18 @@ namespace Synty.SidekickCharacters.Database.DTO
|
||||
/// <returns>A color set with all DTO class properties set</returns>
|
||||
private static void Decorate(DatabaseManager dbManager, SidekickColorSet set)
|
||||
{
|
||||
if (set == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
set.SourceColorPath = DatabaseManager.NormalizeLegacyAssetPath(set.SourceColorPath);
|
||||
set.SourceMetallicPath = DatabaseManager.NormalizeLegacyAssetPath(set.SourceMetallicPath);
|
||||
set.SourceSmoothnessPath = DatabaseManager.NormalizeLegacyAssetPath(set.SourceSmoothnessPath);
|
||||
set.SourceReflectionPath = DatabaseManager.NormalizeLegacyAssetPath(set.SourceReflectionPath);
|
||||
set.SourceEmissionPath = DatabaseManager.NormalizeLegacyAssetPath(set.SourceEmissionPath);
|
||||
set.SourceOpacityPath = DatabaseManager.NormalizeLegacyAssetPath(set.SourceOpacityPath);
|
||||
|
||||
if (set.Species == null && set.PtrSpecies >= 0)
|
||||
{
|
||||
set.Species = SidekickSpecies.GetByID(dbManager, set.PtrSpecies);
|
||||
|
||||
@@ -222,6 +222,8 @@ namespace Synty.SidekickCharacters.Database.DTO
|
||||
{
|
||||
if (part != null)
|
||||
{
|
||||
part.Location = DatabaseManager.NormalizeLegacyAssetPath(part.Location);
|
||||
|
||||
if (part.Species == null && part.PtrSpecies >= 0)
|
||||
{
|
||||
part.Species = SidekickSpecies.GetByID(dbManager, part.PtrSpecies);
|
||||
|
||||
@@ -12,7 +12,9 @@
|
||||
using SQLite;
|
||||
using Synty.SidekickCharacters.Database.DTO;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Synty.SidekickCharacters.Database
|
||||
{
|
||||
@@ -21,11 +23,18 @@ namespace Synty.SidekickCharacters.Database
|
||||
/// </summary>
|
||||
public class DatabaseManager
|
||||
{
|
||||
private static readonly string _DATABASE_PATH = "Assets/Synty/SidekickCharacters/Database/Side_Kick_Data.db";
|
||||
private const string _DATABASE_FILE_NAME = "Side_Kick_Data.db";
|
||||
private const string _LEGACY_PACKAGE_ROOT = "Assets/Synty/SidekickCharacters";
|
||||
private const string _LEGACY_TOOLS_PACKAGE_ROOT = "Assets/Synty/Tools/SidekickCharacters";
|
||||
private const string _WORKING_DATABASE_DIRECTORY = "SidekickCharacters";
|
||||
private readonly string _CURRENT_VERSION = "1.0.2";
|
||||
|
||||
private static SQLiteConnection _connection;
|
||||
private static int _connectionHash;
|
||||
private static string _databasePath;
|
||||
private static string _seedDatabasePath;
|
||||
private static string _packageRootAbsolutePath;
|
||||
private static string _packageRootAssetPath;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the DB connection with the given connection details.
|
||||
@@ -39,7 +48,13 @@ namespace Synty.SidekickCharacters.Database
|
||||
{
|
||||
if (_connection == null)
|
||||
{
|
||||
_connection = new SQLiteConnection(_DATABASE_PATH, true);
|
||||
string databasePath = GetDatabasePath();
|
||||
if (string.IsNullOrEmpty(databasePath))
|
||||
{
|
||||
throw new FileNotFoundException("Unable to locate Sidekick database file in this Unity project.");
|
||||
}
|
||||
|
||||
_connection = new SQLiteConnection(databasePath, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -100,12 +115,12 @@ namespace Synty.SidekickCharacters.Database
|
||||
{
|
||||
Species = new SidekickSpecies { ID = -1, Name = "None" },
|
||||
Name = "Default",
|
||||
SourceColorPath = "Assets/Synty/Tools/SidekickCharacters/Resources/Textures/T_ColorMap.png",
|
||||
SourceMetallicPath = "Assets/Synty/Tools/SidekickCharacters/Resources/Textures/T_MetallicMap.png",
|
||||
SourceSmoothnessPath = "Assets/Synty/Tools/SidekickCharacters/Resources/Textures/T_SmoothnessMap.png",
|
||||
SourceReflectionPath = "Assets/Synty/Tools/SidekickCharacters/Resources/Textures/T_ReflectionMap.png",
|
||||
SourceEmissionPath = "Assets/Synty/Tools/SidekickCharacters/Resources/Textures/T_EmissionMap.png",
|
||||
SourceOpacityPath = "Assets/Synty/Tools/SidekickCharacters/Resources/Textures/T_OpacityMap.png",
|
||||
SourceColorPath = GetPackageAssetPath("Resources", "Textures", "T_ColorMap.png"),
|
||||
SourceMetallicPath = GetPackageAssetPath("Resources", "Textures", "T_MetallicMap.png"),
|
||||
SourceSmoothnessPath = GetPackageAssetPath("Resources", "Textures", "T_SmoothnessMap.png"),
|
||||
SourceReflectionPath = GetPackageAssetPath("Resources", "Textures", "T_ReflectionMap.png"),
|
||||
SourceEmissionPath = GetPackageAssetPath("Resources", "Textures", "T_EmissionMap.png"),
|
||||
SourceOpacityPath = GetPackageAssetPath("Resources", "Textures", "T_OpacityMap.png"),
|
||||
};
|
||||
|
||||
newSet.Save(this);
|
||||
@@ -206,5 +221,216 @@ namespace Synty.SidekickCharacters.Database
|
||||
{
|
||||
return new Version(GetCurrentDbConnection()?.Table<SidekickDBVersion>().FirstOrDefault().SemanticVersion ?? "0.0.1ea");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the absolute filesystem path to the Sidekick database file.
|
||||
/// </summary>
|
||||
/// <returns>The absolute path to the DB file, if found.</returns>
|
||||
public static string GetDatabasePath()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_databasePath) && File.Exists(_databasePath))
|
||||
{
|
||||
return _databasePath;
|
||||
}
|
||||
|
||||
string seedDatabasePath = GetSeedDatabasePath();
|
||||
if (string.IsNullOrEmpty(seedDatabasePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string projectRoot = Directory.GetParent(Application.dataPath)?.FullName;
|
||||
if (string.IsNullOrEmpty(projectRoot))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string workingDirectory = Path.Combine(projectRoot, "Library", _WORKING_DATABASE_DIRECTORY);
|
||||
Directory.CreateDirectory(workingDirectory);
|
||||
|
||||
_databasePath = Path.Combine(workingDirectory, _DATABASE_FILE_NAME);
|
||||
if (!File.Exists(_databasePath))
|
||||
{
|
||||
File.Copy(seedDatabasePath, _databasePath, false);
|
||||
}
|
||||
|
||||
_databasePath = Path.GetFullPath(_databasePath);
|
||||
CleanupLegacyDatabaseArtifacts(seedDatabasePath);
|
||||
return _databasePath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the absolute filesystem path to the Sidekick package root.
|
||||
/// </summary>
|
||||
/// <returns>The absolute path to the package root, if found.</returns>
|
||||
public static string GetPackageRootAbsolutePath()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_packageRootAbsolutePath) && Directory.Exists(_packageRootAbsolutePath))
|
||||
{
|
||||
return _packageRootAbsolutePath;
|
||||
}
|
||||
|
||||
string seedDatabasePath = GetSeedDatabasePath();
|
||||
if (string.IsNullOrEmpty(seedDatabasePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string databaseDirectory = Path.GetDirectoryName(seedDatabasePath);
|
||||
if (string.IsNullOrEmpty(databaseDirectory))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
_packageRootAbsolutePath = Path.GetDirectoryName(databaseDirectory);
|
||||
if (!string.IsNullOrEmpty(_packageRootAbsolutePath))
|
||||
{
|
||||
_packageRootAbsolutePath = Path.GetFullPath(_packageRootAbsolutePath);
|
||||
}
|
||||
|
||||
return _packageRootAbsolutePath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the project-relative asset path to the Sidekick package root.
|
||||
/// </summary>
|
||||
/// <returns>The asset path to the package root, if found.</returns>
|
||||
public static string GetPackageRootAssetPath()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_packageRootAssetPath))
|
||||
{
|
||||
return _packageRootAssetPath;
|
||||
}
|
||||
|
||||
string packageRootAbsolutePath = GetPackageRootAbsolutePath();
|
||||
if (string.IsNullOrEmpty(packageRootAbsolutePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
_packageRootAssetPath = ToAssetPath(packageRootAbsolutePath);
|
||||
return _packageRootAssetPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a project-relative asset path under the Sidekick package root.
|
||||
/// </summary>
|
||||
/// <param name="relativeSegments">Path segments under the package root.</param>
|
||||
/// <returns>The combined asset path, if the package root could be found.</returns>
|
||||
public static string GetPackageAssetPath(params string[] relativeSegments)
|
||||
{
|
||||
string packageRootAssetPath = GetPackageRootAssetPath();
|
||||
if (string.IsNullOrEmpty(packageRootAssetPath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string combinedPath = packageRootAssetPath;
|
||||
foreach (string segment in relativeSegments)
|
||||
{
|
||||
combinedPath = Path.Combine(combinedPath, segment);
|
||||
}
|
||||
|
||||
return NormalizePathSeparators(combinedPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes legacy Sidekick asset paths so moved packages still load correctly.
|
||||
/// </summary>
|
||||
/// <param name="assetPath">The asset path to normalize.</param>
|
||||
/// <returns>The remapped path if it pointed at an old root; otherwise the original path.</returns>
|
||||
public static string NormalizeLegacyAssetPath(string assetPath)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(assetPath))
|
||||
{
|
||||
return assetPath;
|
||||
}
|
||||
|
||||
string normalizedPath = NormalizePathSeparators(assetPath);
|
||||
string packageRootAssetPath = GetPackageRootAssetPath();
|
||||
if (string.IsNullOrEmpty(packageRootAssetPath))
|
||||
{
|
||||
return normalizedPath;
|
||||
}
|
||||
|
||||
if (normalizedPath.StartsWith(_LEGACY_TOOLS_PACKAGE_ROOT, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return packageRootAssetPath + normalizedPath.Substring(_LEGACY_TOOLS_PACKAGE_ROOT.Length);
|
||||
}
|
||||
|
||||
if (normalizedPath.StartsWith(_LEGACY_PACKAGE_ROOT, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return packageRootAssetPath + normalizedPath.Substring(_LEGACY_PACKAGE_ROOT.Length);
|
||||
}
|
||||
|
||||
return normalizedPath;
|
||||
}
|
||||
|
||||
private static string ToAssetPath(string fullPath)
|
||||
{
|
||||
string normalizedFullPath = NormalizePathSeparators(Path.GetFullPath(fullPath));
|
||||
string normalizedAssetsPath = NormalizePathSeparators(Path.GetFullPath(Application.dataPath));
|
||||
|
||||
if (!normalizedFullPath.StartsWith(normalizedAssetsPath, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return normalizedFullPath;
|
||||
}
|
||||
|
||||
return "Assets" + normalizedFullPath.Substring(normalizedAssetsPath.Length);
|
||||
}
|
||||
|
||||
private static string NormalizePathSeparators(string path)
|
||||
{
|
||||
return path?.Replace('\\', '/');
|
||||
}
|
||||
|
||||
private static string GetSeedDatabasePath()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_seedDatabasePath) && File.Exists(_seedDatabasePath))
|
||||
{
|
||||
return _seedDatabasePath;
|
||||
}
|
||||
|
||||
string[] databaseFiles = Directory.GetFiles(Application.dataPath, _DATABASE_FILE_NAME, SearchOption.AllDirectories);
|
||||
_seedDatabasePath = databaseFiles
|
||||
.OrderBy(path => path.Contains($"{Path.DirectorySeparatorChar}SidekickCharacters{Path.DirectorySeparatorChar}Database{Path.DirectorySeparatorChar}") ? 0 : 1)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (!string.IsNullOrEmpty(_seedDatabasePath))
|
||||
{
|
||||
_seedDatabasePath = Path.GetFullPath(_seedDatabasePath);
|
||||
}
|
||||
|
||||
return _seedDatabasePath;
|
||||
}
|
||||
|
||||
private static void CleanupLegacyDatabaseArtifacts(string seedDatabasePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(seedDatabasePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DeleteIfExists(seedDatabasePath + "-journal");
|
||||
DeleteIfExists(seedDatabasePath + "-journal.meta");
|
||||
DeleteIfExists(seedDatabasePath + "-wal");
|
||||
DeleteIfExists(seedDatabasePath + "-wal.meta");
|
||||
DeleteIfExists(seedDatabasePath + "-shm");
|
||||
DeleteIfExists(seedDatabasePath + "-shm.meta");
|
||||
}
|
||||
|
||||
private static void DeleteIfExists(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user