-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from SpaceWarpDev/v1.1.0
V1.1.0
- Loading branch information
Showing
54 changed files
with
2,929 additions
and
1,285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<SpaceWarpVersion>1.1.0</SpaceWarpVersion> | ||
<TargetFramework>net472</TargetFramework> | ||
<LangVersion>11</LangVersion> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies> | ||
<!-- Disabled warning for publicizer attribute until the NuGet exists for KSP2 --> | ||
<NoWarn>CS0436</NoWarn> | ||
</PropertyGroup> | ||
|
||
<!-- | ||
Allows use of some newer C# language features that have compiler gates normally. | ||
Do not modify the included types without checking that they don't require runtime support that doesn't exist. | ||
--> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="PolySharp" Version="1.12.1" PrivateAssets="all"/> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<PolySharpIncludeGeneratedTypes> | ||
System.Index; | ||
System.Range; | ||
System.Diagnostics.CodeAnalysis.NotNullWhenAttribute; | ||
System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute; | ||
</PolySharpIncludeGeneratedTypes> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<AssemblySearchPaths> | ||
$(MSBuildThisFileDirectory)external_dlls; | ||
$(AssemblySearchPaths) | ||
</AssemblySearchPaths> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,136 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using BepInEx.Logging; | ||
using UnityEngine; | ||
using Logger = BepInEx.Logging.Logger; | ||
|
||
namespace SpaceWarp.API.Assets; | ||
|
||
public static class AssetManager | ||
{ | ||
private static readonly Dictionary<string, UnityObject> AllAssets = new(); | ||
|
||
internal static async Task RegisterAssetBundle(string modId, string assetBundleName, AssetBundle assetBundle) | ||
{ | ||
assetBundleName = assetBundleName.Replace(".bundle", ""); | ||
ManualLogSource logger = BepInEx.Logging.Logger.CreateLogSource($"{modId}/{assetBundleName}"); | ||
|
||
string[] names = assetBundle.GetAllAssetNames(); | ||
|
||
foreach (var name in names) | ||
{ | ||
var assetName = name; | ||
|
||
if (assetName.ToLower().StartsWith("assets/")) | ||
{ | ||
assetName = assetName["assets/".Length..]; | ||
} | ||
|
||
if (assetName.ToLower().StartsWith(assetBundleName + "/")) | ||
{ | ||
assetName = assetName[(assetBundleName.Length + 1)..]; | ||
} | ||
|
||
string path = modId + "/" + assetBundleName + "/" + assetName; | ||
path = path.ToLower(); | ||
|
||
await assetBundle.LoadAssetAsync(name); | ||
|
||
UnityObject bundleObject = assetBundle.LoadAssetAsync(name).asset; | ||
logger.LogInfo($"registering path \"{path}\""); | ||
|
||
AllAssets.Add(path, bundleObject); | ||
} | ||
} | ||
|
||
// if (bundleObjects.Length != names.Length) | ||
// { | ||
// logger.Critical("bundle objects length and name lengths do not match"); | ||
// logger.Info("going to dump objects and names"); | ||
// logger.Info("Names"); | ||
// for (int i = 0; i < names.Length; i++) | ||
// { | ||
// logger.Info($"{i} - {names[i]}"); | ||
// } | ||
// | ||
// logger.Info("Objects"); | ||
// for (int i = 0; i < bundleObjects.Length; i++) | ||
// { | ||
// logger.Info($"{i} - {bundleObjects[i]}"); | ||
// } | ||
// throw new System.Exception("bundle objects length and name lengths do not match"); | ||
// } | ||
|
||
|
||
internal static void RegisterSingleAsset<T>(string modId, string internalAssetPath, T asset) where T : UnityObject | ||
{ | ||
var path = $"{modId}/{internalAssetPath}"; | ||
path = path.ToLower(); | ||
ManualLogSource logger = BepInEx.Logging.Logger.CreateLogSource($"{path}"); | ||
logger.LogInfo($"registering path \"{path}\""); | ||
AllAssets.Add(path,asset); | ||
} | ||
|
||
/// <summary> | ||
/// Gets an asset from the specified asset path | ||
/// </summary> | ||
/// <typeparam name="T">The type</typeparam> | ||
/// <param name="path">an asset path, format: {mod_id}/{asset_bundle}/{asset_path}</param> | ||
/// <returns></returns> | ||
public static T GetAsset<T>(string path) where T: UnityEngine.Object | ||
{ | ||
path = path.ToLower(); | ||
string[] subPaths = path.Split('/', '\\'); | ||
if (subPaths.Length < 3) | ||
{ | ||
throw new ArgumentException("Invalid path, asset paths must follow to following structure: {mod_id}/{asset_bundle}/{asset_path}"); | ||
} | ||
|
||
if (!AllAssets.TryGetValue(path, out UnityObject value)) | ||
{ | ||
throw new IndexOutOfRangeException($"Unable to find asset at path \"{path}\""); | ||
} | ||
|
||
if (value is not T tValue) | ||
{ | ||
throw new InvalidCastException($"The asset at path {path} isn't of type {typeof(T).Name} but of type {value.GetType().Name}"); | ||
} | ||
|
||
return tValue; | ||
} | ||
|
||
/// <summary> | ||
/// Tries to get an asset from the specified asset path | ||
/// </summary> | ||
/// <typeparam name="T">The type</typeparam> | ||
/// <param name="path">an asset path, format: {mod_id}/{asset_bundle}/{asset_name}</param> | ||
/// <param name="asset">the asset output</param> | ||
/// <returns>Whether or not the asset exists and is loaded</returns> | ||
public static bool TryGetAsset<T>(string path, out T asset) where T : UnityObject | ||
{ | ||
path = path.ToLower(); | ||
asset = null; | ||
string[] subPaths = path.Split('/', '\\'); | ||
|
||
if (subPaths.Length < 3) | ||
{ | ||
return false; | ||
} | ||
if (!AllAssets.TryGetValue(path, out UnityObject value)) | ||
{ | ||
return false; | ||
} | ||
if (value is not T tValue) | ||
{ | ||
return false; | ||
} | ||
|
||
asset = tValue; | ||
|
||
return true; | ||
} | ||
private static readonly Dictionary<string, UnityObject> AllAssets = new(); | ||
|
||
internal static async Task RegisterAssetBundle(string modId, string assetBundleName, AssetBundle assetBundle) | ||
{ | ||
assetBundleName = assetBundleName.Replace(".bundle", ""); | ||
var logger = Logger.CreateLogSource($"{modId}/{assetBundleName}"); | ||
|
||
var names = assetBundle.GetAllAssetNames(); | ||
|
||
foreach (var name in names) | ||
{ | ||
var assetName = name; | ||
|
||
if (assetName.ToLower().StartsWith("assets/")) | ||
{ | ||
assetName = assetName["assets/".Length..]; | ||
} | ||
|
||
if (assetName.ToLower().StartsWith(assetBundleName + "/")) | ||
{ | ||
assetName = assetName[(assetBundleName.Length + 1)..]; | ||
} | ||
|
||
var path = modId + "/" + assetBundleName + "/" + assetName; | ||
path = path.ToLower(); | ||
|
||
await assetBundle.LoadAssetAsync(name); | ||
|
||
var bundleObject = assetBundle.LoadAssetAsync(name).asset; | ||
logger.LogInfo($"registering path \"{path}\""); | ||
|
||
AllAssets.Add(path, bundleObject); | ||
} | ||
} | ||
|
||
// if (bundleObjects.Length != names.Length) | ||
// { | ||
// logger.Critical("bundle objects length and name lengths do not match"); | ||
// logger.Info("going to dump objects and names"); | ||
// logger.Info("Names"); | ||
// for (int i = 0; i < names.Length; i++) | ||
// { | ||
// logger.Info($"{i} - {names[i]}"); | ||
// } | ||
// | ||
// logger.Info("Objects"); | ||
// for (int i = 0; i < bundleObjects.Length; i++) | ||
// { | ||
// logger.Info($"{i} - {bundleObjects[i]}"); | ||
// } | ||
// throw new System.Exception("bundle objects length and name lengths do not match"); | ||
// } | ||
|
||
|
||
internal static void RegisterSingleAsset<T>(string modId, string internalAssetPath, T asset) where T : UnityObject | ||
{ | ||
var path = $"{modId}/{internalAssetPath}"; | ||
path = path.ToLower(); | ||
var logger = Logger.CreateLogSource($"{path}"); | ||
logger.LogInfo($"registering path \"{path}\""); | ||
AllAssets.Add(path, asset); | ||
} | ||
|
||
/// <summary> | ||
/// Gets an asset from the specified asset path | ||
/// </summary> | ||
/// <typeparam name="T">The type</typeparam> | ||
/// <param name="path">an asset path, format: {mod_id}/{asset_bundle}/{asset_path}</param> | ||
/// <returns></returns> | ||
public static T GetAsset<T>(string path) where T : UnityObject | ||
{ | ||
path = path.ToLower(); | ||
var subPaths = path.Split('/', '\\'); | ||
if (subPaths.Length < 3) | ||
{ | ||
throw new ArgumentException( | ||
"Invalid path, asset paths must follow to following structure: {mod_id}/{asset_bundle}/{asset_path}"); | ||
} | ||
|
||
if (!AllAssets.TryGetValue(path, out var value)) | ||
{ | ||
throw new IndexOutOfRangeException($"Unable to find asset at path \"{path}\""); | ||
} | ||
|
||
if (value is not T tValue) | ||
{ | ||
throw new InvalidCastException( | ||
$"The asset at path {path} isn't of type {typeof(T).Name} but of type {value.GetType().Name}"); | ||
} | ||
|
||
return tValue; | ||
} | ||
|
||
/// <summary> | ||
/// Tries to get an asset from the specified asset path | ||
/// </summary> | ||
/// <typeparam name="T">The type</typeparam> | ||
/// <param name="path">an asset path, format: {mod_id}/{asset_bundle}/{asset_name}</param> | ||
/// <param name="asset">the asset output</param> | ||
/// <returns>Whether or not the asset exists and is loaded</returns> | ||
public static bool TryGetAsset<T>(string path, out T asset) where T : UnityObject | ||
{ | ||
path = path.ToLower(); | ||
asset = null; | ||
var subPaths = path.Split('/', '\\'); | ||
|
||
if (subPaths.Length < 3) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!AllAssets.TryGetValue(path, out var value)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (value is not T tValue) | ||
{ | ||
return false; | ||
} | ||
|
||
asset = tValue; | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
using KSP.Game; | ||
using System.Collections.Generic; | ||
using KSP.Game; | ||
using KSP.Sim.Definitions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SpaceWarp.API.Game.Extensions; | ||
|
||
public static class PartProviderExtensions | ||
{ | ||
public static IEnumerable<PartCore> WithModule<T>(this PartProvider provider) where T : ModuleData | ||
{ | ||
return provider._partData.Values.Where(part => part.modules.OfType<T>().Count() > 0); | ||
return provider._partData.Values.Where(part => part.modules.OfType<T>().Any()); | ||
} | ||
} | ||
} |
Oops, something went wrong.