This commit is contained in:
Joseph Henry
2016-06-14 16:01:19 -07:00
parent 76b7e0fef7
commit c1ce7dc87a
436 changed files with 87247 additions and 473 deletions

View File

@@ -0,0 +1,194 @@

var zoomSpeed : float = 3.0f;
var moveSpeed : float = 3.0f;
var rotateSpeed : float = 6.0f;
var optionalMaterialForSelection : Material;
// Some internal placeholders
private var orbitVector : GameObject;
private var materialForSelection : Material;
private var selectedObjects = new ArrayList();
private var selectedObjectsMaterial = new ArrayList();
function Start() {
// Create a capsule (which will be the lookAt target and global orbit vector)
orbitVector = GameObject.CreatePrimitive(PrimitiveType.Capsule);
orbitVector.transform.position = Vector3.zero;
// Snap the camera to align with the grid in set starting position (otherwise everything gets a bit wonky)
transform.position = Vector3(4, 4, 4);
// Point the camera towards the capsule
transform.LookAt(orbitVector.transform.position, Vector3.up);
// Hide the capsule (disable the mesh renderer)
orbitVector.GetComponent.<Renderer>().enabled = false;
// Create material to apply for selections (or use material supplied by user)
if (optionalMaterialForSelection) {
materialForSelection = optionalMaterialForSelection;
} else {
materialForSelection = new Material(Shader.Find("Diffuse"));
materialForSelection.color = Color.green;
}
}
function Update(){
orbitVector.transform.position = Vector3.zero;
if (Input.GetAxis("Mouse ScrollWheel") < 0) {
var currentZoomSpeedin = -10;
transform.Translate(Vector3.forward * ( currentZoomSpeedin));
}
if (Input.GetAxis("Mouse ScrollWheel") > 0) {
var currentZoomSpeedout = 10;
transform.Translate(Vector3.forward * ( currentZoomSpeedout));
}
}
// Call all of our functionality in LateUpdate() to avoid weird behaviour (as seen in Update())
function LateUpdate() {
// Get mouse vectors
var x = Input.GetAxis("Mouse X");
var y = Input.GetAxis("Mouse Y");
// ALT is pressed, start navigation
//if (Input.GetKey(KeyCode.RightAlt) || Input.GetKey(KeyCode.LeftAlt)) {
// Distance between camera and orbitVector. We'll need this in a few places
var distanceToOrbit = Vector3.Distance(transform.position, orbitVector.transform.position);
//RMB - ZOOM
if (Input.GetMouseButton(2)) {
// Refine the rotateSpeed based on distance to orbitVector
var currentZoomSpeed = Mathf.Clamp(zoomSpeed * (distanceToOrbit / 50), 0.1f, 2.0f);
// Move the camera in/out
transform.Translate(Vector3.forward * (x * currentZoomSpeed));
// If about to collide with the orbitVector, repulse the orbitVector slightly to keep it in front of us
if (Vector3.Distance(transform.position, orbitVector.transform.position) < 3) {
orbitVector.transform.Translate(Vector3.forward, transform);
}
//LMB - PIVOT
} else if (Input.GetMouseButton(1)) {
// Refine the rotateSpeed based on distance to orbitVector
var currentRotateSpeed = Mathf.Clamp(rotateSpeed * (distanceToOrbit / 50), 1.0f, rotateSpeed);
// Temporarily parent the camera to orbitVector and rotate orbitVector as desired
transform.parent = orbitVector.transform;
orbitVector.transform.Rotate(Vector3.right * (y * currentRotateSpeed));
orbitVector.transform.Rotate(Vector3.up * (x * currentRotateSpeed), Space.World);
transform.parent = null;
//MMB - PAN
} else if (Input.GetMouseButton(0)) {
// Calculate move speed
var translateX = Vector3.right * (x * moveSpeed) * -1;
var translateY = Vector3.up * (y * moveSpeed) * -1;
// Move the camera
transform.Translate(translateX);
transform.Translate(translateY);
// Move the orbitVector with the same values, along the camera's axes. In effect causing it to behave as if temporarily parented.
orbitVector.transform.Translate(translateX, transform);
orbitVector.transform.Translate(translateY, transform);
}
/*
// If we're not currently navigating, grab selection if something is clicked
} else if (Input.GetMouseButtonDown(0)) {
var hitInfo : RaycastHit;
var ray : Ray = camera.ScreenPointToRay(Input.mousePosition);
var allowMultiSelect : boolean = false;
// See if the user is holding in CTRL or SHIFT. If so, enable multiselection
if(Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) {
allowMultiSelect = true;
}
// Something was clicked. Fetch.
if (Physics.Raycast(ray, hitInfo, camera.farClipPlane)) {
target = hitInfo.transform;
// If NOT multiselection, remove all prior selections
if (!allowMultiSelect) {
deselectAll();
}
//Toggle between selected and unselected (depending on current state)
if (target.renderer.sharedMaterial != materialForSelection) {
selectedObjects.Add(target.gameObject);
selectedObjectsMaterial.Add(target.gameObject.renderer.sharedMaterial);
target.gameObject.renderer.sharedMaterial = materialForSelection;
} else {
var arrayLocation : int = selectedObjects.IndexOf(target.gameObject);
if (arrayLocation == -1) {return;}; //this shouldn't happen. Ever. But still.
target.gameObject.renderer.sharedMaterial = selectedObjectsMaterial[arrayLocation];
selectedObjects.RemoveAt(arrayLocation);
selectedObjectsMaterial.RemoveAt(arrayLocation);
}
// Else deselect all selected objects (ie. click on empty background)
} else {
// Don't deselect if allowMultiSelect is true
if (!allowMultiSelect) {deselectAll();};
}
// Fetch input of the F-button (focus) -- this is a very dodgy implementation...
} else if (Input.GetKeyDown("f")) {
var backtrack = Vector3(0, 0, -15);
var selectedObject : GameObject;
// If dealing with only one selected object
if (selectedObjects.Count == 1) {
selectedObject = selectedObjects[0];
transform.position = selectedObject.transform.position;
orbitVector.transform.position = selectedObject.transform.position;
transform.Translate(backtrack);
// Else we need to average out the position vectors (this is the proper dodgy part of the implementation)
} else if (selectedObjects.Count > 1) {
selectedObject = selectedObjects[0];
var average = selectedObject.transform.position;
for (var i = 1; i < selectedObjects.Count; i++) {
selectedObject = selectedObjects[i];
average = (average + selectedObject.transform.position) / 2;
}
transform.position = average;
orbitVector.transform.position = average;
transform.Translate(backtrack);
}
}
*/
}
// Function to handle the de-selection of all objects in scene
function deselectAll() {
// Run through the list of selected objects and restore their original materials
for (var currentItem = 0; currentItem < selectedObjects.Count; currentItem++) {
var selectedObject : GameObject = selectedObjects[currentItem];
selectedObject.GetComponent.<Renderer>().sharedMaterial = selectedObjectsMaterial[currentItem];
}
// Clear both arrays
selectedObjects.Clear();
selectedObjectsMaterial.Clear();
}

