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

Xml error fixes, update libs #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
79 changes: 48 additions & 31 deletions KillStreaks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Rocket.Unturned.Events;
using Rocket.Unturned.Player;
using Steamworks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -26,12 +27,6 @@ protected override void Load()
{
instance = this;

if (!File.Exists(KSFILEPATH))
{
Logger.Log("Datafile not found, creating one now...");
File.Create(KSFILEPATH).Dispose();
}

Logger.Log("---------------------------");
Logger.Log("Extra's KillStreaks Loaded!");
Logger.Log("---------------------------");
Expand All @@ -41,7 +36,15 @@ protected override void Load()
Logger.Log(" Restart persistence enabled, loading data...");
try
{
killCount = DeserializePlayerDict();
if (!File.Exists(KSFILEPATH))
{
Logger.Log("Datafile not found, creating one now...");
File.Create(KSFILEPATH).Dispose();
}
else
{
killCount = DeserializePlayerDict();
}
}
catch (XmlException)
{
Expand Down Expand Up @@ -87,45 +90,30 @@ public static void SerializePlayerDict(Dictionary<string, int> dict)
{
players.Add(new KSPlayer(kv.Key, kv.Value));
}
XmlSerializer serializer = new XmlSerializer(typeof(List<KSPlayer>));
var ksplayers = new KSPlayers(players);
XmlSerializer serializer = new XmlSerializer(typeof(KSPlayers));
using (TextWriter stream = new StreamWriter(KSFILEPATH, false))
{
serializer.Serialize(stream, players);
serializer.Serialize(stream, ksplayers);
}
}

public static Dictionary<string, int> DeserializePlayerDict()
{
Dictionary<string, int> dict = new Dictionary<string, int>();
XmlSerializer serializer = new XmlSerializer(typeof(List<KSPlayer>));
List<KSPlayer> players;
using (TextReader stream = new StreamReader(KSFILEPATH))
KSPlayers ksPlayers;
var dict = new Dictionary<string, int>();
XmlSerializer serializer = new XmlSerializer(typeof(KSPlayers));
using (var stream = new StreamReader(KSFILEPATH))
{
players = (List<KSPlayer>)serializer.Deserialize(stream);
ksPlayers = (KSPlayers)serializer.Deserialize(stream);
}
foreach (KSPlayer player in players)
foreach (var player in ksPlayers.ksplayers)
{
dict[player.Id] = player.Kills;
}
return dict;
}

public class KSPlayer
{
public string Id;
public int Kills;

public KSPlayer()
{
}

public KSPlayer(string id, int kills)
{
Id = id;
Kills = kills;
}
}

private void OnConnected(UnturnedPlayer player)
{
if (!killCount.TryGetValue(player.Id, out int count))
Expand Down Expand Up @@ -200,4 +188,33 @@ private void OnDeath(UnturnedPlayer player, SDG.Unturned.EDeathCause cause, SDG.
{"killstreak_remove", "[KillStreaks] Your killstreak has been reset."},
};
}
[Serializable]
public class KSPlayer
{
[XmlAttribute("ID")]
public string Id;
[XmlAttribute("Kills")]
public int Kills;
public KSPlayer() { }
public KSPlayer(string id, int kills)
{
Id = id;
Kills = kills;
}
}

[Serializable]
[XmlRoot("KillStreakData")]
public class KSPlayers
{
[XmlArray("KillStreakPlayers"), XmlArrayItem("KillStreakPlayer")]
public List<KSPlayer> ksplayers;

public KSPlayers() { }
public KSPlayers(List<KSPlayer> Ksplayers)
{
ksplayers = Ksplayers;
}
}

}
17 changes: 14 additions & 3 deletions KillStreaks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>KillStreaks</RootNamespace>
<AssemblyName>KillStreaks</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand All @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,31 +30,41 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>lib\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp-firstpass">
<HintPath>lib\Assembly-CSharp-firstpass.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Rocket.API">
<HintPath>lib\Rocket.API.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Rocket.Core">
<HintPath>lib\Rocket.Core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Rocket.Unturned">
<HintPath>lib\Rocket.Unturned.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="UnityEngine">
<HintPath>lib\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>lib\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions KillStreaks.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.16
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KillStreaks", "KillStreaks.csproj", "{594AAC26-8F96-4E60-A11C-82D2FB1C6B67}"
EndProject
Expand All @@ -20,6 +20,6 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7FD3A334-315E-47A4-8442-EFE7F5500F88}
SolutionGuid = {07366BAA-AD6C-4D33-B4B7-8C178F2B5C67}
EndGlobalSection
EndGlobal
Binary file modified lib/Assembly-CSharp-firstpass.dll
Binary file not shown.
Binary file modified lib/Assembly-CSharp.dll
Binary file not shown.
Binary file modified lib/Rocket.Unturned.dll
Binary file not shown.
Binary file added lib/UnityEngine.CoreModule.dll
Binary file not shown.
Binary file modified lib/UnityEngine.dll
Binary file not shown.