1 #include <stdio.h> |
|
2 #include <stdlib.h> |
|
3 #include <string.h> |
|
4 #include "main.h" |
|
5 #include "str.h" |
|
6 #include "objwriter.h" |
|
7 #include "databuffer.h" |
|
8 #include "stringtable.h" |
|
9 |
|
10 #include "bots.h" |
|
11 |
|
12 extern bool g_GotMainLoop; |
|
13 |
|
14 ObjWriter::ObjWriter (string path) { |
|
15 MainBuffer = new DataBuffer; |
|
16 MainLoopBuffer = new DataBuffer; |
|
17 OnEnterBuffer = new DataBuffer; |
|
18 SwitchBuffer = null; // created on demand |
|
19 numWrittenBytes = 0; |
|
20 numWrittenReferences = 0; |
|
21 filepath = path; |
|
22 } |
|
23 |
|
24 void ObjWriter::WriteString (char* s) { |
|
25 Write (strlen (s)); |
|
26 for (unsigned int u = 0; u < strlen (s); u++) |
|
27 Write ((s)[u]); |
|
28 } |
|
29 |
|
30 void ObjWriter::WriteString (const char* s) { |
|
31 WriteString (const_cast<char*> (s)); |
|
32 } |
|
33 |
|
34 void ObjWriter::WriteString (string s) { |
|
35 WriteString (s.chars()); |
|
36 } |
|
37 |
|
38 void ObjWriter::WriteBuffer (DataBuffer* buf) { |
|
39 GetCurrentBuffer()->Merge (buf); |
|
40 } |
|
41 |
|
42 void ObjWriter::WriteBuffers () { |
|
43 // If there was no mainloop defined, write a dummy one now. |
|
44 if (!g_GotMainLoop) { |
|
45 MainLoopBuffer->Write (DH_MAINLOOP); |
|
46 MainLoopBuffer->Write (DH_ENDMAINLOOP); |
|
47 } |
|
48 |
|
49 // Write the onenter and mainloop buffers, IN THAT ORDER |
|
50 for (int i = 0; i < 2; i++) { |
|
51 DataBuffer** buf = (!i) ? &OnEnterBuffer : &MainLoopBuffer; |
|
52 WriteBuffer (*buf); |
|
53 |
|
54 // Clear the buffer afterwards for potential next state |
|
55 *buf = new DataBuffer; |
|
56 } |
|
57 |
|
58 // Next state definitely has no mainloop yet |
|
59 g_GotMainLoop = false; |
|
60 } |
|
61 |
|
62 // Write string table |
|
63 void ObjWriter::WriteStringTable () { |
|
64 unsigned int stringcount = num_strings_in_table (); |
|
65 if (!stringcount) |
|
66 return; |
|
67 |
|
68 // Write header |
|
69 Write (DH_STRINGLIST); |
|
70 Write (stringcount); |
|
71 |
|
72 // Write all strings |
|
73 for (unsigned int a = 0; a < stringcount; a++) |
|
74 WriteString (get_string_table()[a]); |
|
75 } |
|
76 |
|
77 // Write main buffer to file |
|
78 void ObjWriter::WriteToFile () { |
|
79 fp = fopen (filepath, "w"); |
|
80 CHECK_FILE (fp, filepath, "writing"); |
|
81 |
|
82 // First, resolve references |
|
83 numWrittenReferences = 0; |
|
84 for (unsigned int u = 0; u < MAX_MARKS; u++) { |
|
85 ScriptMarkReference* ref = MainBuffer->refs[u]; |
|
86 if (!ref) |
|
87 continue; |
|
88 |
|
89 // Substitute the placeholder with the mark position |
|
90 union_t<word> uni; |
|
91 uni.val = static_cast<word> (MainBuffer->marks[ref->num]->pos); |
|
92 for (unsigned int v = 0; v < sizeof (word); v++) |
|
93 memset (MainBuffer->buffer + ref->pos + v, uni.b[v], 1); |
|
94 |
|
95 /* |
|
96 printf ("reference %u at %d resolved to %u at %d\n", |
|
97 u, ref->pos, ref->num, MainBuffer->marks[ref->num]->pos); |
|
98 */ |
|
99 numWrittenReferences++; |
|
100 } |
|
101 |
|
102 // Then, dump the main buffer to the file |
|
103 for (unsigned int x = 0; x < MainBuffer->writesize; x++) |
|
104 WriteDataToFile<byte> (*(MainBuffer->buffer+x)); |
|
105 |
|
106 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars()); |
|
107 fclose (fp); |
|
108 } |
|
109 |
|
110 DataBuffer* ObjWriter::GetCurrentBuffer() { |
|
111 return SwitchBuffer ? SwitchBuffer : |
|
112 (g_CurMode == MODE_MAINLOOP) ? MainLoopBuffer : |
|
113 (g_CurMode == MODE_ONENTER) ? OnEnterBuffer : |
|
114 MainBuffer; |
|
115 } |
|
116 |
|
117 ScriptMark* g_ScriptMark = null; |
|
118 |
|
119 // Adds a mark |
|
120 unsigned int ObjWriter::AddMark (string name) { |
|
121 return GetCurrentBuffer()->AddMark (name); |
|
122 } |
|
123 |
|
124 // Adds a reference |
|
125 unsigned int ObjWriter::AddReference (unsigned int mark) { |
|
126 DataBuffer* b = GetCurrentBuffer(); |
|
127 return b->AddMarkReference (mark); |
|
128 } |
|
129 |
|
130 // Finds a mark |
|
131 unsigned int ObjWriter::FindMark (string name) { |
|
132 DataBuffer* b = GetCurrentBuffer(); |
|
133 for (unsigned int u = 0; u < MAX_MARKS; u++) { |
|
134 if (b->marks[u] && b->marks[u]->name.to_uppercase() == name.to_uppercase()) |
|
135 return u; |
|
136 } |
|
137 return MAX_MARKS; |
|
138 } |
|
139 |
|
140 // Moves a mark to the current position |
|
141 void ObjWriter::MoveMark (unsigned int mark) { |
|
142 GetCurrentBuffer()->MoveMark (mark); |
|
143 } |
|
144 |
|
145 // Deletes a mark |
|
146 void ObjWriter::DeleteMark (unsigned int mark) { |
|
147 GetCurrentBuffer()->DeleteMark (mark); |
|
148 } |
|
149 |
|
150 void ObjWriter::OffsetMark (unsigned int mark, int offset) { |
|
151 GetCurrentBuffer()->OffsetMark (mark, offset); |
|
152 } |
|