Update README.md
[tar-legacy.git] / MCDV / wc.hpp
1 #pragma once
2 #include <string>
3 #include <iostream>
4 #include <fstream>
5 #include <stdint.h>
6 #include <vector>
7
8 namespace wc
9 {
10 #pragma pack(push, 1)
11
12 struct Command{
13 int is_enabled; // 0/1, If command is enabled.
14 int special;
15 char executable[260]; // Name of EXE to run.
16 char args[260]; // Arguments for executable.
17 int is_long_filename; // Obsolete, but always set to true. Disables MS-DOS 8-char filenames.
18 int ensure_check; // Ensure file post-exists after compilation
19 char ensure_file[260]; // File to check exists.
20 int use_proc_win; // Use Process Window (ignored if exectuable = $game_exe).
21
22 // V 0.2+ only:
23 int no_wait; // Wait for keypress when done compiling.
24 };
25
26 struct SequenceHeader
27 {
28 char name[128]{ '\0' };
29 uint32_t command_count; // Number of commands
30 };
31
32 struct Sequence
33 {
34 char name[128];
35 std::vector<Command> commands;
36 bool write_enable = true;
37 };
38
39 struct Header {
40 char signature[31] = { 'W','o','r','l','d','c','r','a','f','t',' ','C','o','m','m','a','n','d',' ','S','e','q','u','e','n','c','e','s','\r','\n','\x1a' }; // Yikes.
41 float version = 0.2f;
42 uint32_t seq_count;
43 };
44
45 #pragma pack(pop)
46
47 class filedata
48 {
49 public:
50 std::vector<Sequence> sequences;
51
52 filedata(std::string path)
53 {
54 std::ifstream reader(path, std::ios::in | std::ios::binary);
55
56 if (!reader) {
57 throw std::exception("WC::LOAD Failed"); return;
58 }
59
60 Header header = Header();
61 reader.read((char*)&header, sizeof(header));
62
63 for (int i = 0; i < header.seq_count; i++)
64 {
65 Sequence sequence = Sequence();
66 reader.read((char*)&sequence.name, 128);
67
68 uint32_t command_count;
69 reader.read((char*)&command_count, sizeof(uint32_t));
70
71 if (command_count > 1024) {
72 throw std::exception("Too many commands!!!");
73 }
74
75 for (int cc = 0; cc < command_count; cc++)
76 {
77 Command command = Command();
78
79 reader.read((char*)&command, sizeof(Command));
80
81 sequence.commands.push_back(command);
82 }
83
84 this->sequences.push_back(sequence);
85 }
86
87 reader.close();
88 }
89
90 void serialize(std::string path)
91 {
92 std::fstream writer(path, std::ios::out | std::ios::binary);
93
94 // Write header
95 Header header = Header();
96
97 int count = sequences.size();
98 for (auto && seq : this->sequences) if (!seq.write_enable) count--;
99
100 header.seq_count = count;
101 writer.write((char*)&header, sizeof(header));
102
103 // Write Sequences
104 for (auto && sequence : this->sequences){
105
106 if (!sequence.write_enable) continue;
107
108 writer.write((char*)&sequence.name, 128);
109 uint32_t cmdCount = sequence.commands.size();
110
111 writer.write((char*)&cmdCount, sizeof(uint32_t));
112
113 for (auto && command : sequence.commands)
114 {
115 writer.write((char*)&command, sizeof(command));
116 }
117 }
118
119 writer.close();
120 }
121 };
122 }