| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/editor/ScriptCompilerAPI.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003-08-26 03:57:38 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Implementation files for for the script compiler API |
| 26 | * |
| 27 | * $Log: not supported by cvs2svn $ |
| 28 | * |
| 29 | * 16 6/17/99 10:24a Kevin |
| 30 | * Made things work in a release build |
| 31 | * |
| 32 | * 15 6/08/99 4:14p Jeff |
| 33 | * added some #ifdefs for new editor, so it can be used in the new editor |
| 34 | * project |
| 35 | * |
| 36 | * 14 2/23/99 11:52p Jeff |
| 37 | * new script dll sync dialog |
| 38 | * |
| 39 | * 13 2/17/99 6:54p Jeff |
| 40 | * added check box for auto check in/out scripts with levels...fix |
| 41 | * checksum mprintf bug |
| 42 | * |
| 43 | * 12 2/17/99 3:24a Jeff |
| 44 | * added checksum parameter |
| 45 | * |
| 46 | * 11 1/15/99 5:27p Jeff |
| 47 | * possible long filename fix |
| 48 | * |
| 49 | * 10 1/13/99 5:00p Jeff |
| 50 | * auto generate string table stuff |
| 51 | * |
| 52 | * 9 1/05/99 4:33p Jeff |
| 53 | * oops, forgot STDCALL when making SaveRestoreState |
| 54 | * |
| 55 | * 8 12/31/98 2:37p Jeff |
| 56 | * added SaveRestoreState import function |
| 57 | * |
| 58 | * 7 12/21/98 6:21p Jeff |
| 59 | * added comments to generated blank scripts |
| 60 | * |
| 61 | * 6 12/18/98 4:32p Jeff |
| 62 | * fixed code that creates empty script |
| 63 | * |
| 64 | * 5 12/18/98 2:37p Jeff |
| 65 | * updated the create new script file |
| 66 | * |
| 67 | * 4 12/14/98 11:32a Jeff |
| 68 | * started work on osiris load and bind functions |
| 69 | * |
| 70 | * 3 12/13/98 7:50p Jeff |
| 71 | * automatically add new DLLs that aren't in the manage |
| 72 | * system...implemented create new script function, even though it's still |
| 73 | * in infancy. |
| 74 | * |
| 75 | * 2 12/13/98 3:09a Jeff |
| 76 | * Initial creation, added ScriptCompile() function |
| 77 | * |
| 78 | * $NoKeywords: $ |
| 79 | */ |
| 80 | |
| 81 | #include "stdafx.h" |
| 82 | #include "ScriptCompilerAPI.h" |
| 83 | #include "appdatabase.h" |
| 84 | #include "descent.h" |
| 85 | #include "cfile.h" |
| 86 | #include "manage.h" |
| 87 | #include "pserror.h" |
| 88 | #include "mem.h" |
| 89 | #include "ddio.h" |
| 90 | #include "osiris_dll.h" |
| 91 | #include <stdarg.h> |
| 92 | |
| 93 | #ifdef NEWEDITOR |
| 94 | #include "../neweditor/globals.h" |
| 95 | #endif |
| 96 | |
| 97 | int ScriptCompile(tCompilerInfo *ci) { |
| 98 | char Compiler_path[_MAX_PATH]; |
| 99 | int Warning_level = -1; |
| 100 | int Debug_type = -1; |
| 101 | int len = _MAX_PATH; |
| 102 | |
| 103 | if (!cfexist(ci->source_filename)) |
| 104 | return CERR_SOURCENOEXIST; |
| 105 | |
| 106 | #ifndef NEWEDITOR |
| 107 | // make sure there is a compiler defined |
| 108 | if (Database->read("EditorCompiler", Compiler_path, &len)) { |
| 109 | if (!cfexist(Compiler_path)) { |
| 110 | OutrageMessageBox( |
| 111 | "The configured virtual compiler (%s)\ncannot be found. Please make sure\nyou have specified a proper path " |
| 112 | "and\nthe file exists. Go into the Script and\nLevel dialog to configure your compiler.", |
| 113 | Compiler_path); |
| 114 | return CERR_COMPILERMISSING; |
| 115 | } |
| 116 | } else { |
| 117 | OutrageMessageBox( |
| 118 | "You haven't configured a virtual compiler to use.\nPlease go into the Script and Level Interface\nof the " |
| 119 | "editor and select the configure button\nto configure a virtual compiler to use.\nFind Jeff for help on this."); |
| 120 | return CERR_NOCOMPILERDEFINED; |
| 121 | } |
| 122 | |
| 123 | Database->read_int("EditorVCWarningLevel", &Warning_level); |
| 124 | Database->read_int("EditorVCDebugLevel", &Debug_type); |
| 125 | #else |
| 126 | // New Editor setup |
| 127 | |
| 128 | // Determine the compiler |
| 129 | CString cs_Compiler_Path = AfxGetApp()->GetProfileString("settings", "EditorCompiler", ""); |
| 130 | if (cs_Compiler_Path.GetLength() > 1) { |
| 131 | if (!cfexist(cs_Compiler_Path.GetBuffer(0))) { |
| 132 | OutrageMessageBox( |
| 133 | "The configured virtual compiler (%s)\ncannot be found. Please make sure\nyou have specified a proper path " |
| 134 | "and\nthe file exists. Select the configure button\nto configure your virtual compiler.", |
| 135 | cs_Compiler_Path.GetBuffer(0)); |
| 136 | return CERR_COMPILERMISSING; |
| 137 | } else { |
| 138 | // valid compiler |
| 139 | strcpy(Compiler_path, cs_Compiler_Path.GetBuffer(0)); |
| 140 | } |
| 141 | } else { |
| 142 | OutrageMessageBox("You haven't configured a virtual compiler to use.\nPlease select the configure option\nto " |
| 143 | "configure a virtual compiler to use."); |
| 144 | return CERR_NOCOMPILERDEFINED; |
| 145 | } |
| 146 | |
| 147 | // Determine the Warning level |
| 148 | Warning_level = AfxGetApp()->GetProfileInt("settings", "EditorVCWarningLevel", 3); |
| 149 | ASSERT(Warning_level >= 0 && Warning_level <= 4); |
| 150 | if (Warning_level < 0 || Warning_level > 4) |
| 151 | Warning_level = 3; |
| 152 | |
| 153 | // Determine the Debug type |
| 154 | Debug_type = AfxGetApp()->GetProfileInt("settings", "EditorVCDebugLevel", 2); |
| 155 | ASSERT(Debug_type >= 0 && Debug_type <= 2); |
| 156 | if (Debug_type < 0 || Debug_type > 2) |
| 157 | Debug_type = 2; |
| 158 | |
| 159 | #endif |
| 160 | |
| 161 | SECURITY_ATTRIBUTES sa = {0}; |
| 162 | STARTUPINFO si = {0}; |
| 163 | PROCESS_INFORMATION pi = {0}; |
| 164 | HANDLE hPipeOutputRead = NULL; |
| 165 | HANDLE hPipeOutputWrite = NULL; |
| 166 | HANDLE hPipeInputRead = NULL; |
| 167 | HANDLE hPipeInputWrite = NULL; |
| 168 | BOOL bTest = 0; |
| 169 | DWORD dwNumberOfBytesRead = 0; |
| 170 | CHAR szBuffer[256]; |
| 171 | |
| 172 | sa.nLength = sizeof(sa); |
| 173 | sa.bInheritHandle = TRUE; |
| 174 | sa.lpSecurityDescriptor = NULL; |
| 175 | |
| 176 | // Create pipe for standard output redirection. |
| 177 | CreatePipe(&hPipeOutputRead, // read handle |
| 178 | &hPipeOutputWrite, // write handle |
| 179 | &sa, // security attributes |
| 180 | 0 // number of bytes reserved for pipe - 0 default |
| 181 | ); // Create pipe for standard input redirection. |
| 182 | |
| 183 | CreatePipe(&hPipeInputRead, // read handle |
| 184 | &hPipeInputWrite, // write handle |
| 185 | &sa, // security attributes |
| 186 | 0 // number of bytes reserved for pipe - 0 default |
| 187 | ); |
| 188 | |
| 189 | // Make child process use hPipeOutputWrite as standard out, |
| 190 | // and make sure it does not show on screen. |
| 191 | si.cb = sizeof(si); |
| 192 | si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; |
| 193 | si.wShowWindow = SW_HIDE; |
| 194 | si.hStdInput = hPipeInputRead; |
| 195 | si.hStdOutput = hPipeOutputWrite; |
| 196 | si.hStdError = hPipeOutputWrite; |
| 197 | |
| 198 | CString cline; |
| 199 | |
| 200 | cline = Compiler_path; |
| 201 | cline += " -f "; |
| 202 | cline += "\""; |
| 203 | cline += ci->source_filename; |
| 204 | cline += "\""; |
| 205 | cline += " -dir "; |
| 206 | cline += LocalScriptDir; |
| 207 | if (ci->script_type == ST_LEVEL) |
| 208 | cline += " -level"; |
| 209 | switch (Warning_level) { |
| 210 | case 0: |
| 211 | cline += " -w 0"; |
| 212 | break; |
| 213 | case 1: |
| 214 | cline += " -w 1"; |
| 215 | break; |
| 216 | case 2: |
| 217 | cline += " -w 2"; |
| 218 | break; |
| 219 | case 3: |
| 220 | cline += " -w 3"; |
| 221 | break; |
| 222 | case 4: |
| 223 | cline += " -w 4"; |
| 224 | break; |
| 225 | default: |
| 226 | cline += " -w 3"; |
| 227 | break; |
| 228 | } |
| 229 | switch (Debug_type) { |
| 230 | case 1: |
| 231 | cline += " -d coff"; |
| 232 | break; |
| 233 | case 2: |
| 234 | cline += " -d c7"; |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | // Game structure checksum |
| 239 | #ifndef NEWEDITOR |
| 240 | char temp_c[128]; |
| 241 | sprintf(temp_c, " -checksum %lu", Osiris_game_checksum); |
| 242 | cline += temp_c; |
| 243 | #endif |
| 244 | |
| 245 | CreateProcess(NULL, cline.GetBuffer(0), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); |
| 246 | |
| 247 | // Now that handles have been inherited, close it to be safe. |
| 248 | // You don't want to read or write to them accidentally. |
| 249 | CloseHandle(hPipeOutputWrite); |
| 250 | CloseHandle(hPipeInputRead); |
| 251 | |
| 252 | // Now test to capture DOS application output by reading |
| 253 | // hPipeOutputRead. Could also write to DOS application |
| 254 | // standard input by writing to hPipeInputWrite. |
| 255 | CString temp; |
| 256 | while (TRUE) { |
| 257 | bTest = ReadFile(hPipeOutputRead, // handle of the read end of our pipe |
| 258 | &szBuffer, // address of buffer that receives data |
| 259 | 256, // number of bytes to read |
| 260 | &dwNumberOfBytesRead, // address of number of bytes read |
| 261 | NULL // non-overlapped. |
| 262 | ); |
| 263 | |
| 264 | if (!bTest) { |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | // do something with data. |
| 269 | szBuffer[dwNumberOfBytesRead] = 0; // null terminate |
| 270 | |
| 271 | if (ci->callback) { |
| 272 | int new_line_count = 0; |
| 273 | char *ptr = szBuffer; |
| 274 | while (*ptr) { |
| 275 | if (*ptr == '\n') |
| 276 | new_line_count++; |
| 277 | ptr++; |
| 278 | } |
| 279 | char *data, *d_ptr; |
| 280 | d_ptr = data = (char *)mem_malloc(dwNumberOfBytesRead + 1 + new_line_count); |
| 281 | if (data) { |
| 282 | ptr = (char *)szBuffer; |
| 283 | |
| 284 | while (*ptr) { |
| 285 | if (*ptr == '\n') { |
| 286 | *d_ptr = '\r'; |
| 287 | d_ptr++; |
| 288 | *d_ptr = '\n'; |
| 289 | d_ptr++; |
| 290 | } else if (*ptr != '\r') { |
| 291 | *d_ptr = *ptr; |
| 292 | d_ptr++; |
| 293 | } |
| 294 | ptr++; |
| 295 | } |
| 296 | *d_ptr = '\0'; |
| 297 | |
| 298 | (*ci->callback)(data); |
| 299 | mem_free(data); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // Wait for spawn to finish. |
| 305 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 306 | |
| 307 | // Close all remaining handles |
| 308 | CloseHandle(pi.hProcess); |
| 309 | CloseHandle(hPipeOutputRead); |
| 310 | CloseHandle(hPipeInputWrite); |
| 311 | |
| 312 | return CERR_NOERR; |
| 313 | } |
| 314 | |
| 315 | bool ScriptCreateEmptyLevelScript(char *filename); |
| 316 | bool ScriptCreateEmptyGameScript(char *filename); |
| 317 | bool ScriptCreateEmptyScript(char *filename, uint8_t script_type) { |
| 318 | if (cfexist(filename)) { |
| 319 | char buffer[512]; |
| 320 | sprintf(buffer, "A script by the name of %s\nalready exists.", filename); |
| 321 | AfxMessageBox(buffer, MB_OK); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | switch (script_type) { |
| 326 | case ST_GAME: |
| 327 | return ScriptCreateEmptyGameScript(filename); |
| 328 | break; |
| 329 | case ST_LEVEL: |
| 330 | return ScriptCreateEmptyLevelScript(filename); |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | #define VERSION "0.1" |
| 338 | #define O(x) outputtofile x |
| 339 | CFILE *CurrentFile; |
| 340 | |
| 341 | void outputtofile(char *format, ...) { |
| 342 | char buffer[1024]; |
| 343 | va_list marker; |
| 344 | va_start(marker, format); |
| 345 | std::vsprintf(buffer, format, marker); |
| 346 | cf_WriteString(CurrentFile, buffer); |
| 347 | va_end(marker); |
| 348 | } |
| 349 | |
| 350 | bool ScriptCreateEmptyLevelScript(char *filename) { |
| 351 | char fullpath[_MAX_PATH]; |
| 352 | ddio_MakePath(fullpath, LocalScriptDir, filename, NULL); |
| 353 | CurrentFile = cfopen(fullpath, "wt"); |
| 354 | if (!CurrentFile) |
| 355 | return false; |
| 356 | |
| 357 | O(("// %s", filename)); |
| 358 | O(("// %s", VERSION)); |
| 359 | O(("#include <stdio.h>")); |
| 360 | O(("#include <stdlib.h>")); |
| 361 | O(("#include <string.h>")); |
| 362 | O(("#include \"osiris_import.h\"")); |
| 363 | O(("#include \"osiris_common.h\"")); |
| 364 | O(("")); |
| 365 | |
| 366 | O(("#ifdef _MSC_VER //Visual C++ Build")); |
| 367 | O(("#define STDCALL __stdcall")); |
| 368 | O(("#define STDCALLPTR *STDCALL")); |
| 369 | O(("#else //Non-Visual C++ Build")); |
| 370 | O(("#define STDCALL __attribute__((stdcall))")); |
| 371 | O(("#define STDCALLPTR STDCALL*")); |
| 372 | O(("#endif")); |
| 373 | O(("")); |
| 374 | |
| 375 | O(("#ifdef __cplusplus")); |
| 376 | O(("extern \"C\"{")); |
| 377 | O(("#endif")); |
| 378 | O(("char STDCALL InitializeDLL(tOSIRISModuleInit *func_list);")); |
| 379 | O(("void STDCALL ShutdownDLL(void);")); |
| 380 | O(("int STDCALL GetGOScriptID(const char *name,uint8_t is_door);")); |
| 381 | O(("void STDCALLPTR CreateInstance(int id);")); |
| 382 | O(("void STDCALL DestroyInstance(int id,void *ptr);")); |
| 383 | O(("int16_t STDCALL CallInstanceEvent(int id,void *ptr,int event,tOSIRISEventInfo *data);")); |
| 384 | O(("int STDCALL GetTriggerScriptID(int trigger_room, int trigger_face );")); |
| 385 | O(("int STDCALL GetCOScriptList( int **list, int **id_list );")); |
| 386 | O(("int STDCALL SaveRestoreState( void *file_ptr, uint8_t saving_state );")); |
| 387 | O(("#ifdef __cplusplus")); |
| 388 | O(("}")); |
| 389 | O(("#endif")); |
| 390 | O(("")); |
| 391 | |
| 392 | O(("int String_table_size = 0;")); |
| 393 | O(("char **String_table = NULL;")); |
| 394 | O(("static const char *_Error_string = \"!!ERROR MISSING STRING!!\";")); |
| 395 | O(("static const char *_Empty_string = \"\";")); |
| 396 | O(("const char *GetStringFromTable(int index)")); |
| 397 | O(("{")); |
| 398 | O((" if( (index<0) || (index>=String_table_size) )")); |
| 399 | O((" return _Error_string;")); |
| 400 | O((" if(!String_table[index])")); |
| 401 | O((" return _Empty_string;")); |
| 402 | O((" return String_table[index];")); |
| 403 | O(("}")); |
| 404 | O(("#define TXT(x) GetStringFromTable(x)")); |
| 405 | |
| 406 | O(("// InitializeDLL")); |
| 407 | O(("// Purpose:")); |
| 408 | O(("// This function gets called when the DLL first gets loaded. It will only be called once (until " |
| 409 | "the")); |
| 410 | O(("// DLL is unloaded). Passed in is a struct of data passed from the game needed for the DLL to interact")); |
| 411 | O(("// with D3. Usually this function will just call osicommon_Initialize(), which sets up the imported")); |
| 412 | O(("// functions. However, you can alloc some memory or whatever in this function, and free it in " |
| 413 | "ShutdownDLL().")); |
| 414 | O(("// Note: You cannot call any imported functions until osicommon_Initialize() is called.")); |
| 415 | O(("// Returns 1 if initialization went ok, 0 if there was an error and the DLL should not be loaded.")); |
| 416 | O(("char STDCALL InitializeDLL(tOSIRISModuleInit *func_list)")); |
| 417 | O(("{")); |
| 418 | O((" osicommon_Initialize(func_list);")); |
| 419 | O((" String_table_size = func_list->string_count;")); |
| 420 | O((" String_table = func_list->string_table;")); |
| 421 | O((" if(func_list->game_checksum!=CHECKSUM)")); |
| 422 | O((" { ")); |
| 423 | O((" mprintf(0,\"Game-Checksum FAIL!!! (%%ul!=%%ul)\\n\",func_list->game_checksum,CHECKSUM);")); |
| 424 | O((" mprintf(0,\"RECOMPILE YOUR SCRIPTS!!!\\n\");")); |
| 425 | O((" return 0;")); |
| 426 | O((" }")); |
| 427 | O((" return 1;")); |
| 428 | O(("}")); |
| 429 | O(("")); |
| 430 | |
| 431 | O(("// ShutdownDLL")); |
| 432 | O(("// Purpose:")); |
| 433 | O(("// This function is called right before a DLL is about to be unloaded from memory. You can " |
| 434 | "free")); |
| 435 | O(("// any unfree'd memory, or anything else you need to do. Don't worry about destroying any instances")); |
| 436 | O(("// of scripts, as they will all be automatically destroyed before this function is called. The")); |
| 437 | O(("// same goes for any memory allocated with Scrpt_MemAlloc(), as this will automatically be freed")); |
| 438 | O(("// when a scripts instance is destroyed.")); |
| 439 | O(("void STDCALL ShutdownDLL(void)")); |
| 440 | O(("{")); |
| 441 | O(("}")); |
| 442 | O(("")); |
| 443 | |
| 444 | O(("// GetGOScriptID")); |
| 445 | O(("// Purpose:")); |
| 446 | O(("// Given the name of the object (from it's pagename), this function will search through it's")); |
| 447 | O(("// list of General Object Scripts for a script with a matching name (to see if there is a script")); |
| 448 | O(("// for that type/id of object within this DLL). If a matching scriptname is found, a UNIQUE ID")); |
| 449 | O(("// is to be returned back to Descent 3. This ID will be used from here on out for all future")); |
| 450 | O(("// interaction with the DLL. Since doors are not part of the generic object's, it's possible")); |
| 451 | O(("// for a door to have the same name as a generic object (OBJ_POWERUP, OBJ_BUILDING, OBJ_CLUTTER")); |
| 452 | O(("// or OBJ_ROBOT), therefore, a 1 is passed in for isdoor if the given object name refers to a")); |
| 453 | O(("// door, else it is a 0. The return value is the unique identifier, else -1 if the script")); |
| 454 | O(("// does not exist in the DLL.")); |
| 455 | O(("// The only reserved ID is 0, which must be used for the level script")); |
| 456 | O(("int STDCALL GetGOScriptID(const char *name,uint8_t isdoor)")); |
| 457 | O(("{")); |
| 458 | O((" return -1;")); |
| 459 | O(("}")); |
| 460 | O(("")); |
| 461 | |
| 462 | O(("// CreateInstance")); |
| 463 | O(("// Purpose:")); |
| 464 | O(("// Given an ID from a call to GetGOScriptID(), GetTriggerScriptID() or GetCOScriptList(), this")); |
| 465 | O(("// function will create a new instance for that particular script (by allocating and initializing")); |
| 466 | O(("// memory, etc.). A pointer to this instance is to be returned back to Descent 3. This pointer will")); |
| 467 | O(("// be passed around, along with the ID for CallInstanceEvent() and DestroyInstance(). Return NULL")); |
| 468 | O(("// if there was an error.")); |
| 469 | O(("// The only reserved ID is 0, which must be used for the level script")); |
| 470 | O(("void STDCALLPTR CreateInstance(int id)")); |
| 471 | O(("{")); |
| 472 | O((" return NULL;")); |
| 473 | O(("}")); |
| 474 | O(("")); |
| 475 | |
| 476 | O(("// DestroyInstance")); |
| 477 | O(("// Purpose:")); |
| 478 | O(("// Given an ID, and a pointer to a particular instance of a script, this function will delete " |
| 479 | "and")); |
| 480 | O(("// destruct all information associated with that script, so it will no longer exist.")); |
| 481 | O(("void STDCALL DestroyInstance(int id,void *ptr)")); |
| 482 | O(("{")); |
| 483 | O(("}")); |
| 484 | O(("")); |
| 485 | |
| 486 | O(("// CallInstanceEvent")); |
| 487 | O(("// Purpose:")); |
| 488 | O(("// Given an ID, a pointer to a script instance, an event and a pointer to the struct of")); |
| 489 | O(("// information about the event, this function will translate who this event belongs to and")); |
| 490 | O(("// passes the event to that instance of the script to be handled. Return a combination of")); |
| 491 | O(("// CONTINUE_CHAIN and CONTINUE_DEFAULT, to give instructions on what to do based on the")); |
| 492 | O(("// event. CONTINUE_CHAIN means to continue through the chain of scripts (custom script, level")); |
| 493 | O(("// script, mission script, and finally default script). If CONTINUE_CHAIN is not specified,")); |
| 494 | O(("// than the chain is broken and those scripts of lower priority will never get the event. Return")); |
| 495 | O(("// CONTINUE_DEFAULT in order to tell D3 if you want process the normal action that is built into")); |
| 496 | O(("// the game for that event. This only pertains to certain events. If the chain continues")); |
| 497 | O(("// after this script, than the CONTINUE_DEFAULT setting will be overridden by lower priority")); |
| 498 | O(("// scripts return value.")); |
| 499 | O(("// The only reserved ID is 0, which must be used for the level script")); |
| 500 | O(("int16_t STDCALL CallInstanceEvent(int id,void *ptr,int event,tOSIRISEventInfo *data)")); |
| 501 | O(("{")); |
| 502 | O((" return CONTINUE_CHAIN|CONTINUE_DEFAULT;")); |
| 503 | O(("}")); |
| 504 | O(("")); |
| 505 | |
| 506 | O(("// GetTriggerScriptID")); |
| 507 | O(("// Purpose:")); |
| 508 | O(("// Given a room and face number, this function will return a unique ID (global DLL unique)")); |
| 509 | O(("// in which Descent 3 should use for all future interaction will the DLL when refering to")); |
| 510 | O(("// this trigger. Return -1 if there is no trigger script available for the specified trigger.")); |
| 511 | O(("// The only reserved ID is 0, which must be used for the level script")); |
| 512 | O(("int STDCALL GetTriggerScriptID(int trigger_room,int trigger_face)")); |
| 513 | O(("{")); |
| 514 | O((" return -1;")); |
| 515 | O(("}")); |
| 516 | O(("")); |
| 517 | |
| 518 | O(("// GetCOScriptList")); |
| 519 | O(("// Purpose:")); |
| 520 | O(("// This function returns the pointers to the 2 arrays that Descent 3 should use to determine")); |
| 521 | O(("// what object's have custom scripts in this level DLL. list should be set to point to the array")); |
| 522 | O(("// of object handles that have custom scripts in this DLL. id_list should be set to point to the")); |
| 523 | O(("// corresponding array of unique IDs that match with the object handle list. This function should")); |
| 524 | O(("// return the size of the arrays returned...if no custom scripts are available, than this function")); |
| 525 | O(("// returns 0.")); |
| 526 | O(("// The only reserved ID is 0, which must be used for the level script")); |
| 527 | O(("int STDCALL GetCOScriptList( int **list, int **id_list )")); |
| 528 | O(("{")); |
| 529 | O((" return 0;")); |
| 530 | O(("}")); |
| 531 | |
| 532 | O(("// SaveRestoreState")); |
| 533 | O(("// Purpose:")); |
| 534 | O(("// This function is called when Descent 3 is saving or restoring the game state. In this " |
| 535 | "function")); |
| 536 | O(("// you should save/restore any global data that you want preserved through load/save (which includes")); |
| 537 | O(("// demos). You must be very careful with this function, corrupting the file (reading or writing too")); |
| 538 | O(("// much or too little) may be hazardous to the game (possibly making it impossible to restore the")); |
| 539 | O(("// state). It would be best to use version information to keep older versions of saved states still")); |
| 540 | O(("// able to be used. IT IS VERY IMPORTANT WHEN SAVING THE STATE TO RETURN THE NUMBER OF _BYTES_ WROTE")); |
| 541 | O(("// TO THE FILE. When restoring the data, the return value is ignored. saving_state is 1 when you " |
| 542 | "should")); |
| 543 | O(("// write data to the file_ptr, 0 when you should read in the data.")); |
| 544 | O(("int STDCALL SaveRestoreState( void *file_ptr, uint8_t saving_state )")); |
| 545 | O(("{")); |
| 546 | O((" return 0;")); |
| 547 | O(("}")); |
| 548 | O(("")); |
| 549 | |
| 550 | cfclose(CurrentFile); |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | bool ScriptCreateEmptyGameScript(char *filename) { |
| 555 | char fullpath[_MAX_PATH]; |
| 556 | ddio_MakePath(fullpath, LocalScriptDir, filename, NULL); |
| 557 | CurrentFile = cfopen(fullpath, "wt"); |
| 558 | if (!CurrentFile) |
| 559 | return false; |
| 560 | |
| 561 | O(("// %s", filename)); |
| 562 | O(("// %s", VERSION)); |
| 563 | O(("#include <stdio.h>")); |
| 564 | O(("#include <stdlib.h>")); |
| 565 | O(("#include <string.h>")); |
| 566 | O(("#include \"osiris_import.h\"")); |
| 567 | O(("#include \"osiris_common.h\"")); |
| 568 | O(("")); |
| 569 | |
| 570 | O(("#ifdef _MSC_VER //Visual C++ Build")); |
| 571 | O(("#define STDCALL __stdcall")); |
| 572 | O(("#define STDCALLPTR *STDCALL")); |
| 573 | O(("#else //Non-Visual C++ Build")); |
| 574 | O(("#define STDCALL __attribute__((stdcall))")); |
| 575 | O(("#define STDCALLPTR STDCALL*")); |
| 576 | O(("#endif")); |
| 577 | O(("")); |
| 578 | |
| 579 | O(("#ifdef __cplusplus")); |
| 580 | O(("extern \"C\"{")); |
| 581 | O(("#endif")); |
| 582 | O(("char STDCALL InitializeDLL(tOSIRISModuleInit *func_list);")); |
| 583 | O(("void STDCALL ShutdownDLL(void);")); |
| 584 | O(("int STDCALL GetGOScriptID(const char *name,uint8_t isdoor);")); |
| 585 | O(("void STDCALLPTR CreateInstance(int id);")); |
| 586 | O(("void STDCALL DestroyInstance(int id,void *ptr);")); |
| 587 | O(("int16_t STDCALL CallInstanceEvent(int id,void *ptr,int event,tOSIRISEventInfo *data);")); |
| 588 | O(("int STDCALL SaveRestoreState( void *file_ptr, uint8_t saving_state );")); |
| 589 | O(("#ifdef __cplusplus")); |
| 590 | O(("}")); |
| 591 | O(("#endif")); |
| 592 | O(("")); |
| 593 | |
| 594 | O(("int String_table_size = 0;")); |
| 595 | O(("char **String_table = NULL;")); |
| 596 | O(("static char *_Error_string = \"!!ERROR MISSING STRING!!\";")); |
| 597 | O(("static char *_Empty_string = \"\";")); |
| 598 | O(("const char *GetStringFromTable(int index)")); |
| 599 | O(("{")); |
| 600 | O((" if( (index<0) || (index>=String_table_size) )")); |
| 601 | O((" return _Error_string;")); |
| 602 | O((" if(!String_table[index])")); |
| 603 | O((" return _Empty_string;")); |
| 604 | O((" return String_table[index];")); |
| 605 | O(("}")); |
| 606 | O(("#define TXT(x) GetStringFromTable(x)")); |
| 607 | |
| 608 | O(("// InitializeDLL")); |
| 609 | O(("// Purpose:")); |
| 610 | O(("// This function gets called when the DLL first gets loaded. It will only be called once (until " |
| 611 | "the")); |
| 612 | O(("// DLL is unloaded). Passed in is a struct of data passed from the game needed for the DLL to interact")); |
| 613 | O(("// with D3. Usually this function will just call osicommon_Initialize(), which sets up the imported")); |
| 614 | O(("// functions. However, you can alloc some memory or whatever in this function, and free it in " |
| 615 | "ShutdownDLL().")); |
| 616 | O(("// Note: You cannot call any imported functions until osicommon_Initialize() is called.")); |
| 617 | O(("// Returns 1 if initialization went ok, 0 if there was an error and the DLL should not be loaded.")); |
| 618 | O(("char STDCALL InitializeDLL(tOSIRISModuleInit *func_list)")); |
| 619 | O(("{")); |
| 620 | O((" osicommon_Initialize((tOSIRISModuleInit *)func_list);")); |
| 621 | O((" String_table_size = func_list->string_count;")); |
| 622 | O((" String_table = func_list->string_table;")); |
| 623 | O((" if(func_list->game_checksum!=CHECKSUM)")); |
| 624 | O((" { ")); |
| 625 | O((" mprintf(0,\"Game-Checksum FAIL!!! (%%ul!=%%ul)\\n\",func_list->game_checksum,CHECKSUM);")); |
| 626 | O((" mprintf(0,\"RECOMPILE YOUR SCRIPTS!!!\\n\");")); |
| 627 | O((" return 0;")); |
| 628 | O((" }")); |
| 629 | O((" return 1;")); |
| 630 | O(("}")); |
| 631 | O(("")); |
| 632 | |
| 633 | O(("// ShutdownDLL")); |
| 634 | O(("// Purpose:")); |
| 635 | O(("// This function is called right before a DLL is about to be unloaded from memory. You can " |
| 636 | "free")); |
| 637 | O(("// any unfree'd memory, or anything else you need to do. Don't worry about destroying any instances")); |
| 638 | O(("// of scripts, as they will all be automatically destroyed before this function is called. The")); |
| 639 | O(("// same goes for any memory allocated with Scrpt_MemAlloc(), as this will automatically be freed")); |
| 640 | O(("// when a scripts instance is destroyed.")); |
| 641 | O(("void STDCALL ShutdownDLL(void)")); |
| 642 | O(("{")); |
| 643 | O(("}")); |
| 644 | O(("")); |
| 645 | |
| 646 | O(("// GetGOScriptID")); |
| 647 | O(("// Purpose:")); |
| 648 | O(("// Given the name of the object (from it's pagename), this function will search through it's")); |
| 649 | O(("// list of General Object Scripts for a script with a matching name (to see if there is a script")); |
| 650 | O(("// for that type/id of object within this DLL). If a matching scriptname is found, a UNIQUE ID")); |
| 651 | O(("// is to be returned back to Descent 3. This ID will be used from here on out for all future")); |
| 652 | O(("// interaction with the DLL. Since doors are not part of the generic object's, it's possible")); |
| 653 | O(("// for a door to have the same name as a generic object (OBJ_POWERUP, OBJ_BUILDING, OBJ_CLUTTER")); |
| 654 | O(("// or OBJ_ROBOT), therefore, a 1 is passed in for isdoor if the given object name refers to a")); |
| 655 | O(("// door, else it is a 0. The return value is the unique identifier, else -1 if the script")); |
| 656 | O(("// does not exist in the DLL.")); |
| 657 | O(("int STDCALL GetGOScriptID(const char *name,uint8_t isdoor)")); |
| 658 | O(("{")); |
| 659 | O((" return -1;")); |
| 660 | O(("}")); |
| 661 | O(("")); |
| 662 | |
| 663 | O(("// CreateInstance")); |
| 664 | O(("// Purpose:")); |
| 665 | O(("// Given an ID from a call to GetGOScriptID(), this function will create a new instance for " |
| 666 | "that")); |
| 667 | O(("// particular script (by allocating and initializing memory, etc.). A pointer to this instance")); |
| 668 | O(("// is to be returned back to Descent 3. This pointer will be passed around, along with the ID")); |
| 669 | O(("// for CallInstanceEvent() and DestroyInstance(). Return NULL if there was an error.")); |
| 670 | O(("void STDCALLPTR CreateInstance(int id)")); |
| 671 | O(("{")); |
| 672 | O((" return NULL;")); |
| 673 | O(("}")); |
| 674 | O(("")); |
| 675 | |
| 676 | O(("// DestroyInstance")); |
| 677 | O(("// Purpose:")); |
| 678 | O(("// Given an ID, and a pointer to a particular instance of a script, this function will delete " |
| 679 | "and")); |
| 680 | O(("// destruct all information associated with that script, so it will no longer exist.")); |
| 681 | O(("void STDCALL DestroyInstance(int id,void *ptr)")); |
| 682 | O(("{")); |
| 683 | O(("}")); |
| 684 | O(("")); |
| 685 | |
| 686 | O(("// CallInstanceEvent")); |
| 687 | O(("// Purpose:")); |
| 688 | O(("// Given an ID, a pointer to a script instance, an event and a pointer to the struct of")); |
| 689 | O(("// information about the event, this function will translate who this event belongs to and")); |
| 690 | O(("// passes the event to that instance of the script to be handled. Return a combination of")); |
| 691 | O(("// CONTINUE_CHAIN and CONTINUE_DEFAULT, to give instructions on what to do based on the")); |
| 692 | O(("// event. CONTINUE_CHAIN means to continue through the chain of scripts (custom script, level")); |
| 693 | O(("// script, mission script, and finally default script). If CONTINUE_CHAIN is not specified,")); |
| 694 | O(("// than the chain is broken and those scripts of lower priority will never get the event. Return")); |
| 695 | O(("// CONTINUE_DEFAULT in order to tell D3 if you want process the normal action that is built into")); |
| 696 | O(("// the game for that event. This only pertains to certain events. If the chain continues")); |
| 697 | O(("// after this script, than the CONTINUE_DEFAULT setting will be overridden by lower priority")); |
| 698 | O(("// scripts return value.")); |
| 699 | O(("int16_t STDCALL CallInstanceEvent(int id,void *ptr,int event,tOSIRISEventInfo *data)")); |
| 700 | O(("{")); |
| 701 | O((" return CONTINUE_CHAIN|CONTINUE_DEFAULT;")); |
| 702 | O(("}")); |
| 703 | O(("")); |
| 704 | |
| 705 | O(("// SaveRestoreState")); |
| 706 | O(("// Purpose:")); |
| 707 | O(("// This function is called when Descent 3 is saving or restoring the game state. In this " |
| 708 | "function")); |
| 709 | O(("// you should save/restore any global data that you want preserved through load/save (which includes")); |
| 710 | O(("// demos). You must be very careful with this function, corrupting the file (reading or writing too")); |
| 711 | O(("// much or too little) may be hazardous to the game (possibly making it impossible to restore the")); |
| 712 | O(("// state). It would be best to use version information to keep older versions of saved states still")); |
| 713 | O(("// able to be used. IT IS VERY IMPORTANT WHEN SAVING THE STATE TO RETURN THE NUMBER OF _BYTES_ WROTE")); |
| 714 | O(("// TO THE FILE. When restoring the data, the return value is ignored. saving_state is 1 when you " |
| 715 | "should")); |
| 716 | O(("// write data to the file_ptr, 0 when you should read in the data.")); |
| 717 | O(("int STDCALL SaveRestoreState( void *file_ptr, uint8_t saving_state )")); |
| 718 | O(("{")); |
| 719 | O((" return 0;")); |
| 720 | O(("}")); |
| 721 | O(("")); |
| 722 | |
| 723 | cfclose(CurrentFile); |
| 724 | return true; |
| 725 | } |