Skip to content

Commit

Permalink
Replaced config.ini with xml version, updated resource.lua to fxmanif…
Browse files Browse the repository at this point in the history
…est.lua
  • Loading branch information
carmineos committed Feb 22, 2020
1 parent 14c13f1 commit 945af00
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 143 deletions.
78 changes: 0 additions & 78 deletions VStancer.Client/Config.cs

This file was deleted.

20 changes: 19 additions & 1 deletion VStancer.Client/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using static CitizenFX.Core.Native.API;

Expand All @@ -25,4 +26,21 @@ IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
}
}

public static class Helpers
{
public static string RemoveByteOrderMarks(string xml)
{
/*
string bom = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(bom))
xml = xml.Remove(0, bom.Length);
*/

// Workaround
if (!xml.StartsWith("<", StringComparison.Ordinal))
xml = xml.Substring(xml.IndexOf("<"));
return xml;
}
}
}
2 changes: 1 addition & 1 deletion VStancer.Client/VStancer.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="&#xD;&#xA; if '$(CI)' == '' ( &#xD;&#xA; call &quot;$(SolutionDir)postbuild.bat&quot; &quot;$(TargetPath)&quot;&#xD;&#xA; call &quot;$(SolutionDir)postbuild.bat&quot; &quot;$(SolutionDir)\dist\config.ini&quot;&#xD;&#xA; call &quot;$(SolutionDir)postbuild.bat&quot; &quot;$(SolutionDir)\dist\__resource.lua&quot;&#xD;&#xA; call &quot;$(SolutionDir)postbuild.bat&quot; &quot;$(TargetDir)\MenuAPI.dll&quot;)&#xD;&#xA; " />
<Exec Command="&#xD;&#xA; if '$(CI)' == '' ( &#xD;&#xA; call &quot;$(SolutionDir)postbuild.bat&quot; &quot;$(TargetPath)&quot;&#xD;&#xA; call &quot;$(SolutionDir)postbuild.bat&quot; &quot;$(SolutionDir)\dist\config.xml&quot;&#xD;&#xA; call &quot;$(SolutionDir)postbuild.bat&quot; &quot;$(SolutionDir)\dist\fxmanifest.lua&quot;&#xD;&#xA; call &quot;$(SolutionDir)postbuild.bat&quot; &quot;$(TargetDir)\MenuAPI.dll&quot;)&#xD;&#xA; " />
</Target>

