Exemplo n.º 1
0
        public PlutonUIPanel AddPanel(string name = null, string parent = null, float?fadeout = null)
        {
            PlutonUIPanel panel = new PlutonUIPanel(name, parent, fadeout);

            panels.Add(panel.obj);
            return(panel);
        }
Exemplo n.º 2
0
 public static JSON.Array operator +(JSON.Array lhs, JSON.Array rhs)
 {
     JSON.Array array = new JSON.Array(lhs);
     foreach (Value value2 in rhs.values)
     {
         array.Add(value2);
     }
     return(array);
 }
Exemplo n.º 3
0
        private JSONValue ObjectToJsonObject(object obj)
        {
            if (obj == null)
            {
                return(new JSONValue(JSONValueType.Null));
            }
            if (obj is string)
            {
                return(new JSONValue((string)obj));
            }
            if (obj is double)
            {
                return(new JSONValue((double)obj));
            }
            if (obj is int)
            {
                return(new JSONValue((int)obj));
            }
            if (obj is bool)
            {
                return(new JSONValue((bool)obj));
            }
            var dict = obj as Dictionary <string, object>;

            if (dict != null)
            {
                var newObj = new JSONObject();
                foreach (var prop in dict)
                {
                    newObj.Add(prop.Key, ObjectToJsonObject(prop.Value));
                }
                return(newObj);
            }
            var list = obj as List <object>;

            if (list != null)
            {
                var arr = new JSONArray();
                foreach (var o in list)
                {
                    arr.Add(ObjectToJsonObject(o));
                }
                return(arr);
            }
            Puts("Unknown: " + obj.GetType().FullName + " Value: " + obj);
            return(new JSONValue(JSONValueType.Null));
        }
Exemplo n.º 4
0
        private bool CreateDefaultConfig()
        {
            Config.Clear();
            Config["Version"] = Protocol.network;
            var itemList = Resources.LoadAll <ItemDefinition>("items/").ToList();
            var bpList   = Resources.LoadAll <ItemBlueprint>("items/").ToList();
            var items    = new JSONArray();

            foreach (var definition in itemList)
            {
                var obj      = ToJsonObject(definition);
                var mods     = definition.GetComponentsInChildren <ItemMod>(true);
                var modArray = new JSONArray();
                foreach (var itemMod in mods)
                {
                    if (itemMod.GetType() == typeof(ItemModMenuOption))
                    {
                        continue;
                    }
                    var mod = ToJsonObject(itemMod);
                    if (itemMod.GetType() == typeof(ItemModBurnable))
                    {
                        StripObject(mod["byproductItem"].Obj);
                    }
                    else if (itemMod.GetType() == typeof(ItemModCookable))
                    {
                        StripObject(mod["becomeOnCooked"].Obj);
                    }
                    else if (itemMod.GetType() == typeof(ItemModConsume))
                    {
                        mod["effects"] = ToJsonArray(itemMod.GetComponent <ItemModConsumable>().effects);
                    }
                    else if (itemMod.GetType() == typeof(ItemModSwap))
                    {
                        var becomeItems = mod["becomeItem"].Array;
                        foreach (var entry in becomeItems)
                        {
                            entry.Obj["itemDef"] = entry.Obj.GetObject("itemDef").GetString("shortname", "unnamed");
                        }
                    }
                    if (!mod.Any())
                    {
                        continue;
                    }
                    mod["type"] = itemMod.GetType().FullName;
                    modArray.Add(mod);
                }
                var modProjectile = definition.GetComponent <ItemModProjectile>();
                if (modProjectile != null)
                {
                    var projectile = modProjectile.projectileObject.targetObject.GetComponent <Projectile>();
                    var mod        = ToJsonObject(projectile);
                    mod.Remove("sourceWeapon");
                    mod.Remove("projectileID");
                    mod.Remove("seed");
                    mod.Remove("velocityScalar");
                    mod["type"] = modProjectile.GetType().FullName;
                    modArray.Add(mod);
                }
                obj["modules"] = modArray;

                items.Add(obj);
            }
            Config["Items"] = JsonObjectToObject(items);
            var bps = ToJsonArray(bpList);

            foreach (var bp in bps)
            {
                StripObject(bp.Obj["targetItem"].Obj);
                foreach (var ing in bp.Obj.GetArray("ingredients"))
                {
                    ing.Obj["itemDef"] = ing.Obj.GetObject("itemDef").GetString("shortname", "unnamed");
                }
            }
            Config["Blueprints"] = JsonObjectToObject(bps);

            /*var constructions = new Dictionary<string, object>();
             * Config["Constructions"] = constructions;
             * var protectionProperties = new HashSet<ProtectionProperties>();
             * var constructionSkinArray = Resources.LoadAll<ConstructionSkin>("Prefabs/build/skins");
             * var constructionArray = Resources.LoadAll<Construction>("Prefabs/build/");
             * foreach (var construct in constructionArray)
             * {
             *  var common = new Construction.Common(construct, constructionSkinArray);
             *  var construction = new Dictionary<string, object>();
             *  var grades = new Dictionary<string, object>();
             *  for (var g = 0; g < common.grades.Length; g++)
             *  {
             *      var grade = common.grades[g];
             *      if (grade == null) continue;
             *      var dict = new Dictionary<string, object>();
             *      dict["maxHealth"] = grade.maxHealth;
             *      var costToBuild = ToJsonArray(grade.costToBuild);
             *      foreach (var cost in costToBuild)
             *      {
             *          cost.Obj["itemDef"] = cost.Obj.GetObject("itemDef").GetString("shortname", "unnamed");
             *      }
             *      dict["costToBuild"] = JsonObjectToObject(costToBuild);
             *      if (grade.damageProtecton != null)
             *      {
             *          protectionProperties.Add(grade.damageProtecton);
             *      }
             *      grades[((BuildingGrade.Enum)g).ToString()] = dict;
             *  }
             *  construction["grades"] = grades;
             *  constructions[common.name] = construction;
             * }
             * var protections = new Dictionary<string, object>();
             * Config["DamageProtections"] = protections;
             * foreach (var protectionProperty in protectionProperties)
             * {
             *  var damageProtection = new Dictionary<string, object>();
             *  for (var i = 0; i < protectionProperty.amounts.Length; i++)
             *  {
             *      damageProtection[Enum.GetName(typeof(DamageType), i)] = protectionProperty.amounts[i];
             *  }
             *  protections[protectionProperty.name] = damageProtection;
             * }*/
            try
            {
                Config.Save(_configpath);
            }
            catch (Exception e)
            {
                LocalPuts(e.Message);
                return(false);
            }
            LocalPuts("Created new config");
            return(LoadConfig());
        }
