// Returns whether the command sent successfully or not.
        //  In Parameters: 1
        //   string settingPath
        public override bool SendCommand(Stream ns, Dictionary <string, object> parameters = null)
        {
            // Validate that parameters is valid.
            if (parameters == null)
            {
                throw new CommandException("DownloadSettingRequestCommand", true, CommandException.Reason.MissingParameter);
            }

            string settingPath = null;

            // Load parameters into local variable
            try
            {
                settingPath = (string)parameters["settingPath"];
            } catch (KeyNotFoundException) {
                throw new CommandException("DownloadSettingRequestCommand", true, CommandException.Reason.MissingParameter);
            } catch (InvalidCastException) {
                throw new CommandException("DownloadSettingRequestCommand", true, CommandException.Reason.InvalidParameterType);
            }

            byte[] settingPathBytes = SerializedString.ToNetworkBytes(settingPath);
            byte[] bufferBytes      = new byte[1 + settingPathBytes.Length];

            bufferBytes[0] = (byte)ControlCommand.Command.DownloadSettingRequest;
            Array.Copy(settingPathBytes, 0, bufferBytes, 1, settingPathBytes.Length);

            try
            {
                ns.Write(bufferBytes, 0, bufferBytes.Length);
            } catch (Exception) {
                return(false);
            }

            return(true);
        }
Example #2
0
        // Returns whether the command sent successfully or not.
        //  In Parameters: 1
        //   AvailableOverlaySetting selectedSetting
        public override bool SendCommand(Stream ns, Dictionary <string, object> parameters = null)
        {
            // Validate that parameters is valid.
            if (parameters == null)
            {
                throw new CommandException("OverlaySettingSelect", true, CommandException.Reason.MissingParameter);
            }

            AvailableOverlaySetting selectedSetting = null;

            // Load parameters into local variable
            try
            {
                selectedSetting = (AvailableOverlaySetting)parameters["selectedSetting"];
            } catch (KeyNotFoundException) {
                throw new CommandException("OverlaySettingSelect", true, CommandException.Reason.MissingParameter);
            } catch (InvalidCastException) {
                throw new CommandException("OverlaySettingSelect", true, CommandException.Reason.InvalidParameterType);
            }


            byte[] serverRemoteByte = BitConverter.GetBytes(selectedSetting.Local);
            byte[] settingPath      = SerializedString.ToNetworkBytes(selectedSetting.Path);

            byte[] bufferBytes = new byte[1 + 1 + settingPath.Length];
            bufferBytes[0] = (byte)ControlCommand.Command.OverlaySettingSelect;
            bufferBytes[1] = serverRemoteByte[0];
            Array.Copy(settingPath, 0, bufferBytes, 2, settingPath.Length);

            ns.Write(bufferBytes, 0, bufferBytes.Length);

            return(true);
        }
Example #3
0
        // Returns CommandResult (see below) that holds success/failure and implementation specific data.
        //  Return Data: 1
        //   List<AvailableOverlaySetting> remoteSettings
        public override CommandResult HandleCommand(Stream ns)
        {
            Dictionary <string, object>    resultData     = new Dictionary <string, object>();
            List <AvailableOverlaySetting> remoteSettings = new List <AvailableOverlaySetting>();

            try
            {
                // Read length of the list
                byte[] settingsListSizeBytes = new byte[4];
                StreamHelper.ForceReadAll(ns, settingsListSizeBytes, 0, 4);
                int settingsListSize = BitConverter.ToInt32(settingsListSizeBytes, 0);

                // For each element, read name and path and create an AvailableOverlaySetting for each
                for (int x = 0; x < settingsListSize; ++x)
                {
                    AvailableOverlaySetting aos = new AvailableOverlaySetting();

                    aos.Name  = SerializedString.FromNetworkBytes(ns);
                    aos.Path  = SerializedString.FromNetworkBytes(ns);
                    aos.Local = false;
                    //aos.IsCurrent = (OverlaySettings.Instance.Location.FullName == Path.Combine(OverlaySettings.OverlaysTempBasePath.FullName, aos.Path));

                    remoteSettings.Add(aos);
                }
            } catch (Exception) {
                return(new CommandResult(false));
            }


            resultData["remoteSettings"] = remoteSettings;
            return(new CommandResult(true, resultData));
        }
        // Returns CommandResult (see below) that holds success/failure and implementation specific data.
        //  Return Data: 1
        //   string settingPath
        public override CommandResult HandleCommand(Stream ns)
        {
            Dictionary <string, object> returnData = new Dictionary <string, object>();

            returnData["settingPath"] = SerializedString.FromNetworkBytes(ns);

            return(new CommandResult(true, returnData));
        }
