Example #1
0
        /// <summary>
        /// Rubyオブジェクトを.netで扱う値へ復号する。
        /// </summary>
        /// <param name="data">Marshalから取得したRubyのオブジェクト。</param>
        /// <param name="mode">データベースモード。</param>
        /// <returns>Rubyオブジェクト復号結果のオブジェクト。</returns>
        public static dynamic DecodeRubyObject(
            dynamic data,
            DatabaseMode? mode = null)
        {
            if (data is BigInteger)
            {
                return (int)data;
            }

            if (data == null) return null;

            if (data is RubyArray)
            {
                var ret = new List<dynamic>();
                var array = (RubyArray)data;

                for (int i = 0; i < array.Count; i++)
                {
                    var item = array[i];

                    ret.Add(DecodeRubyObject(item, mode));
                }

                return ret;
            }
            else if (data is int)
            {
                return data;
            }
            else if (data is float)
            {
                return data;
            }
            else if (data is double)
            {
                return data;
            }
            else if (data is bool)
            {
                return data;
            }
            else if (data is MutableString)
            {
                MutableString ms = (MutableString)data;

                if (mode != DatabaseMode.Script)
                {
                    return Encoding.UTF8.GetString(ms.ToByteArray());
                }
                else
                {
                    return ms.ToByteArray();
                }
            }
            else if (data is Hash)
            {
                var hash = (Hash)data;
                var ret = new Dictionary<dynamic, dynamic>();

                foreach (object key in hash.Keys)
                {
                    ret.Add(
                        DecodeRubyObject(key, mode),
                        DecodeRubyObject(hash[key], mode)
                    );
                }

                return ret;
            }
            else if (data is RubyObject)
            {
                RubyObject rObj = (RubyObject)data;
                var ret = new DatabaseClassData()
                {
                    ClassName = rObj.ImmediateClass.GetNonSingletonClass().Name
                };

                var id = rObj.GetInstanceData();
                Dictionary<string, object> variables = (Dictionary<string, object>)id.GetType().GetField(
                    "_instanceVars",
                    BindingFlags.Instance |

                    BindingFlags.NonPublic
                ).GetValue(id);

                foreach (string key in variables.Keys)
                {
                    ret.Variables.Add(key.Substring(1), DecodeRubyObject(variables[key], mode));
                }

                return ret;
            }
            else
            {
                return data;
            }
        }
        /// <summary>
        /// 項目エディタにデータをセットする。
        /// </summary>
        /// <param name="data">設定対象のデータ。</param>
        private void LoadEditorValues(DatabaseClassData data)
        {
            SuspendChange = true;
            try
            {
                foreach (var accessor in ClassDefinition.GetAllAccessors(Project))
                {
                    if (!ItemEditors.ContainsKey(accessor.Name)) continue;
                    var accessorName = String.Format("{0}", accessor.Name);
                    var itemEditor = ItemEditors[accessor.Name];

                    if (itemEditor is TextBox)
                    {
                        var castedEditor = (TextBox)itemEditor;

                        try
                        {
                            if (data.Variables.ContainsKey(accessorName))
                            {
                                castedEditor.Text = data.Variables[accessorName];
                            }
                            else
                            {
                                castedEditor.Text = "";
                            }
                        }
                        catch
                        {
                            castedEditor.Text = "";
                        }
                    }
                    else if (itemEditor is NumericUpDown)
                    {
                        var castedEditor = (NumericUpDown)itemEditor;

                        try
                        {
                            if (data.Variables.ContainsKey(accessorName))
                            {
                                castedEditor.Value = (decimal)data.Variables[accessorName];
                            }
                            else
                            {
                                castedEditor.Value = 0;
                            }
                        }
                        catch
                        {
                            castedEditor.Value = 0;
                        }
                    }
                    else if (itemEditor is CheckBox)
                    {
                        var castedEditor = (CheckBox)itemEditor;

                        try
                        {
                            if (data.Variables.ContainsKey(accessorName))
                            {
                                castedEditor.Checked = data.Variables[accessorName];
                            }
                            else
                            {
                                castedEditor.Checked = false;
                            }
                        }
                        catch
                        {
                            castedEditor.Checked = false;
                        }
                    }
                    else if (itemEditor is ComboBox)
                    {
                        var castedEditor = (ComboBox)itemEditor;

                        try
                        {
                            if (data.Variables.ContainsKey(accessorName))
                            {
                                castedEditor.SelectedItem = (
                                    from ListItem i in castedEditor.Items
                                    where i.Value == data.Variables[accessorName]
                                    select i
                                ).FirstOrDefault();

                                if (castedEditor.SelectedIndex < 0)
                                {
                                    castedEditor.SelectedIndex = 0;
                                }
                            }
                            else
                            {
                                castedEditor.SelectedIndex = 0;
                            }
                        }
                        catch
                        {
                            castedEditor.SelectedIndex = 0;
                        }
                    }
                    else if (itemEditor is ArrayValueBox)
                    {
                        var castedEditor = (ArrayValueBox)itemEditor;

                        try
                        {
                            if (data.Variables.ContainsKey(accessorName))
                            {
                                castedEditor.Values = data.Variables[accessorName];
                            }
                            else
                            {
                                castedEditor.Values = new List<dynamic>();
                            }
                        }
                        catch
                        {
                            castedEditor.Values = new List<dynamic>();
                        }
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
            }
            finally
            {
                SuspendChange = false;
            }
        }
        /// <summary>
        /// 項目エディタの内容をデータに反映する。
        /// </summary>
        /// <param name="data">設定対象のデータ。</param>
        private void SaveEditorValues(DatabaseClassData data)
        {
            foreach (var accessor in ClassDefinition.GetAllAccessors(Project))
            {
                if (!ItemEditors.ContainsKey(accessor.Name)) continue;
                var accessorName = String.Format("{0}", accessor.Name);
                var itemEditor = ItemEditors[accessor.Name];

                if (itemEditor is TextBox)
                {
                    var castedEditor = (TextBox)itemEditor;

                    data.Variables[accessorName] = castedEditor.Text;
                }
                else if (itemEditor is NumericUpDown)
                {
                    var castedEditor = (NumericUpDown)itemEditor;

                    switch (accessor.ValueType)
                    {
                        case "*Integer":
                            data.Variables[accessorName] = (int)castedEditor.Value;

                            break;
                        case "*Float":
                            data.Variables[accessorName] = (float)castedEditor.Value;

                            break;
                    }
                }
                else if (itemEditor is CheckBox)
                {
                    var castedEditor = (CheckBox)itemEditor;

                    data.Variables[accessorName] = castedEditor.Checked;
                }
                else if (itemEditor is ComboBox)
                {
                    var castedEditor = (ComboBox)itemEditor;

                    data.Variables[accessorName] = castedEditor.SelectedValue;
                }
                else if (itemEditor is ArrayValueBox)
                {
                    var castedEditor = (ArrayValueBox)itemEditor;

                    data.Variables[accessorName] = castedEditor.Values;
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
        }
        /// <summary>
        /// クラスデータを生成する。
        /// </summary>
        /// <param name="classDefinition">クラス定義のインスタンス。</param>
        /// <returns>初期値が設定されたクラスデータ。</returns>
        protected DatabaseClassData CreateClassData(ClassDefinition classDefinition)
        {
            if (classDefinition == null)
            {
                return null;
            }

            var ret = new DatabaseClassData()
            {
                ClassName = classDefinition.FullName
            };

            foreach (var accessor in classDefinition.Accessors)
            {
                dynamic value = null;

                if (!accessor.ArrayValue)
                {
                    switch (accessor.ValueType)
                    {
                        case "*String":
                        case "*MultiLineString":
                            value = "";

                            break;
                        case "*Integer":
                            value = 0;

                            break;
                        case "*Float":
                            value = 0F;

                            break;
                        case "*Boolean":
                            value = false;

                            break;
                        default:
                            value = 0;

                            break;
                    }
                }
                else
                {
                    value = new List<dynamic>();
                }

                ret.Variables.Add(String.Format("{0}", accessor.Name), value);
            }

            return ret;
        }