Exemplo n.º 5
0
 private JSONValue ObjectToJsonObject(object obj)
 {
     if (obj == null)
     {
         return new JSONValue(JSONValueType.Null);
     }
     if (obj is string)
     {
         return new JSONValue((string)obj);
     }
     if (obj is double)
     {
         return new JSONValue((double)obj);
     }
     if (obj is int)
     {
         return new JSONValue((int)obj);
     }
     if (obj is bool)
     {
         return new JSONValue((bool)obj);
     }
     var dict = obj as Dictionary<string, object>;
     if (dict != null)
     {
         var newObj = new JSONObject();
         foreach (var prop in dict)
         {
             newObj.Add(prop.Key, ObjectToJsonObject(prop.Value));
         }
         return newObj;
     }
     var list = obj as List<object>;
     if (list != null)
     {
         var arr = new JSONArray();
         foreach (var o in list)
         {
             arr.Add(ObjectToJsonObject(o));
         }
         return arr;
     }
     Puts("Unknown: " + obj.GetType().FullName + " Value: " + obj);
     return new JSONValue(JSONValueType.Null);
 }
Exemplo n.º 6
0
        /*private static void StripObject(JSONObject obj)
         * {
         *  if (obj == null) return;
         *  var keys = obj.Select(entry => entry.Key).ToList();
         *  foreach (var key in keys)
         *  {
         *      if (!key.Equals("shortname") && !key.Equals("itemid"))
         *          obj.Remove(key);
         *  }
         * }
         *
         * private static void StripArray(JSONArray arr, string key)
         * {
         *  if (arr == null) return;
         *  foreach (var obj in arr)
         *  {
         *      StripObject(obj.Obj[key].Obj);
         *  }
         * }*/

        private bool CreateDefaultConfig()
        {
            Config.Clear();
            Config["Version"]       = Protocol.network;
            Config["VersionConfig"] = VersionConfig;
            var gameObjectArray = FileSystem.Load <ObjectList>("Assets/items.asset").objects.Cast <GameObject>().ToArray();
            var itemList        = gameObjectArray.Select(x => x.GetComponent <ItemDefinition>()).Where(x => x != null).ToArray();
            var bpList          = gameObjectArray.Select(x => x.GetComponent <ItemBlueprint>()).Where(x => x != null).ToArray();
            var items           = new JSONArray();

            foreach (var definition in itemList)
            {
                //Puts("Item: {0}", definition.displayName.english);
                var obj = ToJsonObject(definition);
                obj.Remove("itemid");
                obj.Remove("hidden");
                obj.Remove("isWearable");
                obj["Parent"]             = definition.Parent?.shortname;
                obj["displayName"]        = definition.displayName.english;
                obj["displayDescription"] = definition.displayDescription.english;
                var mods     = definition.GetComponentsInChildren <ItemMod>(true);
                var modArray = new JSONArray();
                foreach (var itemMod in mods)
                {
                    if (itemMod.GetType() == typeof(ItemModMenuOption) || itemMod.GetType() == typeof(ItemModConditionHasFlag) || itemMod.GetType() == typeof(ItemModConditionContainerFlag) ||
                        itemMod.GetType() == typeof(ItemModSwitchFlag) || itemMod.GetType() == typeof(ItemModCycle) || itemMod.GetType() == typeof(ItemModConditionHasContents) ||
                        itemMod.GetType() == typeof(ItemModUseContent) || itemMod.GetType() == typeof(ItemModEntity) || itemMod.GetType() == typeof(ItemModUnwrap))
                    {
                        continue;
                    }
                    //Puts("ItemMod: {0}", itemMod.GetType());
                    var mod = ToJsonObject(itemMod);
                    if (itemMod.GetType() == typeof(ItemModBurnable))
                    {
                        mod["byproductItem"] = mod.GetObject("byproductItem")?.GetString("shortname", "unnamed");
                    }
                    else if (itemMod.GetType() == typeof(ItemModCookable))
                    {
                        mod["becomeOnCooked"] = mod.GetObject("becomeOnCooked")?.GetString("shortname", "unnamed");
                    }
                    else if (itemMod.GetType() == typeof(ItemModContainer))
                    {
                        var defaultContents = mod["defaultContents"].Array;
                        foreach (var entry in defaultContents)
                        {
                            entry.Obj["shortname"] = entry.Obj.GetObject("itemDef").GetString("shortname", "unnamed");
                            entry.Obj.Remove("itemDef");
                            entry.Obj.Remove("itemid");
                        }
                        mod["onlyAllowedItemType"] = mod.GetObject("onlyAllowedItemType")?.GetString("shortname", "unnamed");
                    }
                    else if (itemMod.GetType() == typeof(ItemModConsume))
                    {
                        mod["effects"] = ToJsonArray(itemMod.GetComponent <ItemModConsumable>().effects);
                    }
                    else if (itemMod.GetType() == typeof(ItemModReveal))
                    {
                        mod["revealedItemOverride"] = mod.GetObject("revealedItemOverride")?.GetString("shortname", "unnamed");
                    }
                    else if (itemMod.GetType() == typeof(ItemModRecycleInto))
                    {
                        mod["recycleIntoItem"] = mod.GetObject("recycleIntoItem")?.GetString("shortname", "unnamed");
                    }
                    else if (itemMod.GetType() == typeof(ItemModUpgrade))
                    {
                        mod["upgradedItem"] = mod.GetObject("upgradedItem")?.GetString("shortname", "unnamed");
                    }
                    else if (itemMod.GetType() == typeof(ItemModSwap))
                    {
                        var becomeItems = mod["becomeItem"].Array;
                        foreach (var entry in becomeItems)
                        {
                            entry.Obj["shortname"] = entry.Obj.GetObject("itemDef").GetString("shortname", "unnamed");
                            entry.Obj.Remove("itemDef");
                            entry.Obj.Remove("itemid");
                        }
                    }
                    else if (itemMod.GetType() == typeof(ItemModWearable))
                    {
                        var itemModWearable = itemMod.GetComponent <ItemModWearable>();
                        if (itemModWearable.protectionProperties != null)
                        {
                            var protectionObj = new JSONObject
                            {
                                ["density"] = itemModWearable.protectionProperties.density
                            };
                            var amounts = new JSONObject();
                            for (var i = 0; i < itemModWearable.protectionProperties.amounts.Length; i++)
                            {
                                amounts[((DamageType)i).ToString()] = itemModWearable.protectionProperties.amounts[i];
                            }
                            protectionObj["amounts"] = amounts;
                            mod["protection"]        = protectionObj;
                        }
                        if (itemModWearable.armorProperties != null)
                        {
                            mod["armor"] = FromJsonString <string>(ToJsonString((HitAreaUnity)itemModWearable.armorProperties.area));
                        }
                        var targetWearable = mod.GetObject("targetWearable");
                        targetWearable.Remove("showCensorshipCube");
                        targetWearable.Remove("showCensorshipCubeBreasts");
                        targetWearable.Remove("followBone");
                        targetWearable["occupationOver"]  = FromJsonString <string>(ToJsonString((OccupationSlotsUnity)itemModWearable.targetWearable.occupationOver));
                        targetWearable["occupationUnder"] = FromJsonString <string>(ToJsonString((OccupationSlotsUnity)itemModWearable.targetWearable.occupationUnder));
                    }
                    if (!mod.Any())
                    {
                        continue;
                    }
                    mod["type"] = itemMod.GetType().FullName;
                    modArray.Add(mod);
                }
                var modEntity = definition.GetComponent <ItemModEntity>();
                if (modEntity != null)
                {
                    var prefab         = modEntity.entityPrefab?.Get();
                    var timedExplosive = prefab?.GetComponent <ThrownWeapon>()?.prefabToThrow?.Get()?.GetComponent <TimedExplosive>();
                    if (timedExplosive != null)
                    {
                        var mod = ToJsonObject(timedExplosive);
                        mod["type"] = modEntity.GetType().FullName + timedExplosive.GetType().FullName;
                        if (timedExplosive is DudTimedExplosive)
                        {
                            mod.Remove("itemToGive");
                        }
                        modArray.Add(mod);
                    }
                    var modMelee = prefab?.GetComponent <BaseMelee>();
                    if (modMelee != null)
                    {
                        var mod = ToJsonObject(modMelee);
                        mod["type"] = modEntity.GetType().FullName + typeof(BaseMelee).FullName;
                        mod.Remove("strikeEffect");
                        modArray.Add(mod);
                    }
                    var baseProjectile = prefab?.GetComponent <BaseProjectile>();
                    if (baseProjectile != null)
                    {
                        var mod = new JSONObject
                        {
                            ["ammoType"] = baseProjectile.primaryMagazine.ammoType.shortname,
                            //["ammoTypes"] = ToJsonString(baseProjectile.primaryMagazine.definition.ammoTypes),
                            ["builtInSize"] = baseProjectile.primaryMagazine.definition.builtInSize,
                            //["capacity"] = baseProjectile.primaryMagazine.capacity,
                            ["contents"] = baseProjectile.primaryMagazine.contents,
                            ["type"]     = modEntity.GetType().FullName + typeof(BaseProjectile).FullName
                        };
                        modArray.Add(mod);
                    }
                }
                var modProjectile = definition.GetComponent <ItemModProjectile>();
                if (modProjectile != null)
                {
                    var prefab     = modProjectile.projectileObject?.Get();
                    var projectile = prefab?.GetComponent <Projectile>();
                    if (projectile != null)
                    {
                        var mod = ToJsonObject(projectile);
                        mod.Remove("sourceWeapon");
                        mod.Remove("projectileID");
                        mod.Remove("seed");
                        mod.Remove("velocityScalar");
                        if (modProjectile.mods != null)
                        {
                            var modsArray = new JSONArray();
                            foreach (var projectileMod in modProjectile.mods)
                            {
                                var projMod = ToJsonObject(projectileMod);
                                projMod["type"] = projectileMod.GetType().FullName;
                                modsArray.Add(projMod);
                            }
                            mod["mods"] = modsArray;
                        }
                        var spawn = modProjectile as ItemModProjectileSpawn;
                        if (spawn != null)
                        {
                            mod["createOnImpactChance"] = spawn.createOnImpactChance;
                            mod["spreadAngle"]          = spawn.spreadAngle;
                        }
                        mod["type"] = modProjectile.GetType().FullName;
                        modArray.Add(mod);
                    }

                    /*var components = modProjectile.projectileObject.targetObject.GetComponents(typeof (Component));
                     * foreach (var component in components)
                     * {
                     *  LocalPuts("Name: " + component.name + " Type: " + component.GetType().Name);
                     * }*/
                    var timedExplosive = prefab?.GetComponent <TimedExplosive>();
                    if (timedExplosive != null)
                    {
                        var mod = ToJsonObject(timedExplosive);
                        mod["type"] = modProjectile.GetType().FullName + timedExplosive.GetType().FullName;
                        modArray.Add(mod);
                    }
                    var serverProjectile = prefab?.GetComponent <ServerProjectile>();
                    if (serverProjectile != null)
                    {
                        var mod = ToJsonObject(serverProjectile);
                        mod["type"] = modProjectile.GetType().FullName + serverProjectile.GetType().FullName;
                        modArray.Add(mod);
                    }
                }
                obj["modules"] = modArray;

                items.Add(obj);
            }

            Config["Items"] = JsonObjectToObject(items);
            var bps = ToJsonArray(bpList);

            foreach (var bp in bps)
            {
                bp.Obj["targetItem"] = bp.Obj.GetObject("targetItem").GetString("shortname", "unnamed");
                bp.Obj.Remove("userCraftable");
                bp.Obj.Remove("defaultBlueprint");
                foreach (var ing in bp.Obj.GetArray("ingredients"))
                {
                    ing.Obj["shortname"] = ing.Obj.GetObject("itemDef").GetString("shortname", "unnamed");
                    ing.Obj.Remove("itemDef");
                    ing.Obj.Remove("itemid");
                }
            }
            Config["Blueprints"] = JsonObjectToObject(bps);

            try
            {
                Config.Save(_configpath);
            }
            catch (Exception e)
            {
                Puts("Config save failed: {0}{1}{2}", e.Message, Environment.NewLine, e.StackTrace);
                return(false);
            }
            Puts("Created new config");
            return(LoadConfig());
        }