Skip to content

Commit 4e82089

Browse files
committed
Replace sync properties in IWorld with async setter and getter
1 parent 9b1d60a commit 4e82089

File tree

2 files changed

+80
-33
lines changed

2 files changed

+80
-33
lines changed

src/AlternateLife.RageMP.Net/Interfaces/IWorld.cs

+30-8
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,48 @@ namespace AlternateLife.RageMP.Net.Interfaces
88
public interface IWorld
99
{
1010
/// <summary>
11-
/// Get or set the time of the world.
11+
/// Set the time of the world.
1212
/// </summary>
13-
TimeData Time { get; set; }
13+
Task SetTimeAsync(TimeData time);
1414

1515
/// <summary>
16-
/// Get or set the weather of the world.
16+
/// Get the time of the world.
17+
/// </summary>
18+
Task<TimeData> GetTimeAsync();
19+
20+
/// <summary>
21+
/// Set the weather of the world.
22+
///
23+
/// If value is set to WeatherType.XMAS, snow will lay on the ground.
24+
/// </summary>
25+
Task SetWeatherAsync(WeatherType type);
26+
27+
/// <summary>
28+
/// Get the weather of the world.
1729
///
1830
/// If value is set to WeatherType.XMAS, snow will lay on the ground.
1931
/// </summary>
20-
WeatherType Weather { get; set; }
32+
Task<WeatherType> GetWeatherAsync();
33+
34+
/// <summary>
35+
/// Set the traffic lights lock state of the world.
36+
/// </summary>
37+
Task SetTrafficLightsLockedAsync(bool locked);
38+
39+
/// <summary>
40+
/// Get the traffic lights lock state of the world.
41+
/// </summary>
42+
Task<bool> AreTrafficLightsLockedAsync();
2143

2244
/// <summary>
23-
/// Get or set the traffic lights lock state of the world.
45+
/// Set the traffic light state of the world.
2446
/// </summary>
25-
bool AreTrafficLightsLocked { get; set; }
47+
Task SetTrafficLightsStateAsync(int state);
2648

2749
/// <summary>
28-
/// Get or set the traffic light state of the world.
50+
/// Get the traffic light state of the world.
2951
/// </summary>
30-
int TrafficLightsState { get; set; }
52+
Task<int> GetTrafficLightsStateAsync();
3153

3254
/// <summary>
3355
/// Start a weather transition on the world.

src/AlternateLife.RageMP.Net/Scripting/ScriptingClasses/World.cs

+50-25
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,71 @@ internal class World : IWorld
1313
private readonly IntPtr _nativePointer;
1414
private readonly Plugin _plugin;
1515

16-
public TimeData Time
16+
public World(IntPtr nativePointer, Plugin plugin)
17+
{
18+
_nativePointer = nativePointer;
19+
_plugin = plugin;
20+
}
21+
22+
public Task SetTimeAsync(TimeData time)
1723
{
18-
get => StructConverter.PointerToStruct<TimeData>(Rage.World.World_GetTime(_nativePointer));
19-
set => Rage.World.World_SetTime(_nativePointer, value);
24+
return _plugin.Schedule(() => Rage.World.World_SetTime(_nativePointer, time));
2025
}
2126

22-
public WeatherType Weather
27+
public async Task<TimeData> GetTimeAsync()
2328
{
24-
get => ConvertWeatherNameToType(StringConverter.PointerToString(Rage.World.World_GetWeather(_nativePointer)));
25-
set
29+
var timePointer = await _plugin
30+
.Schedule(() => Rage.World.World_GetTime(_nativePointer))
31+
.ConfigureAwait(false);
32+
33+
return StructConverter.PointerToStruct<TimeData>(timePointer);
34+
}
35+
36+
public async Task SetWeatherAsync(WeatherType type)
37+
{
38+
var weatherName = ConvertWeatherTypeToName(type);
39+
if (string.IsNullOrEmpty(weatherName))
2640
{
27-
var weatherName = ConvertWeatherTypeToName(value);
28-
if (string.IsNullOrEmpty(weatherName))
29-
{
30-
return;
31-
}
32-
33-
using (var converter = new StringConverter())
34-
{
35-
Rage.World.World_SetWeather(_nativePointer, converter.StringToPointer(weatherName));
36-
}
41+
return;
42+
}
43+
44+
using (var converter = new StringConverter())
45+
{
46+
var weatherPointer = converter.StringToPointer(weatherName);
47+
48+
await _plugin
49+
.Schedule(() => Rage.World.World_SetWeather(_nativePointer, weatherPointer))
50+
.ConfigureAwait(false);
3751
}
3852
}
3953

40-
public bool AreTrafficLightsLocked
54+
public async Task<WeatherType> GetWeatherAsync()
4155
{
42-
get => Rage.World.World_AreTrafficLightsLocked(_nativePointer);
43-
set => Rage.World.World_LockTrafficLights(_nativePointer, value);
56+
var weatherPointer = await _plugin
57+
.Schedule(() => Rage.World.World_GetWeather(_nativePointer))
58+
.ConfigureAwait(false);
59+
60+
return ConvertWeatherNameToType(StringConverter.PointerToString(weatherPointer));
4461
}
4562

46-
public int TrafficLightsState
63+
public Task SetTrafficLightsLockedAsync(bool locked)
4764
{
48-
get => Rage.World.World_GetTrafficLightsState(_nativePointer);
49-
set => Rage.World.World_SetTrafficLightsState(_nativePointer, value);
65+
return _plugin.Schedule(() => Rage.World.World_LockTrafficLights(_nativePointer, locked));
5066
}
5167

52-
public World(IntPtr nativePointer, Plugin plugin)
68+
public Task<bool> AreTrafficLightsLockedAsync()
5369
{
54-
_nativePointer = nativePointer;
55-
_plugin = plugin;
70+
return _plugin.Schedule(() => Rage.World.World_AreTrafficLightsLocked(_nativePointer));
71+
}
72+
73+
public Task SetTrafficLightsStateAsync(int state)
74+
{
75+
return _plugin.Schedule(() => Rage.World.World_SetTrafficLightsState(_nativePointer, state));
76+
}
77+
78+
public Task<int> GetTrafficLightsStateAsync()
79+
{
80+
return _plugin.Schedule(() => Rage.World.World_GetTrafficLightsState(_nativePointer));
5681
}
5782

5883
public async Task SetWeatherTransitionAsync(WeatherType weather, float time)

0 commit comments

Comments
 (0)