Skip to content

Commit

Permalink
Merge pull request #1 from gantt42/master
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
Ham5ter authored Oct 22, 2017
2 parents 4c03498 + 7fdca0b commit a7c47ae
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin
obj
packages
.vs
.suo
13 changes: 12 additions & 1 deletion CAOS.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio 15
VisualStudioVersion = 15.0.26730.8
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo", "Demo\Demo.csproj", "{013D15C0-A7FF-4D80-B36B-25E3D58E1FA4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CAOS", "CAOS\CAOS.csproj", "{A4EF3E36-9042-44AE-AD4D-B644A4F7C719}"
EndProject
Global
Expand All @@ -13,8 +17,15 @@ Global
{A4EF3E36-9042-44AE-AD4D-B644A4F7C719}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4EF3E36-9042-44AE-AD4D-B644A4F7C719}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4EF3E36-9042-44AE-AD4D-B644A4F7C719}.Release|Any CPU.Build.0 = Release|Any CPU
{013D15C0-A7FF-4D80-B36B-25E3D58E1FA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{013D15C0-A7FF-4D80-B36B-25E3D58E1FA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{013D15C0-A7FF-4D80-B36B-25E3D58E1FA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{013D15C0-A7FF-4D80-B36B-25E3D58E1FA4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6519CBA1-2991-4AE4-895E-92B6AA97DDE4}
EndGlobalSection
EndGlobal
Binary file removed CAOS.v12.suo
Binary file not shown.
1 change: 1 addition & 0 deletions CAOS/CAOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CaosExpections.cs" />
<Compile Include="CaosInjector.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
28 changes: 28 additions & 0 deletions CAOS/CaosExpections.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace CAOS
{
public class CaosExpection : Exception
{
public CaosExpection()
{ }

public CaosExpection(string message) : base(message)
{ }

public CaosExpection(string message, Exception innerException) : base(message, innerException)
{ }
}

public class NoGameCaosException : CaosExpection
{
public NoGameCaosException()
{ }

public NoGameCaosException(string message) : base(message)
{ }

public NoGameCaosException(string message, Exception innerException) : base(message, innerException)
{ }
}
}
131 changes: 104 additions & 27 deletions CAOS/CaosInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,65 @@ namespace CAOS
{
public class CaosInjector
{
static Mutex Mutex;
static MemoryMappedFile MemFile;
static MemoryMappedViewAccessor MemViewAccessor;
static EventWaitHandle ResultEventHandle;
static EventWaitHandle RequestRventHandle;
private Mutex Mutex;
private MemoryMappedFile MemFile;
private MemoryMappedViewAccessor MemViewAccessor;
private EventWaitHandle ResultEventHandle;
private EventWaitHandle RequestRventHandle;
private string GameName;
public CaosInjector(string GameName)

public CaosInjector(string gameName)
{
this.GameName = GameName;
InitInjector();
CloseInjector();
GameName = gameName;
//It seems to me that these exceptions shouldn't be
// thrown from the constructor. But it seems to
// be mostly an opionion-based thing w/o any
// standard best practices. -JG

//InitInjector();
//CloseInjector();
}

/// <summary>
/// This might not be necessary.
/// Hmm -JG
/// </summary>
public bool CanConnectToGame()
{
try
{
InitInjector();
CloseInjector();
return true;
}
catch (NoGameCaosException)
{
return false;
}
}

private void InitInjector()
{
try
{
Mutex = Mutex.OpenExisting(this.GameName + "_mutex");
MemFile = MemoryMappedFile.OpenExisting(this.GameName + "_mem");
Mutex = Mutex.OpenExisting(GameName + "_mutex");
MemFile = MemoryMappedFile.OpenExisting(GameName + "_mem");
MemViewAccessor = MemFile.CreateViewAccessor();
ResultEventHandle = EventWaitHandle.OpenExisting(this.GameName + "_result");
RequestRventHandle = EventWaitHandle.OpenExisting(this.GameName + "_request");
ResultEventHandle = EventWaitHandle.OpenExisting(GameName + "_result");
RequestRventHandle = EventWaitHandle.OpenExisting(GameName + "_request");
}
catch (Exception)
catch (Exception e)
when (e is WaitHandleCannotBeOpenedException
|| e is System.IO.FileNotFoundException)
{
throw new NoGameCaosException("No running game engine found.", e);
}
catch (UnauthorizedAccessException e)
{
throw new NoGameCaosException("Cannot find or access any running game engine.", e);
}
}

private void CloseInjector()
{
RequestRventHandle.Close();
Expand All @@ -41,12 +74,55 @@ private void CloseInjector()
MemFile.Dispose();
Mutex.Close();
}
public CaosResult AddScriptToScriptorium(int Familiy, int Genus, int Species, int Event, string Script)

public bool TryAddScriptToScriptorium(int family, int genus, int species, int eventNum, string script)
{
CaosResult temp;
return TryAddScriptToScriptorium(family, genus, species, eventNum, script, out temp);
}

public bool TryAddScriptToScriptorium(int family, int genus, int species, int eventNum, string script, out CaosResult caosResult)
{
try
{
caosResult = AddScriptToScriptorium(family, genus, species, eventNum, script);
return true;
}
catch (NoGameCaosException)
{
caosResult = null;
return false;
}
}

public CaosResult AddScriptToScriptorium(int family, int genus, int species, int eventNum, string script)
{
return ExecuteCaosGetResult(Script, "scrp " + Familiy + " " + Genus + " " + Species + " " + Event);
return ExecuteCaos(script, "scrp " + family + " " + genus + " " + species + " " + eventNum);
}
public CaosResult ExecuteCaosGetResult(string CaosAsString, string Action = "execute")

public bool TryExecuteCaos(string caosAsString)
{
CaosResult temp;
return TryExecuteCaos(caosAsString, out temp);
}

public bool TryExecuteCaos(string caosAsString, out CaosResult caosResult)
{
try
{
caosResult = ExecuteCaos(caosAsString);
return true;
}
catch (NoGameCaosException)
{
caosResult = null;
return false;
}
}

public CaosResult ExecuteCaos(string CaosAsString, string Action = "execute")
{
//Need more exception checking here - JG
InitInjector();
byte[] CaosBytes = Encoding.UTF8.GetBytes(Action + "\n" + CaosAsString + "\n");
int BufferPosition = 24;
Expand Down Expand Up @@ -79,6 +155,7 @@ public CaosResult ExecuteCaosGetResult(string CaosAsString, string Action = "ex
Thread.Sleep(50);
return new CaosResult(ResultCode, Encoding.UTF8.GetString(ResultBytes), ProcessID);
}

public int ProcessID()
{
Mutex.WaitOne();
Expand All @@ -89,17 +166,17 @@ public int ProcessID()
}
public class CaosResult
{
private bool failed;
public bool Failed { get { return failed; } }
private int processid;
public int ProcessID { get { return processid; } }
private string content;
public string Content { get { return content; } }
public CaosResult(int Failed, string Content, int ProcessID)
public int ResultCode { get; private set; }
public bool Success { get; private set; }
public int ProcessId { get; private set; }
public string Content { get; private set; }

public CaosResult(int resultCode, string content, int processID)
{
this.failed = Convert.ToBoolean(Failed);
this.content = Content;
this.processid = ProcessID;
this.ResultCode = resultCode;
this.Success = (resultCode == 0);
this.Content = content;
this.ProcessId = processID;
}
}
}
4 changes: 2 additions & 2 deletions CAOS/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyFileVersion("2.0.0")]
Binary file removed CAOS/bin/Release/CAOS.dll
Binary file not shown.
6 changes: 6 additions & 0 deletions Demo/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
58 changes: 58 additions & 0 deletions Demo/Demo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{013D15C0-A7FF-4D80-B36B-25E3D58E1FA4}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ConsoleApp1</RootNamespace>
<AssemblyName>ConsoleApp1</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CAOS\CAOS.csproj">
<Project>{a4ef3e36-9042-44ae-ad4d-b644a4f7c719}</Project>
<Name>CAOS</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
68 changes: 68 additions & 0 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using CAOS;
using System.Diagnostics;

namespace ConsoleApp1
{
class Demo
{
static void Main(string[] args)
{
CaosInjector injector = new CaosInjector("Docking Station");

if (injector.CanConnectToGame())
{
TryCatchStrategy(injector);
TryReturnBoolStrategy(injector);
}
else
{
Console.WriteLine("Couldn't connect to game.");
}
Console.ReadKey();
}

private static void TryCatchStrategy(CaosInjector injector)
{
try
{
CaosResult result = injector.ExecuteCaos("outs \"hi\"");
if (result.Success)
{
Console.WriteLine(result.Content);
}
else
{
Console.WriteLine($"Error Code: {result.ResultCode}");
}
}
catch (NoGameCaosException e)
{
Console.WriteLine($"Game exited unexpectedly. Error message: {e.Message}");
}
}

private static void TryReturnBoolStrategy(CaosInjector injector)
{
CaosResult result;
if (injector.TryExecuteCaos("outs \"hi\"", out result))
{
if (result.Success)
{
Console.WriteLine(result.Content);
//Just try to do it, we don't care about the results
injector.TryExecuteCaos("targ norn doif targ <> null sezz \"Yo yo! What up?\" endi");
}
else
{
Debug.Assert(result.ResultCode != 0);
Console.WriteLine($"Error Code: {result.ResultCode}");
}
}
else
{
Console.WriteLine("Execution failed. Game may have exited.");
}
}
}
}
Loading

0 comments on commit a7c47ae

Please sign in to comment.