Skip to content

Commit

Permalink
Merge pull request #200 from SpaceWarpDev/v1.1.0
Browse files Browse the repository at this point in the history
V1.1.0
  • Loading branch information
cheese3660 authored Apr 2, 2023
2 parents 29c033c + f2031d5 commit b22e436
Show file tree
Hide file tree
Showing 54 changed files with 2,929 additions and 1,285 deletions.
29 changes: 29 additions & 0 deletions Directory.Build.props
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>
8 changes: 8 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<AssemblySearchPaths>
$(MSBuildThisFileDirectory)external_dlls;
$(AssemblySearchPaths)
</AssemblySearchPaths>
</PropertyGroup>
</Project>
13 changes: 1 addition & 12 deletions ExampleMod/ExampleMod.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<LangVersion>11</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>..\external_dlls\Assembly-CSharp.dll</HintPath>
<Publicize>true</Publicize>
<Private>false</Private>
</Reference>
<Reference Include="Assembly-CSharp" Private="false" Publicize="true" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SpaceWarp\SpaceWarp.csproj" Private="false" />
Expand Down
6 changes: 6 additions & 0 deletions SpaceWarp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpaceWarp", "SpaceWarp\Spac
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleMod", "ExampleMod\ExampleMod.csproj", "{56255DE9-749D-447D-B1D8-1B634535A087}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpaceWarpPatcher", "SpaceWarpPatcher\SpaceWarpPatcher.csproj", "{2EF642D0-F66D-461C-A5B0-953E39307B76}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{56255DE9-749D-447D-B1D8-1B634535A087}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56255DE9-749D-447D-B1D8-1B634535A087}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56255DE9-749D-447D-B1D8-1B634535A087}.Release|Any CPU.Build.0 = Release|Any CPU
{2EF642D0-F66D-461C-A5B0-953E39307B76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2EF642D0-F66D-461C-A5B0-953E39307B76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2EF642D0-F66D-461C-A5B0-953E39307B76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2EF642D0-F66D-461C-A5B0-953E39307B76}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
248 changes: 126 additions & 122 deletions SpaceWarp/API/Assets/AssetManager.cs
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;
}
}
12 changes: 4 additions & 8 deletions SpaceWarp/API/Game/Extensions/PartProviderExtensions.cs
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());
}
}
}
Loading

0 comments on commit b22e436

Please sign in to comment.