Important JintPlugin Module API

mikec

Master Of All That I Survey
Administrator
Jul 12, 2014
296
148
28
Los Angeles, California, USA
Now that Magma is in its own module, I'm taking the Jint module in a slightly different direction. There won't be as many methods provided to Jint scripts as were provided to Magma scripts. This is because Javascript running in Jint has improved access to C# types. For example, under Jint you can use a Generic Collection of any type by passing the typename as an argument when assigning the class to a variable. Then you can instance the class with the new operator, and manipulate the list with its native C# methods and properties.

JavaScript:
var ListOfObj = System.Collections.Generic.List(System.Object);
var list = new ListOfObj();
list.Add(foo);
list.Add(bar);
list.Add(baz);
list.Add(bum);
Plugin.Log("Jint", "There are " + list.Count + " things.");
So, there is no need for ParamsList, which is just a wrapper around the System.Collections.Generic.List class. Javascript plugins running under Jint can directly use any of these classes.

Continued....
 
Last edited by a moderator:
  • Informative
Reactions: .phase

mikec

Master Of All That I Survey
Administrator
Jul 12, 2014
296
148
28
Los Angeles, California, USA
NEW METHODS FOR JINT2 PLUGINS

Plugin.ToJsonFile(string path, string json):void
- Writes a json string to filename "path".json.
Plugin.FromJsonFile(string path):string
- Returns a json string from filename "path".json.
Usage:
JavaScript:
var obj = {
  propertyA: 'foo',
  propertyB: 'bar',
  propertyC: 12,
  propertyD: true
};

Plugin.ToJsonFile( "Settings", JSON.stringify( obj ) );
var settings = JSON.parse( Plugin.FromJsonFile( "Settings" ) );
// here goes code to make use of settings.propertyA, settings.propertyB, etc...
Plugin.Log(string path, string text):void
Plugin.DeleteLog(string path):void

- same as Magma, but using "path".log for the logfile name
Plugin.RotateLog(string logfile, int max = 6):void
- Rotate logs keeping no more than N max old log files.

Plugin.Today():string
- today's date as a string "8/21/14"
Plugin.Ticks():int
- same as Magma Plugin.GetTicks()
Plugin.ClockTime():string
- current time as string "3:02 AM"
Plugin.Timestamp():int
- Unix epoch time, the number of seconds since midnight, 1970/1/1 UTC
Plugin.TimeSince(int when):int
- the number of seconds since epoch timestamp "when", to now

Plugin.POSTJson(string url, string json):string
- POST a Json string to a HTTP url, return the server response as a string.
Plugin.POSTJsonFile(string url, string path);string
- POST the Json contents of a file to HTTP url, return the server response as a string.
Plugin.GET(string url):string
- GET content from a HTTP url, return as a string
 
Last edited by a moderator:
  • Informative
Reactions: .phase

mikec

Master Of All That I Survey
Administrator
Jul 12, 2014
296
148
28
Los Angeles, California, USA
Remember, the *JsonFile methods don't actually do any JSON serialization. Use the Javascript JSON methods within your script and send the output to one of these methods. The only way Jint can do JSON is to instance a new Javascript Engine and pipe your object through it. But that would be silly. :)

The filenames will have a .json extension, and the POSTJson* methods will send "Content-Type: application/json" to the server, so it can handle it properly. Also, the .json files are overwritten if you use them again. The new json string is not appended to the end of the file, it replaces the file. Use the Log method if you want to append to a file. I'll add an optional parameter to Plugin.Log to omit the date prefix from each log line.

If you want to do more elaborate file operations than what the module provides, please help yourself :)
Code:
var File = importNamespace("System.IO.File");
 

mikec

Master Of All That I Survey
Administrator
Jul 12, 2014
296
148
28
Los Angeles, California, USA
These methods of class Plugin work the same as in Magma:

Plugin.CreateDir(string name):bool
Plugin.CreateTimer(string name, int timeoutDelay):TimedEvent
Plugin.CreateTimer(string name, int timeoutDelay, ParamsList args):TimedEvent
Plugin.GetTimer(string name):TimedEvent
Plugin.KillTimer(string name):void
Plugin.KillTimers():void


These Magma Plugin methods are not supported in JintPlugin, because they've been renamed, or there is no need.

Plugin.CreateIni
Plugin.GetDate
Plugin.GetTicks
Plugin.GetTime
Plugin.IniExists (that reminds me, I need to add Plugin.FileExists)


In addition to Plugin, these Magma classes - now Fougerite classes - are supported and imported already into Javascript namespace.
Server.*
DataStore.*
Util.*
World.*


IniParser still exists in the global C# namespace but isn't in Javascript namespace. You should be able to use importNamespace to get it, if you want it.
Code:
var IniParser = importNamespace("IniParser");
All the same Hooks are available to JintPlugin Javascript as could be used in Magma.

You can define variables in global and reference them in your Hooks, same as in Magma. You can create closures in global and build a complex object, and use it in your Hooks. Same as Magma.

Unlike Magma, you can use the new operator in JintPlugin Javascript. Almost all ECMAscript5 Objects and methods work as documented at Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript
If it says the method is ECMAScript 3rd Edition Standard or ECMAScript 5.1 Standard, it should work as documented.
 
  • Informative
Reactions: Snake

balu92

Moderator
Moderator
Jul 11, 2014
338
75
28
32
On a side note: you can't use other plugin's function, each plugin has its own scope (not like in magma)
 
  • Informative
Reactions: Snake