View File

@@ -0,0 +1,89 @@
using UnityEngine;
using System.Collections;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Demo : MonoBehaviour
{
public float speed = 300f;
private ZeroTierNetworkInterface zt;
string nwid = "";
private void zt_sample_network_test_thread()
{
print("test_network");
byte error;
// Prepare sample data buffer
/*
byte[] buffer = new byte[1024];
Stream stream = new MemoryStream(buffer);
BinaryFormatter f = new BinaryFormatter();
f.Serialize ( stream , "Welcome to the machine! (from Unity3D)" );
int error;
*/
// Connect to server
int connfd = zt.Connect (0, "172.22.211.245", 8888, out error);
print(connfd);
// Send sample data to server
//int bytes_written = zt.Send(connfd,buffer,0, out error);
//print(bytes_written);
char[] buffer = new char[1024];
buffer = "hello".ToCharArray();
//print (buffer);
//Stream stream = new MemoryStream(buffer);
//BinaryFormatter formatter = new BinaryFormatter();
//formatter.Serialize(stream, "HelloServer");
//int bufferSize = 1024;
int bytes_written = zt.Send(connfd, "hello".ToCharArray(),4, out error);
print(bytes_written);
}
public void zt_test_network()
{
Thread networkTestThread = new Thread(() => { zt_sample_network_test_thread();});
networkTestThread.IsBackground = true;
networkTestThread.Start();
}
void Start()
{
// Create new instance of ZeroTier in separate thread
zt = new ZeroTierNetworkInterface ("/Users/Joseph/utest2");
/* This new instance will communicate via a named pipe, so any
* API calls (ZeroTier.Connect(), ZeroTier.Send(), etc) will be sent to the service
* via this pipe.
*/
}
// Terminate the ZeroTier service when the application quits
void OnApplicationQuit() {
zt.Terminate ();
}
// Update is called once per frame
void Update () {
// Rotate ZTCube when ZT is running
if (zt.IsRunning ()) {
GameObject go = GameObject.Find ("ZTCube");
Vector3 rotvec = new Vector3 (10f, 10f, 10f);
go.transform.Rotate (rotvec, speed * Time.deltaTime);
}
/*
GameObject go = GameObject.Find("ZTCube");
Text text = go.GetComponents<Text> ()[0];
if (text) {
text.text = IsRunning() ? "ZeroTier Status: Online" : "ZeroTier Status: Offline";
}
*/
}
}

