2021-11-10 03:37:10 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Arne Elster <arne@elster.li>
|
2024-01-10 22:49:40 +03:00
|
|
|
* Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
|
2021-11-10 03:37:10 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-01-30 14:08:41 +03:00
|
|
|
#include "AnnotationsModel.h"
|
2023-02-08 23:08:01 +03:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2024-01-29 19:03:37 +03:00
|
|
|
#include <AK/String.h>
|
2021-11-10 03:37:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2022-09-08 00:41:37 +03:00
|
|
|
#include <AK/Time.h>
|
2021-11-10 03:37:10 +03:00
|
|
|
#include <AK/Types.h>
|
2022-09-08 00:41:37 +03:00
|
|
|
#include <AK/WeakPtr.h>
|
2023-02-08 23:08:01 +03:00
|
|
|
#include <LibCore/Forward.h>
|
2022-09-08 00:41:37 +03:00
|
|
|
#include <LibGUI/Command.h>
|
2024-01-10 22:49:40 +03:00
|
|
|
#include <LibGfx/Color.h>
|
2021-11-10 03:37:10 +03:00
|
|
|
|
2023-03-13 18:30:34 +03:00
|
|
|
constexpr Duration COMMAND_COMMIT_TIME = Duration::from_milliseconds(400);
|
2022-09-08 00:41:37 +03:00
|
|
|
|
|
|
|
class HexDocument : public Weakable<HexDocument> {
|
2021-11-10 03:37:10 +03:00
|
|
|
public:
|
|
|
|
enum class Type {
|
|
|
|
Memory,
|
|
|
|
File
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Cell {
|
|
|
|
u8 value;
|
|
|
|
bool modified;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~HexDocument() = default;
|
|
|
|
virtual Cell get(size_t position) = 0;
|
2022-09-06 00:49:35 +03:00
|
|
|
virtual u8 get_unchanged(size_t position) = 0;
|
2021-11-10 03:37:10 +03:00
|
|
|
virtual void set(size_t position, u8 value);
|
|
|
|
virtual size_t size() const = 0;
|
|
|
|
virtual Type type() const = 0;
|
|
|
|
virtual bool is_dirty() const;
|
|
|
|
virtual void clear_changes() = 0;
|
|
|
|
|
2024-01-30 14:08:41 +03:00
|
|
|
AnnotationsModel& annotations() { return *m_annotations; }
|
2024-01-10 22:49:40 +03:00
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
protected:
|
2024-01-30 14:08:41 +03:00
|
|
|
HexDocument();
|
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
HashMap<size_t, u8> m_changes;
|
2024-01-30 14:08:41 +03:00
|
|
|
NonnullRefPtr<AnnotationsModel> m_annotations;
|
2021-11-10 03:37:10 +03:00
|
|
|
};
|
|
|
|
|
2021-11-23 13:43:53 +03:00
|
|
|
class HexDocumentMemory final : public HexDocument {
|
2021-11-10 03:37:10 +03:00
|
|
|
public:
|
|
|
|
explicit HexDocumentMemory(ByteBuffer&& buffer);
|
|
|
|
virtual ~HexDocumentMemory() = default;
|
|
|
|
|
|
|
|
Cell get(size_t position) override;
|
2022-09-06 00:49:35 +03:00
|
|
|
u8 get_unchanged(size_t position) override;
|
2021-11-10 03:37:10 +03:00
|
|
|
size_t size() const override;
|
|
|
|
Type type() const override;
|
|
|
|
void clear_changes() override;
|
2023-02-09 05:02:46 +03:00
|
|
|
ErrorOr<void> write_to_file(Core::File& file);
|
2021-11-10 03:37:10 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
ByteBuffer m_buffer;
|
|
|
|
};
|
|
|
|
|
2021-11-23 13:43:53 +03:00
|
|
|
class HexDocumentFile final : public HexDocument {
|
2021-11-10 03:37:10 +03:00
|
|
|
public:
|
2023-02-09 05:02:46 +03:00
|
|
|
static ErrorOr<NonnullOwnPtr<HexDocumentFile>> create(NonnullOwnPtr<Core::File> file);
|
2021-11-10 03:37:10 +03:00
|
|
|
virtual ~HexDocumentFile() = default;
|
|
|
|
|
2023-01-15 06:24:35 +03:00
|
|
|
HexDocumentFile(HexDocumentFile&&) = default;
|
2022-04-01 20:58:27 +03:00
|
|
|
HexDocumentFile(HexDocumentFile const&) = delete;
|
2021-11-10 03:37:10 +03:00
|
|
|
|
2023-02-09 05:02:46 +03:00
|
|
|
ErrorOr<void> set_file(NonnullOwnPtr<Core::File> file);
|
|
|
|
NonnullOwnPtr<Core::File> const& file() const;
|
2023-01-15 06:09:43 +03:00
|
|
|
ErrorOr<void> write_to_file();
|
2023-02-09 05:02:46 +03:00
|
|
|
ErrorOr<void> write_to_file(Core::File& file);
|
2021-11-10 03:37:10 +03:00
|
|
|
Cell get(size_t position) override;
|
2022-09-06 00:49:35 +03:00
|
|
|
u8 get_unchanged(size_t position) override;
|
2021-11-10 03:37:10 +03:00
|
|
|
size_t size() const override;
|
|
|
|
Type type() const override;
|
|
|
|
void clear_changes() override;
|
|
|
|
|
|
|
|
private:
|
2023-02-09 05:02:46 +03:00
|
|
|
explicit HexDocumentFile(NonnullOwnPtr<Core::File> file);
|
2023-01-15 06:42:05 +03:00
|
|
|
ErrorOr<void> initialize_internal_state();
|
|
|
|
|
2022-09-06 00:49:35 +03:00
|
|
|
void ensure_position_in_buffer(size_t position);
|
|
|
|
|
2023-02-09 05:02:46 +03:00
|
|
|
NonnullOwnPtr<Core::File> m_file;
|
2021-11-10 03:37:10 +03:00
|
|
|
size_t m_file_size;
|
|
|
|
|
|
|
|
Array<u8, 2048> m_buffer;
|
|
|
|
size_t m_buffer_file_pos;
|
|
|
|
};
|
2022-09-08 00:41:37 +03:00
|
|
|
|
|
|
|
class HexDocumentUndoCommand : public GUI::Command {
|
|
|
|
public:
|
|
|
|
HexDocumentUndoCommand(WeakPtr<HexDocument> document, size_t position);
|
|
|
|
|
|
|
|
virtual void undo() override;
|
|
|
|
virtual void redo() override;
|
2023-12-16 17:19:34 +03:00
|
|
|
virtual ByteString action_text() const override { return "Update cell"; }
|
2022-09-08 00:41:37 +03:00
|
|
|
|
|
|
|
virtual bool merge_with(GUI::Command const& other) override;
|
|
|
|
|
|
|
|
ErrorOr<void> try_add_changed_byte(u8 old_value, u8 new_value);
|
|
|
|
ErrorOr<void> try_add_changed_bytes(ByteBuffer old_values, ByteBuffer new_values);
|
|
|
|
|
|
|
|
private:
|
2023-03-17 21:50:39 +03:00
|
|
|
bool commit_time_expired() const { return MonotonicTime::now() - m_timestamp >= COMMAND_COMMIT_TIME; }
|
2022-09-08 00:41:37 +03:00
|
|
|
|
2023-03-17 21:50:39 +03:00
|
|
|
MonotonicTime m_timestamp = MonotonicTime::now();
|
2022-09-08 00:41:37 +03:00
|
|
|
WeakPtr<HexDocument> m_document;
|
|
|
|
size_t m_position;
|
|
|
|
ByteBuffer m_old;
|
|
|
|
ByteBuffer m_new;
|
|
|
|
};
|