Example #1
0
    public static void LoadHolding(string reality, Player player)
    {
        if (string.IsNullOrEmpty(reality))
        {
            Debug.LogError("Reality is null or em empty, cannot load held item.");
            return;
        }
        if (player == null)
        {
            Debug.LogError("Null player, cannot perform IO operation for held item.");
            return;
        }

        // Get file path.
        string path = OutputUtils.RealitySaveDirectory + reality + OutputUtils.HeldItemSaveFile;

        // Get the player holding monobehaviour.
        PlayerHolding holding = player.Holding;

        ItemSaveData sd = InputUtils.FileToObject <ItemSaveData>(path);

        // Apply!
        if (sd == null || string.IsNullOrEmpty(sd.Prefab))
        {
            holding.CmdDrop(false, true, player.gameObject, null);
        }
        else
        {
            holding.ServerEquip(sd.Prefab, player.gameObject, sd.Data.Serialize());
        }
    }
Example #2
0
    public static WorldSaveState GetSaveState(string reality)
    {
        string path = OutputUtils.RealitySaveDirectory + reality + OutputUtils.WorldStateSaveFile;
        WorldSaveState sd = InputUtils.FileToObject<WorldSaveState>(path);

        return sd;
    }
Example #3
0
    public static void LoadInventory(string reality, bool clearInventory = true)
    {
        if (string.IsNullOrEmpty(reality))
        {
            Debug.LogError("Reality is null or em empty, cannot load inventory items.");
            return;
        }

        // Get file path.
        string path = OutputUtils.RealitySaveDirectory + reality + OutputUtils.InventoryItemSaveFile;

        // Make the list
        InventoryItemData[] data = InputUtils.FileToObject <InventoryItemData[]>(path);

        if (data != null)
        {
            if (clearInventory)
            {
                PlayerInventory.inv.Inventory.Clear();
            }

            // Apply to the player.
            PlayerInventory.inv.Inventory.Contents = new List <InventoryItemData>(data);
        }
        else
        {
            Debug.LogError("Error loading inventory items from file.");
            return;
        }
    }
Example #4
0
    public static void LoadPlayerState(string reality, Player player)
    {
        if (string.IsNullOrEmpty(reality))
        {
            Debug.LogError("Reality is null or em empty, cannot save player state.");
            return;
        }
        if (player == null)
        {
            Debug.LogError("Null player, cannot save player state.");
            return;
        }

        // Make path.
        string path = OutputUtils.RealitySaveDirectory + reality + OutputUtils.PlayerSaveDirectory + player.Name + OutputUtils.PlayerStateFile;

        Debug.Log("Loading state of player '" + player.Name + "' from '" + path + "'...");

        // Load save data.
        PlayerSaveData sd = InputUtils.FileToObject <PlayerSaveData>(path);

        if (sd == null)
        {
            return;
        }

        // Apply to player.
        sd.Apply(player);
    }
Example #5
0
    public static void LoadWorldState(World world)
    {
        // Tryo to load from file.
        string path = OutputUtils.RealitySaveDirectory + world.RealityName + OutputUtils.WorldStateSaveFile;
        WorldSaveState sd = InputUtils.FileToObject<WorldSaveState>(path);

        if(sd != null)
        {
            sd.Apply(world);
            Debug.Log("Applied loaded world state...");
        }
        else
        {
            Debug.LogError("Null world state save file, no world state data applied.");
        }
    }
Example #6
0
 public static LanguageDefinition LoadDefinition(string name)
 {
     if (Application.isEditor)
     {
         // Load from absolute file path...
         return(InputUtils.FileToObject <LanguageDefinition>(GetDefinitionPath(name)));
     }
     else
     {
         // Load from internal resources...
         string    path = "Languages/Definitions/" + name;
         TextAsset a    = Resources.Load <TextAsset>(path);
         string    json = a.text;
         return(InputUtils.JsonToObject <LanguageDefinition>(json));
     }
 }
Example #7
0
    public static ItemSaveData[] FileToSaveDatas(string reality)
    {
        if (string.IsNullOrEmpty(reality))
        {
            Debug.LogError("Reality is null or empty!");
            return(null);
        }

        string filePath = OutputUtils.RealitySaveDirectory + reality + OutputUtils.WorldItemSaveFile;

        if (!File.Exists(filePath))
        {
            Debug.LogError("File '" + filePath + "' does not exist!");
            return(null);
        }

        ItemSaveData[] data = InputUtils.FileToObject <ItemSaveData[]>(filePath);
        return(data);
    }
