1// Aseprite Document Library
2// Copyright (C) 2019-2020 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "doc/tag_io.h"
13
14#include "base/serialization.h"
15#include "doc/string_io.h"
16#include "doc/tag.h"
17#include "doc/user_data_io.h"
18
19#include <iostream>
20#include <memory>
21
22namespace doc {
23
24using namespace base::serialization;
25using namespace base::serialization::little_endian;
26
27void write_tag(std::ostream& os, const Tag* tag)
28{
29 std::string name = tag->name();
30
31 write32(os, tag->id());
32 write32(os, tag->fromFrame());
33 write32(os, tag->toFrame());
34 write8(os, (int)tag->aniDir());
35 write_string(os, tag->name());
36 write_user_data(os, tag->userData());
37}
38
39Tag* read_tag(std::istream& is,
40 const bool setId,
41 const bool oldVersion)
42{
43 ObjectId id = read32(is);
44 frame_t from = read32(is);
45 frame_t to = read32(is);
46
47 // If we are reading a session from v1.2.x, there is a color field
48 color_t color;
49 if (oldVersion)
50 color = read32(is);
51
52 AniDir aniDir = (AniDir)read8(is);
53 std::string name = read_string(is);
54 UserData userData;
55
56 // If we are reading the new v1.3.x version, there is a user data with the color + text
57 if (!oldVersion)
58 userData = read_user_data(is);
59
60 std::unique_ptr<Tag> tag(new Tag(from, to));
61 tag->setAniDir(aniDir);
62 tag->setName(name);
63 if (oldVersion)
64 tag->setColor(color);
65 else
66 tag->setUserData(userData);
67 if (setId)
68 tag->setId(id);
69 return tag.release();
70}
71
72}
73