Exemplo n.º 1
0
        void solveByType(JToken json, string codePath, OrderedDictionary <string, string> searchReplace)
        {
            List <string> propertiesToRemove            = new List <string>();
            Dictionary <string, JToken> propertiesToAdd = new Dictionary <string, JToken>();

            if (json is JObject)
            {
                foreach (var entry in (json as JObject))
                {
                    if (entry.Key.EndsWith("byType", System.StringComparison.OrdinalIgnoreCase))
                    {
                        foreach (var byTypeProperty in entry.Value.ToObject <OrderedDictionary <string, JToken> >())
                        {
                            if (WildcardUtil.Match(byTypeProperty.Key, codePath))
                            {
                                JToken typedToken = byTypeProperty.Value;
                                solveByType(typedToken, codePath, searchReplace);
                                propertiesToAdd.Add(entry.Key.Substring(0, entry.Key.Length - "byType".Length), typedToken);
                                break;
                            }
                        }
                        propertiesToRemove.Add(entry.Key);
                    }
                }

                foreach (var property in propertiesToRemove)
                {
                    (json as JObject).Remove(property);
                }

                foreach (var property in propertiesToAdd)
                {
                    (json as JObject)[property.Key] = property.Value;
                }

                foreach (var entry in (json as JObject))
                {
                    solveByType(entry.Value, codePath, searchReplace);
                }
            }
            else if (json.Type == JTokenType.String)
            {
                string value = (string)(json as JValue).Value;
                if (value.Contains("{"))
                {
                    (json as JValue).Value = RegistryObject.FillPlaceHolder(value, searchReplace);
                }
            }
            else if (json is JArray)
            {
                foreach (var child in (json as JArray))
                {
                    solveByType(child, codePath, searchReplace);
                }
            }
        }
Exemplo n.º 2
0
        public static string[] GetCreativeTabs(AssetLocation code, Dictionary <string, string[]> CreativeInventory, OrderedDictionary <string, string> searchReplace)
        {
            List <string> tabs = new List <string>();

            foreach (var val in CreativeInventory)
            {
                for (int i = 0; i < val.Value.Length; i++)
                {
                    string blockCode = RegistryObject.FillPlaceHolder(val.Value[i], searchReplace);

                    if (WildcardUtil.Match(blockCode, code.Path))
                    //if (WildCardMatch(blockCode, code.Path))
                    {
                        string tabCode = val.Key;
                        tabs.Add(tabCode);
                    }
                }
            }

            return(tabs.ToArray());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Fills in placeholders in the composite texture (called by the VSGameContent mod during item loading)
        /// </summary>
        /// <param name="searchReplace"></param>
        public void FillPlaceHolders(Dictionary <string, string> searchReplace)
        {
            foreach (CompositeTexture tex in Client.Textures.Values)
            {
                tex.FillPlaceHolders(searchReplace);
            }

            Client.Shape?.FillPlaceHolders(searchReplace);
            foreach (var val in searchReplace)
            {
                Attributes?.FillPlaceHolder(val.Key, val.Value);
            }

            if (Drops != null)
            {
                for (int i = 0; i < Drops.Length; i++)
                {
                    Drops[i].Code = RegistryObject.FillPlaceHolder(Drops[i].Code, searchReplace);
                }
            }
        }
        protected static void solveByType(JToken json, string codePath, OrderedDictionary <string, string> searchReplace)
        {
            if (json is JObject jsonObj)
            {
                List <string> propertiesToRemove            = null;
                Dictionary <string, JToken> propertiesToAdd = null;

                foreach (var entry in jsonObj)
                {
                    if (entry.Key.EndsWith("byType", StringComparison.OrdinalIgnoreCase))
                    {
                        string trueKey = entry.Key.Substring(0, entry.Key.Length - 6);  // 6 is the length of "byType"
                        var    jobj    = entry.Value as JObject;
                        foreach (var byTypeProperty in jobj)
                        {
                            if (WildcardUtil.Match(byTypeProperty.Key, codePath))
                            {
                                JToken typedToken = byTypeProperty.Value;    // Unnecessary to solveByType specifically on this new token's contents as we will be doing a solveByType on all the tokens in the jsonObj anyhow, after adding the propertiesToAdd
                                if (propertiesToAdd == null)
                                {
                                    propertiesToAdd = new Dictionary <string, JToken>();
                                }
                                propertiesToAdd.Add(trueKey, typedToken);
                                break;   // Replaces for first matched key only
                            }
                        }
                        if (propertiesToRemove == null)
                        {
                            propertiesToRemove = new List <string>();
                        }
                        propertiesToRemove.Add(entry.Key);
                    }
                }


                if (propertiesToRemove != null)
                {
                    foreach (var property in propertiesToRemove)
                    {
                        jsonObj.Remove(property);
                    }

                    if (propertiesToAdd != null)
                    {
                        foreach (var property in propertiesToAdd)
                        {
                            if (jsonObj[property.Key] is JObject jobject)
                            {
                                jobject.Merge(property.Value);
                            }
                            else
                            {
                                jsonObj[property.Key] = property.Value;
                            }
                        }
                    }
                }

                foreach (var entry in jsonObj)
                {
                    solveByType(entry.Value, codePath, searchReplace);
                }
            }
            else if (json.Type == JTokenType.String)
            {
                string value = (string)(json as JValue).Value;
                if (value.Contains("{"))
                {
                    (json as JValue).Value = RegistryObject.FillPlaceHolder(value, searchReplace);
                }
            }
            else if (json is JArray jarray)
            {
                foreach (var child in jarray)
                {
                    solveByType(child, codePath, searchReplace);
                }
            }
        }