| 1 /* |
|
| 2 * botc source code |
|
| 3 * Copyright (C) 2012 Santeri `Dusk` Piippo |
|
| 4 * All rights reserved. |
|
| 5 * |
|
| 6 * Redistribution and use in source and binary forms, with or without |
|
| 7 * modification, are permitted provided that the following conditions are met: |
|
| 8 * |
|
| 9 * 1. Redistributions of source code must retain the above copyright notice, |
|
| 10 * this list of conditions and the following disclaimer. |
|
| 11 * 2. Redistributions in binary form must reproduce the above copyright notice, |
|
| 12 * this list of conditions and the following disclaimer in the documentation |
|
| 13 * and/or other materials provided with the distribution. |
|
| 14 * 3. Neither the name of the developer nor the names of its contributors may |
|
| 15 * be used to endorse or promote products derived from this software without |
|
| 16 * specific prior written permission. |
|
| 17 * 4. Redistributions in any form must be accompanied by information on how to |
|
| 18 * obtain complete source code for the software and any accompanying |
|
| 19 * software that uses the software. The source code must either be included |
|
| 20 * in the distribution or be available for no more than the cost of |
|
| 21 * distribution plus a nominal fee, and must be freely redistributable |
|
| 22 * under reasonable conditions. For an executable file, complete source |
|
| 23 * code means the source code for all modules it contains. It does not |
|
| 24 * include source code for modules or files that typically accompany the |
|
| 25 * major components of the operating system on which the executable file |
|
| 26 * runs. |
|
| 27 * |
|
| 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
| 29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
| 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
| 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
| 32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
| 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
| 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
| 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
| 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
| 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
| 38 * POSSIBILITY OF SUCH DAMAGE. |
|
| 39 */ |
|
| 40 |
|
| 41 #define __OBJWRITER_CXX__ |
|
| 42 #include <stdio.h> |
1 #include <stdio.h> |
| 43 #include <stdlib.h> |
2 #include <stdlib.h> |
| 44 #include <string.h> |
3 #include <string.h> |
| 45 #include "common.h" |
4 #include "main.h" |
| 46 #include "str.h" |
5 #include "str.h" |
| 47 #include "objwriter.h" |
6 #include "objwriter.h" |
| 48 #include "databuffer.h" |
7 #include "databuffer.h" |
| 49 #include "stringtable.h" |
8 #include "stringtable.h" |
| 50 |
9 |
| 51 #include "bots.h" |
10 #include "bots.h" |
| 52 |
11 |
| 53 extern bool g_GotMainLoop; |
12 extern bool g_GotMainLoop; |
| 54 |
13 |
| 55 ObjWriter::ObjWriter (str path) { |
14 ObjWriter::ObjWriter (string path) { |
| 56 MainBuffer = new DataBuffer; |
15 MainBuffer = new DataBuffer; |
| 57 MainLoopBuffer = new DataBuffer; |
16 MainLoopBuffer = new DataBuffer; |
| 58 OnEnterBuffer = new DataBuffer; |
17 OnEnterBuffer = new DataBuffer; |
| 59 SwitchBuffer = NULL; // created on demand |
18 SwitchBuffer = null; // created on demand |
| 60 numWrittenBytes = 0; |
19 numWrittenBytes = 0; |
| 61 numWrittenReferences = 0; |
20 numWrittenReferences = 0; |
| 62 filepath = path; |
21 filepath = path; |
| 63 } |
22 } |
| 64 |
23 |
| 100 g_GotMainLoop = false; |
59 g_GotMainLoop = false; |
| 101 } |
60 } |
| 102 |
61 |
| 103 // Write string table |
62 // Write string table |
| 104 void ObjWriter::WriteStringTable () { |
63 void ObjWriter::WriteStringTable () { |
| 105 unsigned int stringcount = CountStringTable (); |
64 unsigned int stringcount = num_strings_in_table (); |
| 106 if (!stringcount) |
65 if (!stringcount) |
| 107 return; |
66 return; |
| 108 |
67 |
| 109 // Write header |
68 // Write header |
| 110 Write (DH_STRINGLIST); |
69 Write (DH_STRINGLIST); |
| 111 Write (stringcount); |
70 Write (stringcount); |
| 112 |
71 |
| 113 // Write all strings |
72 // Write all strings |
| 114 for (unsigned int a = 0; a < stringcount; a++) |
73 for (unsigned int a = 0; a < stringcount; a++) |
| 115 WriteString (g_StringTable[a]); |
74 WriteString (get_string_table()[a]); |
| 116 } |
75 } |
| 117 |
76 |
| 118 // Write main buffer to file |
77 // Write main buffer to file |
| 119 void ObjWriter::WriteToFile () { |
78 void ObjWriter::WriteToFile () { |
| 120 fp = fopen (filepath, "w"); |
79 fp = fopen (filepath, "w"); |
| 153 (g_CurMode == MODE_MAINLOOP) ? MainLoopBuffer : |
112 (g_CurMode == MODE_MAINLOOP) ? MainLoopBuffer : |
| 154 (g_CurMode == MODE_ONENTER) ? OnEnterBuffer : |
113 (g_CurMode == MODE_ONENTER) ? OnEnterBuffer : |
| 155 MainBuffer; |
114 MainBuffer; |
| 156 } |
115 } |
| 157 |
116 |
| 158 ScriptMark* g_ScriptMark = NULL; |
117 ScriptMark* g_ScriptMark = null; |
| 159 |
118 |
| 160 // Adds a mark |
119 // Adds a mark |
| 161 unsigned int ObjWriter::AddMark (str name) { |
120 unsigned int ObjWriter::AddMark (string name) { |
| 162 return GetCurrentBuffer()->AddMark (name); |
121 return GetCurrentBuffer()->AddMark (name); |
| 163 } |
122 } |
| 164 |
123 |
| 165 // Adds a reference |
124 // Adds a reference |
| 166 unsigned int ObjWriter::AddReference (unsigned int mark) { |
125 unsigned int ObjWriter::AddReference (unsigned int mark) { |
| 167 DataBuffer* b = GetCurrentBuffer(); |
126 DataBuffer* b = GetCurrentBuffer(); |
| 168 return b->AddMarkReference (mark); |
127 return b->AddMarkReference (mark); |
| 169 } |
128 } |
| 170 |
129 |
| 171 // Finds a mark |
130 // Finds a mark |
| 172 unsigned int ObjWriter::FindMark (str name) { |
131 unsigned int ObjWriter::FindMark (string name) { |
| 173 DataBuffer* b = GetCurrentBuffer(); |
132 DataBuffer* b = GetCurrentBuffer(); |
| 174 for (unsigned int u = 0; u < MAX_MARKS; u++) { |
133 for (unsigned int u = 0; u < MAX_MARKS; u++) { |
| 175 if (b->marks[u] && !b->marks[u]->name.icompare (name)) |
134 if (b->marks[u] && b->marks[u]->name.to_uppercase() == name.to_uppercase()) |
| 176 return u; |
135 return u; |
| 177 } |
136 } |
| 178 return MAX_MARKS; |
137 return MAX_MARKS; |
| 179 } |
138 } |
| 180 |
139 |