Binary file not shown.

View File

@@ -0,0 +1,8 @@
using UnityEngine;
using System.Collections;
public class MyZeroTier : MonoBehaviour {
void Start() {
Application.OpenURL("https://my.zerotier.com");
}
}

View File

@@ -0,0 +1 @@


Binary file not shown.

View File

@@ -0,0 +1,282 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine.Networking;
using System.Net.Sockets;
using System.Net;
using System.IO;
public class ZeroTierNetworkInterface {
// ZeroTier background thread
private Thread ztThread;
// Interop structures
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct sockaddr {
/// u_short->unsigned short
public ushort sa_family;
/// char[14]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=14)]
public string sa_data;
}
// Virtual network interace config
private int MaxPacketSize;
private string rpc_path = "/does/this/work";
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void MyDelegate(string str);
static void CallBackFunction(string str) {
Debug.Log("Native ZT Plugin: " + str);
}
// ZeroTier service / debug initialization
[DllImport ("ZeroTierUnity")]
public static extern void SetDebugFunction( IntPtr fp );
[DllImport ("ZeroTierUnity")]
private static extern int unity_start_service(string path);
// Connection calls
[DllImport ("ZeroTierUnity")]
private static extern int zt_socket(int family, int type, int protocol);
[DllImport ("ZeroTierUnity")]
unsafe private static extern int zt_bind(int sockfd, System.IntPtr addr, int addrlen);
[DllImport ("ZeroTierUnity")]
unsafe private static extern int zt_connect(int sockfd, System.IntPtr addr, int addrlen);
[DllImport ("ZeroTierUnity")]
private static extern int zt_accept(int sockfd);
[DllImport ("ZeroTierUnity")]
private static extern int zt_listen(int sockfd, int backlog);
[DllImport ("ZeroTierUnity")]
private static extern int zt_close(int sockfd);
// RX / TX
[DllImport ("ZeroTierUnity")]
unsafe private static extern int zt_recv(int sockfd, System.IntPtr buf, int len);
[DllImport ("ZeroTierUnity")]
unsafe private static extern int zt_send(int sockfd, System.IntPtr buf, int len);
// ZT Thread controls
[DllImport ("ZeroTierUnity")]
private static extern bool zt_is_running();
[DllImport ("ZeroTierUnity")]
private static extern void zt_terminate();
// ZT Network controls
[DllImport ("ZeroTierUnity")]
private static extern bool zt_join_network(string nwid);
[DllImport ("ZeroTierUnity")]
private static extern void zt_leave_network(string nwid);
// Thread which starts the ZeroTier service
// The ZeroTier service may spin off a SOCKS5 proxy server
// if -DUSE_SOCKS_PROXY is set when building the bundle
private void zt_service_thread()
{
MyDelegate callback_delegate = new MyDelegate( CallBackFunction );
// Convert callback_delegate into a function pointer that can be
// used in unmanaged code.
IntPtr intptr_delegate =
Marshal.GetFunctionPointerForDelegate(callback_delegate);
// Call the API passing along the function pointer.
SetDebugFunction( intptr_delegate );
unity_start_service (rpc_path);
}
// Start the ZeroTier service
private void Init()
{
// TODO: Handle exceptions from unmanaged code
ztThread = new Thread(() => {
try {
zt_service_thread();
} catch(Exception e) {
Debug.Log(e.Message.ToString());
}
});
ztThread.IsBackground = true; // Allow the thread to be aborted safely
ztThread.Start();
}
// Initialize the ZeroTier service with a given path
public ZeroTierNetworkInterface(string path)
{
rpc_path = path;
Init();
}
// Initialize the ZeroTier service
public ZeroTierNetworkInterface()
{
Init();
}
// Initialize the ZeroTier service
// Use the GlobalConfig to set things like the max packet size
public ZeroTierNetworkInterface(GlobalConfig gConfig)
{
MaxPacketSize = gConfig.MaxPacketSize; // TODO: Do something with this!
Init();
}
// Joins a ZeroTier virtual network
public void JoinNetwork(string nwid)
{
zt_join_network(nwid);
}
// Leaves a ZeroTier virtual network
public void LeaveNetwork(string nwid)
{
zt_leave_network(nwid);
}
// Creates a ZeroTier Socket and binds on the port provided
// A client instance can now connect to this "host"
public int AddHost(int port)
{
int sockfd = zt_socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified);
if (sockfd < 0) {
return -1;
}
GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr("0.0.0.0" + ":" + port);
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
int addrlen = Marshal.SizeOf (pSockAddr);
return zt_bind (sockfd, pSockAddr, addrlen);
}
// hostId - host socket ID for this connection
public int Connect(int hostId, string address, int port, out byte error)
{
int sockfd = zt_socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified);
Debug.Log ("sockfd = " + sockfd);
if (sockfd < 0) {
error = (byte)sockfd;
return -1;
}
GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr(address + ":" + port);
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
int addrlen = Marshal.SizeOf (pSockAddr);
error = (byte)zt_connect (sockfd, pSockAddr, addrlen);
return sockfd;
}
// Returns whether the ZeroTier service is currently running
public bool IsRunning()
{
return zt_is_running ();
}
// Terminates the ZeroTier service
public void Terminate()
{
zt_terminate ();
}
// Shutdown a given connection
public int Disconnect(int fd)
{
return zt_close (fd);
}
// Sends data out over the network
public int Send(int fd, char[] buf, int len, out byte error)
{
int bytes_written = 0;
error = 0;
GCHandle buf_handle = GCHandle.Alloc (buf, GCHandleType.Pinned);
IntPtr pBufPtr = buf_handle.AddrOfPinnedObject ();
//int len = Marshal.SizeOf (pBufPtr);
if((bytes_written = zt_send (fd, pBufPtr, len)) < 0) {
error = (byte)bytes_written;
}
return bytes_written;
}
// Structure used to house arrays meant to be sent to unmanaged memory and passed to the
// ZeroTier service
public struct UnityArrayInput
{
public IntPtr array;
}
/*
// Sends data out over the network
public int Send(int fd, char[] buf, int len, out byte error)
{
//char[] buffer = new char[1024];
UnityArrayInput data = new UnityArrayInput ();
data.array = Marshal.AllocHGlobal (Marshal.SizeOf (typeof(char))*buf.Length);
//data.len = buf.Length;
int bytes_written = 0;
error = 0;
try
{
Marshal.Copy(buf, 0, data.array, buf.Length);
Debug.Log(buf.Length);
// ZT API call
if((bytes_written = zt_send (fd, data.array, buf.Length)) < 0) {
error = (byte)bytes_written;
}
return bytes_written;
}
finally
{
Marshal.FreeHGlobal (data.array);
}
return 0;
}
*/
// Checks for data to RX
public int OnReceive(int fd, byte[] buf, int len)
{
return 0;
//return zt_read(fd, buf, len);
}
}