Example #8
0
    public static void LoadFurniture(World world)
    {
        // As always, only on server!
        if (world == null || string.IsNullOrWhiteSpace(world.RealityName))
        {
            Debug.LogError("World object or reality name is null! Cannot load furniture!");
            return;
        }

        // Get file path...
        string path = OutputUtils.RealitySaveDirectory + world.RealityName + OutputUtils.FurnitureSaveDirectory + OutputUtils.FurnitureSaveFile;

        // Make save datas from the file...
        FurnitureSaveData[] sds = InputUtils.FileToObject <FurnitureSaveData[]>(path);

        // Destroy all placed furniture...
        Furniture[] placed = GameObject.FindObjectsOfType <Furniture>();
        foreach (var f in placed)
        {
            GameObject.Destroy(f.gameObject);
        }
        Debug.Log("Destroyed " + placed.Length + " placed furnitures before load...");
        placed = null;

        if (sds == null)
        {
            Debug.LogError("No furniture got from the save file! Not placing any furniture.");
            return;
        }

        // Place the save datas within the world...
        foreach (var sd in sds)
        {
            if (sd == null)
            {
                continue;
            }

            sd.PlaceInWorld(world.Furniture);
        }
    }
Example #9
0
    public static void LoadKeyBindings()
    {
        // Creates the default key bindings, which never changes.
        CreateDefaultBindings();

        string path = OutputUtils.InputSaveKeyBindings;

        Debug.Log("Loading key bindings from '" + path + "'...");

        // Load from file, may return null!
        Dictionary <string, KeyCode> x = InputUtils.FileToObject <Dictionary <string, KeyCode> >(path);

        if (x == null)
        {
            Debug.LogWarning("File was not found or parsing error occured. (Pre conversion, file)");
            Debug.Log("Saving default key bindings...");

            // Set the default key bindings and save, because no file exists.
            SetDefaultKeyBindings();
            SaveKeyBindings();

            return;
        }
        else
        {
            keyBindings = x;
        }
        // At this point, key bindings object may be null becuase it was not found on file.
        // Merging the default values will create the object if it is null.
        // When we merge the default values will replace an missing ones.
        if (keyBindings != null)
        {
            Debug.Log("Done! Loaded " + keyBindings.Count + " key bindings!");
        }

        // Merge key bindings.
        MergeKeyBindings();
    }
Example #10
0
    public static void LoadBuildingInventory(string reality, Player player)
    {
        if (string.IsNullOrWhiteSpace(reality) || player == null)
        {
            Debug.LogError("Reality is null or player is null! Both must not be null!");
            return;
        }

        // Get path...
        string path = OutputUtils.RealitySaveDirectory + reality + OutputUtils.PlayerSaveDirectory + player.Name + OutputUtils.BuildingInventorySaveFile;

        // Load items from file.
        BuildingItem[] items = InputUtils.FileToObject <BuildingItem[]>(path);

        if (items == null)
        {
            Debug.LogError("Failed to load any inventory building items!");
            return;
        }

        // Apply the items...
        player.BuildingInventory.SetItems(items);
    }
Example #11
0
    public static void LoadGear(string reality, Player player)
    {
        if (string.IsNullOrEmpty(reality))
        {
            Debug.LogError("Reality is null or em empty, cannot load gear items.");
            return;
        }
        if (player == null)
        {
            Debug.LogError("Null player, cannot perform IO operation for gear items.");
            return;
        }

        // Get path.
        string path = OutputUtils.RealitySaveDirectory + reality + OutputUtils.GearItemSaveFile;

        // Make array.
        GearSaveData[] array = InputUtils.FileToObject <GearSaveData[]>(path);

        if (array == null)
        {
            Debug.LogError("Null gear file, removing all player gear...");
            foreach (var key in player.GearMap.Keys)
            {
                player.GearMap[key].SetItem(player.gameObject, null, null, false);
            }
            return;
        }

        foreach (var sd in array)
        {
            if (sd != null)
            {
                string slot = sd.Slot;
                if (player.GearMap.ContainsKey(slot))
                {
                    if (string.IsNullOrEmpty(sd.Prefab))
                    {
                        // Remove any gear in that slot without returning the previous item.
                        player.GearMap[slot].SetItem(player.gameObject, null, null, false);
                    }
                    else
                    {
                        Item i = Item.GetItem(sd.Prefab);
                        if (i == null)
                        {
                            Debug.LogError("Item load for gear slot '" + slot + "' could not be found (" + sd.Prefab + ")!");
                            player.GearMap[slot].SetItem(player.gameObject, null, null, false);
                        }
                        else
                        {
                            // Give loaded item to player, but do not return old one.
                            player.GearMap[slot].SetItem(player.gameObject, i, sd.Data, false);
                        }
                    }
                }
                else
                {
                    Debug.LogError("Saved gear slot '" + slot + "' does not exist on the player '" + player.Name + "'!");
                    continue;
                }
            }
        }
    }