//=============================================================================
// Open External - By TomatoRadio
// TR_OpenExternal.js
//=============================================================================

var Imported = Imported || {};
Imported.TR_OpenExternal = true;

var TR = TR || {};
TR.OE = TR.OE || {};
TR.OE.version = 1.1;

/*: 
 *
 * @plugindesc Easy script for opening files on the users computer.
 * Version 1.1
 * @author TomatoRadio
 * 
 * @help
 * Use this script:
 * window.$OpenExternal(file);
 * to open the file in the user's default media player.
 * This is only known to work with audio and video (audio/ & movies/)
 * You need the filepath and extension in the call.
 * 
 * examples:
 * window.$OpenExternal("movies/kenway_plays_ptl_ch5_gone_sexual.webm");
 * window.$OpenExternal("audio/bgm/sigwa_by_nazoket.ogg");
 * 
 * I do not know if this works on Mac or Linux.
 * 
 * BONUS FEATURE
 * You can also open website urls with this.
 * This must be the full webpage link.
 * INCORRECT: window.$OpenExternal("youtube.com/watch?v=G87XjK4hOLU");
 * CORRECT: window.$OpenExternal("https://www.youtube.com/watch?v=M9J6DKJXoKk");
 * 
*/

window.$OpenExternal = function(file) {
  if (!file) {console.error(`${file} is falsy. File not sent.`); return;};
  if (file.includes("://")) {window.nw.Shell.openExternal(file); return;};
  const oneLoader = typeof $modLoader !== "undefined";
  const path = oneLoader ? window.nw.__dirname+"/www/" : window.nw.__dirname+"/";
  var url = "file:///"+path+file;
  while (url.includes("\\")) {
    url = url.replace("\\","/");
  };
  console.log(oneLoader,path,url,file);
  window.nw.Shell.openExternal(url);
  return;
};