Skip to content

Commit 13e6b7c

Browse files
authored
Merge pull request #90 from KaBooMa/pr-verification
Pr verification
2 parents 62bff73 + ebd9c5c commit 13e6b7c

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

S1API/Vehicles/LandVehicle.cs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#if (IL2CPPMELON)
2+
using S1Vehicles = Il2CppScheduleOne.Vehicles;
3+
using Il2Cpp;
4+
using Il2CppFishNet;
5+
using Il2CppFishNet.Connection;
6+
#elif (MONOMELON || MONOBEPINEX || IL2CPPBEPINEX)
7+
using S1Vehicles = ScheduleOne.Vehicles;
8+
using FishNet;
9+
using FishNet.Connection;
10+
#endif
11+
using System;
12+
using System.Reflection;
13+
using UnityEngine;
14+
using S1API.Logging;
15+
16+
namespace S1API.Vehicles
17+
{
18+
/// <summary>
19+
/// Represents a land vehicle in the game.
20+
/// </summary>
21+
public class LandVehicle
22+
{
23+
// Public members intended to be used by modders
24+
#region Public Members
25+
/// <summary>
26+
/// Creates a new LandVehicle instance.
27+
/// </summary>
28+
public LandVehicle(string vehicleCode)
29+
{
30+
var vehiclePrefab = S1Vehicles.VehicleManager.Instance.GetVehiclePrefab(vehicleCode);
31+
if (vehiclePrefab == null)
32+
{
33+
_logger.Error($"SpawnVehicle: '{vehicleCode}' is not a valid vehicle code!");
34+
return;
35+
}
36+
37+
var component = UnityEngine.Object.Instantiate<GameObject>(vehiclePrefab.gameObject)
38+
.GetComponent<S1Vehicles.LandVehicle>();
39+
40+
component.SetGUID(GUIDManager.GenerateUniqueGUID());
41+
S1Vehicles.VehicleManager.Instance.AllVehicles.Add(component);
42+
43+
S1LandVehicle = component;
44+
SetConnection();
45+
}
46+
47+
/// <summary>
48+
/// Vehicle price.
49+
/// </summary>
50+
public float VehiclePrice
51+
{
52+
get => S1LandVehicle.VehiclePrice;
53+
set => VehiclePriceField?.SetValue(S1LandVehicle, value);
54+
}
55+
56+
/// <summary>
57+
/// Vehicle's top speed.
58+
/// </summary>
59+
public float TopSpeed
60+
{
61+
get => S1LandVehicle.TopSpeed;
62+
set => S1LandVehicle.TopSpeed = value;
63+
}
64+
65+
/// <summary>
66+
/// If the vehicle is owned by the player.
67+
/// </summary>
68+
public bool IsPlayerOwned
69+
{
70+
get => S1LandVehicle.IsPlayerOwned;
71+
set => SetIsPlayerOwned(value);
72+
}
73+
74+
/// <summary>
75+
/// Vehicle's color.
76+
/// </summary>
77+
public VehicleColor Color
78+
{
79+
get => (VehicleColor)S1LandVehicle.OwnedColor;
80+
set => SetColor(value);
81+
}
82+
83+
/// <summary>
84+
/// Spawns the vehicle in the game world.
85+
/// </summary>
86+
/// <param name="position">Position in the world</param>
87+
/// <param name="rotation">Rotation of the vehicle</param>
88+
public void Spawn(Vector3 position, Quaternion rotation)
89+
{
90+
if (!InstanceFinder.IsServer)
91+
{
92+
_logger.Warning("Spawn can only be called on the server!");
93+
return;
94+
}
95+
96+
if (S1LandVehicle == null)
97+
throw new Exception("Unable to spawn vehicle, S1LandVehicle is null!");
98+
99+
S1LandVehicle.transform.position = position;
100+
S1LandVehicle.transform.rotation = rotation;
101+
S1Vehicles.VehicleManager.Instance.Spawn(S1LandVehicle.gameObject);
102+
}
103+
104+
#endregion
105+
106+
// Internal members used by S1API
107+
#region Internal Members
108+
109+
/// <summary>
110+
/// INTERNAL: The stored reference to the land vehicle in-game (see <see cref="S1Vehicles.LandVehicle"/>).
111+
/// </summary>
112+
internal S1Vehicles.LandVehicle S1LandVehicle = null!;
113+
114+
/// <summary>
115+
/// INTERNAL: Creates a LandVehicle instance from an in-game land vehicle instance.
116+
/// </summary>
117+
/// <param name="landVehicle">The in-game land vehicle instance.</param>
118+
internal LandVehicle(S1Vehicles.LandVehicle landVehicle)
119+
{
120+
S1LandVehicle = landVehicle;
121+
SetConnection();
122+
}
123+
124+
#endregion
125+
126+
// Private members used by LandVehicle class
127+
#region Private Members
128+
129+
/// <summary>
130+
/// Logger for the LandVehicle class.
131+
/// </summary>
132+
private static readonly Log _logger = new Log("S1API.LandVehicle");
133+
134+
/// <summary>
135+
/// The stored reference to protected vehiclePrice field in the land vehicle in-game.
136+
/// </summary>
137+
private static readonly FieldInfo? VehiclePriceField =
138+
typeof(S1Vehicles.LandVehicle).GetField("vehiclePrice", BindingFlags.NonPublic);
139+
140+
/// <summary>
141+
/// Connection to the player that owns the vehicle.
142+
/// </summary>
143+
private NetworkConnection? _conn;
144+
145+
/// <summary>
146+
/// Sets the connection to the player that owns the vehicle.
147+
/// </summary>
148+
private void SetConnection()
149+
{
150+
var nm = InstanceFinder.NetworkManager;
151+
if (nm.IsClientOnly)
152+
{
153+
var tempConn = InstanceFinder.ClientManager.Connection;
154+
if (tempConn != null && tempConn.IsValid)
155+
_conn = tempConn;
156+
}
157+
else if (nm.IsServerOnly || (nm.IsServer && !nm.IsClient))
158+
{
159+
var owner = S1LandVehicle.Owner;
160+
if (owner != null && owner.IsValid)
161+
_conn = owner;
162+
}
163+
}
164+
165+
/// <summary>
166+
/// Helper method to set the vehicle as player owned.
167+
/// </summary>
168+
/// <param name="isPlayerOwned">If true, sets vehicle as player owned</param>
169+
private void SetIsPlayerOwned(bool isPlayerOwned)
170+
{
171+
S1LandVehicle.SetIsPlayerOwned(_conn, isPlayerOwned);
172+
// make sure to add/remove the vehicle from the player owned vehicles list
173+
if (isPlayerOwned)
174+
S1Vehicles.VehicleManager.Instance.PlayerOwnedVehicles.Add(S1LandVehicle);
175+
else
176+
S1Vehicles.VehicleManager.Instance.PlayerOwnedVehicles.Remove(S1LandVehicle);
177+
}
178+
179+
/// <summary>
180+
/// Helper method to set the vehicle color.
181+
/// </summary>
182+
/// <param name="color">Vehicle's color</param>
183+
private void SetColor(VehicleColor color)
184+
{
185+
var setOwnedColorMethod =
186+
typeof(S1Vehicles.LandVehicle).GetMethod("SetOwnedColor",
187+
BindingFlags.Instance | BindingFlags.NonPublic);
188+
if (setOwnedColorMethod == null)
189+
{
190+
_logger.Error("SetOwnedColor method not found!");
191+
return;
192+
}
193+
194+
setOwnedColorMethod.Invoke(S1LandVehicle, [_conn, (S1Vehicles.Modification.EVehicleColor)color]);
195+
}
196+
#endregion
197+
}
198+
}

S1API/Vehicles/VehicleColor.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace S1API.Vehicles
2+
{
3+
/// <summary>
4+
/// Represents available colors for vehicles.
5+
/// </summary>
6+
public enum VehicleColor
7+
{
8+
Black,
9+
DarkGrey,
10+
LightGrey,
11+
White,
12+
Yellow,
13+
Orange,
14+
Red,
15+
DullRed,
16+
Pink,
17+
Purple,
18+
Navy,
19+
DarkBlue,
20+
LightBlue,
21+
Cyan,
22+
LightGreen,
23+
DarkGreen,
24+
Custom
25+
}
26+
}

0 commit comments

Comments
 (0)