コード例 #1
0
ファイル: UnityGateway.cs プロジェクト: LBNunes/Avatar
        private void PrepareDeviceAndDrivers()
        {
            UpDevice currentDevice = new UpDevice();

            if (settings.deviceName != null)
            {
                string name = settings.deviceName.Trim();
                if (name.Length > 0)
                    currentDevice.name = name;
            }
            else
                currentDevice.name = Dns.GetHostName();

            if ((currentDevice.name == null) || (currentDevice.name.ToLower() == "localhost"))
                currentDevice.name = SystemInfo.deviceName;
            if ((currentDevice.name == null) || currentDevice.name.ToLower().Contains("unknown"))
                currentDevice.name = System.Environment.UserName;

            currentDevice.AddProperty("platform", "unity-" + Application.platform.ToString().ToLower());

            var networks = new List<UpNetworkInterface>();
            foreach (ChannelManager cm in channelManagers.Values)
            {
                foreach (var host in cm.ListHosts())
                {
                    var nInf = new UpNetworkInterface();
                    nInf.netType = cm.GetNetworkDeviceType();
                    nInf.networkAddress = host;
                    networks.Add(nInf);
                }
            }

            currentDevice.networks = networks;

            driverManager = new DriverManager(settings, this, currentDevice);
            deviceManager = new DeviceManager(settings, this, currentDevice);

            foreach (var driver in settings.drivers)
            {
                System.Type type = null;
                try { type = Util.GetType(driver); }
                catch (System.Exception) { }

                string error = null;
                object instance = null;
                if ((type == null) || (type.GetInterface("UOS.UOSDriver") == null) || (!type.IsClass) || (type.IsAbstract))
                    error = driver + " is not a valid concrete type which implements UOSDriver interface";
                else
                {
                    if (type.IsSubclassOf(typeof(MonoBehaviour)))
                    {
                        object[] components = UnityEngine.Object.FindObjectsOfType(type);
                        if (components.Length == 0)
                            error = "no instance of MonoBehaviour " + driver + " was found in the scene";
                        else if (components.Length > 1)
                            error = "multiple instances of MonoBehaviour " + driver + " were found in the scene";
                        else
                            instance = components[0];
                    }
                    else
                    {
                        try { instance = System.Activator.CreateInstance(type); }
                        catch (System.Reflection.TargetInvocationException e)
                        {
                            error = "constructor exception: " + e.InnerException;
                        }
                        catch (System.Exception)
                        {
                            error = "couldn't instantiate " + driver + " using default constructor";
                        }
                    }
                }

                if (error != null)
                    logger.LogError("Driver initialisation failure: " + error + ".");
                else
                {
                    var driverInstance = (UOSDriver)instance;
                    driverManager.DeployDriver(driverInstance);
                }
            }
            driverManager.InitDrivers();
        }
コード例 #2
0
ファイル: UnityGateway.cs プロジェクト: lhsantos/ubimon
        private void PrepareDeviceAndDrivers()
        {
            UpDevice currentDevice = new UpDevice();

            if (settings.deviceName != null)
            {
                string name = settings.deviceName.Trim();
                if (name.Length > 0)
                {
                    currentDevice.name = name;
                }
            }
            else
            {
                currentDevice.name = Dns.GetHostName();
            }

            if ((currentDevice.name == null) || (currentDevice.name.ToLower() == "localhost"))
            {
                currentDevice.name = SystemInfo.deviceName;
            }
            if ((currentDevice.name == null) || currentDevice.name.ToLower().Contains("unknown"))
            {
                currentDevice.name = System.Environment.UserName;
            }

            currentDevice.AddProperty("platform", "unity-" + Application.platform.ToString().ToLower());

            var networks = new List <UpNetworkInterface>();

            foreach (ChannelManager cm in channelManagers.Values)
            {
                foreach (var host in cm.ListHosts())
                {
                    var nInf = new UpNetworkInterface();
                    nInf.netType        = cm.GetNetworkDeviceType();
                    nInf.networkAddress = host;
                    networks.Add(nInf);
                }
            }

            currentDevice.networks = networks;

            driverManager = new DriverManager(settings, this, currentDevice);
            deviceManager = new DeviceManager(settings, this, currentDevice);

            foreach (var driver in settings.drivers)
            {
                System.Type type = null;
                try { type = Util.GetType(driver); }
                catch (System.Exception) { }

                string error    = null;
                object instance = null;
                if ((type == null) || (type.GetInterface("UOS.UOSDriver") == null) || (!type.IsClass) || (type.IsAbstract))
                {
                    error = driver + " is not a valid concrete type which implements UOSDriver interface";
                }
                else
                {
                    if (type.IsSubclassOf(typeof(MonoBehaviour)))
                    {
                        object[] components = UnityEngine.Object.FindObjectsOfType(type);
                        if (components.Length == 0)
                        {
                            error = "no instance of MonoBehaviour " + driver + " was found in the scene";
                        }
                        else if (components.Length > 1)
                        {
                            error = "multiple instances of MonoBehaviour " + driver + " were found in the scene";
                        }
                        else
                        {
                            instance = components[0];
                        }
                    }
                    else
                    {
                        try { instance = System.Activator.CreateInstance(type); }
                        catch (System.Reflection.TargetInvocationException e)
                        {
                            error = "constructor exception: " + e.InnerException;
                        }
                        catch (System.Exception)
                        {
                            error = "couldn't instantiate " + driver + " using default constructor";
                        }
                    }
                }

                if (error != null)
                {
                    logger.LogError("Driver initialisation failure: " + error + ".");
                }
                else
                {
                    var driverInstance = (UOSDriver)instance;
                    driverManager.DeployDriver(driverInstance);
                }
            }
            driverManager.InitDrivers();
        }