Example #5
0
        private void SendDirectory(DirectoryInfo currentDirectory, Stream ns)
        {
            // Send bytes of Directory name
            byte[] directoryNameBytes = SerializedString.ToNetworkBytes(currentDirectory.Name);
            ns.Write(directoryNameBytes, 0, directoryNameBytes.Length);


            FileInfo[]      files       = currentDirectory.GetFiles();
            DirectoryInfo[] directories = currentDirectory.GetDirectories();

            int filesCount       = files.Length;
            int directoriesCount = directories.Length;

            // Send bytes of filesCount and directoriesCount
            ns.Write(BitConverter.GetBytes(filesCount), 0, 4);
            ns.Write(BitConverter.GetBytes(directoriesCount), 0, 4);

            for (int x = 0; x < filesCount; ++x)
            {
                FileStream fs = files[x].OpenRead();

                // Send bytes of file name
                byte[] fileNameBytes = SerializedString.ToNetworkBytes(files[x].Name);
                ns.Write(fileNameBytes, 0, fileNameBytes.Length);

                // Send bytes of file length (long = Int64)
                ns.Write(BitConverter.GetBytes(fs.Length), 0, 8);

                int    bytesRead = 0;
                byte[] fileBytes = new byte[8192];

                do
                {
                    // Read in 8kB chunks
                    bytesRead = fs.Read(fileBytes, 0, 8192);

                    // Send in 8kB chunks
                    if (bytesRead > 0)
                    {
                        ns.Write(fileBytes, 0, bytesRead);
                    }
                } while (bytesRead != 0);

                fs.Close();
            }

            for (int x = 0; x < directoriesCount; ++x)
            {
                SendDirectory(directories[x], ns);
            }
        }
Example #6
0
        // Returns CommandResult (see below) that holds success/failure and implementation specific data.
        //  Return Data: 2
        //   bool remote
        //   string selectedPath
        public override CommandResult HandleCommand(Stream ns)
        {
            Dictionary <string, object> commandData = new Dictionary <string, object>();

            // Read serverRemoteByte
            byte[] serverRemoteByte = new byte[1];
            StreamHelper.ForceReadAll(ns, serverRemoteByte, 0, 1);
            commandData["remote"] = BitConverter.ToBoolean(serverRemoteByte, 0);

            // Read selectedPath
            commandData["selectedPath"] = SerializedString.FromNetworkBytes(ns);

            return(new CommandResult(true, commandData));
        }
Example #7
0
        // Returns whether the command sent successfully or not.
        //  In Parameters: 2
        //      string variableName - Name of updated variable
        //      string variableValue - Value of updated variable
        public override bool SendCommand(Stream ns, Dictionary <string, object> parameters = null)
        {
            if (parameters == null)
            {
                throw new CommandException("UpdateVariableCommand", true, CommandException.Reason.MissingParameter);
            }

            string variableName  = null;
            string variableValue = null;

            try
            {
                variableName  = (string)parameters["variableName"];
                variableValue = (string)parameters["variableValue"];
            } catch (KeyNotFoundException) {
                throw new CommandException("UpdateVariableCommand", true, CommandException.Reason.MissingParameter);
            } catch (InvalidCastException) {
                throw new CommandException("UpdateVariableCommand", true, CommandException.Reason.InvalidParameterType);
            }

            // Don't resend what they just told us if it was on the same stream
            if (ns == lastStream && lastVariableName != null && lastVariableValue != null &&
                lastVariableName == variableName && lastVariableValue == variableValue)
            {
                return(false);
            }

            byte[] nameBytes  = SerializedString.ToNetworkBytes(variableName);
            byte[] valueBytes = SerializedString.ToNetworkBytes(variableValue);

            byte[] bufferBytes = new byte[1 + nameBytes.Length + valueBytes.Length];

            bufferBytes[0] = (byte)ControlCommand.Command.UpdateVariable;
            Array.Copy(nameBytes, 0, bufferBytes, 1, nameBytes.Length);
            Array.Copy(valueBytes, 0, bufferBytes, 1 + nameBytes.Length, valueBytes.Length);

            try
            {
                ns.Write(bufferBytes, 0, bufferBytes.Length);
            } catch (Exception) {
                return(false);
            }

            lastStream        = ns;
            lastVariableName  = variableName;
            lastVariableValue = variableValue;

            return(true);
        }
