Skip to content

Commit

Permalink
remove json and switch to xml
Browse files Browse the repository at this point in the history
  • Loading branch information
justsaft committed Jan 18, 2025
1 parent 0e063da commit 3cc2d5a
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 303 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/bin
/obj
/.vs
/.idea
13 changes: 0 additions & 13 deletions .idea/.idea.LiveSplit.TimeAttackPause/.idea/.gitignore

This file was deleted.

4 changes: 0 additions & 4 deletions .idea/.idea.LiveSplit.TimeAttackPause/.idea/encodings.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/.idea.LiveSplit.TimeAttackPause/.idea/indexLayout.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/.idea.LiveSplit.TimeAttackPause/.idea/vcs.xml

This file was deleted.

20 changes: 10 additions & 10 deletions DTO/Run.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;

using LiveSplit.Model;

namespace LiveSplit.TimeAttackPause.DTO
namespace LiveSplit.TimeAttackPause.DTO;

public record Run
{
public record Run
{
public TimingMethod TimingMethod { get; set; }
public List<Split> Splits { get; set; }
public int CurrentSplitIndex { get; set; }
public TimeSpan? CurrentSplitTime { get; set; }
public TimeSpan? CurrentTime { get; set; }
}
}
public TimingMethod TimingMethod { get; set; }
public List<Split> Splits { get; set; }
public int CurrentSplitIndex { get; set; }
public TimeSpan? CurrentSplitTime { get; set; }
public TimeSpan? CurrentTime { get; set; }
}
13 changes: 6 additions & 7 deletions DTO/Split.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

using System;

namespace LiveSplit.TimeAttackPause.DTO
namespace LiveSplit.TimeAttackPause.DTO;

public record Split
{
public record Split
{
public TimeSpan? Time { get; set; }
public string? Name { get; set; }
}
}
public TimeSpan? Time { get; set; }
public string? Name { get; set; }
}
69 changes: 69 additions & 0 deletions IO/SplitsStateDeserializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#nullable enable
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using LiveSplit.Model;

using Run = LiveSplit.TimeAttackPause.DTO.Run;

/* using Newtonsoft.Json; */

namespace LiveSplit.TimeAttackPause.IO;

public static class SplitsStateDeserializer
{
public static void ImportState(string filepath, LiveSplitState state, ITimerModel timerModel)
{
// read all the text from the file as string
string data = File.ReadAllText(filepath);
var serializer = new XmlSerializer(typeof(Run));
Run? runDto;

/* if (data.IsNullOrEmpty())
{
throw new Exception("Imported file was empty or null.");
} */

/* var string_reader = new StringReader(data); */
using (var stream_reader = new StreamReader(data))
{
var xml_reader = XmlReader.Create(stream_reader);
/* try
{ runDto = (Run)xml_reader.ReadContentAsObject(); }
catch (FormatException)
{ }
catch (InvalidCastException)
{ }
catch (InvalidOperationException)
{ } */

runDto = (Run)serializer.Deserialize(xml_reader);

xml_reader.Dispose();

if (runDto == null)
{
/* throw new Exception("Couldn't deserialize run (runDto was null)"); */
return;
}
}

/* JsonConvert.DeserializeObject<Run>(data); */

timerModel.Start();

int i = 0;
foreach (ISegment segment in state.Run) // ISegment is a split
{
segment.SplitTime = new Time(runDto.TimingMethod, runDto.Splits[i].Time);
i += 1;
}

state.CurrentSplitIndex = runDto.CurrentSplitIndex;
state.AdjustedStartTime = TimeStamp.Now - runDto.CurrentTime.GetValueOrDefault(TimeSpan.Zero);
state.IsGameTimeInitialized = true;
timerModel.Pause();
}
}
37 changes: 0 additions & 37 deletions IO/SplitsStateImporter.cs

This file was deleted.

60 changes: 60 additions & 0 deletions IO/SplitsStateSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;

using LiveSplit.Model;
using LiveSplit.TimeAttackPause.DTO;

