Skip to content

Commit

Permalink
Merge pull request #177 from SpaceWarpDev/v1.0.0
Browse files Browse the repository at this point in the history
Space Warp - v1.0.0 Release
  • Loading branch information
cheese3660 authored Mar 16, 2023
2 parents 141290f + afe0e9a commit d0c8b92
Show file tree
Hide file tree
Showing 22 changed files with 916 additions and 112 deletions.
1 change: 0 additions & 1 deletion ExampleMod/ExampleMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public override void OnInitialized()
// Example of using the logger, Were going to log a message to the console, ALT + C to open the console.
Logger.LogInfo("Hello World, Im a spacewarp Mod.");


// Register the mod's button in KSP 2s app.bar
// This requires an `icon.png` file to exist under [plugin_folder]/assets/images
Appbar.RegisterAppButton(
Expand Down
1 change: 1 addition & 0 deletions ExampleMod/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"author": "Space-Warp Team",
"description": "A Example Mod for Space-Warp",
"source": "https://github.com/SpaceWarpDev/SpaceWarp/tree/main/ExampleMod",
"version_check": "https://raw.githubusercontent.com/SpaceWarpDev/SpaceWarp/main/ExampleMod/swinfo.json",
"version": "0.4.0",
"dependencies": [],
"ksp2_version": {
Expand Down
50 changes: 26 additions & 24 deletions SpaceWarp/API/Assets/AssetManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BepInEx.Logging;
using UnityEngine;

Expand All @@ -9,13 +10,11 @@ public static class AssetManager
{
private static readonly Dictionary<string, UnityObject> AllAssets = new();

internal static void RegisterAssetBundle(string modId, string assetBundleName, AssetBundle assetBundle)
internal static async Task RegisterAssetBundle(string modId, string assetBundleName, AssetBundle assetBundle)
{
assetBundleName = assetBundleName.Replace(".bundle", "");
ManualLogSource logger = BepInEx.Logging.Logger.CreateLogSource($"{modId}/{assetBundleName}");
// TODO: use async loading instead?

// Object[] bundleObjects = assetBundle.LoadAllAssets();
string[] names = assetBundle.GetAllAssetNames();

foreach (var name in names)
Expand All @@ -31,34 +30,37 @@ internal static void RegisterAssetBundle(string modId, string assetBundleName, A
{
assetName = assetName[(assetBundleName.Length + 1)..];
}

string path = modId + "/" + assetBundleName + "/" + assetName;
path = path.ToLower();

UnityObject bundleObject = assetBundle.LoadAsset(name);

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");
// }
}

// 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
{
Expand Down
17 changes: 17 additions & 0 deletions SpaceWarp/API/Game/Extensions/PartProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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);
}
}
219 changes: 219 additions & 0 deletions SpaceWarp/API/Game/Extensions/VesselVehicleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
using KSP.Sim.impl;
using KSP.Sim.State;
using UnityEngine;

namespace SpaceWarp.API.Game.Extensions;

public static class VesselVehicleExtensions
{
public static void SetMainThrottle(this VesselVehicle vehicle, float throttle)
{
var incremental = new FlightCtrlStateIncremental()
{
mainThrottle = throttle
};
vehicle.AtomicSet(incremental);
}

public static void SetRoll(this VesselVehicle vehicle, float roll)
{
var incremental = new FlightCtrlStateIncremental()
{
roll = roll
};
vehicle.AtomicSet(incremental);
}

public static void SetYaw(this VesselVehicle vehicle, float yaw)
{
var incremental = new FlightCtrlStateIncremental()
{
yaw = yaw
};
vehicle.AtomicSet(incremental);
}
public static void SetPitch(this VesselVehicle vehicle, float pitch)
{
var incremental = new FlightCtrlStateIncremental()
{
pitch = pitch
};
vehicle.AtomicSet(incremental);
}

public static void SetRollYawPitch(this VesselVehicle vehicle, float roll, float yaw, float pitch)
{
var incremental = new FlightCtrlStateIncremental()
{
roll = roll,
yaw = yaw,
pitch = pitch
};
vehicle.AtomicSet(incremental);
}

public static void SetRollTrim(this VesselVehicle vehicle, float rollTrim)
{
var incremental = new FlightCtrlStateIncremental()
{
rollTrim=rollTrim
};
vehicle.AtomicSet(incremental);
}

public static void SetYawTrim(this VesselVehicle vehicle, float yawTrim)
{
var incremental = new FlightCtrlStateIncremental()
{
yawTrim=yawTrim
};
vehicle.AtomicSet(incremental);
}

public static void SetPitchTrim(this VesselVehicle vehicle, float pitchTrim)
{
var incremental = new FlightCtrlStateIncremental()
{
pitchTrim = pitchTrim
};
vehicle.AtomicSet(incremental);
}


public static void SetRollYawPitchTrim(this VesselVehicle vehicle, float rollTrim, float yawTrim, float pitchTrim)
{
var incremental = new FlightCtrlStateIncremental()
{
rollTrim = rollTrim,
yawTrim = yawTrim,
pitchTrim = pitchTrim
};
vehicle.AtomicSet(incremental);
}

public static void SetInputRoll(this VesselVehicle vehicle, float roll)
{
var incremental = new FlightCtrlStateIncremental()
{
inputRoll = roll
};
vehicle.AtomicSet(incremental);
}

public static void SetInputYaw(this VesselVehicle vehicle, float yaw)
{
var incremental = new FlightCtrlStateIncremental()
{
inputYaw = yaw
};
vehicle.AtomicSet(incremental);
}
public static void SetInputPitch(this VesselVehicle vehicle, float pitch)
{
var incremental = new FlightCtrlStateIncremental()
{
inputPitch = pitch
};
vehicle.AtomicSet(incremental);
}

public static void SetInputRollYawPitch(this VesselVehicle vehicle, float roll, float yaw, float pitch)
{
var incremental = new FlightCtrlStateIncremental()
{
inputRoll = roll,
inputYaw = yaw,
inputPitch = pitch
};
vehicle.AtomicSet(incremental);
}

public static void SetWheelSteer(this VesselVehicle vehicle, float wheelSteer, float? wheelSteerTrim=null)
{
var incremental = new FlightCtrlStateIncremental()
{
wheelSteer = wheelSteer,
wheelSteerTrim = wheelSteerTrim
};
vehicle.AtomicSet(incremental);
}

public static void SetWheelThrottle(this VesselVehicle vehicle, float wheelThrottle, float? wheelThrottleTrim=null)
{
var incremental = new FlightCtrlStateIncremental()
{
wheelThrottle = wheelThrottle,
wheelThrottleTrim = wheelThrottleTrim
};
vehicle.AtomicSet(incremental);
}

public static void SetXYZ(this VesselVehicle vehicle, float? X = null, float? Y = null, float? Z = null)
{
var incremental = new FlightCtrlStateIncremental()
{
X = X,
Y = Y,
Z = Z
};
vehicle.AtomicSet(incremental);
}
public static void SetXYZ(this VesselVehicle vehicle, Vector3 XYZ)
{
var incremental = new FlightCtrlStateIncremental()
{
X = XYZ.x,
Y = XYZ.y,
Z = XYZ.z
};
vehicle.AtomicSet(incremental);
}

public static void SetKillRot(this VesselVehicle vehicle, bool killRot)
{
var incremental = new FlightCtrlStateIncremental()
{
killRot = killRot
};
vehicle.AtomicSet(incremental);
}

public static void SetGearState(this VesselVehicle vehicle, bool up)
{
var incremental = new FlightCtrlStateIncremental()
{
gearUp = up,
gearDown = !up
};
vehicle.AtomicSet(incremental);
}


public static void SetHeadlight(this VesselVehicle vehicle, bool on)
{
var incremental = new FlightCtrlStateIncremental()
{
headlight = on
};
vehicle.AtomicSet(incremental);
}


public static void SetBrake(this VesselVehicle vehicle, bool on)
{
var incremental = new FlightCtrlStateIncremental()
{
brakes = on
};
vehicle.AtomicSet(incremental);
}

public static void SetStage(this VesselVehicle vehicle, bool stage)
{
var incremental = new FlightCtrlStateIncremental()
{
stage = stage
};
vehicle.AtomicSet(incremental);
}
}
5 changes: 3 additions & 2 deletions SpaceWarp/API/Game/Messages/StateChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public static class StateChanges
#region State changing

/// <summary>
/// Action(Message,PreviousState,CurrentState>
/// Invoked when the game state is changed
/// <para>Action(Message,PreviousState,CurrentState)</para>
/// </summary>
public static event System.Action<GameStateChangedMessage, GameState, GameState> GameStateChanged;

Expand Down Expand Up @@ -213,7 +214,7 @@ internal static void OnGameStateLeft(MessageCenterMessage message)
internal static void OnGameStateChanged(MessageCenterMessage message)
{
var msg = message as GameStateChangedMessage;
GameStateChanged?.Invoke(msg!,msg!.PreviousState,msg!.CurrentState);
GameStateChanged?.Invoke(msg!, msg!.PreviousState, msg!.CurrentState);
}

#endregion
Expand Down
10 changes: 10 additions & 0 deletions SpaceWarp/API/Game/Vehicle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using KSP.Game;
using KSP.Sim.impl;

namespace SpaceWarp.API.Game;

public static class Vehicle
{
public static VesselVehicle ActiveVesselVehicle => GameManager.Instance.Game.ViewController._activeVesselVehicle;
public static VesselComponent ActiveSimVessel => GameManager.Instance.Game.ViewController.GetActiveSimVessel();
}
5 changes: 5 additions & 0 deletions SpaceWarp/API/Mods/JSON/ModInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace SpaceWarp.API.Mods.JSON;

Expand Down Expand Up @@ -32,4 +33,8 @@ public sealed class ModInfo

[JsonProperty("ksp2_version")]
public SupportedVersionsInfo SupportedKsp2Versions { get; internal set; }

[JsonProperty("version_check", Required = Required.AllowNull)]
[CanBeNull]
public string VersionCheck { get; internal set; }
}
Loading

0 comments on commit d0c8b92

Please sign in to comment.