Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.2] Replace sync properties in IWorld with async setter and getter #81

Merged
merged 2 commits into from
Mar 16, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace sync properties in IWorld with async setter and getter
Micky5991 committed Mar 14, 2019
commit f2f9b0a01fdb59e65e1b1cd83416bdce417481bf
38 changes: 30 additions & 8 deletions src/AlternateLife.RageMP.Net/Interfaces/IWorld.cs
Original file line number Diff line number Diff line change
@@ -8,26 +8,48 @@ namespace AlternateLife.RageMP.Net.Interfaces
public interface IWorld
{
/// <summary>
/// Get or set the time of the world.
/// Set the time of the world.
/// </summary>
TimeData Time { get; set; }
Task SetTimeAsync(TimeData time);

/// <summary>
/// Get or set the weather of the world.
/// Get the time of the world.
/// </summary>
Task<TimeData> GetTimeAsync();

/// <summary>
/// Set the weather of the world.
///
/// If value is set to WeatherType.XMAS, snow will lay on the ground.
/// </summary>
Task SetWeatherAsync(WeatherType type);

/// <summary>
/// Get the weather of the world.
///
/// If value is set to WeatherType.XMAS, snow will lay on the ground.
/// </summary>
WeatherType Weather { get; set; }
Task<WeatherType> GetWeatherAsync();

/// <summary>
/// Set the traffic lights lock state of the world.
/// </summary>
Task SetTrafficLightsLockedAsync(bool locked);

/// <summary>
/// Get the traffic lights lock state of the world.
/// </summary>
Task<bool> AreTrafficLightsLockedAsync();

/// <summary>
/// Get or set the traffic lights lock state of the world.
/// Set the traffic light state of the world.
/// </summary>
bool AreTrafficLightsLocked { get; set; }
Task SetTrafficLightsStateAsync(int state);

/// <summary>
/// Get or set the traffic light state of the world.
/// Get the traffic light state of the world.
/// </summary>
int TrafficLightsState { get; set; }
Task<int> GetTrafficLightsStateAsync();

/// <summary>
/// Start a weather transition on the world.
75 changes: 50 additions & 25 deletions src/AlternateLife.RageMP.Net/Scripting/ScriptingClasses/World.cs
Original file line number Diff line number Diff line change
@@ -13,46 +13,71 @@ internal class World : IWorld
private readonly IntPtr _nativePointer;
private readonly Plugin _plugin;

public TimeData Time
public World(IntPtr nativePointer, Plugin plugin)
{
_nativePointer = nativePointer;
_plugin = plugin;
}

public Task SetTimeAsync(TimeData time)
{
get => StructConverter.PointerToStruct<TimeData>(Rage.World.World_GetTime(_nativePointer));
set => Rage.World.World_SetTime(_nativePointer, value);
return _plugin.Schedule(() => Rage.World.World_SetTime(_nativePointer, time));
}

public WeatherType Weather
public async Task<TimeData> GetTimeAsync()
{
get => ConvertWeatherNameToType(StringConverter.PointerToString(Rage.World.World_GetWeather(_nativePointer)));
set
var timePointer = await _plugin
.Schedule(() => Rage.World.World_GetTime(_nativePointer))
.ConfigureAwait(false);

return StructConverter.PointerToStruct<TimeData>(timePointer);
}

public async Task SetWeatherAsync(WeatherType type)
{
var weatherName = ConvertWeatherTypeToName(type);
if (string.IsNullOrEmpty(weatherName))
{
var weatherName = ConvertWeatherTypeToName(value);
if (string.IsNullOrEmpty(weatherName))
{
return;
}

using (var converter = new StringConverter())
{
Rage.World.World_SetWeather(_nativePointer, converter.StringToPointer(weatherName));
}
return;
}

using (var converter = new StringConverter())
{
var weatherPointer = converter.StringToPointer(weatherName);

await _plugin
.Schedule(() => Rage.World.World_SetWeather(_nativePointer, weatherPointer))
.ConfigureAwait(false);
}
}

public bool AreTrafficLightsLocked
public async Task<WeatherType> GetWeatherAsync()
{
get => Rage.World.World_AreTrafficLightsLocked(_nativePointer);
set => Rage.World.World_LockTrafficLights(_nativePointer, value);
var weatherPointer = await _plugin
.Schedule(() => Rage.World.World_GetWeather(_nativePointer))
.ConfigureAwait(false);

return ConvertWeatherNameToType(StringConverter.PointerToString(weatherPointer));
}

public int TrafficLightsState
public Task SetTrafficLightsLockedAsync(bool locked)
{
get => Rage.World.World_GetTrafficLightsState(_nativePointer);
set => Rage.World.World_SetTrafficLightsState(_nativePointer, value);
return _plugin.Schedule(() => Rage.World.World_LockTrafficLights(_nativePointer, locked));
}

public World(IntPtr nativePointer, Plugin plugin)
public Task<bool> AreTrafficLightsLockedAsync()
{
_nativePointer = nativePointer;
_plugin = plugin;
return _plugin.Schedule(() => Rage.World.World_AreTrafficLightsLocked(_nativePointer));
}

public Task SetTrafficLightsStateAsync(int state)
{
return _plugin.Schedule(() => Rage.World.World_SetTrafficLightsState(_nativePointer, state));
}

public Task<int> GetTrafficLightsStateAsync()
{
return _plugin.Schedule(() => Rage.World.World_GetTrafficLightsState(_nativePointer));
}

public async Task SetWeatherTransitionAsync(WeatherType weather, float time)