1 "use strict"; 2 3 // *** WORKAROUND FOR MISSING println *** 4 if (typeof println === "undefined") { 5 this.println = print; 6 } 7 8 // *** CONSTANTS *** 9 10 /** 11 * The String "This type is not supported by this emulator!" for use with Show_Message(). 12 * @constant 13 * @type String 14 */ 15 var NOT_SUPPORTED = "This type is not supported by this emulator!"; 16 17 //constants for palntsc 18 /** 19 * The String "PAL" for comparison with variable itemPalNtsc. 20 * @constant 21 * @type String 22 */ 23 var PAL = String(Packages.jgamebase.Const.FORDISPLAY_PALNTSC[0]); 24 25 /** 26 * The String "PAL+NTSC" for comparison with variable itemPalNtsc. 27 * @constant 28 * @type String 29 */ 30 var PALNTSC = String(Packages.jgamebase.Const.FORDISPLAY_PALNTSC[1]); 31 32 /** 33 * The String "NTSC" for comparison with variable itemPalNtsc. 34 * @constant 35 * @type String 36 */ 37 var NTSC = String(Packages.jgamebase.Const.FORDISPLAY_PALNTSC[2]); 38 39 /** 40 * The String "PAL(+NTSC?)" for comparison with variable itemPalNtsc. 41 * @constant 42 * @type String 43 */ 44 var PALntsc = String(Packages.jgamebase.Const.FORDISPLAY_PALNTSC[3]); 45 46 // constants for control 47 /** 48 * The String "Joystick Port 2" for comparison with variable itemControl. 49 * @constant 50 * @type String 51 */ 52 var JOYPORT2 = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[0]); 53 54 /** 55 * The String "Joystick Port 1" for comparison with variable itemControl. 56 * @constant 57 * @type String 58 */ 59 var JOYPORT1 = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[1]); 60 61 /** 62 * The String "Keyboard" for comparison with variable itemControl. 63 * @constant 64 * @type String 65 */ 66 var KEYBOARD = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[2]); 67 68 /** 69 * The String "Paddle Port 2" for comparison with variable itemControl. 70 * @constant 71 * @type String 72 */ 73 var PADDLEPORT2 = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[3]); 74 75 /** 76 * The String "Paddle Port 1" for comparison with variable itemControl. 77 * @constant 78 * @type String 79 */ 80 var PADDLEPORT1 = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[4]); 81 82 /** 83 * The String "Mouse" for comparison with variable itemControl. 84 * @constant 85 * @type String */ 86 var MOUSE = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[5]); 87 88 /** 89 * The String "Light Pen" for comparison with variable itemControl. 90 * @constant 91 * @type String 92 */ 93 var LIGHTPEN = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[6]); 94 95 /** 96 * The String "Koala Pad" for comparison with variable itemControl. 97 * @constant 98 * @type String 99 */ 100 var KOALAPAD = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[7]); 101 102 /** 103 * The String "Light Gun" for comparison with variable itemControl. 104 * @constant 105 * @type String 106 */ 107 var LIGHTGUN = String(Packages.jgamebase.Const.FORDISPLAY_CONTROL[8]); 108 109 // *** VARIABLES *** 110 /** @ignore */ 111 var global = (function() { 112 return this; 113 }).call(null); 114 115 /** 116 * The command to execute (eg "x64"). Must be set in the script, 117 * otherwise calls to Run_Emulator() will fail. 118 * @type String 119 */ 120 var command = ""; 121 var commandline = []; 122 123 // Java Objects 124 /** 125 * Reference to the current database. 126 * @name Database 127 * @type jgamebase.model.Database 128 */ 129 130 /** 131 * Reference to the current emulator. 132 * @name Emulator 133 * @type jgamebase.model.Emulator 134 */ 135 136 /** 137 * Reference to the current item. 138 * @name Item 139 * @type jgamebase.db.model.Item 140 */ 141 142 /** 143 * Reference to the current game. 144 * @name Game 145 * @type jgamebase.db.model.Game 146 */ 147 148 /** 149 * Reference to the current music. 150 * @name Music 151 * @type jgamebase.db.model.Music 152 */ 153 154 // convert variables from java String to javascript string 155 /** 156 * The name of the operating system as read from System.getProperty("os.name"). 157 * @type String 158 */ 159 osName = String(osName); 160 161 /** 162 * The line separator as read from System.getProperty("line.separator"). 163 * @type String 164 */ 165 lineSeparator = String(lineSeparator); 166 167 /** 168 * The system-dependent default name-separator character. On UNIX 169 * systems the value of this field is <code>'/'</code>; on Microsoft 170 * Windows systems it is <code>'\'</code>. 171 * @type String 172 */ 173 separator = String(separator); 174 175 /** 176 * The path to the database directory (eg "/home/USER/.jgamebase/C64lite"). 177 * @type String 178 */ 179 dbPath = String(dbPath); 180 181 /** 182 * The path to the emulator script or executable (eg "/home/USER/.jgamebase/C64lite/Scripts"). 183 * @type String 184 */ 185 emulatorPath = String(emulatorPath); 186 187 /** 188 * The configuration file of the emulator (eg "/home/USER/.jgamebase/C64lite/Scripts/vice.ini"). 189 * @type String 190 */ 191 emulatorConfigFile = String(emulatorConfigFile); 192 193 /** 194 * The path to the work directory where GameBase extracts and runs files (eg "/tmp/jgamebase"). 195 * @type String 196 */ 197 workPath = String(workPath); 198 199 /** 200 * The name of the item (eg "3-D Glooper"). 201 * @type String 202 */ 203 itemName = String(itemName); 204 205 /** 206 * The comment of the item (if any). 207 * @type String 208 */ 209 itemComment = String(itemComment); 210 211 /** 212 * The version comment of the item (eg "load manually", or "not working"). 213 * @type String 214 */ 215 itemVersionComment = String(itemVersionComment); 216 217 /** 218 * How the item is controlled (eg "Keyboard", or "Joystick Port 2"). 219 * @type String 220 */ 221 itemControl = String(itemControl); 222 223 /** 224 * If the item needs NTSC or PAL (eg "PAL", or "PAL+NTSC"). 225 * @type String 226 */ 227 itemPalNtsc = String(itemPalNtsc); 228 229 // itemPlayersMin, itemPlayersMax are int, no need to convert 230 /** 231 * The minimum number of players that can play the item simultanously. 232 * 233 * @name itemPlayersMin 234 * @type int 235 */ 236 237 /** 238 * The maximum number of players that can play the item simultanously. 239 * @name itemPlayersMax 240 * @type int 241 */ 242 243 // isGame, isOsWindows, isRunsOnPal, isRunsOnNtsc, isRunsOnTrueDrive, 244 // useHardwareJoystick are boolean, no need to convert 245 /** 246 * <code>true</code> if the item is a Game, <code>false</code> if the item is a piece of Music. 247 * @name isGame 248 * @type boolean 249 */ 250 251 /** 252 * <code>true</code> if the operating system is Microsoft 253 * Windows, <code>false</code> otherwise. 254 * @name isOsWindows 255 * @type boolean 256 */ 257 258 /** 259 * Whether the item works on a PAL system. 260 * @name isRunsOnPal 261 * @type boolean 262 */ 263 264 /** 265 * Whether the item works on a NTSC system. 266 * @name isRunsOnNtsc 267 * @type boolean 268 */ 269 270 /** 271 * Whether the item needs true drive emulation (no fast loader). 272 * @name isRunsOnTrueDrive 273 * @type boolean 274 */ 275 276 /** 277 * Whether real joysticks are to be used (instead of eg keyboard). 278 * @name useHardwareJoystick 279 * @type boolean 280 */ 281 282 /** 283 * The file to be run (eg "/tmp/jgamebase/3DGLOOPR.T64"). 284 * @type String 285 */ 286 fileToRun = String(fileToRun); 287 288 // imageIndex is int, no need to convert 289 /** 290 * The index of the file to be started on the supplied image (eg used to start the second file in the image). 291 * @name imageIndex 292 * @type int 293 */ 294 295 /** 296 * The name of the image. 297 * @type String 298 */ 299 imageName = String(imageName); 300 301 /** 302 * The name of the image in the native character set. 303 * @type String 304 */ 305 imageNameNative = String(imageNameNative); 306 307 //isBootable is boolean, no need to convert 308 /** 309 * <code>true</code> if the first image can be booted, <code>false</code> otherwise. 310 * @name isBootable 311 * @type boolean 312 */ 313 314 /** 315 * Path and filename of the item (eg "/tmp/jgamebase/3DGLOOPR.T64"). 316 * @type String 317 */ 318 itemPathAndFile = String(itemPathAndFile); 319 320 /** 321 * The path (no filename) of the item (eg "/tmp/jgamebase"). 322 * @type String 323 */ 324 itemPath = String(itemPath); 325 326 /** 327 * The filename (no path) of the item (eg "3DGLOOPR.T64"). 328 * @type String 329 */ 330 itemFile = String(itemFile); 331 332 /** 333 * The file extension (no filename) of the item (eg "t64"). 334 * @type String 335 */ 336 itemFileExt = String(itemFileExt); 337 338 /** 339 * The type (equals the file extension) of the item (eg "t64"). 340 * @type String 341 */ 342 itemType = String(itemType); 343 344 /** The filename (no file extension) of the item (eg "3DGLOOPR"). 345 * @type String 346 */ 347 itemFileNoExt = String(itemFileNoExt); 348 349 for (var i = 0; i < itemPathsAndFiles.length; i++) { 350 /** 351 * Paths and filenames of the items. 352 * @name itemPathsAndFiles 353 * @type Array of String 354 */ 355 itemPathsAndFiles[i] = String(itemPathsAndFiles[i]); 356 357 /** 358 * The paths (no filenames) of the items. 359 * @name itemPaths 360 * @type Array of String 361 */ 362 itemPaths[i] = String(itemPaths[i]); 363 364 /** 365 * The filenames (no paths) of the items. 366 * @name itemFiles 367 * @type Array of String 368 */ 369 itemFiles[i] = String(itemFiles[i]); 370 371 /** 372 * The file extensions (no filenames) of the items. 373 * @name itemFilesExt 374 * @type Array of String 375 */ 376 itemFilesExt[i] = String(itemFilesExt[i]); 377 378 /** 379 * The filenames (no file extensions) of the items. 380 * @name itemFilesNoExt 381 * @type Array of String 382 */ 383 itemFilesNoExt[i] = String(itemFilesNoExt[i]); 384 } 385 386 //*** OBJECTS *** 387 /** 388 * @ignore 389 * Transparent wrapper around Java File. 390 */ 391 function File() { 392 if (arguments.length === 1) { 393 if ((typeof arguments[0] !== "string") 394 && (typeof arguments[0] !== "object")) { 395 throw "TypeError: Call to constructor 'File(<string||object>)' with wrong type of argument!"; 396 } 397 return new Packages.jgamebase.model.script.JSFile(arguments[0]); 398 } 399 400 if (arguments.length === 2) { 401 if ((typeof arguments[0] !== "string") 402 && (typeof arguments[0] !== "object") 403 && (typeof arguments[1] !== "string")) { 404 throw "TypeError: Call to constructor 'File(<string||object>, <string>)' with wrong type of argument!"; 405 } 406 return new Packages.jgamebase.model.script.JSFile(arguments[0], 407 arguments[1]); 408 } 409 410 throw "ArgumentCountError: Call to constructor 'File()' with wrong number of arguments!"; 411 } 412 413 // *** METHODS *** 414 415 /** 416 * Tests if this string starts with the specified prefix. 417 * @returns <code>true</code> if the character sequence represented by the 418 * argument is a prefix of the character sequence represented by 419 * this string; <code>false</code> otherwise. 420 */ 421 String.prototype.startsWith = function(/**String*/ needle) /**boolean*/ { 422 if (arguments.length !== 1) { 423 throw "ArgumentCountError: Call to method 'String.startsWith()' with wrong number of arguments!"; 424 } 425 if (typeof arguments[0] !== "string") { 426 throw "TypeError: Call to method 'String.startsWith()' with wrong type of argument (parameter 'needle' must be of type string)!"; 427 } 428 429 if (needle.length > this.length) { 430 return false; 431 } 432 return (this.indexOf(needle) === 0); 433 }; 434 435 /** 436 * Tests if this string ends with the specified suffix. 437 * @returns <code>true</code> if the character sequence represented by the 438 * argument is a suffix of the character sequence represented by 439 * this object; <code>false</code> otherwise. 440 */ 441 String.prototype.endsWith = function(/**String*/ needle) /**boolean*/ { 442 if (arguments.length !== 1) { 443 throw "ArgumentCountError: Call to method 'String.endsWith()' with wrong number of arguments!"; 444 } 445 if (typeof arguments[0] !== "string") { 446 throw "TypeError: Call to method 'String.endsWith()' with wrong type of argument (parameter 'needle' must be of type string)!"; 447 } 448 449 if (needle.length > this.length) { 450 return false; 451 } 452 return (this.lastIndexOf(needle) === (this.length - needle.length)); 453 }; 454 455 /** 456 * Returns a copy of the string, with leading and trailing space omitted. 457 * @return A copy of this string with leading and trailing space removed, 458 * or this string if it has no leading or trailing space. 459 */ 460 String.prototype.trim = function() /**String*/ { 461 if (arguments.length !== 0) { 462 throw "ArgumentCountError: Call to method 'String.trim()' with wrong number of arguments!"; 463 } 464 465 var temp = this.toString(); 466 467 while (temp.startsWith(" ")) { 468 temp = temp.substring(1, temp.length); 469 } 470 471 while (temp.endsWith(" ")) { 472 temp = temp.substring(0, temp.length - 1); 473 } 474 475 return temp; 476 }; 477 478 /** 479 * Returns <code>true</code> if and only if this string contains one or more of the specified 480 * values. <p> 481 * For boolean values of <code>true</code> the String is compared with "yes", "on", 482 * "true", and "1"; For boolean values of <code>false</code> with "no", "off", 483 * "false", and "0".<p> 484 * For String values the following wildcards are supported:<ul> 485 * <li>"*"</li> The String is not empty 486 * <li>"value"</li> The String must match the value 487 * <li>"value*"</li> The String must start with the value 488 * <li>"*value"</li> The String must end with the value 489 * <li>"*value*"</li> The String must contain the value 490 * </ul><p> 491 * E.g. to check if the value of the variable "command" is set<p><pre> 492 * if (command.contains("*")) { 493 * ... 494 * }</pre> 495 * 496 * @param {boolean or String} ... the values to search for 497 * @return <code>true</code> if this string contains one or more of the specified 498 * values, <code>false</code> otherwise 499 */ 500 String.prototype.contains = function() /**boolean*/ { 501 if (arguments.length === 0) { 502 throw "ArgumentCountError: Call to method 'String.contains()' with wrong number of arguments!"; 503 } 504 505 var found = false, haystack = this.toLowerCase(); 506 507 for ( var i = 0; i < arguments.length; i++) { 508 509 if (typeof arguments[i] === "boolean") { 510 if (arguments[i]) { 511 return this.contains("yes", "on", "true", "1"); 512 } else { 513 return this.contains("no", "off", "false", "0"); 514 } 515 } 516 517 if (typeof arguments[i] !== "string") { 518 throw "TypeError: Call to method 'String.contains()' with wrong type of argument (parameters must be of type string or boolean)!"; 519 } 520 521 var needle = arguments[i].toLowerCase(); 522 523 if (needle === "*") { 524 return (haystack !== ""); 525 } 526 527 var firstIsStar = needle.startsWith("*"); 528 var lastIsStar = needle.endsWith("*"); 529 530 if ((firstIsStar) && (lastIsStar) && (!found)) { 531 // *needle* 532 needle = needle.substring(1, needle.length - 1); 533 found = (haystack.indexOf(needle) !== -1); 534 } else if ((!firstIsStar) && (lastIsStar) && (!found)) { 535 // needle* 536 needle = needle.substring(0, needle.length - 1); 537 found = haystack.startsWith(needle); 538 } else if ((firstIsStar) && (!lastIsStar) && (!found)) { 539 // *needle 540 needle = needle.substring(1, needle.length); 541 found = haystack.endsWith(needle); 542 } else if ((!firstIsStar) && (!lastIsStar) && (!found)) { 543 // needle 544 found = (haystack === needle); 545 } 546 } 547 548 return found; 549 }; 550 551 /** 552 * Replaces each substring of this String that matches the given 553 * substring with the given replacement. 554 * @return The resulting <code>String</code> 555 */ 556 String.prototype.replaceAll = function(/**String*/ SearchFor, /**String*/ ReplaceText) /**String*/ { 557 var temp = this.toString(); 558 559 while (temp.indexOf(SearchFor) !== -1) { 560 temp = temp.replace(SearchFor, ReplaceText); 561 } 562 563 return temp; 564 }; 565 566 /** 567 * Tests if an array element contains the specified String by iterating 568 * over each array element and calling String.contains(needle). 569 * @return <code>true</code> if an array element contains the specified 570 * String value, <code>false</code> otherwise. 571 */ 572 Array.prototype.contains = function(/**String*/ needle) /**boolean*/ { 573 for ( var i = 0; i < this.length; i++) { 574 // make object a string 575 if ((String(this[i])).contains(needle)) { 576 return true; 577 } 578 } 579 return false; 580 }; 581 582 /** 583 * Finds which array element contains the specified String by iterating 584 * over each array element and calling String.contains(needle). 585 * @return The index of the first matching array element or "-1" if none 586 * matches. 587 */ 588 Array.prototype.indexOfContains = function(/**String*/ needle) /**int*/ { 589 for ( var i = 0; i < this.length; i++) { 590 // make object a string 591 if ((String(this[i])).contains(needle)) { 592 return i; 593 } 594 } 595 return -1; 596 }; 597 598 // *** FUNCTIONS *** 599 600 /** 601 * Add parameters to the command line (CLP = Command Line Parameters). 602 * @param ... the values to add 603 */ 604 function Add_CLP() { 605 if (arguments.length === 0) { 606 throw "ArgumentCountError: Call to function 'Add_CLP()' with wrong number of arguments!"; 607 } 608 609 for ( var i = 0; i < arguments.length; i++) { 610 commandline.push((arguments[i] + "").trim()); 611 } 612 } 613 614 /** 615 * Run the game emulator (stored in the variable "<code>command</code>") 616 * with the parameters (stored in the array "<code>commandline</code>") 617 * by calling Java "<code>Emulators.executeGame(commandline)</code>". 618 */ 619 function Run_GameEmulator() { 620 if (arguments.length !== 0) { 621 throw "ArgumentCountError: Call to method 'Run_GameEmulator()' with wrong number of arguments!"; 622 } 623 if (command === "") { 624 throw "NoEmulatorSetError: Before calling 'Run_Emulator()' set the emulator command to run (e.g. 'command = \"x64\"')."; 625 } 626 Packages.jgamebase.model.Emulators.executeGame(commandline, tempDir); 627 } 628 629 /** 630 * Run the music emulator (stored in the variable "<code>command</code>") 631 * with the parameters (stored in the array "<code>commandline</code>") 632 * by calling Java "<code>Emulators.executeMusic(commandline)</code>". 633 */ 634 function Run_MusicEmulator() { 635 if (arguments.length !== 0) { 636 throw "ArgumentCountError: Call to method 'Run_MusicEmulator()' with wrong number of arguments!"; 637 } 638 if (command === "") { 639 throw "NoEmulatorSetError: Before calling 'Run_Emulator()' set the emulator command to run (e.g. 'command = \"x64\"')."; 640 } 641 Packages.jgamebase.model.Emulators.executeMusic(commandline); 642 } 643 644 /** 645 * An alias for <code>Run_GameEmulator()</code>. 646 */ 647 function Run_Emulator() { 648 Run_GameEmulator(); 649 } 650 651 /** 652 * @ignore 653 * Internal implementation, should NOT be called directly. 654 */ 655 function _Set_CFG_Item(filename, key, delimiter, value) { 656 var reader; 657 var writer; 658 659 var found = false; 660 var newLine = key + delimiter + value; 661 662 try { 663 var inFile = new File(filename); 664 var outFile = new File(filename + "_new"); 665 666 reader = new java.io.BufferedReader(new java.io.FileReader(inFile)); 667 writer = new java.io.BufferedWriter(new java.io.FileWriter(outFile)); 668 669 // ... Loop as long as there are input lines. 670 var line = null; 671 while ((line = reader.readLine()) !== null) { 672 var lowerLine = line.toLowerCase(); 673 var lowerKey = key.toLowerCase(); 674 675 if ((lowerLine.startsWith(lowerKey + delimiter)) 676 || (lowerLine.startsWith(lowerKey + " " + delimiter)) 677 || (lowerLine.startsWith(lowerKey + "\t" + delimiter))) { 678 writer.write(newLine); // write new line 679 found = true; 680 } else { 681 writer.write(line); // copy line 682 } 683 writer.newLine(); // Write system dependent end of line. 684 } 685 686 if (!found) { // add line at end of file 687 writer.write(newLine); // write new line 688 writer.newLine(); // Write system dependent end of line. 689 } 690 691 inFile.remove(); 692 outFile.renameTo(inFile); 693 694 } catch (error) { 695 println(error.name + ": " + error.message); 696 } finally { 697 if (reader !== null) { 698 try { 699 reader.close(); 700 } catch (ignore1) { 701 } 702 } 703 if (writer !== null) { 704 try { 705 writer.close(); 706 } catch (ignore2) { 707 } 708 } 709 } 710 } 711 712 /** 713 * Sets a value in a configuration file. The file will be searched for a line 714 * starting with key and this line will be replaced by key + delimiter + value. 715 * (eg key = "useJoystick", delimiter = "=", value = "true": will lead to the 716 * line "useJoystick=true") 717 * @param {String or File} filename The filename or File that describes the configuration file. 718 * @param key The parameter to set. 719 * @param delimiter The String between key and value. 720 * @param value The value the parameter will be set to. 721 */ 722 function Set_CFG_Item(filename, /**String*/ 723 key, /**String*/ 724 delimiter, value) { 725 if (arguments.length !== 4) { 726 throw "ArgumentCountError: Call to function 'Set_CFG_Item()' with wrong number of arguments!"; 727 } 728 if ((typeof arguments[0] !== "string") 729 && (typeof arguments[0] !== "object")) { 730 throw "TypeError: Call to function 'Set_CFG_Item()' with wrong type of argument (first argument 'filename' must be string or File)!"; 731 } 732 if (typeof arguments[1] !== "string") { 733 throw "TypeError: Call to function 'Set_CFG_Item()' with wrong type of argument (second argument 'key' must be string)!"; 734 } 735 if (typeof arguments[2] !== "string") { 736 throw "TypeError: Call to function 'Set_CFG_Item()' with wrong type of argument (third argument 'delimeter' must be string)!"; 737 } 738 // fourth argument may be of any type 739 740 if (typeof arguments[0] === "object") { 741 _Set_CFG_Item(filename.getAbsolutePath(), key, delimiter, value); 742 } else { 743 _Set_CFG_Item(filename, key, delimiter, value); 744 } 745 746 } 747 748 /** 749 * Clears a value in a configuration file.<p> 750 * Shortcut for Set_CFG_Item(filename, key, "", ""). 751 * @param {String or File} filename The filename or File that describes the configuration file. 752 * @param key The parameter to set. 753 */ 754 function Set_CFG(filename, /**String*/ 755 key) { 756 if (arguments.length !== 2) { 757 throw "ArgumentCountError: Call to function 'Set_CFG()' with wrong number of arguments!"; 758 } 759 if ((typeof arguments[0] !== "string") 760 && (typeof arguments[0] !== "object")) { 761 throw "TypeError: Call to function 'Set_CFG()' with wrong type of argument (first argument 'filename' must be string or File)!"; 762 } 763 if (typeof arguments[1] !== "string") { 764 throw "TypeError: Call to function 'Set_CFG()' with wrong type of argument (second argument 'key' must be string)!"; 765 } 766 767 if (typeof arguments[0] === "object") { 768 _Set_CFG_Item(filename.getAbsolutePath(), key, "", ""); 769 } else { 770 _Set_CFG_Item(filename, key, "", ""); 771 } 772 } 773 774 /** 775 * Sets a value in a configuration file.<p> 776 * Shortcut for Set_CFG_Item(filename, key, "=", value). 777 * @param {String or File} filename The filename or File that describes the configuration file. 778 * @param key The parameter to set. 779 * @param value The value the parameter will be set to. 780 */ 781 function Set_CFG_Value(filename, /**String*/ 782 key, value) { 783 if (arguments.length !== 3) { 784 throw "ArgumentCountError: Call to function 'Set_CFG_Value()' with wrong number of arguments!"; 785 } 786 if ((typeof arguments[0] !== "string") 787 && (typeof arguments[0] !== "object")) { 788 throw "TypeError: Call to function 'Set_CFG_Value()' with wrong type of argument (first argument 'filename' must be string or File)!"; 789 } 790 if (typeof arguments[1] !== "string") { 791 throw "TypeError: Call to function 'Set_CFG_Value()' with wrong type of argument (second argument 'key' must be string)!"; 792 } 793 // third argument may be of any type 794 795 if (typeof arguments[0] === "object") { 796 _Set_CFG_Item(filename.getAbsolutePath(), key, "=", value); 797 } else { 798 _Set_CFG_Item(filename, key, "=", value); 799 } 800 } 801 802 /** 803 * Sets a value in an windows style ".ini" configuration file. The file will be searched for the section and 804 * the section will be searched for a line starting with key. This line will be replaced by key + "=" + value. 805 * @param {String or File} filename The filename or File that describes the configuration file. 806 * @param section The name of the section in the ini file (started through a "[section]" line). 807 * @param key The parameter to set. 808 * @param value The value the parameter will be set to. 809 */ 810 function Set_INI_Value(/**String*/ 811 filename, /**String*/ 812 section, /**String*/ 813 key, value) { 814 if (arguments.length !== 4) { 815 throw "ArgumentCountError: Call to function 'Set_INI_Value()' with wrong number of arguments!"; 816 } 817 if (typeof arguments[0] !== "string") { 818 throw "TypeError: Call to function 'Set_INI_Value()' with wrong type of argument (first argument 'filename' must be string)!"; 819 } 820 if (typeof arguments[1] !== "string") { 821 throw "TypeError: Call to function 'Set_INI_Value()' with wrong type of argument (second argument 'section' must be string)!"; 822 } 823 if (typeof arguments[2] !== "string") { 824 throw "TypeError: Call to function 'Set_INI_Value()' with wrong type of argument (third argument 'key' must be string)!"; 825 } 826 // fourth argument may be of any type 827 828 var iniFileManager = new Packages.jgamebase.tools.IniFileManager(filename); 829 if (value === "") { 830 // remove empty values 831 iniFileManager.remove(section, key); 832 } else { 833 // set value 834 iniFileManager.set(section, key, value); 835 } 836 iniFileManager.save(); 837 } 838 839 /** 840 * Displays a dialog with a message for the user. 841 * @param message The message to display. 842 */ 843 function Show_Message(/**String*/ 844 message) { 845 if (arguments.length !== 1) { 846 throw "ArgumentCountError: Call to function 'Show_Message()' with wrong number of arguments!"; 847 } 848 if (typeof arguments[0] !== "string") { 849 throw "TypeError: Call to function 'Show_Message()' with wrong type of argument (first argument must be string)!"; 850 } 851 852 javax.swing.JOptionPane.showMessageDialog(null, message, "Info", 853 javax.swing.JOptionPane.INFORMATION_MESSAGE); 854 } 855 856 // DID NOT WORK AS EXPECTED :-( 857 //function Edit_CLP(prompt) { 858 // if (arguments.length > 1) { 859 // throw "ArgumentCountError: Call to function 'Edit_CLP()' with wrong number of arguments!"; 860 // } 861 // if ((arguments.length === 1) && (typeof arguments[0] != "string")) { 862 // throw "TypeError: Call to function 'Edit_CLP()' with wrong type of argument (first argument must be string)!"; 863 // } 864 // 865 // if (prompt === undefined) { 866 // prompt = "Adjust the command line parameters as needed"; 867 // } 868 // temp = javax.swing.JOptionPane.showInputDialog(null, prompt + ":", commandline); 869 // if (temp !== null) { 870 // commandline = temp; 871 // } 872 //} 873 874 /** 875 * Clear all Command Line Parameters. 876 */ 877 function Clear_CLP() { 878 if (arguments.length !== 0) { 879 throw "ArgumentCountError: Call to function 'Clear_CLP()' with wrong number of arguments!"; 880 } 881 882 commandline = []; 883 } 884 885 /** 886 * Get the value of an user defined "KEY=VALUE" pair set in the database.<p> 887 * E.g. to display the value of the user defined variable "msg" only 888 * if it is set:<p><pre> 889 * if (Value("msg").contains("*")) { 890 * Show_Message( Value("msg") ); 891 * }</pre> 892 * 893 * @param key The key to get the value of. 894 * @returns {String} The value of the given key, or "" (empty String) if not set. 895 */ 896 function Value(/**String*/ 897 key) /**String*/ 898 { 899 if (arguments.length !== 1) { 900 throw "ArgumentCountError: Call to function 'Value()' with wrong number of arguments!"; 901 } 902 if (typeof arguments[0] !== "string") { 903 throw "TypeError: Call to function 'Value()' with wrong type of argument (first argument 'key' must be string)!"; 904 } 905 906 var value = ""; 907 908 try { 909 eval(" value = keyAndValue_" + key.toLowerCase() + " + \"\" ;"); 910 } catch (ignore) { 911 } 912 913 return value; 914 } 915 916 /** 917 * Search command in the operating system search path and (last) 918 * in the database script directory. 919 * @return The absolute path to the command, or "" (empty String) 920 * otherwise. 921 */ 922 function findInPath(/**String*/ 923 needle) /**String*/ 924 { 925 if (arguments.length !== 1) { 926 throw "ArgumentCountError: Call to function 'findInPath()' with wrong number of arguments!"; 927 } 928 if (typeof arguments[0] !== "string") { 929 throw "TypeError: Call to function 'findInPath()' with wrong type of argument (first argument 'needle' must be string)!"; 930 } 931 932 return Packages.jgamebase.tools.FileTools.findInPath(needle); 933 } 934 935 /** 936 * Search command in the operating system search path and (last) 937 * in the database script directory. 938 * @return <code>true</code> if the command is found, 939 * <code>false</code> otherwise. 940 */ 941 function existsInPath(/**String*/ 942 needle) /**boolean*/ 943 { 944 if (arguments.length !== 1) { 945 throw "ArgumentCountError: Call to function 'existsInPath()' with wrong number of arguments!"; 946 } 947 if (typeof arguments[0] !== "string") { 948 throw "TypeError: Call to function 'existsInPath()' with wrong type of argument (first argument 'needle' must be string)!"; 949 } 950 951 return Packages.jgamebase.tools.FileTools.existsInPath(needle); 952 } 953 954 /** 955 * Print all variables (for debug purposes). 956 */ 957 function Dump() { 958 println("\n*** START VARIABLE DUMP ***"); 959 println("command='" + command + "'"); 960 println("commandline='" + commandline + "'"); 961 println(""); 962 963 println("dbPath='" + dbPath + "'"); 964 println("emulatorPath='" + emulatorPath + "'"); 965 println("emulatorConfigFile='" + emulatorConfigFile + "'"); 966 println("workPath='" + workPath + "'"); 967 println(""); 968 969 println("itemName='" + itemName + "'"); 970 println("itemComment='" + itemComment + "'"); 971 println("itemVersionComment='" + itemVersionComment + "'"); 972 println("itemControl='" + itemControl + "'"); 973 println("itemPalNtsc='" + itemPalNtsc + "'"); 974 println("itemPlayersMin='" + itemPlayersMin + "'"); 975 println("itemPlayersMax='" + itemPlayersMax + "'"); 976 println("isRunsOnPal='" + isRunsOnPal + "'"); 977 println("isRunsOnNtsc='" + isRunsOnNtsc + "'"); 978 println("isRunsOnTrueDrive='" + isRunsOnTrueDrive + "'"); 979 println(""); 980 981 println("useHardwareJoystick='" + useHardwareJoystick + "'"); 982 println(""); 983 984 println("fileToRun='" + fileToRun + "'"); 985 println("imageIndex='" + imageIndex + "'"); 986 println("imageName='" + imageName + "'"); 987 println("imageNameNative='" + imageNameNative + "'"); 988 println(""); 989 990 println("itemPathAndFile='" + itemPathAndFile + "'"); 991 println("itemPath='" + itemPath + "'"); 992 println("itemFile='" + itemFile + "'"); 993 println("itemFileExt='" + itemFileExt + "'"); 994 println("itemType='" + itemType + "'"); 995 println("itemFileNoExt='" + itemFileNoExt + "'"); 996 println(""); 997 998 println("itemPathsAndFiles.length=" + itemPathsAndFiles.length); 999 println(""); 1000 1001 for ( var i = 0; i < itemPathsAndFiles.length; i++) { 1002 println("itemPathsAndFiles[" + i + "]='" + itemPathsAndFiles[i] + "'"); 1003 println("itemPaths[" + i + "]='" + itemPaths[i] + "'"); 1004 println("itemFiles[" + i + "]='" + itemFiles[i] + "'"); 1005 println("itemFilesExt[" + i + "]='" + itemFilesExt[i] + "'"); 1006 println("itemFilesNoExt[" + i + "]='" + itemFilesNoExt[i] + "'"); 1007 println(""); 1008 } 1009 1010 println("Keys and Values (get via function 'Value(key)':"); 1011 //keyAndValue_ 1012 for ( var prop in global) { 1013 if (typeof prop === "string" && prop.startsWith("keyAndValue_")) { 1014 var key = prop.substring(12, prop.length); 1015 println(key + "='" + Value(key) + "'"); 1016 } 1017 } 1018 println("*** END VARIABLE DUMP ***\n"); 1019 } 1020