Example #8
0
        // Returns CommandResult (see below) that holds success/failure and implementation specific data.
        //  Return Data: 2
        //      string variableName - Name of updated variable
        //      string variableValue - Value of updated variable
        public override CommandResult HandleCommand(Stream ns)
        {
            Dictionary <string, object> resultData = new Dictionary <string, object>();

            string variableName  = SerializedString.FromNetworkBytes(ns);
            string variableValue = SerializedString.FromNetworkBytes(ns);

            resultData["variableName"]  = variableName;
            resultData["variableValue"] = variableValue;

            lastStream        = ns;
            lastVariableName  = variableName;
            lastVariableValue = variableValue;

            return(new CommandResult(true, resultData));
        }
Example #9
0
        public override void OnGUI(Rect rect, SerializedProperty serProp, GUIContent label)
        {
            if (serProp.propertyType != SerializedPropertyType.String)
            {
                EditorGUI.LabelField(EditorGUI.IndentedRect(rect), "Use PoolKey attribute with string");
                return;
            }
            if (m_IPoolKeysList.Count < 1)
            {
                m_IPoolKeysList = Object.FindObjectsOfType <MonoBehaviour>().OfType <IPoolKeys>().ToList();
            }

            int indentLevel = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;
            Rect label1, field1;

            GetRectsPair(rect, indentLevel, out label1, out field1);

            //if (m_IPoolKeysList.Count < 1)
            //    EditorGUI.LabelField(EditorGUI.IndentedRect(rect), "There is no PoolKeys");
            SerializedString serString = new SerializedString(serProp);
            string           curKey    = serString.String;
            List <string>    keyList   = new List <string>();

            keyList.Add("[none]");
            foreach (var poolKeys in m_IPoolKeysList)
            {
                keyList.AddRange(poolKeys.Keys);
            }
            keyList = keyList.Distinct().Where(o => !string.IsNullOrEmpty(o)).ToList();
            int index = string.IsNullOrEmpty(curKey) ? 0 : -1;
            int count = keyList.Count;

            for (int i = 1; i < count; ++i)
            {
                if (keyList[i] == curKey)
                {
                    index = i;
                    break;
                }
            }
            if (index < 0)
            {
                keyList.Add(curKey);
                index = keyList.Count - 1;
            }
            if (m_IPoolKeysList.Count > 0)
            {
                EditorGUI.LabelField(label1, "Pool key");
            }
            else
            {
                GUIStyle warningStyle = new GUIStyle(GUI.skin.label);
                warningStyle.fontStyle        = FontStyle.Bold;
                warningStyle.normal.textColor = Color.red;
                EditorGUI.LabelField(label1, "No pools in scene!", warningStyle);
            }
            int newIndex = EditorGUI.Popup(field1, index, keyList.ToArray());

            if (newIndex != index)
            {
                if (newIndex == 0)
                {
                    serString.String = string.Empty;
                }
                else
                {
                    serString.String = keyList[newIndex];
                }
            }
            EditorGUI.indentLevel = indentLevel;
        }
