Skip to content
Permalink
Browse files

Switched from tabs to spaces in sample-csharp

  • Loading branch information...
s3-mihe committed Jun 20, 2015
1 parent 23d5207 commit 505323ca095e2bca01369d34caee6dcd114e7eee
Showing with 310 additions and 317 deletions.
  1. +119 −126 sample-csharp/src/Program.cs
  2. +176 −176 sample-csharp/src/R3E.cs
  3. +15 −15 sample-csharp/src/Utilities.cs
@@ -9,130 +9,123 @@

namespace R3E
{
class Sample : IDisposable
{
private bool Mapped
{
get { return (_file != null && _view != null); }
}

private MemoryMappedFile _file;
private MemoryMappedViewAccessor _view;

private readonly TimeSpan _timeAlive = TimeSpan.FromMinutes(10);
private readonly TimeSpan _timeInterval = TimeSpan.FromMilliseconds(100);

public void Dispose()
{
_file.Dispose();
_view.Dispose();
}

public void Run()
{
var timeReset = DateTime.UtcNow;
var timeLast = timeReset;

Console.WriteLine("Looking for RRRE.exe...");

while(true)
{
var timeNow = DateTime.UtcNow;

if(timeNow.Subtract(timeReset) > _timeAlive)
{
break;
}

if(timeNow.Subtract(timeLast) < _timeInterval)
{
Thread.Sleep(1);
continue;
}

timeLast = timeNow;

if(Utilities.IsRrreRunning() && !Mapped)
{
Console.WriteLine("Found RRRE.exe, mapping shared memory...");

if(Map())
{
Console.WriteLine("Memory mapped successfully");
timeReset = DateTime.UtcNow;
}
}

if(Mapped)
{
Print();
}
}

Console.WriteLine("All done!");
}

private bool Map()
{
try
{
_file = MemoryMappedFile.OpenExisting(Constant.SharedMemoryName);
_view = _file.CreateViewAccessor(0, Marshal.SizeOf(typeof(Shared)));
return true;
}
catch(FileNotFoundException)
{
return false;
}
}

private void Print()
{
Shared data;
_view.Read(0, out data);

if(data.Gear > -2)
{
Console.WriteLine("Gear: {0}", data.Gear);
}

if(data.EngineRps > -1.0f)
{
Console.WriteLine("RPM: {0}", Utilities.RpsToRpm(data.EngineRps));
Console.WriteLine("Speed: {0}", Utilities.MpsToKph(data.CarSpeed));
}

Console.WriteLine("");
}
}

class Program
{
static void MainSafe(string[] args)
{
using(var sample = new Sample())
{
sample.Run();
}
}

static void Main(string[] args)
{
if(Debugger.IsAttached)
{
MainSafe(args);
}
else
{
try
{
MainSafe(args);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
class Sample : IDisposable
{
private bool Mapped
{
get { return (_file != null && _view != null); }
}

private MemoryMappedFile _file;
private MemoryMappedViewAccessor _view;

private readonly TimeSpan _timeAlive = TimeSpan.FromMinutes(10);
private readonly TimeSpan _timeInterval = TimeSpan.FromMilliseconds(100);

public void Dispose()
{
_file.Dispose();
_view.Dispose();
}

public void Run()
{
var timeReset = DateTime.UtcNow;
var timeLast = timeReset;

Console.WriteLine("Looking for RRRE.exe...");

while(true)
{
var timeNow = DateTime.UtcNow;

if(timeNow.Subtract(timeReset) > _timeAlive)
{
break;
}

if(timeNow.Subtract(timeLast) < _timeInterval)
{
Thread.Sleep(1);
continue;
}

timeLast = timeNow;

if(Utilities.IsRrreRunning() && !Mapped)
{
Console.WriteLine("Found RRRE.exe, mapping shared memory...");

if(Map())
{
Console.WriteLine("Memory mapped successfully");
timeReset = DateTime.UtcNow;
}
}

if(Mapped)
{
Print();
}
}

Console.WriteLine("All done!");
}

private bool Map()
{
try
{
_file = MemoryMappedFile.OpenExisting(Constant.SharedMemoryName);
_view = _file.CreateViewAccessor(0, Marshal.SizeOf(typeof(Shared)));
return true;
}
catch(FileNotFoundException)
{
return false;
}
}

private void Print()
{
Shared data;
_view.Read(0, out data);

Console.WriteLine("Best: {0}", data.LapTimeBest);
Console.WriteLine("Current: {0}", data.LapTimeCurrent);
Console.WriteLine("Previous: {0}", data.LapTimePrevious);

Console.WriteLine("");
}
}

class Program
{
static void MainSafe(string[] args)
{
using(var sample = new Sample())
{
sample.Run();
}
}

static void Main(string[] args)
{
if(Debugger.IsAttached)
{
MainSafe(args);
}
else
{
try
{
MainSafe(args);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
}

0 comments on commit 505323c

Please sign in to comment.
You can’t perform that action at this time.