I try to write a mod for a game. This is totally new territory for me, so I might be on the wrong track here.
The game is written in Unity and you are able to add a .script file to your mod. The .script file can contain javascript that is parsed by Jint.
I tried to output a simple string from one of the game DLLs:
var UnityEngine = importNamespace("UnityEngine");
var IceEngine = importNamespace("IceEngine");
var GameMain = importNamespace("GameMain");
var output = GameMain.Game.ModPath;
UnityEngine.Debug.Log("----- Testmod Output Start-----");
UnityEngine.Debug.Log(output);
UnityEngine.Debug.Log("----- Testmod Output End-----");
In the GameMain.dll it says:
public class Game : MonoBehaviour, IUserManagerListener, IAccountMsg, IMsg, IRenderListener
{
private static string modPath = Game.userPath + "/Mods";
// lots of other code...
public static string ModPath
{
get
{
return Game.modPath;
}
}
My understanding is that GameMain.Game.ModPath should give me a string. But instead the output in the log file is this:
----- Testmod Output Start-----
System.Dynamic.ExpandoObject
----- Testmod Output End-----
No matter wehat I try to output, I get a System.Dynamic.ExpandoObject and don't know what to do with it.
Maybe someone can give me tips/resources to help. :)
The ExpandoObject is the nearest equivalent that .Net has for a JavaScript Object - that is an object to which members can be added or removed on-the-fly.
This suggests to me that the underlying JavaScript method is actually returning an object rather than a string.
You can use Newtonsoft.Json to serialize the object:
var json = (string)JsonConvert.SerializeObject(GameMain.Game.ModPath);
Which will allow you to see if there's a property you should be using. So let's say this gives you {"modPath":"..."}, you should access that property directly like this:
var modPath = (string)GameMain.Game.ModPath.modPath;
Related
I am new to Typescript and I'm trying to understand how Javascript var types work,
I created a simple class: and restricted the name property to string, but when I insert a number to my string variable I get no error and my string property holds the number as it was a string... am I missing something?
When I debugged it, I found that name was 345.
//done in ts
class Customer {
public name: string = "";
validate(input: string): string {
alert("hey");
return "hey";
}
}
The code that uses my class
<script>
var tito = new Customer();
tito.name = 345;
</script>
Assuming that the first portion of code was done using Typescript, the code that uses that class seems to be placed in an HTML and it's pure Javascript, that's why the types are not being checked, since this is not being compiled by Typescript.
In order to use type checking your code needs to be placed in a .ts file:
// Inside a .ts file
var tito = new Customer();
tito.name = 345;
In an application I am working on I need to get a list of the names of all applicationScope variable then I need to cycle through them and filter out the ones starting with a know string say $xyx. I thought that the applicationScope.keySet().
I'm using this code for starter:
var col = applicationScope.keySet();
var itr:java.util.Iterator = col.iterator();
if (itr.hasNext()){
var str:String = itr.next();
dBar.info(str,"Value = ");
}
if I put the variable col in a viewScope it shows a list of all the keys. but when I run the script the values displayed in the dBar info are not the keys but some other information that I'm not sure where it comes from.
I should just be able to iterat through the list of keys, am I missing something?
This code is in the before page loads event
After some poking around and experimenting I got this to work:
var col = applicationScope.keySet();
var itr:java.util.Iterator = col.iterator();
while (itr.hasNext()){
var str:Map.Entry = itr.next();
if (str.substring(0,9) == "$wfsLock_"){
//do stuff
}
}
so I'm now a happy camper.
Although your code works in SSJS, it is not correct (and that's why I don't like SSJS...).
The applicationScope is an implementation of the java.util.Map interface and the keySet() method returns a Set containing the keys in that Map. Every entry is (probably) a String (other data types like integers are actually also valid). The line
var str:Map.Entry = itr.next();
doesn't cast it to a Map.Entry: it doesn't really do anything: str remains a string.
The Map interface also has an entrySet() method that returns the entries (Map.Entry). You can use that to retrieve the key as well as the value:
var it = applicationScope.entrySet().iterator();
while (it.hasNext()) {
var entry = it.next();
print( entry.getKey() + " = " + entry.getValue() );
}
(in this code the print() line will use the toString() method of the key as well as the value to send information to the console)
I see from your code that you've installed my XPages Debug Toolbar. You can also use that to quickly check what's in the scopes and what the actual datatype is.
As a feature in a sort of game created in HTML5/CSS in combination with javascript, I would like to show some randomly looking code which scrolls in a div.
An easy way to accomplish this would be to actually print the code used to create the game, but I cannot seem to manage to do so.
I have tried to call .toString() on an object I have defined, but it only prints [Object Object], which obviously isn't preferably. The desired effect is basically the same as calling .toString() on a single function, however I would like to do so on the entire code/all objects that I use.
How could I accomplish this?
Edit:
Some clarification, I have an object
var Foo = {
stuff: [],
function1: function() { /* code */ }
}
And I would like to be able to use it as follows:
var string = Foo.toString();
$("#myDiv").html(string);
Which will result in myDiv containing the source code of the object called Foo.
This is the answer you are looking for:
How to inspect Javascript Objects
You have to navigate the object, there is no function as PHP's var_dump.
here is something you could use :
var a = {'some':'strange','object':''};
console.log( JSON.stringify(a) );
// outputs : {"some":"strange","object":""}
If your game is targeting IE > 9, then you could further randomize the string with something like (base64 encode/decode):
var a = {'some':'strange','object':''};
console.log( btoa(JSON.stringify(a)) );
// outputs : eyJzb21lIjoic3RyYW5nZSIsIm9iamVjdCI6IiJ9
I need to pass a jQuery object in to a workaround for an eval. The issue is that i need access to a jQuery object that is out side the eval area but i can't see to pass it in. here is what i have.
var jObj = $(selector);
var myCode = "var jObj="+jObj+"; var i="+i+"; "+shape.mouseover.onEnd.replace("\u0027","'");
var myFucn = new Function(myCode);
myFucn();
the oject I'm getting the string out of is
shape.mouseover.onEnd.replace("\u0027","'");
is working and what I'm passing in that string is
open_info(jObj,i)
Which is what i have to fire. The deal is that the code is run thru YUI compressor so the jObj var becomes something else so i need to pass that in. Right now i get an error where it thinks it should have and ending ] which is not right. I is working it seems, just not the jObj var.
EDIT
there are many way to get where i need to be that are close but not quite like
How to pass parameters in eval in an object form?
shape.mouseover.onEnd = "open_info(jObj,i)";
/*
* this is coming in and must be as it is, don't say it's wrong please
* it's not able to be done anyother way!
*/
//lets process the string and pull in the vars
/* BEOFRE YUI COMPRESSOR CHANGES THINGS and works!!!
var jObj = $(selector);
var i = 1;
var myCode = shape.style.events.mouseover.onEnd.replace("\u0027","'");
var myFucn = new Function(myCode);
myFucn();
*/
// AFTER note it can be random as i change code so it fails cause
// var jObj is now var r and var i is now var e
var r = $(selector);
var e = 1;
var p= shape.style.events.mouseover.onEnd.replace("\u0027","'");
var f= new Function(p);
f();
Now it works before the compression.. After is not due to the change. Hope tha tclears it up some
I might be going down the wrong tracks and be confused here..
But isnt this what your trying to do?
Send myFucn the correct object and what ever i is
myFucn($(selector),10);
function myFucn(jObj,i)
{
shape.mouseover.onEnd.replace("\u0027","'");
}
I still don't understand why this question got 2 down votes, but well it's solved and works great. The trick is to do the same manipulation of the dom state. It's really simple once it is placed out.
//so this is what the object is parsed out to from the json string
//since you can't just pass a function stright that way any how
shape.mouseover.onEnd = "open_info(jObj,i)";
//this is what will take that string and process it
//note jObj is what is in the orgain code but it changes to
// var r or something else that is shorter after going thru YUI compressor
// Which is why we can't just use open_info(jObj,i) and it work..
// ie: it's not an issue with scoope but an issues with var names being shortened
(function(){
//this is the trick on passing them so YUI doesn't get them
//use a string and YUI skips it so we directly create the
//needed oject in the window namespace
window['jObj']=jObj; window['i']=i;
var p= shape.mouseover.onEnd;
var f= new Function(p);
f();
})();
That is it.. I put it in a click or hover event so it's kin to an onClick.
/* EXMAPLE OUTPUT AFTER YUI COMPRESSION
//Note after the YUI compressor get ahold of that
//processing code above it'll look like
*/
function(){window.jObj=n,window.i=t;var u=i.mouseover.onEnd,r=new Function(u);r()}();
So the way that works is, I needed to fix the issue of the var jObj being renamed. So I simply made a sting for the name and let the compressed the var name fill the name of the object I need for the processed code string. Don’t know why I didn’t see it before and I would have saved my rep value :-\ .. oh well. May be a way to shorten this but I'm leaving it for now.
Edit
I recant the edit it was working. :) Very well.. Left wondering what any other ways there would be to make it do the same thing.
I have returned object from signature device and when i make quick watch on it, it told me that its an array of long and when i pass it to web method in my code behind (vb.net) it gives me nothing.
i need some help..
note: i'm using an activeX to capture the signature from the device.
this is javascript code :
function OnSave() {
var sign = document.FORM1.SigPlus1.SignatureString;
PageMethods.Save(sign);
}
this is my webmethod:
<WebMethod()> _
Public Shared Function Save(ByVal obj As Object) As String
Dim obj1 As New PFSIGNATURELib.SigniShellSignature
obj1.SignatureMime = obj
obj1.SaveBitmapToFile(System.AppDomain.CurrentDomain.BaseDirectory() & "\sign1.bmp", 200, 200)
Return "\sign1.bmp"
End Function
I don't know much about ASP.Net, but it seems like the PageMethods.Save function can't handle an array of long. Another possibility is that the sign variable is null in the javascript code.
Try adding
alert(sign);
in the middle your Javascript function, or better yet, install firebug and do
console.log(sign);
instead. This way you'll make sure the sign var actually contains what you think it does.
If it indeed contains an array of numbers (javascript doesn't have a long type), maybe you need to convert it to something else before calling the PageMethods.Save function.
For example, this javascript snippet will convert sign into a space-separated string of numbers:
s = ""
for (i in sign) {
s += sign[i] + " ";
}
sign = s
If you manage to pass this string to your webmethod, you can use some string parsing to get back the original array.