</Project>
118 changes: 118 additions & 0 deletions VStancer.Client/VStancerConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Vstancer.Client
{
public class VStancerConfig
{
public bool Debug { get; set; }
public bool ExposeCommand { get; set; }
public bool ExposeEvent { get; set; }
public float ScriptRange { get; set; }
public long Timer { get; set; }
public int ToggleMenuControl { get; set; }
public float FloatStep { get; set; }
public WheelLimits FrontLimits { get; set; }
public WheelLimits RearLimits { get; set; }

public VStancerConfig()
{
Debug = false;
ExposeCommand = false;
ExposeEvent = false;
ScriptRange = 150.0f;
Timer = 1000;
ToggleMenuControl = 167;
FloatStep = 0.01f;
FrontLimits = new WheelLimits { PositionX = 0.25f, RotationY = 0.20f };
RearLimits = new WheelLimits { PositionX = 0.25f, RotationY = 0.20f };
}

public void LoadXml(string xml)
{
string txt = Helpers.RemoveByteOrderMarks(xml);

XmlDocument doc = new XmlDocument();
doc.LoadXml(txt);

var rootNode = doc[nameof(VStancerConfig)];

foreach (XmlNode node in rootNode?.ChildNodes)
{
if (node.NodeType != XmlNodeType.Element)
continue;

if (node.Name == nameof(Debug) && bool.TryParse(node.InnerText, out bool debugValue))
Debug = debugValue;

else if (node.Name == nameof(ExposeCommand) && bool.TryParse(node.InnerText, out bool exposeCommandValue))
ExposeCommand = exposeCommandValue;

else if (node.Name == nameof(ExposeEvent) && bool.TryParse(node.InnerText, out bool exposeEventValue))
ExposeEvent = exposeEventValue;

else if (node.Name == nameof(ScriptRange) && float.TryParse(node.InnerText, out float scriptRangeValue))
ScriptRange = scriptRangeValue;

else if (node.Name == nameof(Timer) && long.TryParse(node.InnerText, out long timerValue))
Timer = timerValue;

else if (node.Name == nameof(ToggleMenuControl) && int.TryParse(node.InnerText, out int toggleMenuControlValue))
ToggleMenuControl = toggleMenuControlValue;

else if (node.Name == nameof(FloatStep) && float.TryParse(node.InnerText, out float floatStepValue))
FloatStep = floatStepValue;

else if (node.Name == nameof(FrontLimits))
{
var frontLimits = new WheelLimits();

foreach (XmlNode child in node.ChildNodes)
{
if (node.NodeType != XmlNodeType.Element)
continue;

if (child.Name == nameof(WheelLimits.PositionX) && float.TryParse(child.InnerText, out float positionXValue))
frontLimits.PositionX = positionXValue;

if (child.Name == nameof(WheelLimits.RotationY) && float.TryParse(child.InnerText, out float rotationYValue))
frontLimits.RotationY = rotationYValue;

FrontLimits = frontLimits;
}
}

else if (node.Name == nameof(RearLimits))
{
var rearLimits = new WheelLimits();

foreach (XmlNode child in node.ChildNodes)
{
if (node.NodeType == XmlNodeType.Comment)
continue;

if (child.Name == nameof(WheelLimits.PositionX) && float.TryParse(child.InnerText, out float positionXValue))
rearLimits.PositionX = positionXValue;

if (child.Name == nameof(WheelLimits.RotationY) && float.TryParse(child.InnerText, out float rotationYValue))
rearLimits.RotationY = rotationYValue;

RearLimits = rearLimits;
}
}

else continue;
}
}
}

public struct WheelLimits
{
public float PositionX { get; set; }
public float RotationY { get; set; }
}
}
48 changes: 24 additions & 24 deletions VStancer.Client/VStancerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class VStancerEditor : BaseScript
#region Config Fields

public int toggleMenu = 167;
public float ScriptRange = 150.0f;
public float scriptRange = 150.0f;
public float FloatStep = 0.01f;
public float frontMaxOffset = 0.25f;
public float frontMaxCamber = 0.20f;
Expand Down Expand Up @@ -208,8 +208,8 @@ public VStancerEditor()

if (float.TryParse(args[0], out float value))
{
ScriptRange = value;
Debug.WriteLine($"{ScriptName}: Received new {nameof(ScriptRange)} value {value}");
scriptRange = value;
Debug.WriteLine($"{ScriptName}: Received new {nameof(scriptRange)} value {value}");
}
else Debug.WriteLine($"{ScriptName}: Error parsing {args[0]} as float");

