Esempio n. 1
0
        public bool startSaveUpdate(string[] clientSaves, NetOutgoingMessage msg)
        {
            try {
                string        savepath = "saves/" + gameFileName + "/";
                List <string> saves    = getSaves(savepath);
                int           find     = -1;
                int           ctr      = 0;
                foreach (string save in clientSaves)
                {
                    find = saves.BinarySearch(save);
                    if (find >= 0)
                    {
                        NetworkedFileUpdate updateLogic = new NetworkedFileUpdate(save, saves[saves.Count - 1]);
                        List <Diff>         diffs       = updateLogic.updateFile();
                        msg.Write((byte)ctr);
                        msg.Write(diffs.Count);
                        msg.Write(COUNT_DELIMITER);

                        foreach (Diff diff in diffs)
                        {
                            msg.Write((byte)diff.operation);
                            if (diff.operation == Operation.INSERT)
                            {
                                msg.Write(diff.contents.Length);
                                msg.Write(DIFF_DELIMITER);
                                msg.Write(diff.contents, 0, diff.contents.Length);
                            }
                            else
                            {
                                msg.Write(diff.length);
                            }
                            msg.Write(DIFF_DELIMITER);
                        }

                        break;
                    }
                    ctr++;
                }

                //we found an update
                if (find > -1)
                {
                    return(true);
                }

                FileStream stream   = new FileStream(saves[saves.Count - 1], FileMode.Open, FileAccess.Read);
                byte[]     contents = new byte[stream.Length];
                stream.Read(contents, 0, contents.Length);
                msg.Write(contents, 0, contents.Length);
                stream.Close();
                return(true);
            }catch (Exception e) {
                Console.WriteLine(e);
            }
            return(false);
        }
Esempio n. 2
0
        public bool readSaveUpdate(NetIncomingMessage msg)
        {
            try {
                int    index        = (int)msg.ReadByte();
                string save         = getGameSave(index);
                int    messageCount = msg.ReadInt32();

                if (msg.ReadByte() == COUNT_DELIMITER)
                {
                    int         ctr     = 0;
                    List <Diff> updates = new List <Diff>(messageCount);

                    while (ctr < messageCount)
                    {
                        Operation operation  = (Operation)msg.ReadByte();
                        int       byteLength = msg.ReadInt32();
                        string    content;
                        if (operation == Operation.INSERT)
                        {
                            msg.ReadByte();
                            byte[] data = msg.ReadBytes(byteLength);
                            content = Encoding.UTF8.GetString(data);
                        }
                        else
                        {
                            content = "";
                        }
                        msg.ReadByte();
                        Diff update = new Diff((Operation)operation, content);
                        update.length = byteLength;
                        updates.Add(update);
                        ctr++;
                    }

                    NetworkedFileUpdate.writeFromUpdates(updates, save);
                }
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                return(false);
            }
            return(true);
        }