Skip to content

Commit

Permalink
Added init file parser properly
Browse files Browse the repository at this point in the history
We will have less crashes
  • Loading branch information
Traderain committed Nov 13, 2016
1 parent fcc8f99 commit 5f74009
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 117 deletions.
Binary file modified .vs/VolvoWrench/v14/.suo
Binary file not shown.
Binary file modified VolvoWrench.VC.db
Binary file not shown.
93 changes: 25 additions & 68 deletions VolvoWrench/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
using VolvoWrench.Overlay;
using VolvoWrench.SaveStuff;
using static System.Convert;
using IniParser;
using IniParser.Model;

namespace VolvoWrench
{
public sealed partial class Main : Form
{
public static readonly string LogPath = string.Format("{0}\\" + "VolvoWrench" + "\\" + "VWLog.log", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
public static readonly string SettingsPath = string.Format("{0}\\" + "VolvoWrench" + "\\" + "VWSettings.config", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
public static readonly string SettingsPath = string.Format("{0}\\" + "VolvoWrench" + "\\" + "VWSettings.ini", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

public static int DemoPopupKey;
public static int OverLayExitKey;
Expand Down Expand Up @@ -282,7 +284,7 @@ private void launchToolStripMenuItem_Click(object sender, EventArgs e)

private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var ols = new OverlaySettings(OverlayFont,OverLayColor))
using (var ols = new OverlaySettings())
{
ols.ShowDialog(this);
}
Expand Down Expand Up @@ -359,35 +361,19 @@ private void fontToolStripMenuItem1_Click(object sender, EventArgs e)
if (fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fd.Font;
#region Save font
var parser = new FileIniDataParser();
var cvt = new FontConverter();
File.WriteAllLines(SettingsPath,
new[]
{
$@">[VolvoWrench config file]
> Here are your hotkeys for the program.
> Every line which starts with a semicolon('>') is ignored.
> Please keep that in mind.
[HOTKEYS]
>You can modify these keys. Google VKEY
demo_popup={DemoPopupKey.ToString("X")}
overlay_exit={OverLayExitKey.ToString("X")}
overlay_rescan={OverLayRescanKey.ToString("X")}
[SETTINGS]
Language=EN
main_font={cvt.ConvertToString(fd.Font)}
overlay_font={cvt.ConvertToString(OverlayFont)}
overlay_color={OverLayColor.A}:{OverLayColor.R}:{OverLayColor.B}:{OverLayColor.G}"
});
#endregion
IniData iniD = parser.ReadFile(SettingsPath);
iniD["SETTINGS"]["main_font"] = cvt.ConvertToString(fd.Font);
parser.WriteFile(SettingsPath, iniD);
}
}
if (CurrentFile == null || (!File.Exists(CurrentFile) || Path.GetExtension(CurrentFile) != ".dem"))
return;
richTextBox1.Text = @"Analyzing file...";
CurrentDemoFile = CrossDemoParser.Parse(CurrentFile);
PrintDemoDetails(CurrentDemoFile);
Log(Path.GetFileName(CurrentFile + " rescanned for font change.")); //Terribble hack for recolor.
Log(Path.GetFileName(CurrentFile + " rescanned for font change."));
Log("Font changed");
}
catch (Exception ex)
Expand All @@ -411,12 +397,12 @@ public static void SettingsManager(bool reset)
File.WriteAllLines(SettingsPath,
new[]
{
$@">[VolvoWrench config file]
> Here are your hotkeys for the program.
> Every line which starts with a semicolon('>') is ignored.
> Please keep that in mind.
$@"[VolvoWrench config file]
; Here are your hotkeys for the program.
; Every line which starts with a semicolon(';') is ignored.
; Please keep that in mind.
[HOTKEYS]
>You can modify these keys. Google VKEY
;You can modify these keys. Google VKEY
demo_popup=0x70
overlay_exit=0x71
overlay_rescan=0x72
Expand All @@ -434,50 +420,21 @@ public static void SettingsManager(bool reset)
}
else
{
#region Parse file
var filteredArray = File.ReadAllLines(SettingsPath).Where(x => !x.StartsWith(">")).ToArray();
DemoPopupKey = ToInt32(filteredArray
.First(x => x
.Contains("demo_popup"))
.Replace(" ", string.Empty)
.Replace(">", string.Empty)
.Trim()
.Split('=')[1], 16);
OverLayExitKey = ToInt32(filteredArray
.First(x => x
.Contains("overlay_exit"))
.Replace(" ", string.Empty)
.Replace(">", string.Empty)
.Trim()
.Split('=')[1], 16);
OverLayRescanKey = ToInt32(filteredArray
.First(x => x
.Contains("overlay_rescan"))
.Replace(" ", string.Empty)
.Replace(">", string.Empty)
.Trim()
.Split('=')[1], 16);
var cvt = new FontConverter();
OverlayFont = cvt.ConvertFromString(filteredArray
.First(x => x
.Contains("overlay_font"))
.Split('=').Skip(1).ToArray().Aggregate((c,n) => c += n)) as Font;
MainFont = cvt.ConvertFromString(filteredArray
.First(x => x
.Contains("main_font"))
.Split('=').Skip(1).ToArray().Aggregate((c, n) => c += n)) as Font;
var colorstring = filteredArray
.First(x => x
.Contains("overlay_color"))
.Replace(" ", string.Empty)
.Replace(">", string.Empty)
.Split('=')[1].Split(':');
var parser = new FileIniDataParser();
IniData data = parser.ReadFile(SettingsPath);
DemoPopupKey = ToInt32(data["HOTKEYS"]["demo_popup"],16);
OverLayExitKey = ToInt32(data["HOTKEYS"]["overlay_exit"],16);
OverLayRescanKey = ToInt32(data["HOTKEYS"]["overlay_rescan"],16);
OverLayExitKey = ToInt32(data["HOTKEYS"]["overlay_rescan"],16);
OverlayFont = cvt.ConvertFromString(data["SETTINGS"]["overlay_font"]) as Font;
MainFont = cvt.ConvertFromString(data["SETTINGS"]["main_font"]) as Font;
var colorstring = data["SETTINGS"]["overlay_color"].Split(':');
OverLayColor = Color.FromArgb(
ToInt32(colorstring[0]),
ToInt32(colorstring[1]),
ToInt32(colorstring[2]),
ToInt32(colorstring[3]));
#endregion
}
}
catch (Exception e)
Expand Down Expand Up @@ -626,8 +583,8 @@ public void PrintDemoDetails(CrossParseResult demo)
Directory offset: {demo.HlsooeDemoInfo.Header.DirectoryOffset}
Map name: {demo.HlsooeDemoInfo.Header.MapName}
Game directory: {demo.HlsooeDemoInfo.Header.GameDirectory}
Length in seconds: {(demo.HlsooeDemoInfo.DirectoryEntries.Last().Frames.LastOrDefault().Key.Frame) *0.015}s
Tick count: {(demo.HlsooeDemoInfo.DirectoryEntries.Last().Frames.LastOrDefault().Key.Frame)}
Length in seconds: {(demo.HlsooeDemoInfo.DirectoryEntries.Last().Frames.LastOrDefault().Key.Tick) *0.015}s
Tick count: {(demo.HlsooeDemoInfo.DirectoryEntries.SkipWhile(x => x.FrameCount < 1).Max(x=>x.Frames.Max(y => y.Key.Tick)))}
----------------------------------------------------------";
UpdateForm();
foreach (
Expand Down
46 changes: 22 additions & 24 deletions VolvoWrench/Overlay/OverlaySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,31 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;

namespace VolvoWrench.Overlay
{
public partial class OverlaySettings : Form
{
public OverlaySettings(Font fo,Color co)
public OverlaySettings()
{
InitializeComponent();
F = fo;
C = co;
var parser = new FileIniDataParser();
var cvt = new FontConverter();
IniData iniD = parser.ReadFile(Main.SettingsPath);
F = cvt.ConvertFromString(iniD["SETTINGS"]["overlay_font"]) as Font;
var colorstring = iniD["SETTINGS"]["overlay_color"].Split(':');
C = Color.FromArgb(
Convert.ToInt32(colorstring[0]),
Convert.ToInt32(colorstring[1]),
Convert.ToInt32(colorstring[2]),
Convert.ToInt32(colorstring[3]));
var tf = new Font(FontFamily.GenericMonospace, 14, FontStyle.Regular, GraphicsUnit.Point);
if (F == null)
F = tf;
label1.Font = F;
button2.BackColor = co;
button2.BackColor = C;
}

public Font F;
Expand Down Expand Up @@ -52,27 +65,12 @@ private void button1_Click(object sender, EventArgs e)

private void button3_Click(object sender, EventArgs e) //OK
{
#region Save font
var parser = new FileIniDataParser();
var cvt = new FontConverter();
File.WriteAllLines(Main.SettingsPath,
new[]
{
$@">[VolvoWrench config file]
> Here are your hotkeys for the program.
> Every line which starts with a semicolon('>') is ignored.
> Please keep that in mind.
[HOTKEYS]
>You can modify these keys. Google VKEY
demo_popup={Main.DemoPopupKey.ToString("X")}
overlay_exit={Main.OverLayExitKey.ToString("X")}
overlay_rescan={Main.OverLayRescanKey.ToString("X")}
[SETTINGS]
Language = EN
main_font = {cvt.ConvertToString(Main.MainFont)}
overlay_font = {cvt.ConvertToString(F)}
overlay_color = {C.A}:{C.R}:{C.B}:{C.G}"
});
#endregion
IniData iniD = parser.ReadFile(Main.SettingsPath);
iniD["SETTINGS"]["overlay_font"] = cvt.ConvertToString(F);
iniD["SETTINGS"]["overlay_color"] = C.A + ":" + C.R + ":" + C.B + ":" + C.G;
parser.WriteFile(Main.SettingsPath, iniD);
var parentT = (Main)Owner;
parentT.UpdateOverLaySettings(F,C);
Close();
Expand Down
30 changes: 11 additions & 19 deletions VolvoWrench/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions VolvoWrench/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version='1.0' encoding='utf-8'?>

<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="VolvoWrench.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="Key" Type="System.String" Scope="Application">
<Value Profile="(Default)">0x71</Value>
</Setting>
</Settings>
</SettingsFile>
5 changes: 5 additions & 0 deletions VolvoWrench/VolvoWrench.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="INIFileParser, Version=2.3.0.0, Culture=neutral, PublicKeyToken=79af7b307b65cf3c, processorArchitecture=MSIL">
<HintPath>..\packages\ini-parser.2.3.0\lib\net20\INIFileParser.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.DebuggerVisualizers, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="PresentationCore" />
<Reference Include="SharpDX, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
Expand Down Expand Up @@ -298,6 +302,7 @@
<EmbeddedResource Include="Demo stuff\Stats.resx">
<DependentUpon>Stats.cs</DependentUpon>
</EmbeddedResource>
<None Include="app.config" />
<None Include="packages.config" />
<None Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
Expand Down
1 change: 1 addition & 0 deletions VolvoWrench/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ini-parser" version="2.3.0" targetFramework="net461" />
<package id="SharpDX" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.Direct2D1" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.DXGI" version="3.1.1" targetFramework="net461" />
Expand Down

0 comments on commit 5f74009

Please sign in to comment.