Esempio n. 1
0
    private IEnumerator LoadVariablesInternal()
    {
        Debug.Log("Loading...");
        using (var reader = new BinaryReader(File.Open(_savePath, FileMode.OpenOrCreate)))
        {
            var dataReader = new DataReader(reader, 0);
            var readData   = new List <SavingVariable>();

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            var collectionSize = dataReader.ReadInt();

            for (int i = 0; i < collectionSize; ++i)
            {
                if (watch.ElapsedMilliseconds > MAX_MILLISECONDS)
                {
                    watch.Reset();
                    yield return(null);

                    watch.Start();
                }

                var savedVariable = SavingVariableSaveLoad.LoadVariables(dataReader);
                readData.Add(savedVariable);
            }

            Debug.Log("Loaded!");
            ListsOfVariable = readData;
            _isLoading      = false;
        }
    }
    //! Save inventory with frame offloading
    public IEnumerator SaveInventoryCoroutine(DataWriter writer, int maxMilliseconds)
    {
        Debug.Log("Saving Inventory...");

        System.Diagnostics.Stopwatch watch         = new System.Diagnostics.Stopwatch();
        System.Diagnostics.Stopwatch periodicWatch = new System.Diagnostics.Stopwatch();
        watch.Start();
        periodicWatch.Start();

        //! Start Saving
        writer.Write(maxSize);
        writer.Write(items.Count);

        for (int i = 0; i < items.Count; ++i)
        {
            SavingVariableSaveLoad.SaveVariables(writer, items[i]);

            if (periodicWatch.ElapsedMilliseconds > maxMilliseconds)
            {
                periodicWatch.Reset();
                yield return(null);

                periodicWatch.Start();
            }
        }

        //! Saving ends
        watch.Stop();
        periodicWatch.Stop();
        Debug.Log("Inventory save completed, total time taken : " + watch.Elapsed);
    }
    //! Load inventory with frame offloading
    public IEnumerator LoadInventoryCoroutine(DataReader reader, int maxMilliseconds)
    {
        Debug.Log("Loading inventory...");

        System.Diagnostics.Stopwatch watch         = new System.Diagnostics.Stopwatch();
        System.Diagnostics.Stopwatch periodicWatch = new System.Diagnostics.Stopwatch();
        watch.Start();
        periodicWatch.Start();

        items.Clear();

        //!Start loading
        maxSize = reader.ReadUInt();
        var count = reader.ReadInt();

        for (int i = 0; i < count; ++i)
        {
            var data = SavingVariableSaveLoad.LoadVariables(reader);

            AddItem(data);

            if (periodicWatch.ElapsedMilliseconds > maxMilliseconds)
            {
                periodicWatch.Reset();
                yield return(null);

                periodicWatch.Start();
            }
        }

        //! End loading
        watch.Stop();
        periodicWatch.Stop();
        Debug.Log("Inventory load completed, total time taken : " + watch.Elapsed);
    }
Esempio n. 4
0
    private IEnumerator SaveVariablesInternal()
    {
        Debug.Log("Saving...");
        using (var writer = new BinaryWriter(File.Open(_savePath, FileMode.OpenOrCreate)))
        {
            var dataWriter   = new DataWriter(writer);
            var variableList = new List <SavingVariable>(ListsOfVariable);

            //! Using stopwatching instead of thread because there's a List.Add in SavingVariable class
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            dataWriter.Write(variableList.Count); //! store how many items are there

            for (int i = 0; i < variableList.Count; ++i)
            {
                if (watch.ElapsedMilliseconds > MAX_MILLISECONDS)
                {
                    watch.Reset();
                    yield return(null);

                    watch.Start();
                }

                SavingVariableSaveLoad.SaveVariables(dataWriter, variableList[i]);
            }

            Debug.Log("Saved!");
            _isSaving = false;
        }
    }
    public override void Save(DataWriter writer, bool singleFrameSave = false)
    {
        base.Save(writer);
        writer.Write(_currentColor);

        SavingVariableSaveLoad.SaveVariables(writer, itemData);
    }
    public override void LoadNoVersion(DataReader reader)
    {
        base.LoadNoVersion(reader);

        SetColor(reader.ReadColor());

        itemData = SavingVariableSaveLoad.LoadVariables(reader);
    }
    public override void Load(DataReader reader, bool singleFrameLoad = false)
    {
        base.Load(reader);

        SetColor(reader.Version > 0 ? reader.ReadColor() : Color.white);

        itemData = SavingVariableSaveLoad.LoadVariables(reader);
    }
Esempio n. 8
0
    /// <summary>
    /// Sends an RPC call to add item to the player's inventory
    /// </summary>
    /// <param name="item"></param>
    public void AddItemToInventory(SavingVariable item)
    {
        if (networkObject.IsServer)
        {
            using (var m = new MemoryStream())
            {
                using (var writer = new BinaryWriter(m))
                {
                    SavingVariableSaveLoad.SaveVariables(new DataWriter(writer), item);

                    MainThreadManager.Run(() => networkObject.SendRpc(RPC_ADD_ITEM_TO_INVENTORY, Receivers.All, m.ToArray()));
                }
            }
        }
    }
    //! Save inventory into writer within a frame
    public void SaveInventory(DataWriter writer)
    {
        Debug.Log("Saving Inventory...");
        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Start();

        //! Start Saving
        writer.Write(maxSize);
        writer.Write(items.Count);

        for (int i = 0; i < items.Count; ++i)
        {
            SavingVariableSaveLoad.SaveVariables(writer, items[i]);
        }

        //! Saving ends
        watch.Stop();
        Debug.Log("Inventory save completed, total time taken : " + watch.Elapsed);
    }
Esempio n. 10
0
    public override void AddItemToInventory(RpcArgs args)
    {
        if (!networkObject.IsOwner)
        {
            return;
        }

        var data = args.GetNext <byte[]>();

        SavingVariable itemData = null;

        using (var m = new MemoryStream(data))
        {
            using (var reader = new BinaryReader(m))
            {
                itemData = SavingVariableSaveLoad.LoadVariables(new DataReader(reader, 0));
            }
        }

        BMSLogger.Instance.Log(string.Format("{0} added to inventory via RPC call", itemData.variableName));
        PlayerState.Instance.AddItemToInventory(itemData);
    }
    //! Load inventory form reader within a frame
    public void LoadInventory(DataReader reader)
    {
        Debug.Log("Loading inventory...");

        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Start();

        items.Clear();

        //!Start loading
        maxSize = reader.ReadUInt();
        var count = reader.ReadInt();

        for (int i = 0; i < count; ++i)
        {
            var data = SavingVariableSaveLoad.LoadVariables(reader);

            AddItem(data);
        }

        //! End loading
        watch.Stop();
        Debug.Log("Inventory load completed, total time taken : " + watch.Elapsed);
    }