Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
carmineos committed May 16, 2020
1 parent 04a7fe7 commit 85423f3
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 84 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Workaround: If a vehicle is damaged, be sure to fix it before to enter it and cr
* `ToggleMenuControl`:The Control to toggle the Menu, default is 167 which is F6 (check the [controls list](https://docs.fivem.net/game-references/controls/))
* `FloatStep`: The step used to increase and decrease a value
* `EnableWheelMod`: Enables the script to edit wheel size and width of tuning wheels
* `EnablePresets`: Enables the script to manage presets
* `EnableClientPresets`: Enables the script to manage clients' presets
* `WheelLimits`:
* `FrontTrackWidth`: The max value you can increase or decrease the front Track Width from its default value
* `RearTrackWidth`: The max value you can increase or decrease the rear Track Width from its default value
Expand All @@ -72,10 +72,10 @@ bool SetFrontCamber(int vehicle, float value);
bool SetRearCamber(int vehicle, float value);
bool SetFrontTrackWidth(int vehicle, float value);
bool SetRearTrackWidth(int vehicle, float value);
bool SaveLocalPreset(string presetName, int vehicle);
bool LoadLocalPreset(string presetName, int vehicle);
bool DeleteLocalPreset(string presetName);
string[] GetLocalPresetList();
bool SaveClientPreset(string presetName, int vehicle);
bool LoadClientPreset(string presetName, int vehicle);
bool DeleteClientPreset(string presetName);
string[] GetClientPresetList();
```

**NOTE**
Expand Down Expand Up @@ -279,7 +279,7 @@ Current API don't support editing of tuning wheel data (wheelSize and wheelWidth
```

</details>
* **SaveLocalPreset**
* **SaveClientPreset**
* string presetName: the name you want to use for the saved preset
* int vehicle: the handle of the vehicle entity you want to save the preset from
* bool result: returns `true` if the action successfully executed otherwise `false`
Expand All @@ -289,15 +289,15 @@ Current API don't support editing of tuning wheel data (wheelSize and wheelWidth

C#:
```csharp
bool result = Exports["vstancer"].SaveLocalPreset(presetName, vehicle);
bool result = Exports["vstancer"].SaveClientPreset(presetName, vehicle);
```
Lua:
```lua
local result = exports["vstancer"]:SaveLocalPreset(presetName, vehicle);
local result = exports["vstancer"]:SaveClientPreset(presetName, vehicle);
```

</details>
* **LoadLocalPreset**
* **LoadClientPreset**
* string presetName: the name of the preset you want to load
* int vehicle: the handle of the vehicle entity you want to load the preset on
* bool result: returns `true` if the action successfully executed otherwise `false`
Expand All @@ -307,15 +307,15 @@ Current API don't support editing of tuning wheel data (wheelSize and wheelWidth

C#:
```csharp
bool result = Exports["vstancer"].LoadLocalPreset(presetName, vehicle);
bool result = Exports["vstancer"].LoadClientPreset(presetName, vehicle);
```
Lua:
```lua
local result = exports["vstancer"]:LoadLocalPreset(presetName, vehicle);
local result = exports["vstancer"]:LoadClientPreset(presetName, vehicle);
```

</details>
* **DeleteLocalPreset**
* **DeleteClientPreset**
* string presetName: the name of the preset you want to delete
* bool result: returns `true` if the action successfully executed otherwise `false`

Expand All @@ -324,27 +324,27 @@ Current API don't support editing of tuning wheel data (wheelSize and wheelWidth

C#:
```csharp
bool result = Exports["vstancer"].DeleteLocalPreset(presetName);
bool result = Exports["vstancer"].DeleteClientPreset(presetName);
```
Lua:
```lua
local result = exports["vstancer"]:DeleteLocalPreset(presetName);
local result = exports["vstancer"]:DeleteClientPreset(presetName);
```

</details>
* **GetLocalPresetList**
* **GetClientPresetList**
* string[] presetList: the list of all the presets saved locally

<details>
<summary>Example</summary>

C#:
```csharp
string[] presetList = Exports["vstancer"].GetLocalPresetList();
string[] presetList = Exports["vstancer"].GetClientPresetList();
```
Lua:
```lua
local presetList = exports["vstancer"]:GetLocalPresetList();
local presetList = exports["vstancer"]:GetClientPresetList();
```

</details>
Expand Down
5 changes: 5 additions & 0 deletions VStancer.Client/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ public static class Globals
/// The expected name of the resource
/// </summary>
public const string ResourceName = "vstancer";

/// <summary>
/// The prefix used for commands exposed by the script
/// </summary>
public const string CommandPrefix = "vstancer_";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@

namespace VStancer.Client.Scripts
{
internal class LocalPresetsScript
internal class ClientPresetsScript
{
private readonly MainScript _mainScript;

internal IPresetsCollection<string, VStancerPreset> Presets { get; private set; }
internal PresetsMenu Menu { get; private set; }
internal ClientPresetsMenu Menu { get; private set; }

public LocalPresetsScript(MainScript mainScript)
public ClientPresetsScript(MainScript mainScript)
{
_mainScript = mainScript;
Presets = new KvpPresetsCollection(Globals.KvpPrefix);

if (!_mainScript.Config.DisableMenu)
{
Menu = new PresetsMenu(this);
Menu = new ClientPresetsMenu(this);

Menu.DeletePresetEvent += (sender, presetID) => OnDeletePresetInvoked(presetID);
Menu.SavePresetEvent += (sender, presetID) => OnSavePresetInvoked(presetID);
Expand All @@ -40,7 +40,7 @@ internal async Task<string> GetPresetNameFromUser(string title, string defaultTe
private void OnDeletePresetInvoked(string presetKey)
{
if (Presets.Delete(presetKey))
Screen.ShowNotification($"Personal preset ~r~{presetKey}~w~ deleted");
Screen.ShowNotification($"Client preset ~r~{presetKey}~w~ deleted");
else
Screen.ShowNotification($"~r~ERROR~w~ No preset found with {presetKey} key.");
}
Expand All @@ -54,7 +54,7 @@ private void OnSavePresetInvoked(string presetKey)
};

if (Presets.Save(presetKey, preset))
Screen.ShowNotification($"Personal preset ~g~{presetKey}~w~ saved");
Screen.ShowNotification($"Client preset ~g~{presetKey}~w~ saved");
else
Screen.ShowNotification($"~r~ERROR~w~ The name {presetKey} is invalid or already used.");
}
Expand All @@ -66,14 +66,14 @@ private async void OnApplyPresetInvoked(string presetKey)
if (loadedPreset == null)
{
await Task.FromResult(0);
Screen.ShowNotification($"~r~ERROR~w~ Personal preset ~b~{presetKey}~w~ corrupted");
Screen.ShowNotification($"~r~ERROR~w~ Client preset ~b~{presetKey}~w~ corrupted");
return;
}

await _mainScript.WheelScript.SetWheelPreset(loadedPreset.WheelPreset);
await _mainScript.WheelModScript.SetWheelModPreset(loadedPreset.WheelModPreset);

Screen.ShowNotification($"Personal preset ~b~{presetKey}~w~ applied");
Screen.ShowNotification($"Client preset ~b~{presetKey}~w~ applied");
}

internal bool API_DeletePreset(string presetKey)
Expand Down Expand Up @@ -119,7 +119,7 @@ internal bool API_LoadPreset(string presetKey, int vehicle)
return false;
}

internal IEnumerable<string> API_GetLocalPresetList()
internal IEnumerable<string> API_GetClientPresetList()
{
return Presets.GetKeys();
}
Expand Down
72 changes: 36 additions & 36 deletions VStancer.Client/Scripts/MainScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ private set
internal VStancerConfig Config { get; private set; }
internal WheelScript WheelScript { get; private set; }
internal WheelModScript WheelModScript { get; private set; }
internal LocalPresetsScript LocalPresetsScript { get; private set; }
internal ClientPresetsScript ClientPresetsScript { get; private set; }

public MainScript()
{
if (GetCurrentResourceName() != Globals.ResourceName)
{
Debug.WriteLine($"{Globals.ScriptName}: Invalid resource name, be sure the resource name is {Globals.ResourceName}");
Debug.WriteLine($"{nameof(MainScript)}: Invalid resource name, be sure the resource name is {Globals.ResourceName}");
return;
}

Expand All @@ -85,9 +85,9 @@ public MainScript()
RegisterScript(WheelModScript);
}

if (Config.EnablePresets)
if (Config.EnableClientPresets)
{
LocalPresetsScript = new LocalPresetsScript(this);
ClientPresetsScript = new ClientPresetsScript(this);
}

if (!Config.DisableMenu)
Expand All @@ -113,10 +113,10 @@ public MainScript()
Exports.Add("GetFrontTrackWidth", new Func<int, float[]>(GetFrontTrackWidth));
Exports.Add("GetRearTrackWidth", new Func<int, float[]>(GetRearTrackWidth));

Exports.Add("SaveLocalPreset", new Func<string, int, bool>(SaveLocalPreset));
Exports.Add("LoadLocalPreset", new Func<string, int, bool>(LoadLocalPreset));
Exports.Add("DeleteLocalPreset", new Func<string, bool>(DeleteLocalPreset));
Exports.Add("GetLocalPresetList", new Func<string[]>(GetLocalPresetList));
Exports.Add("SaveClientPreset", new Func<string, int, bool>(SaveClientPreset));
Exports.Add("LoadClientPreset", new Func<string, int, bool>(LoadClientPreset));
Exports.Add("DeleteClientPreset", new Func<string, bool>(DeleteClientPreset));
Exports.Add("GetClientPresetList", new Func<string[]>(GetClientPresetList));
}

private async Task HideUITask()
Expand Down Expand Up @@ -194,11 +194,11 @@ private VStancerConfig LoadConfig(string filename = "config.json")
string strings = LoadResourceFile(Globals.ResourceName, filename);
config = JsonConvert.DeserializeObject<VStancerConfig>(strings);

Debug.WriteLine($"{Globals.ScriptName}: Loaded config from {filename}");
Debug.WriteLine($"{nameof(MainScript)}: Loaded config from {filename}");
}
catch (Exception e)
{
Debug.WriteLine($"{Globals.ScriptName}: Impossible to load {filename}", e.Message);
Debug.WriteLine($"{nameof(MainScript)}: Impossible to load {filename}", e.Message);
Debug.WriteLine(e.StackTrace);

config = new VStancerConfig();
Expand All @@ -217,7 +217,7 @@ public async Task<string> GetOnScreenString(string title, string defaultText)

private void RegisterCommands()
{
RegisterCommand("vstancer_decorators", new Action<int, dynamic>((source, args) =>
RegisterCommand($"{Globals.CommandPrefix}decorators", new Action<int, dynamic>((source, args) =>
{
if (args.Count < 1)
{
Expand All @@ -231,59 +231,59 @@ private void RegisterCommands()
WheelScript.PrintDecoratorsInfo(value);
WheelModScript.PrintDecoratorsInfo(value);
}
else Debug.WriteLine($"{Globals.ScriptName}: Error parsing entity handle {args[0]} as int");
else Debug.WriteLine($"{nameof(MainScript)}: Error parsing entity handle {args[0]} as int");
}
}), false);

RegisterCommand("vstancer_range", new Action<int, dynamic>((source, args) =>
RegisterCommand($"{Globals.CommandPrefix}range", new Action<int, dynamic>((source, args) =>
{
if (args.Count < 1)
{
Debug.WriteLine($"{Globals.ScriptName}: Missing float argument");
Debug.WriteLine($"{nameof(MainScript)}: Missing float argument");
return;
}

if (float.TryParse(args[0], out float value))
{
Config.ScriptRange = value;
_maxDistanceSquared = (float)Math.Sqrt(value);
Debug.WriteLine($"{Globals.ScriptName}: {nameof(Config.ScriptRange)} updated to {value}");
Debug.WriteLine($"{nameof(MainScript)}: {nameof(Config.ScriptRange)} updated to {value}");
}
else Debug.WriteLine($"{Globals.ScriptName}: Error parsing {args[0]} as float");
else Debug.WriteLine($"{nameof(MainScript)}: Error parsing {args[0]} as float");

}), false);

RegisterCommand("vstancer_debug", new Action<int, dynamic>((source, args) =>
RegisterCommand($"{Globals.CommandPrefix}debug", new Action<int, dynamic>((source, args) =>
{
if (args.Count < 1)
{
Debug.WriteLine($"{Globals.ScriptName}: Missing bool argument");
Debug.WriteLine($"{nameof(MainScript)}: Missing bool argument");
return;
}

if (bool.TryParse(args[0], out bool value))
{
Config.Debug = value;
Debug.WriteLine($"{Globals.ScriptName}: {nameof(Config.Debug)} updated to {value}");
Debug.WriteLine($"{nameof(MainScript)}: {nameof(Config.Debug)} updated to {value}");
}
else Debug.WriteLine($"{Globals.ScriptName}: Error parsing {args[0]} as bool");
else Debug.WriteLine($"{nameof(MainScript)}: Error parsing {args[0]} as bool");

}), false);

RegisterCommand("vstancer_preset", new Action<int, dynamic>((source, args) =>
RegisterCommand($"{Globals.CommandPrefix}preset", new Action<int, dynamic>((source, args) =>
{
if (WheelScript?.WheelData != null)
Debug.WriteLine(WheelScript.WheelData.ToString());
else
Debug.WriteLine($"{Globals.ScriptName}: {nameof(WheelScript.WheelData)} is null");
Debug.WriteLine($"{nameof(MainScript)}: {nameof(WheelScript.WheelData)} is null");

if (WheelModScript?.WheelModData != null)
Debug.WriteLine(WheelModScript.WheelModData.ToString());
else
Debug.WriteLine($"{Globals.ScriptName}: {nameof(WheelModScript.WheelModData)} is null");
Debug.WriteLine($"{nameof(MainScript)}: {nameof(WheelModScript.WheelModData)} is null");
}), false);

RegisterCommand("vstancer_print", new Action<int, dynamic>((source, args) =>
RegisterCommand($"{Globals.CommandPrefix}print", new Action<int, dynamic>((source, args) =>
{
if (WheelScript != null)
WheelScript.PrintVehiclesWithDecorators(_worldVehiclesHandles);
Expand Down Expand Up @@ -405,36 +405,36 @@ public float[] GetRearTrackWidth(int vehicle)
return new float[] { };
}

public bool SaveLocalPreset(string id, int vehicle)
public bool SaveClientPreset(string id, int vehicle)
{
if (LocalPresetsScript == null)
if (ClientPresetsScript == null)
return false;

return LocalPresetsScript.API_SavePreset(id, vehicle);
return ClientPresetsScript.API_SavePreset(id, vehicle);
}

public bool LoadLocalPreset(string id, int vehicle)
public bool LoadClientPreset(string id, int vehicle)
{
if (LocalPresetsScript == null)
if (ClientPresetsScript == null)
return false;

return LocalPresetsScript.API_LoadPreset(id, vehicle);
return ClientPresetsScript.API_LoadPreset(id, vehicle);
}

public bool DeleteLocalPreset(string id)
public bool DeleteClientPreset(string id)
{
if (LocalPresetsScript == null)
if (ClientPresetsScript == null)
return false;

return LocalPresetsScript.API_DeletePreset(id);
return ClientPresetsScript.API_DeletePreset(id);
}

public string[] GetLocalPresetList()
public string[] GetClientPresetList()
{
if (LocalPresetsScript == null)
if (ClientPresetsScript == null)
return new string[] { };

return LocalPresetsScript.API_GetLocalPresetList().ToArray();
return ClientPresetsScript.API_GetClientPresetList().ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace VStancer.Client.UI
{
internal class PresetsMenu : Menu
internal class ClientPresetsMenu : Menu
{
private readonly LocalPresetsScript _script;
private readonly ClientPresetsScript _script;

internal PresetsMenu(LocalPresetsScript script, string name = Globals.ScriptName, string subtitle = "Personal Presets Menu") : base(name, subtitle)
internal ClientPresetsMenu(ClientPresetsScript script, string name = Globals.ScriptName, string subtitle = "Client Presets Menu") : base(name, subtitle)
{
_script = script;

Expand Down
Loading

0 comments on commit 85423f3

Please sign in to comment.