View File

@@ -0,0 +1,55 @@
using UnityEngine;
using System.Collections;
using System.Net;
using System;
using System.Globalization;
using System.Runtime.InteropServices;
public class ZeroTierUtils {
// Handles IPv4 and IPv6 notation.
public static IPEndPoint CreateIPEndPoint(string endPoint)
{
string[] ep = endPoint.Split(':');
if (ep.Length < 2) throw new FormatException("Invalid endpoint format");
IPAddress ip;
if (ep.Length > 2) {
if (!IPAddress.TryParse(string.Join(":", ep, 0, ep.Length - 1), out ip)) {
throw new FormatException("Invalid ip-adress");
}
}
else {
if (!IPAddress.TryParse(ep[0], out ip)) {
throw new FormatException("Invalid ip-adress");
}
}
int port;
if (!int.TryParse(ep[ep.Length - 1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port)) {
throw new FormatException("Invalid port");
}
return new IPEndPoint(ip, port);
}
// Generates an unmanaged sockaddr structure from a string-formatted endpoint
public static GCHandle Generate_unmananged_sockaddr(string endpoint_str)
{
IPEndPoint ipEndPoint;
ipEndPoint = ZeroTierUtils.CreateIPEndPoint (endpoint_str);
SocketAddress socketAddress = ipEndPoint.Serialize ();
// use an array of bytes instead of the sockaddr structure
byte[] sockAddrStructureBytes = new byte[socketAddress.Size];
GCHandle sockAddrHandle = GCHandle.Alloc (sockAddrStructureBytes, GCHandleType.Pinned);
for (int i = 0; i < socketAddress.Size; ++i) {
sockAddrStructureBytes [i] = socketAddress [i];
}
return sockAddrHandle;
}
public static GCHandle Generate_unmanaged_buffer(byte[] buf)
{
// use an array of bytes instead of the sockaddr structure
GCHandle sockAddrHandle = GCHandle.Alloc (buf, GCHandleType.Pinned);
return sockAddrHandle;
}
}

View File

@@ -0,0 +1 @@
-unsafe

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB