- Home /
Whats wrong with my .NET Reflector code.
Here is the code that I converted from JavaScript to C# using .NET Reflector, whats wrong on line 143, it says a type is expected. I was told that Reflector is supposed to be perfect, but I guess not. Here is the the code:
public class Menu_multiplayerCode : MonoBehaviour { // Fields public float CONNECT_NAT_TIMEOUT = 5f; public float CONNECT_TIMEOUT = 0.75f; public string connectionInfo = string.Empty; private bool doneTestingNAT = false; public string errorMessage = string.Empty; public bool filterNATHosts = false; public string gameName = "Example4"; private bool hideTest = false; public HostData[] hostData; private float lastHostListRequest = 0; public bool lastMSConnectionAttemptForcedNat = false; private float lastPlayNowConnectionTime; private ConnectionTesterStatus natCapable = -1; private bool NAToptionWasSwitchedForTesting = false; public bool nowConnecting = false; private bool officialNATstatus = Network.get_useNat(); private bool probingPublicIP = false; private string[] remoteIP = new string[3]; private int[] remotePort = new int[3]; public int serverPort = 0x88b9; public Array sortedHostList; private string testMessage = "Undetermined NAT capabilities"; private float timer = 0f; private bool tryingToConnectPlayNow = false; public int tryingToConnectPlayNowNumber = 0;
// Methods
public void AddToArray(int nr)
{
this.sortedHostList.Add(nr);
this.SortLastItem();
}
public void Awake()
{
this.sortedHostList = new Array();
this.natCapable = Network.TestConnection();
if (Network.HavePublicAddress())
{
Debug.Log("This machine has a public IP address");
}
else
{
Debug.Log("This machine has a private IP address");
}
}
public void Connect(string[] ip, int port, bool usenat)
{
Network.set_useNat(usenat);
this.lastMSConnectionAttemptForcedNat = usenat;
Debug.Log((((("Connecting to " + ip[0]) + ":") + port) + " NAT:") + usenat);
Network.Connect(ip, port);
this.nowConnecting = true;
}
public void Connect(string ip, int port, bool usenat)
{
Network.set_useNat(usenat);
this.lastMSConnectionAttemptForcedNat = usenat;
Debug.Log((((("Connecting to " + ip) + ":") + port) + " NAT:") + usenat);
Network.Connect(ip, port);
this.nowConnecting = true;
}
public void CreateSortedArray()
{
this.sortedHostList = new Array();
int nr = 0;
HostData[] hostData = this.hostData;
int num2 = 0;
HostData[] dataArray2 = hostData;
int length = dataArray2.Length;
while (num2 < length)
{
this.AddToArray(nr);
nr++;
num2++;
}
}
public void FailedConnRetry(NetworkConnectionError info)
{
bool flag;
if (info == 0x16)
{
flag = false;
}
this.nowConnecting = false;
if (this.tryingToConnectPlayNow)
{
if ((flag && Network.get_useNat()) && this.lastMSConnectionAttemptForcedNat)
{
Debug.Log("Failed connect 1A: retry without NAT");
this.remotePort[0] = this.serverPort;
this.Connect(this.remoteIP, this.remotePort[0], false);
this.lastPlayNowConnectionTime = Time.get_time();
}
else
{
Debug.Log("Failed connect 1B: Don't retry");
Network.set_useNat(this.officialNATstatus);
this.tryingToConnectPlayNowNumber++;
this.tryingToConnectPlayNow = false;
}
}
else
{
this.connectionInfo = "Failed to connect!";
if ((flag && Network.get_useNat()) && this.lastMSConnectionAttemptForcedNat)
{
Network.set_useNat(false);
Network.Connect(this.remoteIP, this.remotePort[0]);
this.nowConnecting = true;
this.lastPlayNowConnectionTime = Time.get_time();
}
else
{
Debug.Log("Failed 2b");
if (info == 0x16)
{
this.errorMessage = "Failed to connect: Wrong password supplied";
}
else if (info == 0x11)
{
this.errorMessage = "Failed to connect: Server is full";
}
else
{
this.errorMessage = "Failed to connect";
}
this.remotePort[0] = this.serverPort;
Network.set_useNat(this.officialNATstatus);
}
}
}
public IEnumerator FetchHostList(bool manual)
{
return new FetchHostList$14(manual, this).GetEnumerator();
}
public void Main()
{
}
public void OnConnectedToServer()
{
Network.set_isMessageQueueRunning(false);
PlayerPrefs.SetString("connectIP", Network.get_connections()[0].get_ipAddress());
PlayerPrefs.SetInt("connectPort", Network.get_connections()[0].get_port());
}
public void OnFailedToConnect(NetworkConnectionError info)
{
Debug.Log("FailedToConnect info:" + info);
this.FailedConnRetry(info);
}
public void OnFailedToConnectToMasterServer(NetworkConnectionError info)
{
}
public string PlayNow(float timeStarted)
{
string str;
int num = 0;
IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(this.sortedHostList);
while (enumerator.MoveNext())
{
object current = enumerator.Current;
HostData data = this.hostData[RuntimeServices.NormalizeArrayIndex(this.hostData, RuntimeServices.UnboxInt32(current))];
UnityRuntimeServices.Update(enumerator, current);
if ((!this.filterNATHosts || !data.get_useNat()) && (!data.get_passwordProtected() && (data.get_connectedPlayers() < data.get_playerLimit())))
{
if (this.tryingToConnectPlayNow)
{
object obj3 = null;
if (Network.get_useNat())
{
obj3 = " with option 1/2";
}
else
{
obj3 = " with option 2/2";
}
if ((!Network.get_useNat() && ((this.lastPlayNowConnectionTime + this.CONNECT_TIMEOUT) <= Time.get_time())) || (Network.get_useNat() && ((this.lastPlayNowConnectionTime + this.CONNECT_NAT_TIMEOUT) <= Time.get_time())))
{
Debug.Log("Interrupted by timer, NAT:" + Network.get_useNat());
this.FailedConnRetry(14);
}
return ((((("Trying to connect to host " + (this.tryingToConnectPlayNowNumber + 1)) + "/") + this.sortedHostList.get_length()) + " ") + obj3);
}
if (!this.tryingToConnectPlayNow && (this.tryingToConnectPlayNowNumber <= num))
{
Debug.Log((("Trying to connect to game NR " + num) + " & ") + this.tryingToConnectPlayNowNumber);
this.tryingToConnectPlayNow = true;
this.tryingToConnectPlayNowNumber = num;
this.lastMSConnectionAttemptForcedNat = data.get_useNat();
Network.set_useNat(data.get_useNat());
int num3 = data.get_port();
if (Network.get_useNat())
{
MonoBehaviour.print("Using Nat punchthrough to connect");
}
else
{
MonoBehaviour.print("Connecting directly to host");
}
Debug.Log((((("connecting to " + data.get_gameName()) + " ") + data.get_ip()) + ":") + num3);
Network.Connect(data.get_ip(), num3);
this.lastPlayNowConnectionTime = Time.get_time();
}
num++;
}
}
if (Time.get_time() < (timeStarted + 7))
{
this.StartCoroutine_Auto(this.FetchHostList(true));
return ("Waiting for masterserver..." + ((string) Mathf.Ceil((timeStarted + 7) - Time.get_time())));
}
if (!this.tryingToConnectPlayNow)
{
str = "failed";
}
return str;
}
public void SortLastItem()
{
if (this.sortedHostList.get_length() > 1)
{
for (int i = this.sortedHostList.get_length() - 1; i > 0; i--)
{
int num2 = this.hostData[RuntimeServices.NormalizeArrayIndex(this.hostData, RuntimeServices.UnboxInt32(this.sortedHostList.get_Item(i - 1)))].get_connectedPlayers();
int num3 = this.hostData[RuntimeServices.NormalizeArrayIndex(this.hostData, RuntimeServices.UnboxInt32(this.sortedHostList.get_Item(i)))].get_connectedPlayers();
if (num2 >= num3)
{
break;
}
this.SwapArrayItem(i - 1, i);
}
}
}
public IEnumerator Start()
{
return new Start$15(this).GetEnumerator();
}
public void StartHost(int players, int port)
{
if (players <= 1)
{
players = 1;
}
Network.InitializeServer(players, port);
}
public void SwapArrayItem(object nr1, object nr2)
{
object obj2 = this.sortedHostList.get_Item(RuntimeServices.UnboxInt32(nr1));
this.sortedHostList.set_Item(RuntimeServices.UnboxInt32(nr1), this.sortedHostList.get_Item(RuntimeServices.UnboxInt32(nr2)));
this.sortedHostList.set_Item(RuntimeServices.UnboxInt32(nr2), obj2);
}
public void TestConnection()
{
this.natCapable = Network.TestConnection();
ConnectionTesterStatus natCapable = this.natCapable;
if (natCapable == -2)
{
this.testMessage = "Problem determining NAT capabilities";
this.doneTestingNAT = true;
}
else if (natCapable == -1)
{
this.testMessage = "Undetermined NAT capabilities";
this.doneTestingNAT = false;
}
else if (natCapable == 0)
{
this.testMessage = "Cannot do NAT punchthrough, filtering NAT enabled hosts for client connections, local LAN games only.";
this.filterNATHosts = true;
Network.set_useNat(true);
this.doneTestingNAT = true;
}
else if (natCapable == 1)
{
if (this.probingPublicIP)
{
this.testMessage = ("Non-connectable public IP address (port " + this.serverPort) + " blocked), NAT punchthrough can circumvent the firewall.";
}
else
{
this.testMessage = "NAT punchthrough capable. Enabling NAT punchthrough functionality.";
}
Network.set_useNat(true);
this.doneTestingNAT = true;
}
else if (natCapable == 2)
{
this.testMessage = "Directly connectable public IP address.";
Network.set_useNat(false);
this.doneTestingNAT = true;
}
else if (natCapable == 3)
{
this.testMessage = ("Non-connectble public IP address (port " + this.serverPort) + " blocked), running a server is impossible.";
Network.set_useNat(false);
if (!this.probingPublicIP)
{
Debug.Log("Testing if firewall can be circumnvented");
this.natCapable = Network.TestConnectionNAT();
this.probingPublicIP = true;
this.timer = Time.get_time() + 10;
}
else if (Time.get_time() > this.timer)
{
this.probingPublicIP = false;
Network.set_useNat(true);
this.doneTestingNAT = true;
}
}
else if (natCapable == 4)
{
this.testMessage = "Public IP address but server not initialized, it must be started to check server accessibility. Restart connection test when ready.";
}
else
{
this.testMessage = "Error in test routine, got " + this.natCapable;
}
this.officialNATstatus = Network.get_useNat();
if (this.doneTestingNAT)
{
Debug.Log("TestConn:" + this.testMessage);
Debug.Log((((("TestConn:" + this.natCapable) + " ") + this.probingPublicIP) + " ") + this.doneTestingNAT);
}
}
public void Update()
{
if (!this.doneTestingNAT)
{
this.TestConnection();
}
}
// Nested Types
[Serializable, CompilerGenerated]
internal sealed class FetchHostList$14 : GenericGenerator<WaitForSeconds>
{
// Fields
internal bool $manual60;
internal Menu_multiplayerCode $self_61;
// Methods
public FetchHostList$14(bool manual, Menu_multiplayerCode self_)
{
this.$manual60 = manual;
this.$self_61 = self_;
}
public override IEnumerator<WaitForSeconds> GetEnumerator()
{
return new $(this.$manual60, this.$self_61);
}
// Nested Types
[Serializable, CompilerGenerated]
private sealed class $ : GenericGeneratorEnumerator<WaitForSeconds>, IEnumerator
{
// Fields
internal bool $manual58;
internal Menu_multiplayerCode $self_59;
internal int $timeout$57;
// Methods
public $(bool manual, Menu_multiplayerCode self_)
{
this.$manual58 = manual;
this.$self_59 = self_;
}
public override bool MoveNext()
{
bool flag;
switch (base._state)
{
case 1:
return flag;
case 2:
this.$self_59.hostData = MasterServer.PollHostList();
return this.Yield(3, new WaitForSeconds((float) 1));
case 3:
this.$self_59.CreateSortedArray();
Debug.Log("Requested new host list, got: " + Extensions.get_length(this.$self_59.hostData));
break;
default:
this.$timeout$57 = 120;
if (this.$manual58)
{
this.$timeout$57 = 5;
}
if ((this.$self_59.lastHostListRequest == 0) || (Time.get_realtimeSinceStartup() > (this.$self_59.lastHostListRequest + this.$timeout$57)))
{
this.$self_59.lastHostListRequest = Time.get_realtimeSinceStartup();
MasterServer.RequestHostList(this.$self_59.gameName);
return this.Yield(2, new WaitForSeconds((float) 1));
}
break;
}
this.Yield(1, null);
return flag;
}
}
}
[Serializable, CompilerGenerated]
internal sealed class Start$15 : GenericGenerator<WaitForSeconds>
{
// Fields
internal Menu_multiplayerCode $self_56;
// Methods
public Start$15(Menu_multiplayerCode self_)
{
this.$self_56 = self_;
}
public override IEnumerator<WaitForSeconds> GetEnumerator()
{
return new $(this.$self_56);
}
// Nested Types
[Serializable, CompilerGenerated]
private sealed class $ : GenericGeneratorEnumerator<WaitForSeconds>, IEnumerator
{
// Fields
internal Menu_multiplayerCode $self_55;
internal int $tries$54;
// Methods
public $(Menu_multiplayerCode self_)
{
this.$self_55 = self_;
}
public override bool MoveNext()
{
bool flag;
switch (base._state)
{
case 1:
return flag;
case 2:
this.$tries$54 = 0;
break;
case 3:
this.$tries$54++;
break;
default:
return this.Yield(2, new WaitForSeconds(0.5f));
}
if (this.$tries$54 <= 10)
{
if ((this.$self_55.hostData == null) || (Extensions.get_length(this.$self_55.hostData) <= 0))
{
this.$self_55.StartCoroutine_Auto(this.$self_55.FetchHostList(true));
}
return this.Yield(3, new WaitForSeconds(0.5f));
}
this.Yield(1, null);
return flag;
}
}
}
}
Please also tell me if you see any other errors with the code. Also, I'm not sure why it put the $ signs in different spots of the code. Any help would be appreciated.
Answer by Mike 3 · Jul 13, 2010 at 08:11 PM
It's unable to reflect it properly - most likely because reflector can't work out dynamic typing, and other such js features
My advice is to go back to the last version you posted up and change the 5 or so words which were missing the type to string/int/bool, and then you're sorted - the above is a complete mess
$$anonymous$$ike could you help me here? http://forum.unity3d.com/viewtopic.php?t=58091 Thanks.
Not really the place for this - but either way, just check the tag or gameobject in the raycast for mouse up, and that's about it
Yeah, I will go back to the original converter I was using. But I actually followed your instructions last time to no aveil. After I fixed what you told me to, there were still many problems with it, that is what the questions I had asked after it were about. Is there some way I could make arrangement with you to fix the code for me for a price, or do you know of some company that provides that sort of service. Because it would just be stupid to post like 25 questions just to get a code converted. And my back ground is ActionScript, so that is why I ask these questions.
Answer by skovacs1 · Jul 13, 2010 at 08:37 PM
This isn't a forum for questions about .NET Reflector, but Mike is probably right about what's wrong - dynamic typing and C# don't play nicely. If you want help with the script or what it's actually doing, ask about that in stead of .NET Reflector.
The code posted here doesn't have line numbers, so line 143 of 488 is like a needle in a haystack here. When the code is a mess like this, you're asking too much. Post a smaller sample of the problem that is of interest to at least one other person and is easier to follow.
Post your errors when you get errors. They can be helpful.
The error at line 143 (and throughout the code) appears to be the very $ you're unsure of. Try pulling up your code in a C# IDE with syntax highlighting and error messages (like VS for example) and this error is made quite obvious.
As Mike said, specify the types where they are dynamic if you must get reflector to do the work. Otherwise, just fix the dollar signs by hand because they are not an accepted character within the contexts they exist in within this code. It's always a good idea to read and understand the code you're using so that if it breaks, you can fix it.
I'm not sure the dollar sign bits are even fixable, they're boo specific things for dynamic typing if i remember correctly
Answer by Mike 25 · Jan 17, 2011 at 01:22 AM
A little off topic, but how did you convert "from JavaScript to C# using .NET Reflector"??? I have been trying to find something that would do this... but no luck so far. Hope you can help!
Reflector takes a .NET dll and spits out c# (or one of the other languages it supports). The main issue is that it spits out a lot of unusable code, as it relies on javascript specific libraries.
This is what you'll want to download though: http://www.red-gate.com/products/dotnet-development/reflector/
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do i fix "Script does not exist" error when trying to add my script to something 3 Answers
About "translating" js into C# 1 Answer
I need help translating Javascript to C# 0 Answers