Develop #8

Merged
SemvdH merged 10 commits from develop into master 2020-09-18 15:16:53 +00:00
5 changed files with 699 additions and 0 deletions

329
RH-Engine/Command.cs Normal file
View File

@@ -0,0 +1,329 @@
using LibNoise.Primitive;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace RH_Engine
{
class Command
{
public const string STANDARD_HEAD = "Head";
public const string STANDARD_GROUND = "GroundPlane";
public const string STANDARD_SUN = "SunLight";
public const string STANDARD_LEFTHAND = "LeftHand";
public const string STANDARD_RIGHTHAND = "RightHand";
string tunnelID;
public Command(string tunnelID)
{
this.tunnelID = tunnelID;
}
public string TerrainCommand(int[] sizeArray, float[] heightsArray)
{
dynamic payload = new
{
id = "scene/terrain/add",
data = new
{
size = sizeArray,
heights = heightsArray
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string AddLayer(string uid, string texture)
{
dynamic payload = new
{
id = "scene/node/addlayer",
data = new
{
id = uid,
diffuse = @"C:\Users\woute\Downloads\NetworkEngine.18.10.10.1\NetworkEngine\data\NetworkEngine\textures\terrain\adesert_cracks_d.jpg",
normal = @"C:\Users\woute\Downloads\NetworkEngine.18.10.10.1\NetworkEngine\data\NetworkEngine\textures\terrain\adesert_mntn_d.jpg",
minHeight = 0,
maxHeight = 10,
fadeDist = 1
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string UpdateTerrain()
{
dynamic payload = new
{
id = "scene/terrain/update",
data = new
{
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string AddNodeCommand()
{
dynamic payload = new
{
id = "scene/node/add",
data = new
{
name = "newNode",
components = new
{
terrain = new
{
smoothnormals = true
}
}
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string DeleteNode(string uuid)
{
dynamic payload = new
{
id = "scene/node/delete",
data = new
{
id = uuid,
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string AddBikeModel()
{
return AddModel("bike", "data\\NetworkEngine\\models\\bike\\bike.fbx");
}
public string AddModel(string nodeName, string fileLocation)
{
return AddModel(nodeName, fileLocation, null, new float[] { 0, 0, 0 }, 1, new float[] { 0, 0, 0 });
}
public string AddModel(string nodeName, string fileLocation, float[] positionVector, float scalar, float[] rotationVector)
{
return AddModel(nodeName, fileLocation, null, positionVector, scalar, rotationVector);
}
public string AddModel(string nodeName, string fileLocation, string animationLocation, float[] positionVector, float scalar, float[] rotationVector)
{
string namename = nodeName;
bool animatedBool = false;
if (animationLocation != null)
{
animatedBool = true;
}
dynamic payload = new
{
id = "scene/node/add",
data = new
{
name = namename,
components = new
{
transform = new
{
position = positionVector,
scale = scalar,
rotation = rotationVector
},
model = new
{
file = fileLocation,
cullbackfaces = true,
animated = animatedBool,
animation = animationLocation
},
}
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string MoveTo(string uuid, float[] positionVector, float rotateValue, float speedValue, float timeValue)
{
return MoveTo(uuid, "idk", positionVector, rotateValue, "linear", false, speedValue, timeValue);
}
private string MoveTo(string uuid, string stopValue, float[] positionVector, float rotateValue, string interpolateValue, bool followHeightValue, float speedValue, float timeValue)
{
dynamic payload = new
{
id = "scene/node/moveto",
data = new
{
id = uuid,
stop = stopValue,
position = positionVector,
rotate = rotateValue,
interpolate = interpolateValue,
followheight = followHeightValue,
speed = speedValue,
time = timeValue
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string RouteCommand()
{
ImprovedPerlin improvedPerlin = new ImprovedPerlin(4325, LibNoise.NoiseQuality.Best);
Random r = new Random();
dynamic payload = new
{
id = "route/add",
data = new
{
nodes = new dynamic[]
{
new
{
/*pos = GetPos(0.6f, improvedPerlin)*/
pos = new int[] {0,0,5 },
dir = new int[] { r.Next(20,100),0,-r.Next(20, 100) }
},
new
{
//pos = GetPos(1.6f, improvedPerlin),
pos = new int[] {50,0,0 },
dir = new int[] { r.Next(20, 100),0,r.Next(20, 100) }
},
new
{
//pos = GetPos(2.654f, improvedPerlin),
pos = new int[] {20,0,20 },
dir = new int[] { r.Next(20, 100),0,r.Next(20, 100) }
},
new
{
//pos = GetPos(3.6543f, improvedPerlin),
pos = new int[] {10,0,50 },
dir = new int[] { -r.Next(3,7),0,r.Next(3,7) }
},
new
{
pos = new int[] {0,0,50 },
dir = new int[] { -r.Next(20, 50),0,-r.Next(20, 50) }
}
}
}
};
Console.WriteLine("route command: " + JsonConvert.SerializeObject(Payload(payload)));
return JsonConvert.SerializeObject(Payload(payload));
}
private float[] GetPos(float n, ImprovedPerlin improvedPerlin)
{
float[] res = new float[] { improvedPerlin.GetValue(n) * 50, 0, improvedPerlin.GetValue(n) * 50 };
return res;
}
private int[] GetDir()
{
Random rng = new Random();
int[] dir = {rng.Next(50), 0, rng.Next(50)};
return dir;
}
public string FollowRouteCommand()
{
return "";
}
public string RoadCommand(string uuid_route)
{
Console.WriteLine("road");
dynamic payload = new
{
id = "scene/road/add",
data = new
{
route = uuid_route,
diffuse = "data/NetworkEngine/textures/tarmac_diffuse.png",
normal = "data/NetworkEngine/textures/tarmac_normale.png",
specular = "data/NetworkEngine/textures/tarmac_specular.png",
heightoffset = 1f
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string GetSceneInfoCommand()
{
dynamic payload = new
{
id = "scene/get"
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string ResetScene()
{
dynamic payload = new
{
id = "scene/reset"
};
return JsonConvert.SerializeObject(Payload(payload));
}
public string SkyboxCommand(double timeToSet)
{
if (timeToSet < 0 || timeToSet > 24)
{
throw new Exception("The time must be between 0 and 24!");
}
dynamic payload = new
{
id = "scene/skybox/settime",
data = new
{
time = timeToSet
}
};
return JsonConvert.SerializeObject(Payload(payload));
}
private object Payload(dynamic message)
{
return new
{
id = "tunnel/send",
data = new
{
dest = tunnelID,
data = message,
}
};
}
}
}

74
RH-Engine/JSONParser.cs Normal file
View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;
namespace RH_Engine
{
class JSONParser
{
/// <summary>
/// returns all the users from the given response
/// </summary>
/// <param name="msg">the message gotten from the server, without the length prefix</param>
/// <returns></returns>
public static PC[] GetUsers(string msg)
{
dynamic jsonData = JsonConvert.DeserializeObject(msg);
Newtonsoft.Json.Linq.JArray data = jsonData.data;
PC[] res = new PC[data.Count];
int counter = 0;
foreach (dynamic d in data)
{
res[counter] = new PC((string)d.clientinfo.host, (string)d.clientinfo.user);
counter++;
}
return res;
}
public static string GetSessionID(string msg, PC[] PCs)
{
dynamic jsonData = JsonConvert.DeserializeObject(msg);
Newtonsoft.Json.Linq.JArray data = jsonData.data;
for (int i = data.Count-1; i >= 0; i--)
{
dynamic d = data[i];
foreach (PC pc in PCs)
{
if (d.clientinfo.host == pc.host && d.clientinfo.user == pc.user)
{
Console.WriteLine("connecting to {0}, on {1} with id {2}", pc.user, pc.host, d.id);
return d.id;
}
}
}
return null;
}
public static string GetTunnelID(string json)
{
dynamic jsonData = JsonConvert.DeserializeObject(json);
if (jsonData.data.status == "ok")
{
return jsonData.data.id;
}
return null;
}
public static string GetRouteID(string json)
{
dynamic jsonData = JsonConvert.DeserializeObject(json);
if (jsonData.data.status == "ok")
{
return jsonData.data.uuid;
}
return null;
}
}
}

257
RH-Engine/Program.cs Normal file
View File

@@ -0,0 +1,257 @@
using LibNoise.Primitive;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Globalization;
using System.IO;
using System.Net.Sockets;
using System.Runtime.Intrinsics.X86;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
namespace RH_Engine
{
internal class Program
{
private static PC[] PCs = {
//new PC("DESKTOP-M2CIH87", "Fabian"),
new PC("T470S", "Shinichi"),
//new PC("DESKTOP-DHS478C", "semme"),
//new PC("DESKTOP-TV73FKO", "Wouter"),
//new PC("DESKTOP-SINMKT1", "Ralf"),
//new PC("NA", "Bart")
};
private static void Main(string[] args)
{
TcpClient client = new TcpClient("145.48.6.10", 6666);
CreateConnection(client.GetStream());
}
/// <summary>
/// writes a message to the server
/// </summary>
/// <param name="stream">the network stream to use</param>
/// <param name="message">the message to send</param>
public static void WriteTextMessage(NetworkStream stream, string message)
{
byte[] msg = Encoding.ASCII.GetBytes(message);
byte[] res = new byte[msg.Length + 4];
Array.Copy(BitConverter.GetBytes(msg.Length), 0, res, 0, 4);
Array.Copy(msg, 0, res, 4, msg.Length);
stream.Write(res);
//Console.WriteLine("sent message " + message);
}
/// <summary>
/// reads a response from the server
/// </summary>
/// <param name="stream">the network stream to use</param>
/// <returns>the returned message from the server</returns>
public static string ReadPrefMessage(NetworkStream stream)
{
byte[] lengthBytes = new byte[4];
stream.Read(lengthBytes, 0, 4);
Console.WriteLine("read message..");
int length = BitConverter.ToInt32(lengthBytes);
//Console.WriteLine("length is: " + length);
byte[] buffer = new byte[length];
int totalRead = 0;
//read bytes until stream indicates there are no more
do
{
int read = stream.Read(buffer, totalRead, buffer.Length - totalRead);
totalRead += read;
//Console.WriteLine("ReadMessage: " + read);
} while (totalRead < length);
return Encoding.UTF8.GetString(buffer, 0, totalRead);
}
/// <summary>
/// connects to the server and creates the tunnel
/// </summary>
/// <param name="stream">the network stream to use</param>
private static void CreateConnection(NetworkStream stream)
{
WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}");
string id = JSONParser.GetSessionID(ReadPrefMessage(stream), PCs);
string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + id + "\"}}";
WriteTextMessage(stream, tunnelCreate);
string tunnelResponse = ReadPrefMessage(stream);
Console.WriteLine(tunnelResponse);
string tunnelID = JSONParser.GetTunnelID(tunnelResponse);
if (tunnelID == null)
{
Console.WriteLine("could not find a valid tunnel id!");
return;
}
sendCommands(stream, tunnelID);
}
/// <summary>
/// sends all the commands to the server
/// </summary>
/// <param name="stream">the network stream to use</param>
/// <param name="tunnelID">the tunnel id to use</param>
private static void sendCommands(NetworkStream stream, string tunnelID)
{
Command mainCommand = new Command(tunnelID);
WriteTextMessage(stream, mainCommand.ResetScene());
ReadPrefMessage(stream);
string routeid = CreateRoute(stream, mainCommand);
WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null));
Console.WriteLine(ReadPrefMessage(stream));
string command;
command = mainCommand.AddBikeModel();
WriteTextMessage(stream, command);
Console.WriteLine(ReadPrefMessage(stream));
command = mainCommand.AddModel("car", "data\\customModels\\TeslaRoadster.fbx");
WriteTextMessage(stream, command);
Console.WriteLine(ReadPrefMessage(stream));
}
/// <summary>
/// gets the id of the object with the given name
/// </summary>
/// <param name="name">the name of the object</param>
/// <param name="stream">the network stream to send requests to</param>
/// <param name="createGraphics">the create graphics object to create all the commands</param>
/// <returns> the uuid of the object with the given name, <c>null</c> otherwise.</returns>
public static string GetId(string name, NetworkStream stream, Command createGraphics)
{
JArray children = GetChildren(stream, createGraphics);
foreach (dynamic child in children)
{
if (child.name == name)
{
return child.uuid;
}
}
Console.WriteLine("Could not find id of " + name);
return null;
}
public static string CreateRoute(NetworkStream stream, Command createGraphics)
{
WriteTextMessage(stream, createGraphics.RouteCommand());
dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream));
if (response.data.data.id == "route/add")
{
return response.data.data.data.uuid;
}
return null;
}
public static void CreateTerrain(NetworkStream stream, Command createGraphics)
{
float x = 0f;
float[] height = new float[256 * 256];
ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best);
for (int i = 0; i < 256 * 256; i++)
{
height[i] = improvedPerlin.GetValue(x / 10, x / 10, x * 100) + 1;
x += 0.001f;
}
WriteTextMessage(stream, createGraphics.TerrainCommand(new int[] { 256, 256 }, height));
Console.WriteLine(ReadPrefMessage(stream));
WriteTextMessage(stream, createGraphics.AddNodeCommand());
Console.WriteLine(ReadPrefMessage(stream));
}
/// <summary>
/// gets all the children in the current scene
/// </summary>
/// <param name="stream">the network stream to send requests to</param>
/// <param name="createGraphics">the create graphics object to create all the commands</param>
/// <returns>all the children objects in the current scene</returns>
public static JArray GetChildren(NetworkStream stream, Command createGraphics)
{
WriteTextMessage(stream, createGraphics.GetSceneInfoCommand());
dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream));
return response.data.data.data.children;
}
/// <summary>
/// returns all objects in the current scene, as name-uuid tuples.
/// </summary>
/// <param name="stream">the network stream to send requests to</param>
/// <param name="createGraphics">the create graphics object to create all the commands</param>
/// <returns>an array of name-uuid tuples for each object</returns>
public static (string, string)[] GetObjectsInScene(NetworkStream stream, Command createGraphics)
{
JArray children = GetChildren(stream, createGraphics);
(string, string)[] res = new (string, string)[children.Count];
int i = 0;
foreach (dynamic child in children)
{
res[i] = (child.name, child.uuid);
i++;
}
return res;
}
public static string getUUIDFromResponse(string response)
{
dynamic JSON = JsonConvert.DeserializeObject(response);
return JSON.data.data.data.uuid;
}
}
/// <summary>
/// struct used to store the host pc name and user
/// </summary>
public readonly struct PC
{
public PC(string host, string user)
{
this.host = host;
this.user = user;
}
public string host { get; }
public string user { get; }
public override string ToString()
{
return "PC - host:" + host + " - user:" + user;
}
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>RH_Engine</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LibNoise" Version="0.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

25
RH-Engine/RH-Engine.sln Normal file
View File

@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30503.244
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RH-Engine", "RH-Engine.csproj", "{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C75B8E99-BE3D-496F-B2F0-03C4069493B2}
EndGlobalSection
EndGlobal