Example #10
0
        // Returns whether the command sent successfully or not.
        //  In Parameters: 1
        //   List<AvailableOverlaySetting> availableSettings
        public override bool SendCommand(Stream ns, Dictionary <string, object> parameters = null)
        {
            // Validate that parameters is valid.
            if (parameters == null)
            {
                throw new CommandException("AvailableSettingsResponseCommand", true, CommandException.Reason.MissingParameter);
            }

            List <AvailableOverlaySetting> availableSettings = null;

            // Load parameters into local variable
            try
            {
                availableSettings = (List <AvailableOverlaySetting>)parameters["availableSettings"];
            } catch (KeyNotFoundException) {
                throw new CommandException("AvailableSettingsResponseCommand", true, CommandException.Reason.MissingParameter);
            } catch (InvalidCastException) {
                throw new CommandException("AvailableSettingsResponseCommand", true, CommandException.Reason.InvalidParameterType);
            }

            // List size
            byte[] settingsListSize = new byte[4];
            settingsListSize = BitConverter.GetBytes(availableSettings.Count);

            // Bytes for each of the availableSettings
            byte[][] settingsList       = new byte[availableSettings.Count][];
            int      totalListBytesSize = 0;

            // Serialize each AvailableOverlaySetting to its bytes
            for (int x = 0; x < availableSettings.Count; ++x)
            {
                AvailableOverlaySetting aos = availableSettings[x];
                byte[] serializedName       = SerializedString.ToNetworkBytes(aos.Name);
                byte[] serializedPath       = SerializedString.ToNetworkBytes(aos.Path);

                settingsList[x] = new byte[serializedName.Length + serializedPath.Length];
                Array.Copy(serializedName, 0, settingsList[x], 0, serializedName.Length);
                Array.Copy(serializedPath, 0, settingsList[x], serializedName.Length, serializedPath.Length);

                // Keep track of the total length of the bytes put together are.
                totalListBytesSize += settingsList[x].Length;
            }

            // Create final buffer
            byte[] bufferBytes = new byte[1 + 4 + totalListBytesSize];

            bufferBytes[0] = (byte)ControlCommand.Command.AvailableSettingsResponse;
            Array.Copy(settingsListSize, 0, bufferBytes, 1, 4);

            // Concatenate all the AvailableOverlaySetting bytes together
            int curOffset = 5;

            foreach (byte[] setting in settingsList)
            {
                Array.Copy(setting, 0, bufferBytes, curOffset, setting.Length);
                curOffset += setting.Length;
            }

            // Write out the bytes to the network socket
            try
            {
                ns.Write(bufferBytes, 0, bufferBytes.Length);
            } catch (Exception) {
                return(false);
            }

            return(true);
        }
        public override void OnGUI(Rect rect, SerializedProperty serProp, GUIContent label)
        {
            if (serProp.propertyType != SerializedPropertyType.String)
            {
                EditorGUI.LabelField(EditorGUI.IndentedRect(rect), "Use ResourcesPath attribute with string");
                return;
            }


            int indentLevel = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;

            Rect rect1 = rect;

            rect1.yMax -= LineHeight + LineSpacing;
            Rect rect2 = rect;

            rect2.yMin += LineHeight + LineSpacing;
            Rect label1, field1;
            Rect label2, field2;

            GetRectsPair(rect1, indentLevel, out label1, out field1);
            GetRectsPair(rect2, indentLevel, out label2, out field2);
            GUI.Box(EditorGUI.IndentedRect(new Rect()
            {
                xMin = field1.xMin, xMax = field2.xMax, yMin = field1.yMin, yMax = field2.yMax
            }), "");

            SerializedString pathProp = new SerializedString(serProp);
            Object           asset    = null;
            bool             editable = false;

            if (!string.IsNullOrEmpty(pathProp.String))
            {
                asset = Resources.Load(pathProp.String);
                if (asset == null)
                {
                    GUIStyle warningStyle = new GUIStyle(GUI.skin.label);
                    warningStyle.fontStyle        = FontStyle.Bold;
                    warningStyle.normal.textColor = Color.red;
                    warningStyle.alignment        = TextAnchor.MiddleRight;
                    EditorGUI.LabelField(label2, "Invalid path!", warningStyle);
                    editable = true;
                }
            }

            EditorGUI.LabelField(label1, "Resources path");
            Object obj = EditorGUI.ObjectField(field1, asset, typeof(Object), false);

            if (obj != asset)
            {
                asset           = obj;
                pathProp.String = GetResourcesPath(asset);
            }
            EditorGUI.BeginDisabledGroup(!editable);
            pathProp.String = EditorGUI.TextField(field2, pathProp.String);
            EditorGUI.EndDisabledGroup();

            EditorGUI.indentLevel = indentLevel;
        }