Expand Down Expand Up @@ -382,7 +382,7 @@ private async Task UpdateWorldVehicles()
{
Vector3 coords = GetEntityCoords(entity, true);

if (Vector3.Distance(currentCoords, coords) <= ScriptRange)
if (Vector3.Distance(currentCoords, coords) <= scriptRange)
RefreshVehicleUsingDecorators(entity);
}
}
Expand Down Expand Up @@ -794,37 +794,37 @@ private bool HasDecorators(int entity)
/// Loads the config file containing all the customizable properties
/// </summary>
/// <param name="filename">The name of the file</param>
private void LoadConfig(string filename = "config.ini")
private void LoadConfig(string filename = "config.xml")
{
string strings = null;
VStancerConfig config = new VStancerConfig();
try
{
strings = LoadResourceFile(ResourceName, filename);
string strings = LoadResourceFile(ResourceName, filename);

Debug.WriteLine($"{ScriptName}: Loaded settings from {filename}");
config.LoadXml(strings);

Debug.WriteLine($"{ScriptName}: Loaded config from {filename}");
}
catch (Exception e)
{
Debug.WriteLine($"{ScriptName}: Impossible to load {filename}");
Debug.WriteLine($"{ScriptName}: Impossible to load {filename}", e.Message);
Debug.WriteLine(e.StackTrace);
}
finally
{
Config config = new Config(strings);

toggleMenu = config.GetIntValue("toggleMenu", toggleMenu);
FloatStep = config.GetFloatValue("FloatStep", FloatStep);
ScriptRange = config.GetFloatValue("ScriptRange", ScriptRange);
frontMaxOffset = config.GetFloatValue("frontMaxOffset", frontMaxOffset);
frontMaxCamber = config.GetFloatValue("frontMaxCamber", frontMaxCamber);
rearMaxOffset = config.GetFloatValue("rearMaxOffset", rearMaxOffset);
rearMaxCamber = config.GetFloatValue("rearMaxCamber", rearMaxCamber);
timer = config.GetLongValue("timer", timer);
debug = config.GetBoolValue("debug", debug);
exposeCommand = config.GetBoolValue("exposeCommand", exposeCommand);
exposeEvent = config.GetBoolValue("exposeEvent", exposeEvent);

Debug.WriteLine($"{ScriptName}: Settings {nameof(frontMaxOffset)}={frontMaxOffset} {nameof(frontMaxCamber)}={frontMaxCamber} {nameof(rearMaxOffset)}={rearMaxOffset} {nameof(rearMaxCamber)}={rearMaxCamber} {nameof(timer)}={timer} {nameof(debug)}={debug} {nameof(ScriptRange)}={ScriptRange}");
toggleMenu = config.ToggleMenuControl;
FloatStep = config.FloatStep;
scriptRange = config.ScriptRange;
frontMaxOffset = config.FrontLimits.PositionX;
frontMaxCamber = config.FrontLimits.RotationY;
rearMaxOffset = config.RearLimits.PositionX;
rearMaxCamber = config.RearLimits.RotationY;
timer = config.Timer;
debug = config.Debug;
exposeCommand = config.ExposeCommand;
exposeEvent = config.ExposeEvent;

Debug.WriteLine($"{ScriptName}: Settings {nameof(frontMaxOffset)}={frontMaxOffset} {nameof(frontMaxCamber)}={frontMaxCamber} {nameof(rearMaxOffset)}={rearMaxOffset} {nameof(rearMaxCamber)}={rearMaxCamber} {nameof(timer)}={timer} {nameof(debug)}={debug} {nameof(scriptRange)}={scriptRange}");
}
}

Expand Down
32 changes: 0 additions & 32 deletions dist/config.ini

This file was deleted.

18 changes: 18 additions & 0 deletions dist/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<VStancerConfig>
<Debug>false</Debug>
<ExposeCommand>false</ExposeCommand>
<ExposeEvent>false</ExposeEvent>
<ScriptRange>150</ScriptRange>
<Timer>1000</Timer>
<ToggleMenuControl>167</ToggleMenuControl>
<FloatStep>0.01</FloatStep>
<FrontLimits>
<PositionX>0.25</PositionX>
<RotationY>0.2</RotationY>
</FrontLimits>
<RearLimits>
<PositionX>0.25</PositionX>
<RotationY>0.2</RotationY>
</RearLimits>
</VStancerConfig>
6 changes: 3 additions & 3 deletions dist/__resource.lua → dist/fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

fx_version 'adamant'
games { 'gta5' }
--dependency 'MenuAPI'

files {
--'@MenuAPI/MenuAPI.dll',
'MenuAPI.dll',
'config.ini'
'config.xml'
}

client_scripts {
Expand Down
Loading

0 comments on commit 945af00

Please sign in to comment.