using Run = LiveSplit.TimeAttackPause.DTO.Run;

namespace LiveSplit.TimeAttackPause.IO;

public static class SplitsStateSerializer
{
public static void SaveSplitsState(LiveSplitState state, string saveFilePath)
{
TimingMethod timingMethod = state.CurrentTimingMethod;

var splits = state.Run.ToList();

var splitsDTOs = new List<Split>();
foreach (ISegment split in splits)
{
TimeSpan? time = split.SplitTime[timingMethod];
var splitDto = new Split()
{
Time = time,
Name = split.Name
};
splitsDTOs.Add(splitDto);
}

var run = new Run()
{
TimingMethod = timingMethod,
Splits = splitsDTOs,
CurrentSplitIndex = state.CurrentSplitIndex,
CurrentSplitTime = state.CurrentSplit.SplitTime[timingMethod],
CurrentTime = state.CurrentTime[timingMethod],
};

// Use xml to serialize the run
var serializer = new XmlSerializer(typeof(Run));
string data = "";

using (var string_writer = new StringWriter())
{
var xml_writer = XmlWriter.Create(string_writer);
serializer.Serialize(xml_writer, run);
data = string_writer.ToString(); // The run basically
xml_writer.Dispose();
}

// use newtonsoft json to serialize the run object to json
/* string json = JsonConvert.SerializeObject(run, Newtonsoft.Json.Formatting.Indented); */
File.WriteAllText(saveFilePath, data);
}
}
44 changes: 0 additions & 44 deletions IO/SplitsStateWriter.cs

This file was deleted.

39 changes: 19 additions & 20 deletions LiveSplit.TimeAttackPause.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LiveSplit.TimeAttackPause</RootNamespace>
<AssemblyName>LiveSplit.TimeAttackPause</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<LangVersion>10</LangVersion>
<TargetFrameworkProfile />
<!-- <RuntimeIdentifiers>win</RuntimeIdentifiers> -->
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Optimize>true</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
Expand All @@ -26,16 +28,14 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>..\..\bin\release\Components\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PreferNativeArm64>false</PreferNativeArm64>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
Expand All @@ -52,10 +52,10 @@
<ItemGroup>
<Compile Include="DTO\Run.cs" />
<Compile Include="DTO\Split.cs" />
<Compile Include="IO\SplitsStateImporter.cs" />
<Compile Include="IO\SplitsStateWriter.cs" />
<Compile Include="UI\Components\TimeAttackPauseComponent.cs" />
<Compile Include="IO\SplitsStateDeserializer.cs" />
<Compile Include="IO\SplitsStateSerializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UI\Components\TimeAttackPauseComponent.cs" />
<Compile Include="UI\Components\TimeAttackPauseFactory.cs" />
<Compile Include="UI\Components\TimeAttackPauseSettings.cs">
<SubType>UserControl</SubType>
Expand All @@ -64,24 +64,23 @@
<DependentUpon>TimeAttackPauseSettings.cs</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\LiveSplit\LiveSplit.Core\LiveSplit.Core.csproj">
<Project>{6de847db-20a3-4848-aeee-1b4364aecdfb}</Project>
<Name>LiveSplit.Core</Name>
</ProjectReference>
<ProjectReference Include="..\..\LiveSplit\UpdateManager\UpdateManager.csproj">
<Project>{56dea3a0-2eb7-493b-b50f-a5e3aa8ae52a}</Project>
<Name>UpdateManager</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="UI\Components\TimeAttackPauseSettings.resx">
<DependentUpon>TimeAttackPauseSettings.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\LiveSplit.Core\LiveSplit.Core.csproj">
<Project>{26d65334-18e2-4c86-9bf3-193fdcb6886a}</Project>
<Name>LiveSplit.Core</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\UpdateManager\UpdateManager.csproj">
<Project>{d995ff48-716d-4d47-81da-6a2c0a8f0f8e}</Project>
<Name>UpdateManager</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading

0 comments on commit 3cc2d5a

Please sign in to comment.