Example #12
0
        private DirectoryInfo ReceiveDirectory(Stream ns, DirectoryInfo parentDirectory)
        {
            // Create subdirectory, remove all \, /, and ..'s to preserve security
            string directoryName = SerializedString.FromNetworkBytes(ns).Replace("\\", "").Replace("/", "").Replace("..", "");


            DirectoryInfo receivedDirectory = parentDirectory.CreateSubdirectory(directoryName);

            byte[] fileCountBytes      = new byte[4];
            byte[] directoryCountBytes = new byte[4];

            StreamHelper.ForceReadAll(ns, fileCountBytes, 0, 4);
            StreamHelper.ForceReadAll(ns, directoryCountBytes, 0, 4);

            int fileCount      = BitConverter.ToInt32(fileCountBytes, 0);
            int directoryCount = BitConverter.ToInt32(directoryCountBytes, 0);

            // Receive files
            for (int x = 0; x < fileCount; ++x)
            {
                string   fileName     = SerializedString.FromNetworkBytes(ns).Replace("\\", "").Replace("/", "");
                FileInfo receivedFile = new FileInfo(Path.Combine(receivedDirectory.FullName, fileName));

                byte[] fileSizeBytes = new byte[8];
                StreamHelper.ForceReadAll(ns, fileSizeBytes, 0, 8);

                long fileSize = BitConverter.ToInt64(fileSizeBytes, 0);

                // If it exists, try to delete and start new
                try
                {
                    if (receivedFile.Exists)
                    {
                        receivedFile.Delete();
                    }
                } catch (Exception) {
                }

                FileStream fs = null;
                try
                {
                    fs = receivedFile.OpenWrite();
                } catch (Exception) {
                    // Unable to open file for write
                }

                long remainingBytes = fileSize;

                byte[] fileBytes = new byte[8192];

                while (remainingBytes > 0)
                {
                    // Read from network and write to file
                    int chunkSize = (remainingBytes > 8192 ? 8192 : (int)remainingBytes);
                    StreamHelper.ForceReadAll(ns, fileBytes, 0, chunkSize);
                    if (fs != null)
                    {
                        fs.Write(fileBytes, 0, chunkSize);
                    }

                    // Subtract out bytes just received
                    remainingBytes -= chunkSize;
                }

                if (fs != null)
                {
                    fs.Close();
                }
            }

            // Receive directory
            for (int x = 0; x < directoryCount; ++x)
            {
                ReceiveDirectory(ns, receivedDirectory);
            }

            return(receivedDirectory);
        }
Example #13
0
        public override void OnGUI(Rect rect, SerializedProperty serProp, GUIContent label)
        {
            if (serProp.propertyType != SerializedPropertyType.String)
            {
                EditorGUI.LabelField(EditorGUI.IndentedRect(rect), "Use ResourcesPath attribute with string");
                return;
            }


            int indentLevel = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;

            Rect rect1 = rect;

            rect1.yMax -= LineHeight + LineSpacing;
            Rect rect2 = rect;

            rect2.yMin += LineHeight + LineSpacing;
            Rect label1, field1;
            Rect label2, field2;

            GetRectsPair(rect1, indentLevel, out label1, out field1);
            GetRectsPair(rect2, indentLevel, out label2, out field2);
            GUI.Box(EditorGUI.IndentedRect(new Rect()
            {
                xMin = field1.xMin, xMax = field2.xMax, yMin = field1.yMin, yMax = field2.yMax
            }), "");

            SerializedString pathProp = new SerializedString(serProp);
            Object           asset    = null;
            bool             invalid  = false;

            if (!string.IsNullOrEmpty(pathProp.String))
            {
                asset = Resources.Load(pathProp.String);
                if (asset == null)
                {
                    invalid = true;
                }
            }

            EditorGUI.LabelField(label1, label);
            if (invalid)
            {
                EditorGUI.LabelField(label2, "Invalid path!", m_WarningStyle);
            }
            else
            {
                EditorGUI.LabelField(label2, "Resource path", m_LabelStyle);
            }

            Object obj = EditorGUI.ObjectField(field1, asset, typeof(Object), false);

            if (obj != asset)
            {
                asset           = obj;
                pathProp.String = GetResourcesPath(asset);
            }
            EditorGUI.BeginDisabledGroup(!invalid);
            if (invalid)
            {
                pathProp.String = EditorGUI.TextField(field2, pathProp.String, m_InvalidPathTextFieldStyle);
            }
            else
            {
                pathProp.String = EditorGUI.TextField(field2, pathProp.String);
            }
            EditorGUI.EndDisabledGroup();

            EditorGUI.indentLevel = indentLevel;
        }