public static void RemoveSchema(string key, bool deleteItems = true)
        {
            if (deleteItems)
            {
                // Delete the items with this schema
                List <string> itemList;
                if (ItemListBySchema.TryGetValue(key, out itemList))
                {
                    List <string> itemListCopy = new List <string>(itemList);
                    foreach (string itemKey in itemListCopy)
                    {
                        RemoveItem(itemKey);
                    }
                }
            }
            ItemListBySchema.Remove(key);

            // Remove all the fields so the lookup lists get updated
            List <string> allFields = GetAllFieldKeys(key, AllSchemas);

            foreach (string field in allFields)
            {
                RemoveFieldFromSchema(key, field, deleteItems);
            }

            AllSchemas.Remove(key);
            SchemasNeedSave = true;

            SchemaKeyArray       = BuildSchemaKeyArray();
            FilterSchemaKeyArray = BuildSchemaFilterKeyArray();
        }
        public static void RemoveFieldFromSchema(string schemaKey, string deletedFieldKey, bool deleteFromItem = true)
        {
            Dictionary <string, object> schemaData;

            if (AllSchemas.TryGetValue(schemaKey, out schemaData))
            {
                RemoveFieldFromSchema(schemaKey, schemaData, deletedFieldKey, deleteFromItem);
            }
        }
        public static void ClearAll()
        {
            AllItems.Clear();

            AllSchemas.Clear();
            ListByFieldName.Clear();
            ItemListBySchema.Clear();
            FilterSchemaKeyArray = null;
            SchemaKeyArray       = null;

            ItemsNeedSave   = true;
            SchemasNeedSave = true;
        }
        private static bool SchemaExistsForItem(string itemKey, Dictionary <string, object> itemData)
        {
            bool   result = false;
            object schemaType;

            if (itemData.TryGetValue(GDEConstants.SchemaKey, out schemaType))
            {
                string schemaKey = schemaType as string;
                if (AllSchemas.ContainsKey(schemaKey))
                {
                    result = true;
                }
            }

            return(result);
        }
        public static bool IsSchemaNameValid(string name, out string error)
        {
            bool result = true;

            error = "";

            if (AllSchemas.ContainsKey(name))
            {
                error  = "Schema name already exists.";
                result = false;
            }
            else if (!GDEValidateIdentifier.IsValidIdentifier(name))
            {
                error  = "Schema name is invalid.";
                result = false;
            }

            return(result);
        }
        public static bool IsFieldNameValid(string schemaKey, string fieldName, out string error)
        {
            bool result = true;

            error = "";

            Dictionary <string, object> data;

            if (AllSchemas.TryGetValue(schemaKey, out data))
            {
                result = IsFieldNameValid(data, fieldName, out error);
            }
            else
            {
                result = false;
                error  = "Error reading item data.";
            }

            return(result);
        }
        public static bool AddSchema(string name, Dictionary <string, object> data, out string error, bool rebuildArrays = true)
        {
            bool result = true;

            if (IsSchemaNameValid(name, out error))
            {
                result = AllSchemas.TryAddValue(name, data);
            }
            else
            {
                result = false;
            }

            if (result)
            {
                BuildSortingAndLookupListFor(name, data, rebuildArrays);
                SchemasNeedSave = true;
            }

            return(result);
        }
        private static void LoadSchemas()
        {
            try
            {
                string json = File.ReadAllText(DataFilePath);
                _dataFileMD5 = json.Md5Sum();

                Dictionary <string, object> data = Json.Deserialize(json) as Dictionary <string, object>;

                // Clear all schema related lists
                AllSchemas.Clear();
                ListByFieldName.Clear();
                ItemListBySchema.Clear();
                FilterSchemaKeyArray = null;
                SchemaKeyArray       = null;

                string error;
                string schemaName;
                foreach (KeyValuePair <string, object> pair in data)
                {
                    if (!pair.Key.StartsWith(GDEConstants.SchemaPrefix))
                    {
                        continue;
                    }

                    Dictionary <string, object> schemaData = pair.Value as Dictionary <string, object>;
                    schemaName = pair.Key.Replace(GDEConstants.SchemaPrefix, "");
                    AddSchema(schemaName, schemaData, out error, false);
                }

                SchemaKeyArray       = BuildSchemaKeyArray();
                FilterSchemaKeyArray = BuildSchemaFilterKeyArray();

                SchemasNeedSave = false;
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
        }
        public static bool RenameSchema(string oldSchemaKey, string newSchemaKey, out string error)
        {
            bool result = true;

            if (IsSchemaNameValid(newSchemaKey, out error))
            {
                Dictionary <string, object> schemaData;
                if (AllSchemas.TryGetValue(oldSchemaKey, out schemaData))
                {
                    List <string> itemsWithSchema = GetItemsOfSchemaType(oldSchemaKey);
                    Dictionary <string, object> schemaDataCopy = schemaData.DeepCopy();

                    // First remove the schema from the dictionary
                    RemoveSchema(oldSchemaKey, false);

                    // Then add the schema data under the new schema key
                    if (AddSchema(newSchemaKey, schemaDataCopy, out error))
                    {
                        List <string> itemBySchemaList;
                        ItemListBySchema.TryGetValue(newSchemaKey, out itemBySchemaList);

                        // Update the schema key on any existing items
                        foreach (string itemKey in itemsWithSchema)
                        {
                            Dictionary <string, object> itemData;
                            if (AllItems.TryGetValue(itemKey, out itemData))
                            {
                                itemData.TryAddOrUpdateValue(GDEConstants.SchemaKey, newSchemaKey);
                            }
                            itemBySchemaList.Add(itemKey);
                        }

                        // Update any custom fields in schemas that had the old schema name
                        foreach (string curSchemaKey in AllSchemas.Keys)
                        {
                            List <string> fieldsOfSchemaType = SchemaFieldKeysOfType(curSchemaKey, oldSchemaKey);
                            fieldsOfSchemaType.AddRange(SchemaListFieldKeysOfType(curSchemaKey, oldSchemaKey));

                            if (fieldsOfSchemaType.Count > 0)
                            {
                                Dictionary <string, object> curSchemaData;
                                AllSchemas.TryGetValue(curSchemaKey, out curSchemaData);

                                if (curSchemaData == null)
                                {
                                    continue;
                                }

                                foreach (string schemaFieldKey in fieldsOfSchemaType)
                                {
                                    curSchemaData.TryAddOrUpdateValue(string.Format(GDEConstants.MetaDataFormat, GDEConstants.TypePrefix, schemaFieldKey), newSchemaKey);
                                }
                            }
                        }

                        // Lastly, update any custom fields that had the old schema name
                        foreach (string curItemKey in AllItems.Keys)
                        {
                            List <string> fieldsOfSchemaType = ItemFieldKeysOfType(curItemKey, oldSchemaKey);
                            fieldsOfSchemaType.AddRange(ItemListFieldKeysOfType(curItemKey, oldSchemaKey));

                            if (fieldsOfSchemaType.Count > 0)
                            {
                                Dictionary <string, object> curItemData;
                                AllItems.TryGetValue(curItemKey, out curItemData);

                                if (curItemData == null)
                                {
                                    continue;
                                }

                                foreach (string itemFieldKey in fieldsOfSchemaType)
                                {
                                    curItemData.TryAddOrUpdateValue(string.Format(GDEConstants.MetaDataFormat, GDEConstants.TypePrefix, itemFieldKey), newSchemaKey);
                                }
                            }
                        }
                    }
                    else
                    {
                        // Add the schema back under the old key if this step failed
                        AddSchema(oldSchemaKey, schemaDataCopy, out error);
                        result = false;
                    }
                }
                else
                {
                    error  = "Failed to read schema data.";
                    result = false;
                }
            }
            else
            {
                result = false;
            }

            SchemasNeedSave |= result;
            ItemsNeedSave   |= result;

            return(result);
        }
示例#10
0
 public bool ExistsSchema(string schemaName)
 {
     return(AllSchemas.ContainsKey(schemaName.ToQuotedIdentifier()));
 }