1 /* |
|
2 Copyright (c) 2013-2014, Santeri Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions are met: |
|
7 |
|
8 * Redistributions of source code must retain the above copyright |
|
9 notice, this list of conditions and the following disclaimer. |
|
10 |
|
11 * Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 |
|
15 * Neither the name of the <organization> nor the |
|
16 names of its contributors may be used to endorse or promote products |
|
17 derived from this software without specific prior written permission. |
|
18 |
|
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
|
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #include "main.h" |
|
32 #include "str.h" |
|
33 #include "object_writer.h" |
|
34 #include "data_buffer.h" |
|
35 #include "stringtable.h" |
|
36 #include "bots.h" |
|
37 |
|
38 extern bool g_GotMainLoop; |
|
39 |
|
40 object_writer::ObjectWriter (string path) |
|
41 { |
|
42 MainBuffer = new data_buffer; |
|
43 MainLoopBuffer = new data_buffer; |
|
44 OnEnterBuffer = new data_buffer; |
|
45 SwitchBuffer = null; // created on demand |
|
46 numWrittenBytes = 0; |
|
47 numWrittenReferences = 0; |
|
48 } |
|
49 |
|
50 void object_writer::write_string (string a) |
|
51 { |
|
52 write (a.length()); |
|
53 |
|
54 for (int i = 0; i < a.length(); i++) |
|
55 write (a[i]); |
|
56 } |
|
57 |
|
58 void object_writer::write_buffer (data_buffer* buf) |
|
59 { |
|
60 get_current_buffer()->merge (buf); |
|
61 } |
|
62 |
|
63 void object_writer::write_member_buffers() |
|
64 { |
|
65 // If there was no mainloop defined, write a dummy one now. |
|
66 if (!g_GotMainLoop) |
|
67 { |
|
68 MainLoopBuffer->write (DH_MAINLOOP); |
|
69 MainLoopBuffer->write (DH_ENDMAINLOOP); |
|
70 } |
|
71 |
|
72 // Write the onenter and mainloop buffers, IN THAT ORDER |
|
73 for (int i = 0; i < 2; i++) |
|
74 { |
|
75 data_buffer** buf = (!i) ? &OnEnterBuffer : &MainLoopBuffer; |
|
76 write_buffer (*buf); |
|
77 |
|
78 // Clear the buffer afterwards for potential next state |
|
79 *buf = new data_buffer; |
|
80 } |
|
81 |
|
82 // Next state definitely has no mainloop yet |
|
83 g_GotMainLoop = false; |
|
84 } |
|
85 |
|
86 // Write string table |
|
87 void object_writer::write_string_table() |
|
88 { |
|
89 int stringcount = num_strings_in_table(); |
|
90 |
|
91 if (!stringcount) |
|
92 return; |
|
93 |
|
94 // Write header |
|
95 write (DH_STRINGLIST); |
|
96 write (stringcount); |
|
97 |
|
98 // Write all strings |
|
99 for (int a = 0; a < stringcount; a++) |
|
100 write_string (get_string_table() [a]); |
|
101 } |
|
102 |
|
103 // Write main buffer to file |
|
104 void object_writer::write_to_file (string filepath) |
|
105 { |
|
106 fp = fopen (filepath, "w"); |
|
107 CHECK_FILE (fp, filepath, "writing"); |
|
108 |
|
109 // First, resolve references |
|
110 numWrittenReferences = 0; |
|
111 |
|
112 for (int u = 0; u < MAX_MARKS; u++) |
|
113 { |
|
114 ScriptMarkReference* ref = MainBuffer->refs[u]; |
|
115 |
|
116 if (!ref) |
|
117 continue; |
|
118 |
|
119 // Substitute the placeholder with the mark position |
|
120 generic_union<word> uni; |
|
121 uni.as_word = static_cast<word> (MainBuffer->marks[ref->num]->pos); |
|
122 |
|
123 for (int v = 0; v < sizeof (word); v++) |
|
124 memset (MainBuffer->buffer + ref->pos + v, uni.as_bytes[v], 1); |
|
125 |
|
126 /* |
|
127 printf ("reference %u at %d resolved to %u at %d\n", |
|
128 u, ref->pos, ref->num, MainBuffer->marks[ref->num]->pos); |
|
129 */ |
|
130 numWrittenReferences++; |
|
131 } |
|
132 |
|
133 // Then, dump the main buffer to the file |
|
134 for (int x = 0; x < MainBuffer->writesize; x++) |
|
135 write_data_to_file<byte> (*(MainBuffer->buffer + x)); |
|
136 |
|
137 print ("-- %1 byte%s1 written to %2\n", numWrittenBytes, filepath); |
|
138 fclose (fp); |
|
139 } |
|
140 |
|
141 data_buffer* object_writer::get_current_buffer() |
|
142 { |
|
143 return SwitchBuffer ? SwitchBuffer : |
|
144 (g_CurMode == MODE_MAINLOOP) ? MainLoopBuffer : |
|
145 (g_CurMode == MODE_ONENTER) ? OnEnterBuffer : |
|
146 MainBuffer; |
|
147 } |
|
148 |
|
149 ScriptMark* g_ScriptMark = null; |
|
150 |
|
151 // Adds a mark |
|
152 int object_writer::add_mark (string name) |
|
153 { |
|
154 return get_current_buffer()->add_mark (name); |
|
155 } |
|
156 |
|
157 // Adds a reference |
|
158 int object_writer::add_reference (int mark) |
|
159 { |
|
160 data_buffer* b = get_current_buffer(); |
|
161 return b->add_reference (mark); |
|
162 } |
|
163 |
|
164 // Finds a mark |
|
165 int object_writer::find_byte_mark (string name) |
|
166 { |
|
167 data_buffer* b = get_current_buffer(); |
|
168 |
|
169 for (int u = 0; u < MAX_MARKS; u++) |
|
170 { |
|
171 if (b->marks[u] && b->marks[u]->name.to_uppercase() == name.to_uppercase()) |
|
172 return u; |
|
173 } |
|
174 |
|
175 return MAX_MARKS; |
|
176 } |
|
177 |
|
178 // Moves a mark to the current position |
|
179 void object_writer::move_mark (int mark) |
|
180 { |
|
181 get_current_buffer()->move_mark (mark); |
|
182 } |
|
183 |
|
184 // Deletes a mark |
|
185 void object_writer::delete_mark (int mark) |
|
186 { |
|
187 get_current_buffer()->delete_mark (mark); |
|
188 } |
|
189 |
|
190 void object_writer::offset_mark (int mark, int offset) |
|
191 { |
|
192 get_current_buffer()->offset_mark (mark, offset); |
|
193 } |
|